def run_server(self): # Run server in separate process signal.signal(signal.SIGINT, lambda x, y: None) # dict to send back in queue once server is started new_info = {} creds = self.server_info.get('credentials', {}).get('ftp', {}) username = creds.get('username', self._generate_credential()) password = creds.get( 'password', SecretString.from_plaintext(self._generate_credential())) # return credentials back to parent process new_info['credentials'] = Credentials( {'ftp': { 'username': username, 'password': password }}) address = self.server_info['address'] port = self.server_info.get('port', DEFAULT_PORT) address = (address, port) path = self.server_info.setdefault('path', '/') new_info['path'] = path # Create FTP Server auth = DummyAuthorizer() auth.add_user(username, to_plaintext(password), path, perm='elradfmwMT') handler = FTPHandler handler.authorizer = auth server = FTPServer(address, handler) server.max_cons = 256 server.max_cons_per_ip = 5 # Set up logging for the FTP Server logfile = self.server_info.get('logfile', None) if logfile: ftp_logger = logging.getLogger('pyftpdlib') ftp_logger.setLevel(logging.DEBUG) ftp_logger.propagate = False ftp_handler = logging.FileHandler(logfile) ftp_logger.addHandler(ftp_handler) # Retrieve allocated port _, new_info['port'] = server.address # Send new info back to parent process self.queue.put(new_info) # Listen server.serve_forever()
def _encode_secret(self, plain_text): """ Performs password encoding. Args: plain_text ('str'): the plain text password. Returns: str: The encoded password. """ encoded = SecretString.from_plaintext(plain_text) return '%ENC{' + encoded.data + '}'
def run_server(self): # Run server in separate process address = self.server_info.get('address', '0.0.0.0') port = self.server_info.get('port', DEFAULT_PORT) local_dir = self.server_info.get('path', '/') # use authentication by default http_auth = self.server_info.get('custom', {}).get('http_auth', True) # Setup local HTTP server server_address = (address, port) httpd = http.server.HTTPServer(server_address, HTTPRequestHandler) httpd.directory = local_dir local_port = httpd.server_port if http_auth: creds = self.server_info.get('credentials', {}).get('http', {}) username = creds.get('username', self._generate_credential()) password = creds.get( 'password', SecretString.from_plaintext(self._generate_credential())) httpd.auth = base64.b64encode("{}:{}".format( username, to_plaintext(password)).encode()).decode() else: httpd.auth = None # Send new info back to parent process self.queue.put({ 'port': local_port, 'path': local_dir, 'credentials': Credentials({'http': { 'username': username, 'password': password }}) if http_auth else {} }) # Keep process alive httpd.serve_forever()
def wrapper(*args, **kwargs): # adding extra is done only when it's pyATS health if kwargs['self'].__class__.__name__ == 'Health': extra_action_result = {} section = kwargs['section'] extra_action_result.setdefault('health_name', kwargs['name']) extra_action_result.setdefault('action', func.__name__) extra_action_result.setdefault( 'section', json.loads(json.dumps(section.uid, default=lambda a: str(a)))) extra_action_result.setdefault( 'section_parent', json.loads( json.dumps(section.parent.uid, default=lambda a: str(a)))) extra_action_result.setdefault('job', runtime.job.uid) # action start time action_start = datetime.now() # execute action for Health class action_output = func(*args, **kwargs) # action stop time action_stop = datetime.now() # calculate delta (action_stop - action_start) action_runtime = str(action_stop - action_start) # date format change action_start = action_start.isoformat() action_stop = action_stop.isoformat() extra_action_result.setdefault('starttime', action_start) extra_action_result.setdefault('stoptime', action_stop) extra_action_result.setdefault('runtime', action_runtime) extra_action_result.setdefault('steps_result', kwargs['steps'].result.name) # encode any action to protect contents such as password if 'reply' in kwargs: for reply in kwargs['reply']: for key, value in reply.items(): if key == 'action': reply['action'] = '%ENC{{{action}}}'.format( action=SecretString.from_plaintext(value).data) # added `data` and `output` to ret_dict extra_action_result.setdefault( 'kwargs', json.loads(json.dumps(kwargs, default=lambda a: str(a)))) extra_action_result.setdefault('output', action_output) # add extra to testsuite extra_args = { 'pyats_health_{action_start}'.format(action_start=action_start): extra_action_result } # add extra to processor section.reporter.client.add_extra(**extra_args) log.debug('extra:\n' + json.dumps(extra_args, indent=2, sort_keys=True)) else: # execute action for Blitz class action_output = func(*args, **kwargs) return action_output