forked from TerryHowe/ansible-modules-hashivault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashivault_k8s_auth_config.py
106 lines (94 loc) · 4.02 KB
/
hashivault_k8s_auth_config.py
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
#!/usr/bin/python
from ansible.module_utils.hashivault import hashivault_argspec
from ansible.module_utils.hashivault import hashivault_auth_client
from ansible.module_utils.hashivault import hashivault_init
from ansible.module_utils.hashivault import hashiwrapper
from ansible.module_utils.hashivault import get_keys_updated
from hvac.exceptions import InvalidPath
ANSIBLE_METADATA = {'status': ['stableinterface'], 'supported_by': 'community', 'version': '1.1'}
DOCUMENTATION = '''
---
module: hashivault_k8s_auth_config
version_added: "4.3.0"
short_description: Hashicorp Vault k8s auth config
description:
- Module to configure an k8s auth mount
options:
mount_point:
description:
- name of the secret engine mount name.
default: kubernetes
kubernetes_host:
description:
- host must be a host string, a host:port pair, or a URL to the base of the Kubernetes API server
token_reviewer_jwt:
description:
- a service account JWT used to access the TokenReview API to validate other JWTs during login
kubernetes_ca_cert:
description:
- PEM encoded CA cert for use by the TLS client used to talk with the Kubernetes API
pem_keys:
description:
- Optional list of PEM-formatted public keys or certificates used to verify the signatures of Kubernetes
service account JWTs. If a certificate is given, its public key will be extracted.
issuer:
description:
- Optional JWT issuer. If no issuer is specified, then this plugin will use kubernetes.io/serviceaccount as
the default issuer (Available in hvac 0.10.2).
extends_documentation_fragment: hashivault
'''
EXAMPLES = '''
---
- hosts: localhost
tasks:
- hashivault_k8s_auth_config:
kubernetes_host: https://192.168.99.100:8443
kubernetes_ca_cert: "-----BEGIN CERTIFICATE-----\n.....\n-----END CERTIFICATE-----"
'''
def main():
argspec = hashivault_argspec()
argspec['mount_point'] = dict(required=False, type='str', default='kubernetes')
argspec['kubernetes_host'] = dict(required=False, type='str', default=None)
argspec['token_reviewer_jwt'] = dict(required=False, type='str', default=None)
argspec['kubernetes_ca_cert'] = dict(required=False, type='str', default=None)
argspec['pem_keys'] = dict(required=False, type='list', default=None)
argspec['issuer'] = dict(required=False, type='str', default=None)
required_together = [['kubernetes_host', 'kubernetes_ca_cert']]
module = hashivault_init(argspec, supports_check_mode=True, required_together=required_together)
result = hashivault_k8s_auth_config(module)
if result.get('failed'):
module.fail_json(**result)
else:
module.exit_json(**result)
@hashiwrapper
def hashivault_k8s_auth_config(module):
params = module.params
client = hashivault_auth_client(params)
mount_point = params.get('mount_point').strip('/')
desired_state = dict()
desired_state['kubernetes_host'] = params.get('kubernetes_host')
desired_state['token_reviewer_jwt'] = params.get('token_reviewer_jwt')
desired_state['kubernetes_ca_cert'] = params.get('kubernetes_ca_cert')
desired_state['pem_keys'] = params.get('pem_keys')
if params.get('issuer'):
desired_state['issuer'] = params.get('issuer')
desired_state['mount_point'] = mount_point
try:
current_state = client.auth.kubernetes.read_config(mount_point=mount_point)
except InvalidPath:
current_state = {}
ignore_list = [
'mount_point',
'token_reviewer_jwt',
]
keys_updated = get_keys_updated(desired_state, current_state, ignore_list)
if 'pem_keys' in keys_updated:
if current_state.get('pem_keys', []) == []:
keys_updated.remove('pem_keys')
if not keys_updated:
return {'changed': False}
if not module.check_mode:
client.auth.kubernetes.configure(**desired_state)
return {'changed': True, 'keys_updated': keys_updated}
if __name__ == '__main__':
main()