Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Add configurable options #1

Merged
merged 4 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,24 @@ if __name__ == "__main__":
print("Secret: %s" % secret)
```

- Run the sample application. You must provide the dedicated user's access key and secret key, and the region as environment variables.
- Run the sample application. You must provide the dedicated user's access key and secret key, and the region as environment variables or pass to `configure` function.

### Provide as environment variables
```bash
$ export SECRETKEEPER_AWS_ACCESS_KEY="YOUR_ACCESS_KEY_ID"
$ export SECRETKEEPER_AWS_SECRET_KEY="YOUR_SECRET_ACCESS_KEY"
$ export SECRETKEEPER_AWS_REGION="us-east-1"
$ python sample.py
Secret: pa$$w@rd!
```

### Pass to `configure` function
```python
from ridi import secret_keeper

secret_keeper.configure(
aws_access_key="YOUR_ACCESS_KEY_ID",
aws_secret_key="YOUR_SECRET_ACCESS_KEY",
aws_region="us-east-1",
)
```
40 changes: 2 additions & 38 deletions ridi/secret_keeper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,2 @@
import os

import boto3

ENVNAME_AWS_ACCESS_KEY = "SECRETKEEPER_AWS_ACCESS_KEY"
ENVNAME_AWS_SECRET_KEY = "SECRETKEEPER_AWS_SECRET_KEY"
ENVNAME_AWS_REGION = "SECRETKEEPER_AWS_REGION"


def _get_client():
access_key = os.environ[ENVNAME_AWS_ACCESS_KEY]
secret_key = os.environ[ENVNAME_AWS_SECRET_KEY]
region = os.environ[ENVNAME_AWS_REGION]

client = boto3.client(
'ssm',
region_name=region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
)
return client


def tell(alias):
response = _get_client().get_parameter(
Name=alias, WithDecryption=True,
)
return response['Parameter']['Value']


def tell_safe(alias):
try:
return tell(alias)
except Exception:
return None


__all__ = ["tell", "tell_safe", "ENVNAME_AWS_ACCESS_KEY", "ENVNAME_AWS_SECRET_KEY", "ENVNAME_AWS_REGION"]
from .config import *
from .connect import *
31 changes: 31 additions & 0 deletions ridi/secret_keeper/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os

from collections import namedtuple

ENVNAME_AWS_ACCESS_KEY = "SECRETKEEPER_AWS_ACCESS_KEY"
ENVNAME_AWS_SECRET_KEY = "SECRETKEEPER_AWS_SECRET_KEY"
ENVNAME_AWS_REGION = "SECRETKEEPER_AWS_REGION"

ConnectArgsType = namedtuple('ConnectArgsType', [
'aws_access_key', 'aws_secret_key', 'aws_region'
])

AWS_CONNECT_ARGS = None


def configure(aws_access_key=None, aws_secret_key=None, aws_region=None):
global AWS_CONNECT_ARGS
AWS_CONNECT_ARGS = ConnectArgsType(
aws_access_key=aws_access_key,
aws_secret_key=aws_secret_key,
aws_region=aws_region,
)


configure(
aws_access_key=os.environ.get(ENVNAME_AWS_ACCESS_KEY),
aws_secret_key=os.environ.get(ENVNAME_AWS_SECRET_KEY),
aws_region=os.environ.get(ENVNAME_AWS_REGION),
)

__all__ = ["configure", "ENVNAME_AWS_ACCESS_KEY", "ENVNAME_AWS_SECRET_KEY", "ENVNAME_AWS_REGION"]
30 changes: 30 additions & 0 deletions ridi/secret_keeper/connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import boto3


def _get_client():
from ridi.secret_keeper.config import AWS_CONNECT_ARGS

client = boto3.client(
'ssm',
region_name=AWS_CONNECT_ARGS.aws_region,
aws_access_key_id=AWS_CONNECT_ARGS.aws_access_key,
aws_secret_access_key=AWS_CONNECT_ARGS.aws_secret_key,
)
return client


def tell(alias):
response = _get_client().get_parameter(
Name=alias, WithDecryption=True,
)
return response['Parameter']['Value']


def tell_safe(alias):
try:
return tell(alias)
except Exception:
return None


__all__ = ["tell", "tell_safe"]
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
packages=[
'ridi.secret_keeper',
],
version='0.1.2',
version='0.2.0',
description='Secret Keeper',
url='/~https://github.com/ridi/secret-keeper-python',
keywords=['secret', 'secret-keeper', 'ridi', 'ridibooks'],
Expand All @@ -20,4 +20,4 @@
install_requires=[
'boto3>=1.9.16',
],
)
)