def delete_user(self, name): """Delete a new user.""" try: ProxyServer.delete_user(name, ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e))
def delete_role(self, name): """Delete a role.""" try: ProxyServer.delete_role(name, ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def get_assignment(self, user_name): """Return the details of the assignment for the given user name.""" try: return ProxyServer.get_assignment(user_name, ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def all_roles(self): """Return a list of all roles.""" try: return ProxyServer.all_roles(ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def add_role(self, name, description, perm_ids): """Add a new role.""" try: ProxyServer.add_role(name, description, perm_ids, ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def set_assignment(self, user_name, role_names): """Save the roles assigned to a user.""" try: ProxyServer.set_assignment(user_name, role_names, ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def is_empty(self): """See if the database is empty.""" try: return ProxyServer.is_empty_policy() except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def add_user(self, name, description, password): """Add a new user.""" try: ProxyServer.add_user(name, description, password, ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e))
def modify_role(self, name, description, perm_ids): """Update the description and permissions for the given role.""" try: ProxyServer.modify_role(name, description, perm_ids, ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def authenticate_user(self, name, password): """Return the tuple of the user name, description, and blob if the user was successfully authenticated.""" try: key, name, description, blob = ProxyServer.authenticate_user(name, password) # We don't save the cache because we should be about to read the # real permission ids and we do it then. ProxyServer.cache = description, blob, [] except Exception, e: # See if we couldn't connect to the server. if not isinstance(e, socket.error): raise UserStorageError(ProxyServer.error(e)) err, _ = e.args if err != errno.ECONNREFUSED: raise UserStorageError(ProxyServer.error(e)) try: ok = ProxyServer.read_cache() except Exception, e: raise UserStorageError(str(e))
def run(cmdargs): args = [ (cmdargs.localhost, cmdargs.localport), (cmdargs.remotehost, cmdargs.remoteport) ] kwargs = {} if cmdargs.sslboth: kwargs['ssl'] = True if not cmdargs.certfile or not cmdargs.keyfile: print ('You need to specify a valid certificate file and a key file!') sys.exit(1) kwargs['certfile'] = cmdargs.certfile kwargs['keyfile'] = cmdargs.keyfile elif cmdargs.sslout: kwargs['ssl_out_only'] = True kwargs['debug'] = False if cmdargs.username and cmdargs.password: credentials = store_credentials.StoreCredentials() credentials.username = cmdargs.username credentials.password = cmdargs.password credentials.stored = True else: credentials = credentials_emailing.StoreCredentials() #credentials.username = config.username #credentials.password = config.password credentials.stored = True kwargs['credential_validator'] = credentials server = ProxyServer(*args, **kwargs) server.run()
def authenticate_user(self, name, password): """Return the tuple of the user name, description, and blob if the user was successfully authenticated.""" try: key, name, description, blob = ProxyServer.authenticate_user( name, password) # We don't save the cache because we should be about to read the # real permission ids and we do it then. ProxyServer.cache = description, blob, [] except Exception, e: # See if we couldn't connect to the server. if not isinstance(e, socket.error): raise UserStorageError(ProxyServer.error(e)) err, _ = e.args if err != errno.ECONNREFUSED: raise UserStorageError(ProxyServer.error(e)) try: ok = ProxyServer.read_cache() except Exception, e: raise UserStorageError(str(e))
def modify_user(self, name, description, password): """Update the description and password for the given user.""" try: ProxyServer.modify_user(name, description, password, ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e))
def matching_roles(self, name): """Return the full name, description and permissions of all the roles that match the given name.""" try: return ProxyServer.matching_roles(name, ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e))
def matching_users(self, name): """Return the full name and description of all the users that match the given name.""" try: return ProxyServer.matching_users(name, ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e))
def test_log_info(self): proxy = ProxyServer() conn = connection.Connection(None, CLIENT_IP, 'scratchpads.eu', 80) result = proxy.get_log_info(conn, HTTP_PACKAGE.decode()) self.assertEqual( result, CLIENT_IP + ' POST ' 'http://scratchpads.eu/modules/' 'statistics/statistics.php')
def unauthenticate_user(self, user): """Unauthenticate the given user.""" if ProxyServer.key is None: ok = True else: try: ok = ProxyServer.unauthenticate_user(ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e))
def update_password(self, name, password): """Update the password for the given user.""" # If the remote server disappeared after the capabilities were read but # before the user was authenticated then we could get here. if ProxyServer.key is None: raise UserStorageError("It is not possible to change password " "when disconnected from the permissions server.") try: ProxyServer.update_password(name, password, ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e))
def update_password(self, name, password): """Update the password for the given user.""" # If the remote server disappeared after the capabilities were read but # before the user was authenticated then we could get here. if ProxyServer.key is None: raise UserStorageError( "It is not possible to change password " "when disconnected from the permissions server.") try: ProxyServer.update_password(name, password, ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e))
def Run(self): if self.proxy_port == '-1': return 1 # Temporary means to determine if we are running on CI # TODO: Update to IS_CI environment variable or similar out_dir = _launcher_params.out_directory is_ci = out_dir and 'mh_lab' in out_dir target = (_launcher_params.platform, _launcher_params.config) if is_ci and '{}/{}'.format(*target) in _DISABLED_BLACKBOXTEST_CONFIGS: logging.warning( 'Blackbox tests disabled for platform:{} config:{}'.format( *target)) return 0 logging.info('Using proxy port: %s', self.proxy_port) with ProxyServer(port=self.proxy_port, host_resolve_map=self.host_resolve_map, client_ips=self.device_ips): if self.test_name: suite = unittest.TestLoader().loadTestsFromModule( importlib.import_module(_TEST_DIR_PATH + self.test_name)) else: suite = LoadTests(_launcher_params) return_code = not unittest.TextTestRunner( verbosity=0, stream=sys.stdout).run(suite).wasSuccessful() return return_code
def _capabilities_default(self): """Return the storage capabilities.""" try: caps = ProxyServer.capabilities() except: caps = [] return caps
def get_policy(self, name): """Return the details of the policy for the given user name.""" description, blob, perm_ids = ProxyServer.cache if ProxyServer.key is not None: try: name, perm_ids = ProxyServer.get_policy(name, ProxyServer.key) except Exception, e: raise PolicyStorageError(ProxyServer.error(e)) # Save the permissions ids in the persistent cache. ProxyServer.cache = description, blob, perm_ids try: ProxyServer.write_cache() except: pass
def test_handling_clients(self): url = 'http://{}:{}'.format(WEB_SERVER_ADDRESS[0], WEB_SERVER_ADDRESS[1]) server = HTTPServer(WEB_SERVER_ADDRESS, CGIHTTPRequestHandler) proxy = ProxyServer() proxy_th = threading.Thread(target=proxy.start) http_th = threading.Thread(target=server.serve_forever) proxy_th.start() http_th.start() proxy_url = 'http://{}:{}'.format(*proxy.get_addr()) proxies = {'http': proxy_url} r = requests.get(url, proxies=proxies) self.assertEqual(r.status_code, 200) proxy.stop() server.shutdown() http_th.join() proxy_th.join() self.assertEqual(proxy.executor, None)
def test_starting_stopping_server(self): proxy = ProxyServer() expected_host = socket.gethostbyname(socket.gethostname()) expected_port = 1111 th = threading.Thread(target=proxy.start, kwargs={ 'host': '0.0.0.0', 'port': expected_port }) th.start() time.sleep(0.1) host, port = proxy.get_addr() proxy.stop() self.assertEqual(host, expected_host) self.assertEqual(port, expected_port) self.assertEqual(proxy.sever_sock.fileno(), -1) self.assertIsNone(proxy.executor)
def test_init_options(self): server = ProxyServer(cert_ca=ROOT_CRT, cert_key=ROOT_KEY, certs_folder=CERTIFICATES_FOLDER) certs_path_exist = False if os.path.exists(server.certs_folder): certs_path_exist = True self.assertEqual(certs_path_exist, True)
def test_handling_https(self): proxy = ProxyServer() th = threading.Thread(target=proxy.start) th.start() host, port = proxy.get_addr() serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.connect((host, port)) serv.sendall(HTTPS_PACKAGE) response = b'' while True: data = serv.recv(BUFFER_SIZE) if not data: break response += data self.assertEqual(SUCCESS_MESSAGE, response) proxy.stop() th.join()
def Run(self): if self.proxy_port_number == '-1': return 1 logging.info('Using proxy port number: %s', self.proxy_port_number) with ProxyServer(port=self.proxy_port_number, host_resolve_map=self.host_resolve_map): if self.test_name: suite = unittest.TestLoader().loadTestsFromModule( importlib.import_module(_TEST_DIR_PATH + self.test_name)) else: suite = LoadTests(_device_params.platform, _device_params.config, _device_params.device_id, _device_params.out_directory) return_code = not unittest.TextTestRunner( verbosity=0, stream=sys.stdout).run(suite).wasSuccessful() return return_code
def update_blob(self, name, blob): """Update the blob for the given user.""" # Update the cache. description, _, perm_ids = ProxyServer.cache ProxyServer.cache = description, blob, perm_ids if ProxyServer.key is None: # Write the cache and tell the user about any errors. ProxyServer.write_cache() else: try: ProxyServer.update_blob(name, blob, ProxyServer.key) except Exception, e: raise UserStorageError(ProxyServer.error(e)) # Write the cache but ignore any errors. try: ProxyServer.write_cache() except: pass
parser.add_argument('id', metavar='I', help='Server id', choices=[*SERVER_MAPPINGS]) args = parser.parse_args() '''Logging Configuration''' logging.basicConfig( level=logging.DEBUG, format='%(asctime)-15s - %(name)s: %(message)s', filename='{}.log'.format(args.id), ) log = logging.getLogger('server.py') ''' Setup Server ''' SERVER_ADDRESS = (SERVER_URL, SERVER_MAPPINGS[args.id]) event_loop = asyncio.get_event_loop() server_factory = event_loop.create_server( lambda: ProxyServer(args.id, event_loop), *SERVER_ADDRESS) server = event_loop.run_until_complete(server_factory) log.debug('starting up on {} port {}'.format(*SERVER_ADDRESS)) try: event_loop.run_forever() except KeyboardInterrupt: pass log.debug('closing server') server.close() event_loop.run_until_complete(server.wait_closed()) log.debug('closing event loop') event_loop.close()
def test_http_package_parsing(self): host, port, is_https = ProxyServer.get_conn_info(HTTP_PACKAGE) self.assertEqual('scratchpads.eu', host) self.assertEqual(80, port) self.assertEqual(False, is_https)
# See if we couldn't connect to the server. if not isinstance(e, socket.error): raise UserStorageError(ProxyServer.error(e)) err, _ = e.args if err != errno.ECONNREFUSED: raise UserStorageError(ProxyServer.error(e)) try: ok = ProxyServer.read_cache() except Exception, e: raise UserStorageError(str(e)) if not ok: raise UserStorageError(ProxyServer.error(e)) # We are in "disconnect" mode. key = None description, blob, _ = ProxyServer.cache ProxyServer.key = key return name, description, blob def delete_user(self, name): """Delete a new user.""" try: ProxyServer.delete_user(name, ProxyServer.key) except Exception, e:
def test_https_package_parsing(self): host, port, is_https = ProxyServer.get_conn_info(HTTPS_PACKAGE) self.assertEqual('anytask.org', host) self.assertEqual(443, port) self.assertEqual(True, is_https)