Skip to content

Commit

Permalink
Rename hashivault_policy_set_from_file and test
Browse files Browse the repository at this point in the history
  • Loading branch information
TerryHowe committed Oct 7, 2018
1 parent 98392ab commit 7eb99bb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -169,7 +169,7 @@ Policy from a file support::
name: 'drew'

tasks:
- hashivault_policy_set_file:
- hashivault_policy_set_from_file:
name: "{{name}}"
rules_file: /home/drew/my_policy.hcl
register: 'vault_policy_set'
Expand Down
3 changes: 3 additions & 0 deletions ansible/modules/hashivault/hashivault_policy_set.py
Expand Up @@ -50,6 +50,9 @@
name:
description:
- policy name.
rules:
description:
- policy rules.
'''
EXAMPLES = '''
---
Expand Down
Expand Up @@ -50,6 +50,9 @@
name:
description:
- policy name.
rules_file:
description:
- policy rules file.
'''
EXAMPLES = '''
---
Expand All @@ -65,7 +68,7 @@ def main():
argspec['name'] = dict(required=True, type='str')
argspec['rules_file'] = dict(required=True, type='str')
module = hashivault_init(argspec)
result = hashivault_policy_set_file(module.params)
result = hashivault_policy_set_from_file(module.params)
if result.get('failed'):
module.fail_json(**result)
else:
Expand All @@ -77,7 +80,7 @@ def main():


@hashiwrapper
def hashivault_policy_set_file(params):
def hashivault_policy_set_from_file(params):
client = hashivault_auth_client(params)
name = params.get('name')
rules = open(params.get('rules_file'), 'r').read()
Expand Down
6 changes: 6 additions & 0 deletions functional/templates/policy_rules.hcl
@@ -0,0 +1,6 @@
path "secret/bob/*" {
capabilities = ["create", "read", "update", "delete", "list"]
}
path "secret/bob" {
capabilities = ["list"]
}
33 changes: 33 additions & 0 deletions functional/test_policy.yml
Expand Up @@ -11,6 +11,14 @@
capabilities = ["list"]
}
expected: "{{rules | regex_replace('\n', '')}}"
bobs_rules: >
path "secret/bob/*" {
capabilities = ["create", "read", "update", "delete", "list"]
}
path "secret/bob" {
capabilities = ["list"]
}
bobs_expected: "{{bobs_rules | regex_replace('\n', '') | regex_replace(' ', '')}}"
tasks:
- hashivault_policy_list:
register: 'vault_policy_list'
Expand Down Expand Up @@ -82,3 +90,28 @@
- assert: { that: "{{vault_policy_get.rc}} == 1" }
- assert: { that: "{{vault_policy_get.failed}} == False" }
- assert: { that: "'{{vault_policy_get.msg}}' == 'Policy \"terrybogus\" does not exist.'" }

- name: Set new policy from file
hashivault_policy_set_from_file:
name: "bob"
rules_file: "templates/policy_rules.hcl"
register: 'vault_policy_set'
- assert: { that: "{{vault_policy_set.changed}} == True" }
- assert: { that: "{{vault_policy_set.rc}} == 0" }

- name: Get new from file policy and make sure it set properly
hashivault_policy_get:
name: 'bob'
register: 'vault_policy_get'
- assert: { that: "{{vault_policy_get.changed}} == False" }
- set_fact:
actual: "{{vault_policy_get.rules | regex_replace('\n', '') | regex_replace(' ', '')}}"
- assert: { that: "'{{bobs_expected}}' == '{{actual}}'" }
- assert: { that: "{{vault_policy_get.rc}} == 0" }

- name: Delete our new policy from file
hashivault_policy_delete:
name: 'bob'
register: 'vault_policy_delete'
- assert: { that: "{{vault_policy_delete.changed}} == True" }
- assert: { that: "{{vault_policy_delete.rc}} == 0" }

0 comments on commit 7eb99bb

Please sign in to comment.