diff options
author | Sam Chudnick <sam@chudnick.com> | 2022-07-02 15:35:50 -0400 |
---|---|---|
committer | Sam Chudnick <sam@chudnick.com> | 2022-07-02 15:35:50 -0400 |
commit | 8472b394ee44cd46cc36fd4fe0a4882364cab602 (patch) | |
tree | 301fcb5e0becbebff4486b556e561afac61e11ca /client | |
parent | 01c24eb1f6f6a54bb780940c7665acd280b42aaf (diff) |
Read options from config file
Set a standardized configuration file location and read options from
there. Allow for specifiying alternate location on command line.
Options can still be specified on the command line, and any command line
options take priority over those given in the configuration file.
Diffstat (limited to 'client')
-rwxr-xr-x | client/client.py | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/client/client.py b/client/client.py index b2429b6..1c7e155 100755 --- a/client/client.py +++ b/client/client.py | |||
@@ -4,6 +4,7 @@ import socket | |||
4 | import time | 4 | import time |
5 | import argparse | 5 | import argparse |
6 | import sys | 6 | import sys |
7 | import os | ||
7 | 8 | ||
8 | HEADER_LENGTH = 64 | 9 | HEADER_LENGTH = 64 |
9 | KEY_LENGTH = 64 | 10 | KEY_LENGTH = 64 |
@@ -11,6 +12,7 @@ DISCONNECT_LENGTH = ACK_LENGTH = 3 | |||
11 | ACK_MESSAGE = "ACK" | 12 | ACK_MESSAGE = "ACK" |
12 | DISCONNECT_MESSAGE = "BYE" | 13 | DISCONNECT_MESSAGE = "BYE" |
13 | FORMAT = "utf-8" | 14 | FORMAT = "utf-8" |
15 | import configparser | ||
14 | 16 | ||
15 | def parse_arguments(): | 17 | def parse_arguments(): |
16 | parser = argparse.ArgumentParser() | 18 | parser = argparse.ArgumentParser() |
@@ -18,7 +20,7 @@ def parse_arguments(): | |||
18 | parser.add_argument("--port",type=int,help="Port to connect to") | 20 | parser.add_argument("--port",type=int,help="Port to connect to") |
19 | parser.add_argument("--config",type=str,help="Path to config file",\ | 21 | parser.add_argument("--config",type=str,help="Path to config file",\ |
20 | default="/etc/mfa/mfa.conf") | 22 | default="/etc/mfa/mfa.conf") |
21 | parser.add_argument("--key",type=str,help="Client connection key",required=True) | 23 | parser.add_argument("--key",type=str,help="Client connection key") |
22 | return parser.parse_args() | 24 | return parser.parse_args() |
23 | 25 | ||
24 | def prompt_user(prompt): | 26 | def prompt_user(prompt): |
@@ -53,32 +55,48 @@ def init_connection(mfa_server, client_port, client_key): | |||
53 | 55 | ||
54 | 56 | ||
55 | def read_config(config_file): | 57 | def read_config(config_file): |
56 | # Read config file for server and port info | 58 | parser = configparser.ConfigParser(inline_comment_prefixes="#") |
57 | # Return tuple (server,port) | 59 | parser.read(config_file) |
58 | server = "" | 60 | return parser |
59 | port = 0 | 61 | |
60 | with open(config_file) as conf: | 62 | |
61 | line = None | 63 | def get_vars(args,confparser): |
62 | while line != "": | 64 | if not os.path.exists(args.config): |
63 | line = conf.readline() | 65 | print("Unable to open config file") |
64 | if line.startswith("server ="): | 66 | sys.exit(1) |
65 | server = line.split("=")[1].strip() | 67 | |
66 | if line.startswith("port ="): | 68 | server = None |
67 | port = int(line.split("=")[1].strip()) | 69 | port = None |
68 | return (server,port) | 70 | key = None |
71 | |||
72 | # Set values from config file first | ||
73 | if confparser.has_section("client"): | ||
74 | server = confparser.get("client","server",fallback=None) | ||
75 | port = confparser.get("client","port",fallback=None) | ||
76 | key = confparser.get("client","key",fallback=None) | ||
77 | |||
78 | # Let command line args overwrite any values | ||
79 | if args.server: | ||
80 | server = args.server | ||
81 | if args.port: | ||
82 | port = args.port | ||
83 | if args.key: | ||
84 | key = args.key | ||
85 | |||
86 | # Exit if any value is null | ||
87 | if None in [server,port,key]: | ||
88 | print("error: one or more items unspecified") | ||
89 | sys.exit(1) | ||
90 | |||
91 | return server,port,key | ||
92 | |||
69 | 93 | ||
70 | def main(): | 94 | def main(): |
71 | # Get arguments, exit if unable to connect | 95 | # Get arguments, exit if unable to connect |
72 | args = parse_arguments() | 96 | args = parse_arguments() |
73 | client_key = args.key | 97 | confparser = read_config(args.config) |
74 | 98 | ||
75 | # Read server and port from config file but allow command line options | 99 | mfa_server,client_port,client_key = get_vars(args,confparser) |
76 | # to override those settings | ||
77 | mfa_server, client_port = read_config(args.config) | ||
78 | if args.server != None: | ||
79 | mfa_server = args.server | ||
80 | if args.port != None: | ||
81 | client_port = args.port | ||
82 | 100 | ||
83 | # Exit if invalid key is provided | 101 | # Exit if invalid key is provided |
84 | if len(client_key) != KEY_LENGTH: | 102 | if len(client_key) != KEY_LENGTH: |