Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authdriver #236

Merged
merged 16 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
29a108e
Added provide_default filter to provide default values if value is nu…
samvarankashyap May 12, 2017
c3f6f4e
added auth_driver module to fetch credentials from specified locations
samvarankashyap May 12, 2017
594627c
updated schema v3 and schema_v4 to incorporate auth_driver functionality
samvarankashyap May 12, 2017
a9341ad
Added credentials folder to linchpin conf and templates
samvarankashyap May 12, 2017
442e9e7
updated openstack playbooks to incorporate auth_driver functionality
samvarankashyap May 12, 2017
2ffac00
updated aws playbooks to incorporate auth_driver functionality
samvarankashyap May 12, 2017
84c41ec
updated gcloud playbooks to incorporate auth_driver functionality
samvarankashyap May 12, 2017
b5e1291
Updated linchpin cli , and api to fetch the creds_path variable pass …
samvarankashyap May 15, 2017
11af8b4
Renamed the parameter --creds to --creds-path
samvarankashyap May 22, 2017
df29db9
bugfix: Make provide default return the fetched items
samvarankashyap May 22, 2017
48d165d
Updated schema for aws resource group type
samvarankashyap May 22, 2017
810f991
Updated Authdriver references inside aws role
samvarankashyap May 22, 2017
80655fb
Updated setting for creds_path evar param using set_evar
samvarankashyap May 22, 2017
2d7176d
Added multiple tasks to handle auth_var when not given, as its not po…
samvarankashyap May 22, 2017
ee7fd2f
Added creds_path to linchpin api context and defaulted it to None
samvarankashyap May 22, 2017
0fd98c7
bugfix: Updated gcloud schema , added name, auth_type to credentials …
samvarankashyap May 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion linchpin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ def get_command(self, ctx, name):
help='Enable verbose output')
@click.option('--version', is_flag=True,
help='Prints the version and exits')
@click.option('-cp', '--creds-path', type=click.Path(), envvar='LP_CREDS',
help='Use the specified credentials path if WORKSPACE environment variable '
'is not set')
@pass_context
def runcli(ctx, config, workspace, verbose, version):
def runcli(ctx, config, workspace, verbose, version, creds_path):
"""linchpin: hybrid cloud orchestration"""

ctx.verbose = verbose
Expand All @@ -80,8 +83,16 @@ def runcli(ctx, config, workspace, verbose, version):

if workspace is not None:
ctx.workspace = os.path.realpath(os.path.expanduser(workspace))
else:
ctx.workspace = os.getenv('PWD')

if creds_path is not None:
ctx.creds_path = os.path.realpath(os.path.expanduser(creds_path))
else:
ctx.creds_path = str(None)

ctx.log_debug("ctx.workspace: {0}".format(ctx.workspace))
ctx.log_debug("ctx.creds_path: {0}".format(ctx.creds_path))

ctx.pinfile = ctx.cfgs['init']['pinfile']

Expand Down
2 changes: 2 additions & 0 deletions linchpin/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def run_playbook(self, pinfile, targets='all', playbook='up'):
if self.ctx.cfgs.get('ansible'):
ansible_console = ast.literal_eval(self.ctx.cfgs['ansible'].get('console', 'False'))

self.set_evar('creds_path', self.ctx.creds_path)

if not ansible_console:
ansible_console = self.ctx.verbose

Expand Down
1 change: 1 addition & 0 deletions linchpin/api/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self):
self.lib_path = os.path.realpath(os.path.join(lib_path, os.pardir))

self.workspace = os.path.realpath(os.path.curdir)
self.creds_path = None


