def seed_aws_roles(client, mount, roles, opt): """Handles the seeding of roles associated with an AWS account""" for role in roles: aomi.validation.aws_role_obj(role) role_path = "%s/roles/%s" % (mount, role['name']) if role.get('state', 'present') == 'present': if 'policy' in role: role_file = hard_path(role['policy'], opt.policies) role_template_obj = role.get('vars', {}) cli_obj = merge_dicts(load_var_files(opt), cli_hash(opt.extra_vars)) obj = merge_dicts(role_template_obj, cli_obj) data = render(role_file, obj) log( 'writing inline role %s from %s' % (role['name'], role_file), opt) write(client, role_path, {'policy': data}, opt) elif 'arn' in role: log('writing role %s for %s' % (role['name'], role['arn']), opt) write(client, role_path, {'arn': role['arn']}, opt) else: log('removing role %s' % role['name'], opt) delete(client, role_path, opt)
def __init__(self, obj, opt): super(Policy, self).__init__(obj, opt) self.path = obj['name'] if self.present: self.filename = hard_path(obj['file'], opt.policies) cli_obj = merge_dicts(load_var_files(opt), cli_hash(opt.extra_vars)) self._obj = merge_dicts(cli_obj, obj.get('vars', {}))
def blend_vars(secrets, opt): """Blends secret and static variables together""" extra_obj = merge_dicts(load_var_files(opt), cli_hash(opt.extra_vars)) merged = merge_dicts(extra_obj, secrets) template_obj = dict((k, v) for k, v in iteritems(merged) if v) # give templates something to iterate over template_obj['aomi_items'] = template_obj.copy() return template_obj
def obj(self): s_obj = {} if 'policy' in self._obj: role_template_obj = self._obj.get('vars', {}) cli_obj = merge_dicts(load_var_files(self.opt), cli_hash(self.opt.extra_vars)) template_obj = merge_dicts(role_template_obj, cli_obj) s_obj = {'policy': render(self._obj['policy'], template_obj)} elif 'arn' in self._obj: s_obj = {'arn': self._obj['arn']} return s_obj
def __init__(self, obj, opt): super(Policy, self).__init__(obj, opt) self.path = obj['name'] if self.present: self.filename = obj['file'] base_obj = load_vars(opt) self._obj = merge_dicts(base_obj, obj.get('vars', {}))
def __init__(self, obj, opt): super(Policy, self).__init__(obj, opt) self.path = obj['name'] if self.present: self.filename = obj['file'] base_obj = load_vars(opt) self._obj = merge_dicts(base_obj, obj.get('vars', {}))
def blend_vars(secrets, opt): """Blends secret and static variables together""" base_obj = load_vars(opt) merged = merge_dicts(base_obj, secrets) template_obj = dict((k, v) for k, v in iteritems(merged) if v) # give templates something to iterate over template_obj['aomi_items'] = template_obj.copy() return template_obj
def blend_vars(secrets, opt): """Blends secret and static variables together""" base_obj = load_vars(opt) merged = merge_dicts(base_obj, secrets) template_obj = dict((k, v) for k, v in iteritems(merged) if v) # give templates something to iterate over template_obj['aomi_items'] = template_obj.copy() return template_obj
def load_var_files(opt): """Load variable files, merge, return contents""" obj = {} for var_file in opt.extra_vars_file: yamlz = yaml.safe_load(open(abspath(var_file)).read()) obj = merge_dicts(obj.copy(), yamlz) return obj
def load_vars(opt): """Loads variable from cli and var files, passing in cli options as a seed (although they can be overwritten!). Note, turn this into an object so it's a nicer "cache".""" if not hasattr(opt, '_vars_cache'): cli_opts = cli_hash(opt.extra_vars) setattr(opt, '_vars_cache', merge_dicts(load_var_files(opt, cli_opts), cli_opts)) return getattr(opt, '_vars_cache')
def load_var_files(opt, p_obj=None): """Load variable files, merge, return contents""" obj = {} if p_obj: obj = p_obj for var_file in opt.extra_vars_file: LOG.debug("loading vars from %s", var_file) obj = merge_dicts(obj.copy(), load_var_file(var_file, obj)) return obj
def obj(self): s_obj = {} if 'policy' in self._obj: role_template_obj = self._obj.get('vars', {}) base_obj = load_vars(self.opt) template_obj = merge_dicts(role_template_obj, base_obj) aws_role = render(self._obj['policy'], template_obj) aws_role = aws_role.replace(" ", "").replace("\n", "") s_obj = {'policy': aws_role} elif 'arn' in self._obj: s_obj = {'arn': self._obj['arn']} return s_obj
def obj(self): s_obj = {} if 'policy' in self._obj: role_template_obj = self._obj.get('vars', {}) base_obj = load_vars(self.opt) template_obj = merge_dicts(role_template_obj, base_obj) aws_role = render(self._obj['policy'], template_obj) aws_role = aws_role.replace(" ", "").replace("\n", "") s_obj = {'policy': aws_role} elif 'arn' in self._obj: s_obj = {'arn': self._obj['arn']} return s_obj
def policy_data(file_name, policy_vars, opt): """Returns the rendered policy""" policy_path = hard_path(file_name, opt.policies) cli_obj = merge_dicts(load_var_files(opt), cli_hash(opt.extra_vars)) obj = merge_dicts(policy_vars, cli_obj) return render(policy_path, obj)
def get_secretfile(opt): """Renders, YAMLs, and returns the Secretfile construct""" secretfile_path = abspath(opt.secretfile) obj = merge_dicts(load_var_files(opt), cli_hash(opt.extra_vars)) return yaml.safe_load(render(secretfile_path, obj))