class GatekeeperServer(Server): NAME = "gatekeeper" def server_init(self): self.log = LogSinkClient() self.userstore_client = UserStoreClient() self.controller_client = ControllerClient() self.basic_auth = HTTPBasic(self.wsgi_app_post_auth, self.userstore_client) def get_requested_fib(self, environ): # parse integer fibonacci sequence index return environ['PATH_INFO'][1:] def set_custom_environment(self, query_dict): if "environment" in query_dict: # environments set in the request should be # stored in request_meta, not server_meta. # Storing environment in request_meta will # propagate it to other services as well. environment = query_dict['environment'][-1] get_request_meta()['environment'] = environment self.log.info('Using custom environment: %s' % environment) return environment def set_custom_endpoints(self, query_dict): # custom enpoint information travels on in request_meta custom_endpoints = {} for key in query_dict.keys(): if key.startswith(ENDPOINT_OVERRIDE_PREFIX): endpoint = key[(len(ENDPOINT_OVERRIDE_PREFIX)):] custom_endpoints[endpoint] = query_dict[key][-1] if custom_endpoints: get_request_meta()['custom_endpoints'] = custom_endpoints self.log.info('Using custom endpoints: %s' % str(custom_endpoints)) return custom_endpoints def parse_request(self, environ): query_dict = parse_qs(environ['QUERY_STRING']) self.set_custom_environment(query_dict) self.set_custom_endpoints(query_dict) return self.get_requested_fib(environ) def wsgi_app_post_auth(self, environ, start_response): username = environ.get('REMOTE_USER', None) if username is not None: raw_requested_fib = self.parse_request(environ) status, body = self.controller_client.generate_response( raw_requested_fib, username) else: status, body = ["401 UNAUTHENTICATED", "Please log in"] return http_response(start_response, status, body) def wsgi_app(self, environ, start_response): set_new_request_id() response = self.basic_auth(environ, start_response) # clear request meta set_request_meta({}) return response
class UserStoreServer(UserStoreBase, Server): def server_init(self): self.users = {} self.credit = {} self.log = LogSinkClient() self._init_user_db() def _add_user(self, user_dict, credit): self.users[user_dict['username']] = user_dict self.log.info("Added user: %s with credit %s" % ( str(user_dict), credit)) self.set_credit(user_dict['username'], credit) def _init_user_db(self, filename=USER_DB_FILE): raw_user_list = load_config(filename)['users'] for raw_user in raw_user_list: username, password, credit = raw_user self._add_user( dict(username=username, password=password), credit) def get_user(self, username=None): return self.users.get(username, {}) def set_credit(self, username=None, credit=None): if username is None or credit is None: return False self.credit[username] = credit self.log.info("Credit for %s set to %s" % ( username, credit)) return True def increment_credit(self, username=None, increment_by=0): if username is None or username not in self.credit: return False self.credit[username] += increment_by self.log.info("Credit for %s incremented %s yielding %s" % ( username, increment_by, self.credit[username])) return self.credit[username] def get_credit(self, username=None): return self.credit.get(username)
class UserStoreServer(UserStoreBase, Server): def server_init(self): self.users = {} self.credit = {} self.log = LogSinkClient() self._init_user_db() def _add_user(self, user_dict, credit): self.users[user_dict['username']] = user_dict self.log.info("Added user: %s with credit %s" % (str(user_dict), credit)) self.set_credit(user_dict['username'], credit) def _init_user_db(self, filename=USER_DB_FILE): raw_user_list = load_config(filename)['users'] for raw_user in raw_user_list: username, password, credit = raw_user self._add_user(dict(username=username, password=password), credit) def get_user(self, username=None): return self.users.get(username, {}) def set_credit(self, username=None, credit=None): if username is None or credit is None: return False self.credit[username] = credit self.log.info("Credit for %s set to %s" % (username, credit)) return True def increment_credit(self, username=None, increment_by=0): if username is None or username not in self.credit: return False self.credit[username] += increment_by self.log.info("Credit for %s incremented %s yielding %s" % (username, increment_by, self.credit[username])) return self.credit[username] def get_credit(self, username=None): return self.credit.get(username)