-
Notifications
You must be signed in to change notification settings - Fork 156
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 support for ACL policies #480
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
98f68ad
Add support for ACL policies
mleneveut 43903a7
ACL policies needs hvac 2.1.0
mleneveut ec214f8
Fix ACL policies structure in hvac 2.1.0
mleneveut a43e614
Fix client.write breaking change with hvac 2.0.0
mleneveut 7364cc5
Merge branch 'main' into acl_policies
TerryHowe 65acb0b
Handle hvac read_acl_policy InvalidPath exception
mleneveut bbdbfa4
Fix lint
mleneveut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
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 | ||
|
||
ANSIBLE_METADATA = ANSIBLE_METADATA = {'status': ['stableinterface'], 'supported_by': 'community', 'version': '1.1'} | ||
DOCUMENTATION = ''' | ||
--- | ||
module: hashivault_acl_policy | ||
version_added: "2.1.0" | ||
short_description: Hashicorp Vault acl policy set module | ||
description: | ||
- Module to set an ACL policy in Hashicorp Vault. | ||
options: | ||
name: | ||
description: | ||
- policy name. | ||
state: | ||
type: str | ||
choices: ["present", "absent"] | ||
default: present | ||
description: | ||
- present or absent | ||
rules: | ||
description: | ||
- policy rules. | ||
rules_file: | ||
description: | ||
- name of local file to read for policy rules. | ||
extends_documentation_fragment: hashivault | ||
''' | ||
EXAMPLES = ''' | ||
--- | ||
- hosts: localhost | ||
tasks: | ||
- hashivault_acl_policy: | ||
name: my_policy | ||
rules: '{{rules}}' | ||
''' | ||
|
||
|
||
def main(): | ||
argspec = hashivault_argspec() | ||
argspec['name'] = dict(required=True, type='str') | ||
argspec['rules'] = dict(required=False, type='str') | ||
argspec['rules_file'] = dict(required=False, type='str') | ||
argspec['state'] = dict(required=False, choices=['present', 'absent'], default='present') | ||
mutually_exclusive = [['rules', 'rules_file']] | ||
module = hashivault_init(argspec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) | ||
result = hashivault_acl_policy(module) | ||
if result.get('failed'): | ||
module.fail_json(**result) | ||
else: | ||
module.exit_json(**result) | ||
|
||
|
||
@hashiwrapper | ||
def hashivault_acl_policy(module): | ||
params = module.params | ||
client = hashivault_auth_client(params) | ||
state = params.get('state') | ||
name = params.get('name') | ||
exists = False | ||
changed = False | ||
current_state = {} | ||
desired_state = {} | ||
|
||
# get current policies | ||
current_policies = client.sys.list_acl_policies() | ||
if isinstance(current_policies, dict): | ||
current_policies = current_policies.get('data', current_policies).get('keys', current_policies) | ||
if name in current_policies: | ||
exists = True | ||
current_state = client.sys.read_acl_policy(name) | ||
current_state = current_state.get('data', current_state).get('policy', current_state) | ||
|
||
# Define desired rules | ||
rules_file = params.get('rules_file') | ||
if rules_file: | ||
try: | ||
desired_state = open(rules_file, 'r').read() | ||
except Exception as e: | ||
return {'changed': False, | ||
'failed': True, | ||
'msg': 'Error opening rules file <%s>: %s' % (rules_file, str(e))} | ||
else: | ||
desired_state = params.get('rules') | ||
|
||
# Check required actions | ||
if state == 'present' and not exists: | ||
changed = True | ||
elif state == 'absent' and exists: | ||
changed = True | ||
elif state == 'present' and exists: | ||
if current_state != desired_state: | ||
changed = True | ||
|
||
if changed and not module.check_mode: | ||
# create or update | ||
if state == 'present': | ||
client.sys.create_or_update_acl_policy(name, desired_state) | ||
# delete | ||
elif state == 'absent': | ||
client.sys.delete_acl_policy(name) | ||
|
||
return { | ||
"changed": changed, | ||
"diff": { | ||
"before": current_state, | ||
"after": desired_state, | ||
}, | ||
} | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
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 | ||
|
||
ANSIBLE_METADATA = {'status': ['stableinterface'], 'supported_by': 'community', 'version': '1.1'} | ||
DOCUMENTATION = ''' | ||
--- | ||
module: hashivault_acl_policy_get | ||
version_added: "2.1.0" | ||
short_description: Hashicorp Vault ACL policy get module | ||
description: | ||
- Module to get an ACL policy from Hashicorp Vault. | ||
options: | ||
name: | ||
description: | ||
- policy name. | ||
extends_documentation_fragment: hashivault | ||
''' | ||
EXAMPLES = ''' | ||
--- | ||
- hosts: localhost | ||
tasks: | ||
- hashivault_acl_policy_get: | ||
name: 'annie' | ||
register: 'vault_acl_policy_get' | ||
- debug: msg="User policy is {{vault_acl_policy_get.policy}}" | ||
''' | ||
|
||
|
||
def main(): | ||
argspec = hashivault_argspec() | ||
argspec['name'] = dict(required=True, type='str') | ||
module = hashivault_init(argspec) | ||
result = hashivault_acl_policy_get(module.params) | ||
if result.get('failed'): | ||
module.fail_json(**result) | ||
else: | ||
module.exit_json(**result) | ||
|
||
|
||
@hashiwrapper | ||
def hashivault_acl_policy_get(params): | ||
name = params.get('name') | ||
client = hashivault_auth_client(params) | ||
policy = client.sys.read_acl_policy(name) | ||
policy = policy.get('data', policy).get('policy', policy) | ||
if policy is None: | ||
result = {"changed": False, "rc": 1, "failed": True} | ||
result['msg'] = u"Policy \"%s\" does not exist." % name | ||
return result | ||
else: | ||
return {'rules': policy} | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
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 | ||
|
||
ANSIBLE_METADATA = {'status': ['stableinterface'], 'supported_by': 'community', 'version': '1.1'} | ||
DOCUMENTATION = ''' | ||
--- | ||
module: hashivault_acl_policy_list | ||
version_added: "2.1.0" | ||
short_description: Hashicorp Vault ACL policy list module | ||
description: | ||
- Module to list ACL policies in Hashicorp Vault. | ||
extends_documentation_fragment: hashivault | ||
''' | ||
EXAMPLES = ''' | ||
--- | ||
- hosts: localhost | ||
tasks: | ||
- hashivault_acl_policy_list: | ||
register: 'vault_acl_policy_list' | ||
- debug: msg="Policies are {{vault_acl_policy_list.policy}}" | ||
''' | ||
|
||
|
||
def main(): | ||
argspec = hashivault_argspec() | ||
module = hashivault_init(argspec) | ||
result = hashivault_acl_policy_list(module.params) | ||
if result.get('failed'): | ||
module.fail_json(**result) | ||
else: | ||
module.exit_json(**result) | ||
|
||
|
||
@hashiwrapper | ||
def hashivault_acl_policy_list(params): | ||
client = hashivault_auth_client(params) | ||
current_policies = client.sys.list_acl_policies() | ||
if isinstance(current_policies, dict): | ||
current_policies = current_policies.get('data', current_policies) | ||
current_policies = current_policies.get('keys', current_policies) | ||
return {'policies': current_policies} | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this required for hvac 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's a breaking change of 2.0.0 : https://github.com/hvac/hvac/blob/main/CHANGELOG.md#200
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/hvac/hvac/pull/1028/files#diff-c14f37a0d0f4f67dc145b13ddba81fe57b72293de4f5d39b443b97a96736dad1R257-R259
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#483