def test_omegaauth_headers(self): class Request: headers = {} auth = OmegaRestApiAuth("foo", "bar", qualifier='xyz') self.assertEqual(auth.get_credentials(), 'ApiKey foo:bar') r = Request() auth(r) self.assertEqual(r.headers['Authorization'], 'ApiKey foo:bar') self.assertEqual(r.headers['Qualifier'], 'xyz')
def test_omegaauth_qualifier(self): auth = OmegaRestApiAuth("foo", "bar", qualifier='xyz') self.assertEqual(auth.username, 'foo') self.assertEqual(auth.apikey, 'bar') self.assertEqual( repr(auth), 'OmegaRestApiAuth(username=foo, apikey="*****",qualifier=xyz)')
def _request_service_api(self, om, method, service=None, data=None, uri=None): """ call a service API for a given omega instance Args: om (Omega): the omega instance, must be authenticated to the cloud method (str): the method verb (CREATE, GET, POST, UPDATE, DELETE) service (str): the name of the service endpoint as in /api/service/<name> data (dict): the data to send Returns: The response json """ auth = OmegaRestApiAuth.make_from(om) restapi_url = getattr(om.defaults, 'OMEGA_RESTAPI_URL', 'not configured') uri = uri or '/admin/api/v2/service/{service}'.format(**locals()) service_url = '{restapi_url}{uri}'.format(**locals()) method = getattr(requests, method) resp = method(service_url, json=data, auth=auth) resp.raise_for_status() if resp.status_code == requests.codes.created: # always request the actual object url = resp.headers['Location'] resp = requests.get(url, auth=auth) resp.raise_for_status() return resp.json()
def test_omegaauth(self): """ test OmegaRestApiAuth works ok """ auth = OmegaRestApiAuth("foo", "bar") self.assertEqual(auth.username, 'foo') self.assertEqual(auth.apikey, 'bar') self.assertEqual( repr(auth), 'OmegaRestApiAuth(username=foo, apikey="*****",qualifier=default)')
def get_omega_from_apikey(userid, apikey, api_url=None, requested_userid=None, qualifier=None, view=False): """ setup an Omega instance from userid and apikey :param userid: the userid :param apikey: the apikey :param api_url: the api URL :param requested_userid: the userid to request config for. in this case userid and apikey must for a staff user for the request to succeed :param qualifier: the database qualifier requested. defaults to 'default' :returns: the Omega instance configured for the given user """ from omegaml.client.cloud import OmegaCloud from omegaml import settings, _base_config defaults = settings(reload=True) qualifier = qualifier or 'default' api_url = ensure_api_url(api_url, defaults) if api_url.startswith('http') or any('test' in v for v in sys.argv): api_auth = OmegaRestApiAuth(userid, apikey) configs = get_user_config_from_api(api_auth, api_url=api_url, requested_userid=requested_userid, view=view) configs = configs['objects'][0]['data'] elif api_url == 'local': configs = { k: getattr(defaults, k) for k in dir(defaults) if k.startswith('OMEGA') } else: raise ValueError('invalid api_url {}'.format(api_url)) if qualifier == 'default': config = configs.get(qualifier, configs) else: config = configs[qualifier] # update _base_config.update_from_dict(config, attrs=defaults) _base_config.load_framework_support(defaults) _base_config.load_user_extensions(defaults) auth = OmegaRuntimeAuthentication(userid, apikey, qualifier) om = OmegaCloud(defaults=defaults, auth=auth) # update config to reflect request om.defaults.OMEGA_RESTAPI_URL = api_url om.defaults.OMEGA_USERID = userid om.defaults.OMEGA_APIKEY = apikey return om
def save_userconfig_from_apikey(configfile, userid, apikey, api_url=None, requested_userid=None, view=False): from omegaml import settings defaults = settings() api_url = api_url or defaults.OMEGA_RESTAPI_URL with open(configfile, 'w') as fconfig: auth = OmegaRestApiAuth(userid, apikey) configs = get_user_config_from_api(auth, api_url=api_url, requested_userid=requested_userid, view=view) config = configs['objects'][0]['data'] config['OMEGA_RESTAPI_URL'] = api_url yaml.safe_dump(config, fconfig, default_flow_style=False) print("Config is in {configfile}".format(**locals()))
def get_omega_from_apikey(userid, apikey, api_url=None, requested_userid=None, qualifier=None, view=False): """ setup an Omega instance from userid and apikey :param userid: the userid :param apikey: the apikey :param api_url: the api URL :param requested_userid: the userid to request config for. in this case userid and apikey must for a staff user for the request to succeed :param qualifier: the database qualifier requested. defaults to 'default' :returns: the Omega instance configured for the given user """ from omegaml import Omega from omegaml import settings, _base_config defaults = settings() qualifier = qualifier or 'default' api_url = api_url or defaults.OMEGA_RESTAPI_URL if api_url.startswith('http') or any('test' in v for v in sys.argv): api_auth = OmegaRestApiAuth(userid, apikey) configs = get_user_config_from_api(api_auth, api_url=api_url, requested_userid=requested_userid, view=view) configs = configs['objects'][0]['data'] elif api_url == 'local': configs = { k: getattr(defaults, k) for k in dir(defaults) if k.startswith('OMEGA') } else: raise ValueError('invalid api_url {}'.format(api_url)) if qualifier == 'default': config = configs.get(qualifier, configs) else: config = configs[qualifier] _base_config.update_from_dict(config) settings(reload=True) om = Omega(defaults=defaults) return om
def setUp(self): self.client = RequestsLikeTestClient(app, is_json=True) self.om = Omega() self.auth = OmegaRestApiAuth('user', 'pass') self.clean()
def setUp(self): self.client = RequestsLikeTestClient(app) self.om = Omega() self.auth = OmegaRestApiAuth('user', 'pass')
def setUp(self): self.client = RequestsLikeTestClient(app) self.om = Omega()['mybucket'] self.auth = OmegaRestApiAuth('user', 'pass') self.clean() self.clean('mybucket')