def __init__(self, data): ''' Initialize the action. data: the action description from YML file(s). ''' self.data = data if 'preflight' in data and 'env_variables' in data['preflight']: # pylint: disable=W0612 for key, val in enumerate(data['preflight']['env_variables']): my_env.get(val['name'], val['desc'])
def assign(val, json_string): ''' Set an environment variable if possible. val: json of dict having possible key assign, which itself has an array, each item having key jsonpath, for example $.whatever, and var, for example: ID json: a json string ''' candidates = my_jsonpath.find(val, '$.assign', [], True) # pylint: disable=W0612 for key, candidate in enumerate(json.loads(candidates)): env_var = candidate['var'] jsonpath = candidate['jsonpath'] debug.debug('assignment', 'Checking if jsonpath needs replacements') if 'replace' in candidate: debug.debug('assignment', 'replace key exists') # pylint: disable=W0612 for key2, val2 in enumerate(candidate['replace']): debug.debug('assignment', 'about to perform replacement') jsonpath = jsonpath.replace(val2['string'], my_env.get(val2['env_var'])) debug.debug('assignment', jsonpath) value = my_jsonpath.find(json_string, jsonpath, None, True) debug.debug( 'assignment', 'Assigning environment variable ' + env_var + ' to ' + value) os.environ[env_var] = value
def body(self): ''' Get the body to pass to the API request. ''' ret = {} # pylint: disable=W0612 for key, val in enumerate(self.get_data('body', {})): ret[val['name']] = my_env.get(val['env_var']) debug.debug('action.body returning', ret) return ret
def get_data_with_replacements(self, key, default): ''' Get data, replacing certain strings as per the YML file. For example, see ./plugins/digitalocean/dropletinfo/dropletinfo.yml. ''' candidate = json.dumps(self.get_data(key, default)) # pylint: disable=W0612 for key2, val in enumerate(self.get_data('replace', {})): candidate = candidate.replace(val['string'], my_env.get(val['env_var'])) return json.loads(candidate)
def run(action): ''' Run an action on an Oauth2 type provider. ''' key = my_env.get('KEY') secret = my_env.get('SECRET') myvars = { 'client_id': key, 'client_secret': secret, 'grant_type': 'client_credentials', } api_url = action.auth_base() + '/auth/oauth/token' response = requests.post(api_url, data=myvars) if response.status_code == 200: token = json.loads(response.content.decode('utf-8'))['access_token'] return authenticator_header_bearer.run(action, token) print('==> COULD NOT GET A TOKEN') print(response.status_code) print(json.loads(response.content.decode('utf-8'))) return None
def run(action, token=None): ''' Run an action on a header-bearer type provider. ''' if token is None: token = my_env.get('TOKEN') headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer {0}'.format(token) } api_url = action.url() body = action.body() verb = action.verb() if verb == 'get': response = requests.get(api_url, headers=headers, json=body) elif verb == 'post': response = requests.post(api_url, headers=headers, json=body) elif verb == 'delete': response = requests.delete(api_url, headers=headers, json=body) else: raise Exception('Unknow verb ' + verb) expectedcode = action.successcode() if response.status_code == expectedcode: if action.expecting_content(): ret = response.content.decode('utf-8') return ret return json.dumps(True) print('ERROR - expected status code ' + str(expectedcode) + ' but got ') print(str(response.status_code)) print('For ' + api_url + ' with ' + verb) print(response.content.decode('utf-8')) return None
''' Test Authenticator Multistep. See Testing Individual Files in README.md. ''' import json import authenticator_multistep import my_env print('') print('try and set an environment variable to HELLO') authenticator_multistep.assign( json.dumps({"assign": [{ "jsonpath": "$.whatever", "var": "HELLO" }]}), json.dumps({"whatever": "hello world"})) print('fetching environment variable') print(my_env.get('HELLO'))