2121'''
2222
2323import argparse
24+ import base64
25+ import binascii
2426import os
2527from messages import MSG
2628from metaclasses import Singleton
@@ -36,14 +38,10 @@ def checkFileExists(path, filename):
3638
3739
3840def checkTLSsettings (args ):
39- if args .get ('prometheus' ) and (not args .get ('tlsKeyPath' ) or not
40- args .get ('tlsKeyFile' ) or not
41- args .get ('tlsCertFile' )):
42- return False , MSG ['MissingSSLCert' ]
43- elif args .get ('protocol' ) == "https" and (not args .get ('tlsKeyPath' ) or not
44- args .get ('tlsKeyFile' ) or not
45- args .get ('tlsCertFile' )
46- ):
41+ if args .get ('protocol' ) == "https" and (not args .get ('tlsKeyPath' ) or not
42+ args .get ('tlsKeyFile' ) or not
43+ args .get ('tlsCertFile' )
44+ ):
4745 return False , MSG ['MissingParm' ]
4846 elif args .get ('protocol' ) == "https" and not os .path .exists (args .get ('tlsKeyPath' )):
4947 return False , MSG ['KeyPathError' ]
@@ -56,6 +54,23 @@ def checkTLSsettings(args):
5654 return True , ''
5755
5856
57+ def checkBasicAuthsettings (args ):
58+ if args .get ('enabled' ) and (not args .get ('username' ) or not
59+ args .get ('password' )
60+ ):
61+ return False , MSG ['MissingParm' ]
62+ elif args .get ('enabled' ) and ("/" in str (args .get ('password' )) and not
63+ os .path .isfile (args .get ('password' ))
64+ ):
65+ return False , MSG ['FileNotFound' ].format (args .get ('password' ))
66+ elif args .get ('enabled' ) and "/" not in str (args .get ('password' )):
67+ try :
68+ base64 .b64decode (args .get ('password' ), validate = True )
69+ except binascii .Error :
70+ return False , MSG ['WrongFormat' ].format ("basic auth password" )
71+ return True , ''
72+
73+
5974def checkApplicationPort (args ):
6075 if not args .get ('port' , None ) and not args .get ('prometheus' , None ):
6176 return False , MSG ['MissingPortParam' ]
@@ -78,7 +93,6 @@ def checkCAsettings(args):
7893
7994def getSettings (argv ):
8095 settings = {}
81- msg = ''
8296 defaults = ConfigManager ().defaults
8397 args , msg = parse_cmd_args (argv )
8498 if args and defaults :
@@ -93,6 +107,10 @@ def getSettings(argv):
93107 return None , msg
94108 # check API key settings
95109 valid , msg = checkAPIsettings (settings )
110+ if not valid :
111+ return None , msg
112+ # check basic auth settings
113+ valid , msg = checkBasicAuthsettings (settings )
96114 if not valid :
97115 return None , msg
98116 # check TLS settings
@@ -186,7 +204,10 @@ class Password(argparse.Action):
186204
187205 def __call__ (self , parser , namespace , values , option_string ):
188206 if values is None :
189- print ('Enter your apiKey value' )
207+ if self .dest == 'password' :
208+ print ('Enter your basic auth password' )
209+ else :
210+ print ('Enter your apiKey value' )
190211 values = getpass .getpass ()
191212
192213 setattr (namespace , self .dest , values )
@@ -213,6 +234,12 @@ def parse_cmd_args(argv):
213234 help = 'port number listening on OpenTSDB API HTTP(S) connections (Default from config.ini: 4242, if enabled)' )
214235 parser .add_argument ('-r' , '--protocol' , action = "store" , choices = ["http" , "https" ], default = None ,
215236 help = 'Connection protocol HTTP/HTTPS (Default from config.ini: "http")' )
237+ parser .add_argument ('-b' , '--enabled' , action = "store" , choices = ["True" , "False" ], default = None ,
238+ help = 'Controls if HTTP/S basic authentication should be enabled or not (Default from config.ini: "True")' )
239+ parser .add_argument ('-u' , '--username' , action = "store" , default = None ,
240+ help = 'HTTP/S basic authentication user name(Default from config.ini: \' scale_admin\' )' )
241+ parser .add_argument ('-a' , '--password' , action = Password , nargs = '?' , dest = 'password' , default = None ,
242+ help = 'Enter your HTTP/S basic authentication password:' )
216243 parser .add_argument ('-t' , '--tlsKeyPath' , action = "store" , default = None ,
217244 help = 'Directory path of tls privkey.pem and cert.pem file location (Required only for HTTPS ports 8443/9250)' )
218245 parser .add_argument ('-k' , '--tlsKeyFile' , action = "store" , default = None ,
0 commit comments