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

Add template lookup feature #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
35 changes: 35 additions & 0 deletions credstash.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,32 @@ def get_session_params(profile, arn):
return params


@clean_fail
def template_lookup(args, region, **session_params):
'''convert credstash secrets in template file for pattern "{{ secrect_key_name }}"
'''

file = args.file
with open(file, 'r+') as f:
data = f.read()
pattern = re.compile(r'({{ ([^}]+) }})')
match = pattern.findall(data)

for source, key in match:
try:
secret = getSecret(key)
except Exception:
print("Can't get the secret %s, did you set it?" % key)
sys.exit(1)

data = data.replace(source, secret)

f.seek(0)
f.truncate()
f.write(data)
f.close()


def get_parser():
"""get the parsers dict"""
parsers = {}
Expand Down Expand Up @@ -881,6 +907,12 @@ def get_parser():
parsers[action] = subparsers.add_parser(action,
help='setup the credential store')
parsers[action].set_defaults(action=action)
action = 'template'
parsers[action] = subparsers.add_parser(action,
help='convert secrets in tempalte')
parsers[action].add_argument("file", type=str,
help="name of template file.")
parsers[action].set_defaults(action=action)
return parsers


Expand Down Expand Up @@ -928,6 +960,9 @@ def main():
createDdbTable(region=region, table=args.table,
**session_params)
return
if args.action == "template":
template_lookup(args, region, **session_params)
return
else:
parsers['super'].print_help()

Expand Down