Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit e59db22

Browse files
author
Nish Bhat
committed
Support retrieving secrets from SSM Parameter Store
1 parent 307f951 commit e59db22

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

clrenv/lazy_env.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
from itertools import chain, groupby
77
import shlex
88
import sys
9-
import types
109

1110
from munch import Munch, munchify
1211
import yaml
1312

1413
from .path import find_environment_path, find_user_environment_paths
1514
from functools import reduce
1615

17-
1816
class LazyEnv(object):
1917
def __init__(self):
2018
self.__mode = tuple(shlex.split(environ.get('CLRENV_MODE', '')))
@@ -152,6 +150,14 @@ def _get_keyfile_cache():
152150
_kf_dict_cache = clrypt.read_file_as_dict('keys', 'keys')
153151
return _kf_dict_cache
154152

153+
_ssm_client = None
154+
def _get_ssm_client():
155+
import boto3
156+
global _ssm_client
157+
if not _ssm_client:
158+
_ssm_client = boto3.client('ssm')
159+
return _ssm_client
160+
155161
def _clear_keyfile_cache():
156162
global _kf_dict_cache
157163
_kf_dict_cache = {}
@@ -162,19 +168,26 @@ def _apply_functions(d, recursive=False):
162168
163169
^function rest
164170
165-
Currently, the only function available is `keyfile', which attempts
166-
to replace with a value from the currently loaded keyfile."""
171+
Available functions:
172+
^keyfile: Looks up the given value in the current environment's clrypt keyfile.
173+
^parameter: Looks up the given value in AWS Parameter store.
174+
"""
167175
new = Munch()
168176

169-
for k, v in list(d.items()):
170-
if isinstance(v, dict):
171-
v = _apply_functions(v, recursive=True)
172-
elif isinstance(v, str):
173-
if v.startswith('^keyfile '):
174-
v = v[9:]
175-
v = _get_keyfile_cache().get(v, '')
176-
177-
new[k] = v
177+
for key, value in list(d.items()):
178+
if isinstance(value, dict):
179+
value = _apply_functions(value, recursive=True)
180+
elif isinstance(value, str):
181+
if value.startswith('^keyfile '):
182+
value = value[9:]
183+
value = _get_keyfile_cache().get(value, '')
184+
elif value.startswith("^parameter "):
185+
value = value.split(' ', 1)[1]
186+
value = _get_ssm_client().get_parameter(
187+
Name=value,
188+
WithDecryption=True
189+
)['Parameter']['Value']
190+
new[key] = value
178191

179192
if not recursive:
180193
# Cache no longer needed, clear encrypted data.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
PyYAML==4.2b1
22
munch==2.2.0
33
future==0.16.0
4+
boto3==1.5.36
45
-e git+https://github.com/ColorGenomics/[email protected]#egg=clrypt

0 commit comments

Comments
 (0)