class PricingServer(PricingBase, Server):

    def server_init(self):
        self.log = LogSinkClient(self.service_dir_client)
        self.userstore_client = UserStoreClient(self.service_dir_client)

    def price_request(self, requested_fib):
        return int(floor(logarithm(requested_fib,10))) + 1

    def pay_for_user_request(self, requested_fib, username):
        request_cost = self.price_request(requested_fib)
        credit = self.userstore_client.get_credit(username)
        if credit < request_cost:
            self.log.info(
                'User "%s" denied fib(%s), credit: %s' % (
                username,
                requested_fib,
                credit))
            return [
                False,
                "Error: fib(%s) costs %s, user %s has insufficient credit(%s)" % (
                requested_fib, request_cost, username, credit)]
        new_credit = self.userstore_client.increment_credit(
            username, -1 * request_cost)
        self.log.info("%s used %s credit, balance: %s" % (
            username, request_cost, new_credit))
        return [True, new_credit]
Beispiel #2
0
class ControllerServer(ControllerBase, Server):

    def server_init(self):
        self.log = LogSinkClient(self.service_dir_client)
        self.pricing_client = PricingClient(self.service_dir_client)
        self.compute_worker_client = GenericClient(self.service_dir_client, name="compute_worker")

    def parse_requested_fib(self, raw_requested_fib):
        try:
            return int(raw_requested_fib)
        except ValueError, e:
            self.log.warn('Request to %s resulted in %s' % (
                raw_requested_fib, str(e)))
            return None
Beispiel #3
0
class PricingServer(PricingBase, Server):
    def server_init(self):
        self.log = LogSinkClient(self.service_dir_client)
        self.userstore_client = UserStoreClient(self.service_dir_client)

    def price_request(self, requested_fib):
        return int(floor(logarithm(requested_fib, 10))) + 1

    def pay_for_user_request(self, requested_fib, username):
        request_cost = self.price_request(requested_fib)
        credit = self.userstore_client.get_credit(username)
        if credit < request_cost:
            self.log.info('User "%s" denied fib(%s), credit: %s' %
                          (username, requested_fib, credit))
            return [
                False,
                "Error: fib(%s) costs %s, user %s has insufficient credit(%s)"
                % (requested_fib, request_cost, username, credit)
            ]
        new_credit = self.userstore_client.increment_credit(
            username, -1 * request_cost)
        self.log.info("%s used %s credit, balance: %s" %
                      (username, request_cost, new_credit))
        return [True, new_credit]
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)
Beispiel #6
0
 def server_init(self):
     self.log = LogSinkClient(self.service_dir_client)
     self.userstore_client = UserStoreClient(self.service_dir_client)
Beispiel #7
0
 def server_init(self):
     self.log = LogSinkClient(self.service_dir_client)
     self.pricing_client = PricingClient(self.service_dir_client)
     self.compute_worker_client = GenericClient(self.service_dir_client, name="compute_worker")
 def server_init(self):
     self.log = LogSinkClient(self.service_dir_client)
     self.userstore_client = UserStoreClient(self.service_dir_client)
Beispiel #9
0
 def server_init(self):
     self.log = LogSinkClient(self.service_dir_client)
     self.userstore_client = UserStoreClient(self.service_dir_client)
     self.controller_client = GenericClient(self.service_dir_client, name="controller")
     self.basic_auth = HTTPBasic(self.wsgi_app_post_auth, self.userstore_client) 
Beispiel #10
0
class GatekeeperServer(Server):

    NAME = "gatekeeper"
    
    def server_init(self):
        self.log = LogSinkClient(self.service_dir_client)
        self.userstore_client = UserStoreClient(self.service_dir_client)
        self.controller_client = GenericClient(self.service_dir_client, name="controller")
        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=raw_requested_fib,
                username=username)
        else:
            status, body = ["401 UNAUTHENTICATED", "Please log in"]
        return http_response(start_response, status, body)

    def wsgi_app(self, environ, start_response):
        self.set_environment(self.environment)
        self.set_server_name(self.name)
        set_new_request_id()
        response = self.basic_auth(environ, start_response)
        # clear request meta
        set_request_meta({})
        return response
 def server_init(self):
     self.users = {}
     self.credit = {}
     self.log = LogSinkClient()
     self._init_user_db()
 def server_init(self):
     self.users = {}
     self.credit = {}
     self.log = LogSinkClient()
     self._init_user_db()