summaryrefslogtreecommitdiffstats
path: root/ansible_collections/azure/azcollection/plugins/modules/azure_rm_keyvaultsecret.py
blob: f36c86f1a460fcc8787e5990a7d8f1a2a5b808ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/python
# Copyright: Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type


DOCUMENTATION = '''
---
module: azure_rm_keyvaultsecret
version_added: "0.1.2"
short_description: Use Azure KeyVault Secrets
description:
    - Create or delete a secret within a given keyvault.
    - By using Key Vault, you can encrypt keys and secrets.
    - Such as authentication keys, storage account keys, data encryption keys, .PFX files, and passwords.
options:
    keyvault_uri:
        description:
            - URI of the keyvault endpoint.
        required: true
        type: str
    content_type:
        description:
            - Type of the secret value such as a password.
        type: str
    secret_name:
        description:
            - Name of the keyvault secret.
        required: true
        type: str
    secret_value:
        description:
            - Secret to be secured by keyvault.
        type: str
    secret_expiry:
        description:
            - Optional expiry datetime for secret
        type: str
    secret_valid_from:
        description:
            - Optional valid-from datetime for secret
        type: str
    recover_if_need:
        description:
            - Whether to permanently recover delete secrets.
        type: bool
    purge_if_need:
        description:
            - Whether to permanently delete secrets.
        type: bool
    state:
        description:
            - Assert the state of the subnet. Use C(present) to create or update a secret and C(absent) to delete a secret .
        type: str
        default: present
        choices:
            - absent
            - present

extends_documentation_fragment:
    - azure.azcollection.azure
    - azure.azcollection.azure_tags

author:
    - Ian Philpot (@iphilpot)

'''

EXAMPLES = '''
- name: Create a secret
  azure_rm_keyvaultsecret:
    secret_name: MySecret
    secret_value: My_Pass_Sec
    keyvault_uri: https://contoso.vault.azure.net/
    tags:
      testing: testing
      delete: never

- name: Delete a secret
  azure_rm_keyvaultsecret:
    secret_name: MySecret
    keyvault_uri: https://contoso.vault.azure.net/
    state: absent

- name: Recover a delete secret
  azure_rm_keyvaultsecret:
    secret_name: MySecret
    keyvault_uri: https://contoso.vault.azure.net/
    recover_if_need: true

- name: Purge a delete secret
  azure_rm_keyvaultsecret:
    secret_name: MySecret
    keyvault_uri: https://contoso.vault.azure.net/
    purge_if_need: true
'''

RETURN = '''
state:
    description:
        - Current state of the secret.
    returned: success
    type: complex
    contains:
        secret_id:
          description:
              - Secret resource path.
          type: str
          example: https://contoso.vault.azure.net/secrets/hello/e924f053839f4431b35bc54393f98423
'''

from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase

try:
    from azure.keyvault.secrets import SecretClient
    from azure.core.exceptions import ResourceNotFoundError
    from azure.core.exceptions import HttpResponseError
    import dateutil.parser
except ImportError:
    # This is handled in azure_rm_common
    pass