def load_config(self, lpconfig=None):
Expand Down
15 changes: 13 additions & 2 deletions linchpin/defaults/schemas/schema_v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -643,9 +643,20 @@
"assoc_creds": {
"description":"contains creds file associated to this resource",
"type":"string"
}
},
"credentials": {
"type": "object",
"properties":{
"name": {
"type": "string"
},
"auth_type": {
"type": "string"
}
}
}
},
"required":["resource_group_name","res_group_type","res_defs","assoc_creds"],
"required":["resource_group_name","res_group_type","res_defs"],
"additionalProperties": true
},
"aws": {
Expand Down
48 changes: 33 additions & 15 deletions linchpin/defaults/schemas/schema_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@
"description": "Date of creation",
"type": "string"
},
"site": {
"description": "site of provisioning ",
"type": "string"
},
"credentials": {
"description": "contains various credential_vaults names",
"type": "array",
"items":{
"type":"string"
},
"minimum":1
},
"resource_groups": {
"description": "contains list of resources ",
"type": "array",
Expand Down Expand Up @@ -705,7 +693,18 @@
},
"credentials": {
"description":"contains creds file associated to this resource",
"type":"string"
"type":"object",
"properties": {
"profile": {
"type": "string"
},
"auth_type": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
},
"required":["resource_group_name","resource_group_type","resource_definitions","credentials"],
Expand Down Expand Up @@ -736,7 +735,18 @@
},
"credentials": {
"description":"contains creds file associated to this resource",
"type":"string"
"type":"object",
"properties": {
"profile": {
"type": "string"
},
"auth_type": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
},
"required":["resource_group_name","resource_group_type","resource_definitions","credentials"],
Expand All @@ -763,7 +773,15 @@
},
"credentials": {
"description":"contains creds file associated to this resource",
"type":"string"
"type":"object",
"properties": {
"auth_type": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
},
"required":["resource_group_name","resource_group_type","resource_definitions","credentials"],
Expand Down
2 changes: 1 addition & 1 deletion linchpin/linchpin.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async = False
async_timeout = 1000
output = True
check_mode = False

creds_path = None
# default paths in playbooks
#
# lp_path = <src_dir>/linchpin
Expand Down
15 changes: 15 additions & 0 deletions linchpin/provision/filter_plugins/provide_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
import os

def provide_default(fetched, default):
if fetched == "":
return default
else:
return fetched

class FilterModule(object):
''' A filter to fix interface's name format '''
def filters(self):
return {
'provide_default': provide_default
}
86 changes: 71 additions & 15 deletions linchpin/provision/library/auth_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@
- This module allows a user to fetch credentials on request and egister it as variable in ansible.

options:
type:
name:
description:
type of credential required
name of the credential file to be used
required: true

cred_type:
description:
credential type , type of credential to be used.
eg: aws, gcloud , openstack , etc.,
required: false
cred_path:
description:
credentials path where the credentials are to be stored
required: false
driver:
description:
defaults to file type.
required: true

author: Samvaran Kashyap Rallabandi -
'''

Expand All @@ -29,28 +42,71 @@
import shlex
import tempfile
import yaml
import glob
try:
import configparser as ConfigParser
except ImportError:
import ConfigParser as ConfigParser


def check_file_paths(module, *args):
for file_path in args:
if not os.path.exists(file_path):
module.fail_json(msg= "File not found %s not found" % (file_path))
if not os.access(file_path, os.R_OK):
module.fail_json(msg= "File not accesible %s not found" % (file_path))
if os.path.isdir(file_path):
module.fail_json(msg= "Recursive directory not supported %s " % (file_path))
class ConfigDict(ConfigParser.ConfigParser):

def as_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(self._defaults, **d[k])
d[k].pop('__name__', None)
return d

def list_files(path):
return glob.glob(path+"/*.*")

def parse_file(filename):
cred_str = open(filename, "r").read()
try:
out = json.loads(cred_str)
except Exception as e:
try:
out = yaml.load(cred_str)
except Exception as e:
try:
config = ConfigDict()
f = open(filename)
config.readfp(f)
out = config.as_dict()
f.close()
except Exception as e:
module.fail_json(msg= "Error {0} ".format(str(e)))
return out

def get_cred(name, creds_path):
paths = creds_path.split(";")
files = []
for path in paths:
files = list_files(path)
for filename in files:
if name == filename.split("/")[-1].split(".")[0]:
out = parse_file(filename)
return out, path
module.fail_json(msg= "Error: Credential not found")
def main():
global module
module = AnsibleModule(
argument_spec={
'type': {'required': True, 'aliases': ['auth_type']},
'creds_store': {'required': False, 'aliases': ['credential_store']},
'name': {'required': True, 'aliases': ['name']},
'cred_type': {'required': False, 'aliases': ['credential_type']},
'cred_path': {'required': True, 'aliases': ['credential_store']},
'driver': {'required': True, 'aliases': ['driver_type']},
},
required_one_of=[],
supports_check_mode=True
)
name = module.params["name"]
cred_type = module.params["cred_type"]
cred_path = module.params["cred_path"]
driver_type = module.params["driver"]
output, path = get_cred(name, cred_path)
changed = True
module.exit_json(changed=changed, output={})
module.exit_json(changed=changed, output=output, params=module.params, path=path)

from ansible.module_utils.basic import *
main()
2 changes: 1 addition & 1 deletion linchpin/provision/roles/aws/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
include: teardown_resource_group.yml res_grp={{ item.0 }} topo_output_file={{ item.1 }}
with_nested:
- "{{ aws_res_grps }}"
- ["{{ resources_file | default( default_resources_path+'/'+outputs.topology_name+'.output.yaml' ) }}"]
- ["{{ resources_file | default( default_resources_path+'/'+outputs.topology_name+'.output' ) }}"]
when: state == "absent"
8 changes: 4 additions & 4 deletions linchpin/provision/roles/aws/tasks/provision_aws_cfn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

- name: "Provision cloud formation stack"
cloudformation:
aws_access_key: "{{ aws_access_key_id | default(omit) }}"
aws_secret_key: "{{ aws_secret_access_key | default(omit) }}"
aws_access_key: "{{ auth_var['aws_access_key_id'] | default(omit) }}"
aws_secret_key: "{{ auth_var['aws_secret_access_key'] | default(omit) }}"
stack_name: "{{ res_def['res_name'] | default(res_def['name']) }}"
state: "{{ state }}"
region: "{{ res_def['region'] }}"
Expand All @@ -27,8 +27,8 @@

- name: "Provision cloud formation stack"
cloudformation:
aws_access_key: "{{ aws_access_key_id | default(omit) }}"
aws_secret_key: "{{ aws_secret_access_key | default(omit) }}"
aws_access_key: "{{ auth_var['aws_access_key_id'] | default(omit) }}"
aws_secret_key: "{{ auth_var['aws_secret_access_key'] | default(omit) }}"
stack_name: "{{ res_def['res_name'] | default(res_def['name']) }}"
state: "{{ state }}"
region: "{{ res_def['region'] }}"
Expand Down
8 changes: 4 additions & 4 deletions linchpin/provision/roles/aws/tasks/provision_aws_ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

- name: "Provisioning AWS_EC2 Resource when not async"
ec2:
aws_access_key: "{{ aws_access_key_id | default(omit) }}"
aws_secret_key: "{{ aws_secret_access_key | default(omit) }}"
aws_access_key: "{{ auth_var['aws_access_key_id'] | default(omit) }}"
aws_secret_key: "{{ auth_var['aws_secret_access_key'] | default(omit) }}"
key_name: "{{ res_def['keypair'] }}"
instance_type: "{{ res_def['flavor'] }}"
image: "{{ res_def['image'] }}"
Expand All @@ -30,8 +30,8 @@

- name: "Async:: Provisioning AWS_EC2 Resource"
ec2:
aws_access_key: "{{ aws_access_key_id | default(omit) }}"
aws_secret_key: "{{ aws_secret_access_key | default(omit) }}"
aws_access_key: "{{ auth_var['aws_access_key_id'] | default(omit) }}"
aws_secret_key: "{{ auth_var['aws_secret_access_key'] | default(omit) }}"
key_name: "{{ res_def['keypair'] }}"
instance_type: "{{ res_def['flavor'] }}"
image: "{{ res_def['image'] }}"
Expand Down
4 changes: 2 additions & 2 deletions linchpin/provision/roles/aws/tasks/provision_aws_ec2_key.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- name: "Provisioning AWS EC2 KEY "
ec2_key:
aws_access_key: "{{ aws_access_key_id | default(omit) }}"
aws_secret_key: "{{ aws_secret_access_key | default(omit) }}"
aws_access_key: "{{ auth_var['aws_access_key_id'] | default(omit) }}"
aws_secret_key: "{{ auth_var['aws_secret_access_key'] | default(omit) }}"
region: "{{ res_def['region'] }}"
name: "{{ res_def['res_name'] | default(res_def['name']) }}"
state: "{{ state }}"
Expand Down
4 changes: 2 additions & 2 deletions linchpin/provision/roles/aws/tasks/provision_aws_s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

- name: "Provisioning AWS_S3 Resource"
s3:
aws_access_key: "{{ aws_access_key_id | default(omit) }}"
aws_secret_key: "{{ aws_secret_access_key | default(omit) }}"
aws_access_key: "{{ auth_var['aws_access_key_id'] | default(omit) }}"
aws_secret_key: "{{ auth_var['aws_secret_access_key'] | default(omit) }}"
bucket: "{{ res_def['res_name'] | default(res_def['name']) }}"
mode: "{{ s3_mode }}"
region: "{{ res_def['region'] }}"
Expand Down
4 changes: 2 additions & 2 deletions linchpin/provision/roles/aws/tasks/provision_aws_sg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name: "{{ res_def['res_name'] | default(res_def['name']) }}"
description: "{{ res_def['description']}}"
region: "{{ res_def['region']}}"
aws_access_key: "{{ aws_access_key_id }}"
aws_secret_key: "{{ aws_secret_access_key }}"
aws_access_key: "{{ auth_var['aws_access_key_id'] }}"
aws_secret_key: "{{ auth_var['aws_secret_access_key'] }}"
rules: "{{ res_def['rules'] | aws_sg_rules('inbound') }}"
rules_egress: "{{ res_def['rules'] | aws_sg_rules('outbound') }}"
24 changes: 21 additions & 3 deletions linchpin/provision/roles/aws/tasks/provision_resource_group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
debug:
msg: "The current Resource Group obj is {{ res_grp }} "

- name: "Including credentials of current resource {{ res_grp['resource_group_name'] }} "
include_vars: "roles/aws/vars/{{ res_grp['assoc_creds'] | default(res_grp['credentials']) }}.yml"
no_log: true
- name: "Unset the authvar from previous run"
set_fact:
auth_var: ""

- name: "Set cred profile"
set_fact:
cred_profile: "{{ res_grp['credentials']['profile'] | default('default') }}"

- name: "Get creds from auth driver"
auth_driver:
name: "{{ res_grp['credentials']['name'] }}"
cred_type: "aws"
cred_path: "{{ creds_path }}"
driver: "file"
register: auth_var
ignore_errors: true

- name: "Set auth_var "
set_fact:
auth_var: "{{ auth_var['output'][cred_profile] | default('') }}"
ignore_errors: true

- name: "provisioning resource definitions of current group"
include: provision_res_defs.yml res_def={{ res_item.0 }} res_grp_name={{ res_item.1 }}
Expand Down
Loading