Skip to content

Commit

Permalink
Issue #11: load credentials from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
machristie committed Apr 30, 2018
1 parent 8d201e0 commit a77a07c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
22 changes: 22 additions & 0 deletions cloudlaunch_cli/api/cloud_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ def load_from_environment(cloud_type):
return AWSCredentials.from_environment()
return None

@staticmethod
def load_from_dict(cloud_type, creds_dict):
"""Load a CloudCredentials subclass instance from given dict."""
if cloud_type == 'aws':
return AWSCredentials.from_dict(creds_dict)
return None

@staticmethod
@abc.abstractmethod
def from_environment():
"""Load and return an instance of CloudCredentials using env vars."""
pass

@staticmethod
@abc.abstractmethod
def from_dict(creds_dict):
"""Load and return an instance of CloudCredentials from given dict."""
pass

@abc.abstractmethod
def to_http_headers(self):
"""Convert credentials to dict of http header name/values."""
Expand All @@ -41,6 +54,15 @@ def from_environment():
else:
return None

@staticmethod
def from_dict(creds_dict):
aws_access_key = creds_dict.get('aws_access_key')
aws_secret_key = creds_dict.get('aws_secret_key')
if aws_access_key and aws_secret_key:
return AWSCredentials(aws_access_key, aws_secret_key)
else:
return None

def to_http_headers(self):
return {
'cl-aws-access-key': self.aws_access_key,
Expand Down
27 changes: 23 additions & 4 deletions cloudlaunch_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@

conf = Configuration()

cli_context = {}


def create_api_client(cloud=None, cloud_credentials_json=None):

cloudlaunch_client = APIClient(url=conf.url, token=conf.token)
# Recreate client with cloud credentials if available
if cloud:
cloud_resource = cloudlaunch_client.infrastructure.clouds.get(cloud)
cloud_creds = CloudCredentials.load_from_environment(
cloud_resource.cloud_type)
# Try to load if specified on command line first, then look in
# environment variables
if 'cloud-credentials' in cli_context:
creds_dict = json.loads(cli_context['cloud-credentials'].read())
cloud_creds = CloudCredentials.load_from_dict(
cloud_resource.cloud_type, creds_dict)
else:
cloud_creds = CloudCredentials.load_from_environment(
cloud_resource.cloud_type)
if cloud_creds:
return APIClient(url=conf.url, token=conf.token,
cloud_credentials=cloud_creds)
Expand Down Expand Up @@ -64,8 +73,18 @@ def show_config():


@click.group()
def deployments():
pass
@click.option('--cloud-credentials', type=click.File('rb'),
help="JSON file with cloud credentials.")
def deployments(cloud_credentials):
"""Manage CloudLaunch deployments.
You can pass cloud credentials either as environment variables or as a JSON
file.
TODO: document the expected names of variables.
"""
global cli_context
if cloud_credentials:
cli_context['cloud-credentials'] = cloud_credentials


@click.command()
Expand Down

0 comments on commit a77a07c

Please sign in to comment.