-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #11: Load AWS creds from env, pass as http headers
- Loading branch information
1 parent
7cd5b20
commit 8d201e0
Showing
4 changed files
with
89 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Utilities for handling cloud credentials.""" | ||
import abc | ||
import os | ||
|
||
|
||
class CloudCredentials(abc.ABC): | ||
"""Base class representing cloud credentials.""" | ||
|
||
@staticmethod | ||
def load_from_environment(cloud_type): | ||
"""Load a CloudCredentials subclass instance from env vars.""" | ||
if cloud_type == 'aws': | ||
return AWSCredentials.from_environment() | ||
return None | ||
|
||
@staticmethod | ||
@abc.abstractmethod | ||
def from_environment(): | ||
"""Load and return an instance of CloudCredentials using env vars.""" | ||
pass | ||
|
||
@abc.abstractmethod | ||
def to_http_headers(self): | ||
"""Convert credentials to dict of http header name/values.""" | ||
pass | ||
|
||
|
||
class AWSCredentials(CloudCredentials): | ||
"""CloudCredentials subclass representing AWS credentials.""" | ||
|
||
def __init__(self, aws_access_key, aws_secret_key): | ||
self.aws_access_key = aws_access_key | ||
self.aws_secret_key = aws_secret_key | ||
|
||
@staticmethod | ||
def from_environment(): | ||
aws_access_key = os.environ.get('AWS_ACCESS_KEY') | ||
aws_secret_key = os.environ.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, | ||
'cl-aws-secret-key': self.aws_secret_key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters