| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
ANSIBLE_METADATA = { |
| 12 |
"metadata_version": "1.1", |
| 13 |
"status": ["preview"], |
| 14 |
"supported_by": "community", |
| 15 |
} |
| 16 |
|
| 17 |
DOCUMENTATION = r""" |
| 18 |
--- |
| 19 |
module: purefb_userpolicy |
| 20 |
version_added: '1.6.0' |
| 21 |
short_description: Manage FlashBlade Object Store User Access Policies |
| 22 |
description: |
| 23 |
- Add or Remove FlashBlade Object Store Access Policies for Account User |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- Name of the Object Store User |
| 30 |
- The user to have the policy request applied to |
| 31 |
type: str |
| 32 |
account: |
| 33 |
description: |
| 34 |
- Name of the Object Store Account associated with the user |
| 35 |
type: str |
| 36 |
state: |
| 37 |
description: |
| 38 |
- Define whether the Access Policy should be added or deleted |
| 39 |
- Option to list all available policies |
| 40 |
default: present |
| 41 |
choices: [ absent, present, show ] |
| 42 |
type: str |
| 43 |
policy: |
| 44 |
description: |
| 45 |
- Policies to added or deleted from the Object Store User |
| 46 |
- Only valid policies can be used |
| 47 |
- use I(list) to see available policies |
| 48 |
type: list |
| 49 |
elements: str |
| 50 |
context: |
| 51 |
description: |
| 52 |
- Name of fleet member on which to perform the operation. |
| 53 |
- This requires the array receiving the request is a member of a fleet |
| 54 |
and the context name to be a member of the same fleet. |
| 55 |
type: str |
| 56 |
default: "" |
| 57 |
version_added: "1.22.0" |
| 58 |
extends_documentation_fragment: |
| 59 |
- purestorage.flashblade.purestorage.fb |
| 60 |
""" |
| 61 |
|
| 62 |
EXAMPLES = r""" |
| 63 |
- name: List existng ruser access policies for a specific user |
| 64 |
purestorage.flashblade.purefb_userpolicy: |
| 65 |
state: show |
| 66 |
account: foo |
| 67 |
name: bar |
| 68 |
fb_url: 10.10.10.2 |
| 69 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 70 |
register: policy_list |
| 71 |
|
| 72 |
- name: List all available user access policies |
| 73 |
purestorage.flashblade.purefb_userpolicy: |
| 74 |
state: show |
| 75 |
fb_url: 10.10.10.2 |
| 76 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 77 |
register: policy_list |
| 78 |
|
| 79 |
- name: Add user access policies to account user foo/bar |
| 80 |
purestorage.flashblade.purefb_userpolicy: |
| 81 |
name: bar |
| 82 |
account: foo |
| 83 |
policy: |
| 84 |
- pure:policy/bucket-create |
| 85 |
- pure:policy/bucket-delete |
| 86 |
fb_url: 10.10.10.2 |
| 87 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 88 |
|
| 89 |
- name: Delete user access policies to account user foo/bar |
| 90 |
purestorage.flashblade.purefb_userpolicy: |
| 91 |
name: bar |
| 92 |
account: foo |
| 93 |
policy: |
| 94 |
- pure:policy/bucket-create |
| 95 |
- pure:policy/bucket-delete |
| 96 |
state: absent |
| 97 |
fb_url: 10.10.10.2 |
| 98 |
api_token: T-68618f31-0c9e-4e57-aa44-5306a2cf10e3 |
| 99 |
""" |
| 100 |
|
| 101 |
RETURN = r""" |
| 102 |
policy_list: |
| 103 |
description: |
| 104 |
- Returns the list of access policies for a user |
| 105 |
- If no user specified returns all available access policies |
| 106 |
returned: always |
| 107 |
type: list |
| 108 |
elements: str |
| 109 |
sample: ['pure:policy/object-list', 'pure:policy/bucket-list', 'pure:policy/object-read', 'pure:policy/bucket-delete', 'pure:policy/full-access'] |
| 110 |
""" |
| 111 |
|
| 112 |
from ansible.module_utils.basic import AnsibleModule |
| 113 |
from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import ( |
| 114 |
get_system, |
| 115 |
purefb_argument_spec, |
| 116 |
) |
| 117 |
|
| 118 |
CONTEXT_API_VERSION = "2.17" |
| 119 |
|
| 120 |
|
| 121 |
def _check_valid_policy(module, blade, policy): |
| 122 |
api_version = list(blade.get_versions().items) |
| 123 |
if CONTEXT_API_VERSION in api_version: |
| 124 |
return bool( |
| 125 |
blade.get_object_store_access_policies( |
| 126 |
names=[policy], context_names=[module.params["context"]] |
| 127 |
) |
| 128 |
) |
| 129 |
return bool(blade.get_object_store_access_policies(names=[policy])) |
| 130 |
|
| 131 |
|
| 132 |
def add_policy(module, blade): |
| 133 |
"""Add a single or list of policies to an account user""" |
| 134 |
changed = False |
| 135 |
api_version = list(blade.get_versions().items) |
| 136 |
user_policy_list = [] |
| 137 |
policy_list = module.params["policy"] |
| 138 |
for policy in range(len(policy_list)): |
| 139 |
if not _check_valid_policy(module, blade, policy_list[policy]): |
| 140 |
module.fail_json(msg="Policy {0} is not valid.".format(policy_list[policy])) |
| 141 |
username = module.params["account"] + "/" + module.params["name"] |
| 142 |
for policy in range(len(policy_list)): |
| 143 |
if CONTEXT_API_VERSION in api_version: |
| 144 |
res = blade.get_object_store_users_object_store_access_policies( |
| 145 |
member_names=[username], |
| 146 |
policy_names=[policy_list[policy]], |
| 147 |
context_names=[module.params["context"]], |
| 148 |
) |
| 149 |
else: |
| 150 |
res = blade.get_object_store_users_object_store_access_policies( |
| 151 |
member_names=[username], policy_names=[policy_list[policy]] |
| 152 |
) |
| 153 |
if not list(res.items): |
| 154 |
if not module.check_mode: |
| 155 |
changed = True |
| 156 |
if CONTEXT_API_VERSION in api_version: |
| 157 |
res = blade.post_object_store_access_policies_object_store_users( |
| 158 |
member_names=[username], |
| 159 |
policy_names=[policy_list[policy]], |
| 160 |
context_names=[module.params["context"]], |
| 161 |
) |
| 162 |
res = blade.post_object_store_access_policies_object_store_users( |
| 163 |
member_names=[username], policy_names=[policy_list[policy]] |
| 164 |
) |
| 165 |
if res.status_code != 200: |
| 166 |
module.fail_json( |
| 167 |
msg="Failed to add policy {0}. Error: {1}".format( |
| 168 |
policy_list[policy], res.errors[0].message |
| 169 |
) |
| 170 |
) |
| 171 |
if CONTEXT_API_VERSION in api_version: |
| 172 |
user_policies = list( |
| 173 |
blade.get_object_store_access_policies_object_store_users( |
| 174 |
member_names=[username], |
| 175 |
context_names=[module.params["context"]], |
| 176 |
).items |
| 177 |
) |
| 178 |
else: |
| 179 |
user_policies = list( |
| 180 |
blade.get_object_store_access_policies_object_store_users( |
| 181 |
member_names=[username] |
| 182 |
).items |
| 183 |
) |
| 184 |
for user_policy in range(len(user_policies)): |
| 185 |
user_policy_list.append(user_policies[user_policy].policy.name) |
| 186 |
if res.status_code != 200: |
| 187 |
module.fail_json( |
| 188 |
msg="Failed to add policy {0} to account user {1}. Error: {2}".format( |
| 189 |
policy_list[policy], username, res.errors[0].message |
| 190 |
) |
| 191 |
) |
| 192 |
module.exit_json(changed=changed, policy_list=user_policy_list) |
| 193 |
|
| 194 |
|
| 195 |
def remove_policy(module, blade): |
| 196 |
"""Remove a single or list of policies to an account user""" |
| 197 |
changed = False |
| 198 |
api_version = list(blade.get_versions().items) |
| 199 |
user_policy_list = [] |
| 200 |
policy_list = module.params["policy"] |
| 201 |
for policy in range(len(policy_list)): |
| 202 |
if not _check_valid_policy(module, blade, policy): |
| 203 |
module.fail_json(msg="Policy {0} is not valid.".format(policy)) |
| 204 |
username = module.params["account"] + "/" + module.params["name"] |
| 205 |
for policy in range(len(policy_list)): |
| 206 |
if CONTEXT_API_VERSION in api_version: |
| 207 |
res = blade.get_object_store_users_object_store_access_policies( |
| 208 |
member_names=[username], |
| 209 |
policy_names=[policy_list[policy]], |
| 210 |
context_names=[module.params["context"]], |
| 211 |
).total_item_count |
| 212 |
else: |
| 213 |
res = blade.get_object_store_users_object_store_access_policies( |
| 214 |
member_names=[username], policy_names=[policy_list[policy]] |
| 215 |
).total_item_count |
| 216 |
if res == 1: |
| 217 |
if not module.check_mode: |
| 218 |
changed = True |
| 219 |
if CONTEXT_API_VERSION in api_version: |
| 220 |
res = blade.delete_object_store_access_policies_object_store_users( |
| 221 |
member_names=[username], |
| 222 |
policy_names=[policy_list[policy]], |
| 223 |
context_names=[module.params["context"]], |
| 224 |
) |
| 225 |
else: |
| 226 |
res = blade.delete_object_store_access_policies_object_store_users( |
| 227 |
member_names=[username], policy_names=[policy_list[policy]] |
| 228 |
) |
| 229 |
if CONTEXT_API_VERSION in api_version: |
| 230 |
user_policies = list( |
| 231 |
blade.get_object_store_access_policies_object_store_users( |
| 232 |
member_names=[username], |
| 233 |
context_names=[module.params["context"]], |
| 234 |
).items |
| 235 |
) |
| 236 |
else: |
| 237 |
user_policies = list( |
| 238 |
blade.get_object_store_access_policies_object_store_users( |
| 239 |
member_names=[username] |
| 240 |
).items |
| 241 |
) |
| 242 |
for user_policy in range(len(user_policies)): |
| 243 |
user_policy_list.append(user_policies[user_policy].policy.name) |
| 244 |
if res.status_code != 200: |
| 245 |
module.fail_json( |
| 246 |
msg="Failed to remove policy {0} from account user {1}. Error: {2}".format( |
| 247 |
policy_list[policy], username, res.errors[0].message |
| 248 |
) |
| 249 |
) |
| 250 |
module.exit_json(changed=changed, policy_list=user_policy_list) |
| 251 |
|
| 252 |
|
| 253 |
def list_policy(module, blade): |
| 254 |
"""List Object Store User Access Policies""" |
| 255 |
changed = True |
| 256 |
api_version = list(blade.get_versions().items) |
| 257 |
policy_list = [] |
| 258 |
if not module.check_mode: |
| 259 |
if module.params["account"] and module.params["name"]: |
| 260 |
username = module.params["account"] + "/" + module.params["name"] |
| 261 |
if CONTEXT_API_VERSION in api_version: |
| 262 |
user_policies = list( |
| 263 |
blade.get_object_store_access_policies_object_store_users( |
| 264 |
member_names=[username], |
| 265 |
context_names=[module.params["context"]], |
| 266 |
).items |
| 267 |
) |
| 268 |
else: |
| 269 |
user_policies = list( |
| 270 |
blade.get_object_store_access_policies_object_store_users( |
| 271 |
member_names=[username] |
| 272 |
).items |
| 273 |
) |
| 274 |
for user_policy in range(len(user_policies)): |
| 275 |
policy_list.append(user_policies[user_policy].policy.name) |
| 276 |
else: |
| 277 |
if CONTEXT_API_VERSION in api_version: |
| 278 |
policies = blade.get_object_store_access_policies( |
| 279 |
context_names=[module.params["context"]] |
| 280 |
) |
| 281 |
else: |
| 282 |
policies = blade.get_object_store_access_policies() |
| 283 |
p_list = list(policies.items) |
| 284 |
if policies.status_code != 200: |
| 285 |
module.fail_json(msg="Failed to get Object Store User Access Policies") |
| 286 |
for policy in range(len(p_list)): |
| 287 |
policy_list.append(p_list[policy].name) |
| 288 |
module.exit_json(changed=changed, policy_list=policy_list) |
| 289 |
|
| 290 |
|
| 291 |
def main(): |
| 292 |
argument_spec = purefb_argument_spec() |
| 293 |
argument_spec.update( |
| 294 |
dict( |
| 295 |
state=dict( |
| 296 |
type="str", default="present", choices=["absent", "present", "show"] |
| 297 |
), |
| 298 |
name=dict(type="str"), |
| 299 |
account=dict(type="str"), |
| 300 |
policy=dict(type="list", elements="str"), |
| 301 |
context=dict(type="str", default=""), |
| 302 |
) |
| 303 |
) |
| 304 |
required_if = [ |
| 305 |
["state", "present", ["name", "account", "policy"]], |
| 306 |
["state", "absent", ["name", "account", "policy"]], |
| 307 |
] |
| 308 |
module = AnsibleModule( |
| 309 |
argument_spec, required_if=required_if, supports_check_mode=True |
| 310 |
) |
| 311 |
|
| 312 |
blade = get_system(module) |
| 313 |
|
| 314 |
state = module.params["state"] |
| 315 |
if ( |
| 316 |
blade.get_object_store_users( |
| 317 |
names=[module.params["account"] + "/" + module.params["name"]] |
| 318 |
).status_code |
| 319 |
!= 200 |
| 320 |
and state != "show" |
| 321 |
): |
| 322 |
module.fail_json( |
| 323 |
msg="Account User {0}/{1} does not exist".format( |
| 324 |
module.params["account"], module.params["name"] |
| 325 |
) |
| 326 |
) |
| 327 |
if state == "show": |
| 328 |
list_policy(module, blade) |
| 329 |
elif state == "present": |
| 330 |
add_policy(module, blade) |
| 331 |
elif state == "absent": |
| 332 |
remove_policy(module, blade) |
| 333 |
|
| 334 |
module.exit_json(changed=False) |
| 335 |
|
| 336 |
|
| 337 |
if __name__ == "__main__": |
| 338 |
main() |