def do_login( self, s ): '''Login to the BE using credentials stored in a config file.''' parser = self.getParser( 'login' ) parser.add_argument( 'configFile', type = argparse.FileType( 'r' ), help = 'config file specifying the endpoint and token to use' ) parser.add_argument( '-k', '--key', required = False, default = None, #type = argparse.FileType( 'r' ), type = str, help = 'key to use to sign hbs tasks', dest = 'key' ) arguments = self.parse( parser, s ) if arguments is not None: try: config = json.loads( arguments.configFile.read() ) except: print( "Invalid config file format (JSON): %s" % traceback.format_exc() ) return if 'beach_config' not in config or 'token' not in config: print( "Missing endpoint or token in config." ) return _ = os.getcwd() os.chdir( os.path.dirname( __file__ ) ) self.be = BEAdmin( config[ 'beach_config' ], config[ 'token' ] ) os.chdir( _ ) remoteTime = self.be.testConnection() if remoteTime.isTimedOut: print( "Endpoint did not respond." ) return if 'pong' not in remoteTime.data: print( "Endpoint responded with invalid data." ) return if arguments.key is not None: if os.path.isfile( arguments.key ): try: password = getpass.getpass() print( "...decrypting key..." ) # There are weird problems with pexpect and newlines and binary, so # we have to brute force it a bit for i in range( 0, 30 ): proc = pexpect.spawn( 'openssl aes-256-cbc -d -in %s' % arguments.key ) proc.expect( [ 'enter aes-256-cbc decryption password: *' ] ) proc.sendline( password ) proc.expect( "\r\n" ) proc.expect( ".*" ) self.hbsKey = proc.match.group( 0 ).replace( "\r\n", "\n" ) try: testSign = Signing( self.hbsKey ) testSig = testSign.sign( 'a' ) if testSig is not None: break except: self.hbsKey = None if self.hbsKey is not None: print( "success, authenticated!" ) else: print( "error loading key, bad key format or password?" ) except: self.hbsKey = None print( "error getting cloud key: %s" % traceback.format_exc() ) if self.hbsKey is not None and 'bad decrypt' in self.hbsKey: print( "Invalid password" ) self.hbsKey = None else: print( "Invalid key file: %s." % arguments.key ) self.hbsKey = None else: self.hbsKey = None remoteTime = remoteTime.data.get( 'pong', 0 ) print( "Successfully logged in." ) print( "Remote endpoint time: %s." % remoteTime ) self.user = config[ 'token' ].split( '/' )[ 0 ] self.updatePrompt()
def connectWithConfig( self, beachConfig, token ): self.be = BEAdmin( beachConfig, token ) self.outputString( "Interface to cloud set." )