class AzureRMKeyVaultSecret(AzureRMModuleBase):
    ''' Module that creates or deletes secrets in Azure KeyVault '''

    def __init__(self):

        self.module_arg_spec = dict(
            secret_name=dict(type='str', required=True),
            secret_value=dict(type='str', no_log=True),
            secret_valid_from=dict(type='str', no_log=True),
            secret_expiry=dict(type='str', no_log=True),
            keyvault_uri=dict(type='str', no_log=True, required=True),
            state=dict(type='str', default='present', choices=['present', 'absent']),
            recover_if_need=dict(type='bool'),
            purge_if_need=dict(type='bool'),
            content_type=dict(type='str')
        )

        required_if = [
            ('state', 'present', ['secret_value'])
        ]

        self.results = dict(
            changed=False,
            state=dict()
        )

        self.secret_name = None
        self.secret_value = None
        self.secret_valid_from = None
        self.secret_expiry = None
        self.keyvault_uri = None
        self.state = None
        self.data_creds = None
        self.client = None
        self.tags = None
        self.recover_if_need = None
        self.purge_if_need = None
        self.content_type = None

        super(AzureRMKeyVaultSecret, self).__init__(self.module_arg_spec,
                                                    supports_check_mode=True,
                                                    required_if=required_if,
                                                    supports_tags=True)

    def exec_module(self, **kwargs):

        for key in list(self.module_arg_spec.keys()) + ['tags']:
            setattr(self, key, kwargs[key])

        # Create KeyVault Client
        self.client = self.get_keyvault_client()

        results = dict()
        changed = False

        try:
            results = self.get_secret(self.secret_name)

            # Secret exists and will be deleted
            if self.state == 'absent':
                changed = True
            elif self.secret_value and results['secret_value'] != self.secret_value:
                changed = True

        except ResourceNotFoundError as ec:
            # Secret doesn't exist
            if self.state == 'present':
                changed = True
        except Exception as ec2:
            self.fail("Find the key vault secret got exception, exception as {0}".format(str(ec2)))

        self.results['changed'] = changed
        self.results['state'] = results

        valid_from = self.secret_valid_from
        if isinstance(valid_from, str) and len(valid_from) > 0:
            valid_from = dateutil.parser.parse(valid_from)

        expiry = self.secret_expiry
        if isinstance(expiry, str) and len(expiry) > 0:
            expiry = dateutil.parser.parse(expiry)

        if not self.check_mode:
            # Create secret
            if self.state == 'present' and changed:
                if self.get_delete_secret(self.secret_name):
                    if self.recover_if_need:
                        results['secret_id'] = self.recover_delete_secret(self.secret_name)
                        status = 'Recover'
                    elif self.purge_if_need:
                        self.purge_deleted_secret(self.secret_name)
                        status = 'Purged'
                    else:
                        self.fail("Secret {0} is currently in a deleted but recoverable state, and its name cannot be reused; in this state,\
                                  the secret can only be recovered or purged.".format(self.secret_name))
                else:
                    results['secret_id'] = self.create_update_secret(self.secret_name, self.secret_value, self.tags, self.content_type, valid_from, expiry)
                    status = 'Created'
                self.results['state'] = results
                self.results['state']['status'] = status
            # Delete secret
            elif self.state == 'absent' and changed:
                results['secret_id'] = self.delete_secret(self.secret_name)
                self.results['state'] = results
                self.results['state']['status'] = 'Deleted'
        else:
            if self.state == 'present' and changed:
                self.results['state']['status'] = 'Created'
            elif self.state == 'absent' and changed:
                self.results['state']['status'] = 'Deleted'

        return self.results

    def get_keyvault_client(self):

        return SecretClient(vault_url=self.keyvault_uri, credential=self.azure_auth.azure_credential_track2)

    def get_secret(self, name, version=''):
        ''' Gets an existing secret '''
        secret_bundle = self.client.get_secret(name=name, version=version)

        if secret_bundle:
            return dict(secret_id=secret_bundle.id, secret_value=secret_bundle.value)
        return None

    def create_update_secret(self, name, secret, tags, content_type, valid_from, expiry):
        ''' Creates/Updates a secret '''
        secret_bundle = self.client.set_secret(name=name,
                                               value=secret,
                                               tags=tags,
                                               content_type=content_type,
                                               expires_on=expiry,
                                               not_before=valid_from)
        return secret_bundle._properties._id

    def delete_secret(self, name):
        ''' Deletes a secret '''
        deleted_secret = self.client.begin_delete_secret(name)
        result = self.get_poller_result(deleted_secret)
        return result.properties._id

    def recover_delete_secret(self, name):
        ''' Recover a delete secret '''
        try:
            recover_delete_secret = self.client.begin_recover_deleted_secret(name)
            result = self.get_poller_result(recover_delete_secret)
            return result._id
        except HttpResponseError as ec:
            self.fail("Recover the delete secret fail, detail info {0}".format(ec))

    def purge_deleted_secret(self, name):
        ''' Purge delete secret '''
        try:
            purge_deleted_secret = self.client.purge_deleted_secret(name)
            return purge_deleted_secret
        except HttpResponseError as ec:
            self.fail("Purge delete secret fail, detail info {0}".format(ec))

    def get_delete_secret(self, name):
        ''' Get delete secret '''
        try:
            self.client.get_deleted_secret(name=name)
        except ResourceNotFoundError:
            return False
        return True


def main():
    AzureRMKeyVaultSecret()


if __name__ == '__main__':
    main()