| 1 |
# Copyright (c) 2015, Ensighten <[email protected]> |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
author: Unknown (!UNKNOWN) |
| 9 |
name: credstash |
| 10 |
short_description: Retrieve secrets from Credstash on AWS |
| 11 |
requirements: |
| 12 |
- credstash (python library) |
| 13 |
description: |
| 14 |
- "Credstash is a small utility for managing secrets using AWS's KMS and DynamoDB: https://github.com/fugue/credstash." |
| 15 |
options: |
| 16 |
_terms: |
| 17 |
description: Term or list of terms to lookup in the credit store. |
| 18 |
type: list |
| 19 |
elements: string |
| 20 |
required: true |
| 21 |
table: |
| 22 |
description: Name of the credstash table to query. |
| 23 |
type: str |
| 24 |
default: 'credential-store' |
| 25 |
version: |
| 26 |
description: Credstash version. |
| 27 |
type: str |
| 28 |
default: '' |
| 29 |
region: |
| 30 |
description: AWS region. |
| 31 |
type: str |
| 32 |
profile_name: |
| 33 |
description: AWS profile to use for authentication. |
| 34 |
type: str |
| 35 |
env: |
| 36 |
- name: AWS_PROFILE |
| 37 |
aws_access_key_id: |
| 38 |
description: AWS access key ID. |
| 39 |
type: str |
| 40 |
env: |
| 41 |
- name: AWS_ACCESS_KEY_ID |
| 42 |
aws_secret_access_key: |
| 43 |
description: AWS access key. |
| 44 |
type: str |
| 45 |
env: |
| 46 |
- name: AWS_SECRET_ACCESS_KEY |
| 47 |
aws_session_token: |
| 48 |
description: AWS session token. |
| 49 |
type: str |
| 50 |
env: |
| 51 |
- name: AWS_SESSION_TOKEN |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: first use credstash to store your secrets |
| 56 |
ansible.builtin.shell: credstash put my-github-password secure123 |
| 57 |
|
| 58 |
- name: "Test credstash lookup plugin -- get my github password" |
| 59 |
ansible.builtin.debug: |
| 60 |
msg: "Credstash lookup! {{ lookup('community.general.credstash', 'my-github-password') }}" |
| 61 |
|
| 62 |
- name: "Test credstash lookup plugin -- get my other password from us-west-1" |
| 63 |
ansible.builtin.debug: |
| 64 |
msg: "Credstash lookup! {{ lookup('community.general.credstash', 'my-other-password', region='us-west-1') }}" |
| 65 |
|
| 66 |
- name: "Test credstash lookup plugin -- get the company's github password" |
| 67 |
ansible.builtin.debug: |
| 68 |
msg: "Credstash lookup! {{ lookup('community.general.credstash', 'company-github-password', table='company-passwords') }}" |
| 69 |
|
| 70 |
- name: Example play using the 'context' feature |
| 71 |
hosts: localhost |
| 72 |
vars: |
| 73 |
context: |
| 74 |
app: my_app |
| 75 |
environment: production |
| 76 |
tasks: |
| 77 |
|
| 78 |
- name: "Test credstash lookup plugin -- get the password with a context passed as a variable" |
| 79 |
ansible.builtin.debug: |
| 80 |
msg: "{{ lookup('community.general.credstash', 'some-password', context=context) }}" |
| 81 |
|
| 82 |
- name: "Test credstash lookup plugin -- get the password with a context defined here" |
| 83 |
ansible.builtin.debug: |
| 84 |
msg: "{{ lookup('community.general.credstash', 'some-password', context=dict(app='my_app', environment='production')) }}" |
| 85 |
""" |
| 86 |
|
| 87 |
RETURN = r""" |
| 88 |
_raw: |
| 89 |
description: |
| 90 |
- Value(s) stored in Credstash. |
| 91 |
type: str |
| 92 |
""" |
| 93 |
|
| 94 |
from ansible.errors import AnsibleError |
| 95 |
from ansible.plugins.lookup import LookupBase |
| 96 |
|
| 97 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 98 |
|
| 99 |
CREDSTASH_INSTALLED = False |
| 100 |
|
| 101 |
try: |
| 102 |
import credstash |
| 103 |
|
| 104 |
CREDSTASH_INSTALLED = True |
| 105 |
except ImportError: |
| 106 |
CREDSTASH_INSTALLED = False |
| 107 |
|
| 108 |
|
| 109 |
class LookupModule(LookupBase): |
| 110 |
def run(self, terms, variables=None, **kwargs): |
| 111 |
if not CREDSTASH_INSTALLED: |
| 112 |
raise AnsibleError("The credstash lookup plugin requires credstash to be installed.") |
| 113 |
|
| 114 |
self.set_options(var_options=variables, direct=kwargs) |
| 115 |
check_for_wrong_terms(self, direct=kwargs) |
| 116 |
|
| 117 |
version = self.get_option("version") |
| 118 |
region = self.get_option("region") |
| 119 |
table = self.get_option("table") |
| 120 |
profile_name = self.get_option("profile_name") |
| 121 |
aws_access_key_id = self.get_option("aws_access_key_id") |
| 122 |
aws_secret_access_key = self.get_option("aws_secret_access_key") |
| 123 |
aws_session_token = self.get_option("aws_session_token") |
| 124 |
|
| 125 |
context = { |
| 126 |
k: v |
| 127 |
for k, v in kwargs.items() |
| 128 |
if k |
| 129 |
not in ( |
| 130 |
"version", |
| 131 |
"region", |
| 132 |
"table", |
| 133 |
"profile_name", |
| 134 |
"aws_access_key_id", |
| 135 |
"aws_secret_access_key", |
| 136 |
"aws_session_token", |
| 137 |
) |
| 138 |
} |
| 139 |
|
| 140 |
kwargs_pass = { |
| 141 |
"profile_name": profile_name, |
| 142 |
"aws_access_key_id": aws_access_key_id, |
| 143 |
"aws_secret_access_key": aws_secret_access_key, |
| 144 |
"aws_session_token": aws_session_token, |
| 145 |
} |
| 146 |
|
| 147 |
ret = [] |
| 148 |
for term in terms: |
| 149 |
try: |
| 150 |
ret.append(credstash.getSecret(term, version, region, table, context=context, **kwargs_pass)) |
| 151 |
except credstash.ItemNotFound as e: |
| 152 |
raise AnsibleError(f"Key {term} not found") from e |
| 153 |
except Exception as e: |
| 154 |
raise AnsibleError(f"Encountered exception while fetching {term}: {e}") from e |
| 155 |
|
| 156 |
return ret |