Debian Codegraph @

ansible_collections/community/general/plugins/modules/rundeck_acl_policy.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017, Loic Blot <[email protected]>
4 # Sponsored by Infopro Digital. http://www.infopro-digital.com/
5 # Sponsored by E.T.A.I. http://www.etai.fr/
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import annotations
10
11 DOCUMENTATION = r"""
12 module: rundeck_acl_policy
13
14 short_description: Manage Rundeck ACL policies
15 description:
16 - Create, update and remove Rundeck ACL policies through HTTP API.
17 author: "Loic Blot (@nerzhul)"
18 attributes:
19 check_mode:
20 support: full
21 diff_mode:
22 support: none
23 options:
24 state:
25 type: str
26 description:
27 - Create or remove Rundeck project.
28 choices: ['present', 'absent']
29 default: 'present'
30 name:
31 type: str
32 description:
33 - Sets the project name.
34 required: true
35 api_token:
36 description:
37 - Sets the token to authenticate against Rundeck API.
38 aliases: ["token"]
39 project:
40 type: str
41 description:
42 - Sets the project which receive the ACL policy.
43 - If unset, it is a system ACL policy.
44 policy:
45 type: str
46 description:
47 - Sets the ACL policy content.
48 - ACL policy content is a YAML object as described in U(http://rundeck.org/docs/man5/aclpolicy.html).
49 - It can be a YAML string or a pure Ansible inventory YAML object.
50 client_cert:
51 version_added: '0.2.0'
52 client_key:
53 version_added: '0.2.0'
54 force:
55 version_added: '0.2.0'
56 force_basic_auth:
57 version_added: '0.2.0'
58 http_agent:
59 version_added: '0.2.0'
60 url_password:
61 version_added: '0.2.0'
62 url_username:
63 version_added: '0.2.0'
64 use_proxy:
65 version_added: '0.2.0'
66 validate_certs:
67 version_added: '0.2.0'
68 extends_documentation_fragment:
69 - ansible.builtin.url
70 - community.general._attributes
71 - community.general._rundeck
72 """
73
74 EXAMPLES = r"""
75 - name: Create or update a rundeck ACL policy in project Ansible
76 community.general.rundeck_acl_policy:
77 name: "Project_01"
78 api_version: 18
79 url: "https://rundeck.example.org"
80 token: "mytoken"
81 state: present
82 project: "Ansible"
83 policy:
84 description: "my policy"
85 context:
86 application: rundeck
87 for:
88 project:
89 - allow: read
90 by:
91 group: "build"
92
93 - name: Remove a rundeck system policy
94 community.general.rundeck_acl_policy:
95 name: "Project_01"
96 url: "https://rundeck.example.org"
97 token: "mytoken"
98 state: absent
99 """
100
101 RETURN = r"""
102 rundeck_response:
103 description: Rundeck response when a failure occurs.
104 returned: failed
105 type: str
106 before:
107 description: Dictionary containing ACL policy information before modification.
108 returned: success
109 type: dict
110 after:
111 description: Dictionary containing ACL policy information after modification.
112 returned: success
113 type: dict
114 """
115
116 # import module snippets
117 import re
118
119 from ansible.module_utils.basic import AnsibleModule
120
121 from ansible_collections.community.general.plugins.module_utils._rundeck import (
122 api_argument_spec,
123 api_request,
124 )
125
126
127 class RundeckACLManager:
128 def __init__(self, module):
129 self.module = module
130 if module.params.get("project"):
131 self.endpoint = f"project/{self.module.params['project']}/acl/{self.module.params['name']}.aclpolicy"
132 else:
133 self.endpoint = f"system/acl/{self.module.params['name']}.aclpolicy"
134
135 def get_acl(self):
136 resp, info = api_request(
137 module=self.module,
138 endpoint=self.endpoint,
139 )
140
141 return resp
142
143 def create_or_update_acl(self):
144 facts = self.get_acl()
145 if facts is None:
146 # If in check mode don't create project, simulate a fake project creation
147 if self.module.check_mode:
148 self.module.exit_json(changed=True, before={}, after=self.module.params["policy"])
149
150 resp, info = api_request(
151 module=self.module,
152 endpoint=self.endpoint,
153 method="POST",
154 data={"contents": self.module.params["policy"]},
155 )
156
157 if info["status"] == 201:
158 self.module.exit_json(changed=True, before={}, after=self.get_acl())
159 elif info["status"] == 400:
160 self.module.fail_json(
161 msg=f"Unable to validate acl {self.module.params['name']}. Please ensure it is a valid ACL"
162 )
163 elif info["status"] == 409:
164 self.module.fail_json(msg=f"ACL {self.module.params['name']} already exists")
165 else:
166 self.module.fail_json(
167 msg=f"Unhandled HTTP status {info['status']}, please report the bug",
168 before={},
169 after=self.get_acl(),
170 )
171 else:
172 if facts["contents"] == self.module.params["policy"]:
173 self.module.exit_json(changed=False, before=facts, after=facts)
174
175 if self.module.check_mode:
176 self.module.exit_json(changed=True, before=facts, after=facts)
177
178 resp, info = api_request(
179 module=self.module,
180 endpoint=self.endpoint,
181 method="PUT",
182 data={"contents": self.module.params["policy"]},
183 )
184
185 if info["status"] == 200:
186 self.module.exit_json(changed=True, before=facts, after=self.get_acl())
187 elif info["status"] == 400:
188 self.module.fail_json(
189 msg=f"Unable to validate acl {self.module.params['name']}. Please ensure it is a valid ACL"
190 )
191 elif info["status"] == 404:
192 self.module.fail_json(msg=f"ACL {self.module.params['name']} doesn't exists. Cannot update.")
193
194 def remove_acl(self):
195 facts = self.get_acl()
196
197 if facts is None:
198 self.module.exit_json(changed=False, before={}, after={})
199 else:
200 # If not in check mode, remove the project
201 if not self.module.check_mode:
202 api_request(
203 module=self.module,
204 endpoint=self.endpoint,
205 method="DELETE",
206 )
207
208 self.module.exit_json(changed=True, before=facts, after={})
209
210
211 def main():
212 # Also allow the user to set values for fetch_url
213 argument_spec = api_argument_spec()
214 argument_spec.update(
215 dict(
216 state=dict(type="str", choices=["present", "absent"], default="present"),
217 name=dict(required=True, type="str"),
218 policy=dict(type="str"),
219 project=dict(type="str"),
220 )
221 )
222
223 argument_spec["api_token"]["aliases"] = ["token"]
224
225 module = AnsibleModule(
226 argument_spec=argument_spec,
227 required_if=[
228 ["state", "present", ["policy"]],
229 ],
230 supports_check_mode=True,
231 )
232
233 if not bool(re.match("[a-zA-Z0-9,.+_-]+", module.params["name"])):
234 module.fail_json(
235 msg="Name contains forbidden characters. The policy can contain the characters: a-zA-Z0-9,.+_-"
236 )
237
238 if module.params["api_version"] < 14:
239 module.fail_json(msg="API version should be at least 14")
240
241 rundeck = RundeckACLManager(module)
242 if module.params["state"] == "present":
243 rundeck.create_or_update_acl()
244 elif module.params["state"] == "absent":
245 rundeck.remove_acl()
246
247
248 if __name__ == "__main__":
249 main()