Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_bucket_access.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2025, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
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_bucket_access
20 version_added: '1.20.0'
21 short_description: Manage FlashBlade bucket access policies
22 description:
23 - Manage object store bucket policies.
24 - This modules allows the management of both bucket access and cross-origin
25 resource sharing policies and their associated rules.
26 author:
27 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
28 options:
29 state:
30 description:
31 - Create or delete policy or rule.
32 default: present
33 type: str
34 choices: [ absent, present ]
35 policy_type:
36 description:
37 - Type of policy
38 type: str
39 choices: [ access, cors ]
40 default: access
41 name:
42 description:
43 - Name of Object Store bucket the policy applies to.
44 type: str
45 required: true
46 rule:
47 description:
48 - Name of the rule in the Bucket Policy
49 - Required if I(state) is I(present)
50 type: str
51 effect:
52 description:
53 - Allow S3 requests that match all of the I(actions) item selected.
54 Rules are additive.
55 type: str
56 default: allow
57 choices: [ allow, deny ]
58 actions:
59 description:
60 - List of permissions to grant.
61 - System-wide policy rules cannot be deleted or modified
62 - Currently only s3:GetObject is allowed
63 type: list
64 elements: str
65 default: [ "s3:GetObject" ]
66 choices:
67 - s3:*
68 - s3:AbortMultipartUpload
69 - s3:BypassGovernanceRetention
70 - s3:CreateBucket
71 - s3:DeleteBucket
72 - s3:DeleteObject
73 - s3:DeleteObjectVersion
74 - s3:ExtendSafemodeRetentionPeriod
75 - s3:GetBucketAcl
76 - s3:GetBucketLocation
77 - s3:GetBucketVersioning
78 - s3:GetLifecycleConfiguration
79 - s3:GetObject
80 - s3:GetObjectAcl
81 - s3:GetObjectLegalHold
82 - s3:GetObjectLockConfiguration
83 - s3:GetObjectRetention
84 - s3:GetObjectTagging
85 - s3:GetObjectVersion
86 - s3:GetObjectVersionTagging
87 - s3:ListAllMyBuckets
88 - s3:ListBucket
89 - s3:ListBucketMultipartUploads
90 - s3:ListBucketVersions
91 - s3:ListMultipartUploadParts
92 - s3:PutBucketVersioning
93 - s3:PutLifecycleConfiguration
94 - s3:PutObject
95 - s3:PutObjectLegalHold
96 - s3:PutObjectLockConfiguration
97 - s3:PutObjectRetention
98 - s3:ResolveSafemodeConflicts
99 methods:
100 description:
101 - A list of HTTP methods that are permitted for cross-origin requests to access a bucket.
102 - The only currently supported combination of allowed methods is all methods.
103 choices:
104 - GET
105 - PUT
106 - HEAD
107 - POST
108 - DELETE
109 type: list
110 elements: str
111 default: ["GET", "PUT", "HEAD", "POST", "DELETE"]
112 headers:
113 description:
114 - A list of headers that are permitted to be included in cross-origin requests to access a bucket.
115 - The only currently supported allowed header is '*'.
116 type: list
117 elements: str
118 default: ["*"]
119 origins:
120 description:
121 - A list of origins (domains) that are permitted to make cross-origin requests to access a bucket.
122 - The only currently supported allowed origin is '*'.
123 type: list
124 elements: str
125 default: ["*"]
126 resources:
127 description:
128 - The list of resources which this rule applies to.
129 - The only currently supported resource is all objects in a bucket to
130 which the parent policy belongs.
131 elements: str
132 type: list
133 default: ["*"]
134 principals:
135 description:
136 - Defines if the rule will apply to all object store users
137 regardless of their origin or principal.
138 type: bool
139 default: true
140 context:
141 description:
142 - Name of fleet member on which to perform the operation.
143 - This requires the array receiving the request is a member of a fleet
144 and the context name to be a member of the same fleet.
145 type: str
146 default: ""
147 version_added: "1.22.0"
148 extends_documentation_fragment:
149 - purestorage.flashblade.purestorage.fb
150 """
151
152 EXAMPLES = r"""
153 - name: Create a bucket access policy rule for bucket bar
154 purestorage.flashblade.purefb_bucket_policy:
155 rule: foo
156 name: bar
157 policy_type: access
158 fb_url: 10.10.10.2
159 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
160 - name: Create a CORS policy rule for bucket bar
161 purestorage.flashblade.purefb_bucket_policy:
162 rule: foo
163 name: bar
164 policy_type: cors
165 fb_url: 10.10.10.2
166 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
167 - name: Delete bucket policy rule foo from bucket bar
168 purestorage.flashblade.purefb_bucket_policy:
169 rule: foo
170 name: bar
171 policy_type: access
172 state: absent
173 fb_url: 10.10.10.2
174 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
175 - name: Delete all bucket policy rules from bucket bar
176 purestorage.flashblade.purefb_bucket_policy:
177 name: bar
178 policy_type: access
179 state: absent
180 fb_url: 10.10.10.2
181 api_token: T-9f276a18-50ab-446e-8a0c-666a3529a1b6
182 """
183
184 RETURN = r"""
185 """
186
187 HAS_PYPURECLIENT = True
188 try:
189 from pypureclient.flashblade import (
190 BucketAccessPolicyRulePost,
191 BucketAccessPolicyRulePrincipal,
192 CrossOriginResourceSharingPolicyRulePost,
193 )
194 except ImportError:
195 HAS_PYPURECLIENT = False
196
197
198 from ansible.module_utils.basic import AnsibleModule
199 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
200 get_system,
201 purefb_argument_spec,
202 )
203
204 MIN_API_VERSION = "2.12"
205 CONTEXT_API_VERSION = "2.17"
206
207
208 def delete_cors_policy(module, blade):
209 """Delete cross-origin resource sharing policy or rule"""
210 api_version = list(blade.get_versions().items)
211 changed = True
212 if not module.check_mode:
213 if module.params["rule"]:
214 if CONTEXT_API_VERSION in api_version:
215 res = blade.get_buckets_cross_origin_resource_sharing_policies_rules(
216 bucket_names=[module.params["name"]],
217 names=[module.params["rule"]],
218 context_names=[module.params["context"]],
219 )
220 else:
221 res = blade.get_buckets_cross_origin_resource_sharing_policies_rules(
222 bucket_names=[module.params["name"]], names=[module.params["rule"]]
223 )
224 if res.status_code == 200:
225 changed = False
226 module.exit_json(changed=changed)
227
228 if CONTEXT_API_VERSION in api_version:
229 res = blade.delete_buckets_cross_origin_resource_sharing_policies_rules(
230 names=module.params["rule"],
231 bucket_names=module.params["name"],
232 context_names=[module.params["context"]],
233 )
234 else:
235 res = blade.delete_buckets_cross_origin_resource_sharing_policies_rules(
236 names=module.params["rule"], bucket_names=module.params["name"]
237 )
238 if res.status_code != 200:
239 module.fail_json(
240 msg="Failed to delete CORS rule {0} for bucket {1}. Error: {2}".format(
241 module.params["rule"],
242 module.params["name"],
243 res.errors[0].message,
244 )
245 )
246 else:
247 if CONTEXT_API_VERSION in api_version:
248 res = blade.get_buckets_cross_origin_resource_sharing_policies(
249 bucket_names=[module.params["name"]],
250 context_names=[module.params["context"]],
251 )
252 else:
253 res = blade.get_buckets_cross_origin_resource_sharing_policies(
254 bucket_names=[module.params["name"]],
255 )
256 if res.total_item_count == 0:
257 changed = False
258 module.exit_json(changed=changed)
259
260 if CONTEXT_API_VERSION in api_version:
261 res = blade.delete_buckets_cross_origin_resource_sharing_policies(
262 bucket_names=module.params["name"],
263 context_names=[module.params["context"]],
264 )
265 else:
266 res = blade.delete_buckets_cross_origin_resource_sharing_policies(
267 bucket_names=module.params["name"]
268 )
269 if res.status_code != 200:
270 module.fail_json(
271 msg="Failed to delete CORS policy for bucket {0}. Error: {1}".format(
272 module.params["name"], res.errors[0].message
273 )
274 )
275
276 module.exit_json(changed=changed)
277
278
279 def delete_access_policy(module, blade):
280 """Delete bucket access policy or rule"""
281
282 changed = True
283 api_version = list(blade.get_versions().items)
284 if not module.check_mode:
285 if module.params["rule"]:
286 if CONTEXT_API_VERSION in api_version:
287 res = blade.get_buckets_bucket_access_policies_rules(
288 bucket_names=[module.params["name"]],
289 names=[module.params["rule"]],
290 context_names=[module.params["context"]],
291 )
292 else:
293 res = blade.get_buckets_bucket_access_policies_rules(
294 bucket_names=[module.params["name"]], names=[module.params["rule"]]
295 )
296 if res.status_code != 200:
297 changed = False
298 module.exit_json(changed=changed)
299
300 if CONTEXT_API_VERSION in api_version:
301 res = blade.delete_buckets_bucket_access_policies_rules(
302 names=module.params["rule"],
303 bucket_names=module.params["name"],
304 context_names=[module.params["context"]],
305 )
306 else:
307 res = blade.delete_buckets_bucket_access_policies_rules(
308 names=module.params["rule"], bucket_names=module.params["name"]
309 )
310 if res.status_code != 200:
311 module.fail_json(
312 msg="Failed to delete access rule {0} for bucket {1}. Error: {2}".format(
313 module.params["rule"],
314 module.params["name"],
315 res.errors[0].message,
316 )
317 )
318 else:
319 if CONTEXT_API_VERSION in api_version:
320 res = blade.get_buckets_bucket_access_policies(
321 bucket_names=[module.params["name"]],
322 context_names=[module.params["context"]],
323 )
324 else:
325 res = blade.get_buckets_bucket_access_policies(
326 bucket_names=[module.params["name"]]
327 )
328 if res.total_item_count == 0:
329 changed = False
330 module.exit_json(changed=changed)
331
332 if CONTEXT_API_VERSION in api_version:
333 res = blade.delete_buckets_bucket_access_policies(
334 bucket_names=module.params["name"],
335 context_names=[module.params["context"]],
336 )
337 else:
338 res = blade.delete_buckets_bucket_access_policies(
339 bucket_names=module.params["name"]
340 )
341 if res.status_code != 200:
342 module.fail_json(
343 msg="Failed to delete access policy for bucket {0}. Error: {1}".format(
344 module.params["name"], res.errors[0].message
345 )
346 )
347
348 module.exit_json(changed=changed)
349
350
351 def create_access_policy(module, blade):
352 """Create bucket access policy or rule"""
353 changed = False
354 api_version = list(blade.get_versions().items)
355 if CONTEXT_API_VERSION in api_version:
356 res = blade.get_buckets_bucket_access_policies(
357 bucket_names=[module.params["name"]],
358 context_names=[module.params["context"]],
359 )
360 else:
361 res = blade.get_buckets_bucket_access_policies(
362 bucket_names=[module.params["name"]]
363 )
364 if res.total_item_count == 0:
365 # Need to create the policy with its first rule
366 changed = True
367 if not module.check_mode:
368 if CONTEXT_API_VERSION in api_version:
369 res = blade.post_buckets_bucket_access_policies(
370 bucket_names=[module.params["name"]],
371 context_names=[module.params["context"]],
372 )
373 else:
374 res = blade.post_buckets_bucket_access_policies(
375 bucket_names=[module.params["name"]]
376 )
377 if res.status_code != 200:
378 module.fail_json(
379 msg="Failed to create initial bucket access policy for {0}. Error: {1}".format(
380 module.params["name"], res.errors[0].message
381 )
382 )
383 # Create a new rule for the policy
384 if not module.check_mode:
385 changed = True
386 if CONTEXT_API_VERSION in api_version:
387 res = blade.get_buckets_bucket_access_policies_rules(
388 bucket_names=[module.params["name"]],
389 names=[module.params["rule"]],
390 context_names=[module.params["context"]],
391 )
392 else:
393 res = blade.get_buckets_bucket_access_policies_rules(
394 bucket_names=[module.params["name"]], names=[module.params["rule"]]
395 )
396 if res.status_code == 200:
397 changed = False
398 module.exit_json(changed=changed)
399
400 all_resources = []
401 for resource in module.params["resources"]:
402 all_resources.append(module.params["name"] + "/" + resource)
403 if CONTEXT_API_VERSION in api_version:
404 res = blade.post_buckets_bucket_access_policies_rules(
405 bucket_names=[module.params["name"]],
406 names=module.params["rule"],
407 rule=BucketAccessPolicyRulePost(
408 effect=module.params["effect"],
409 resources=all_resources,
410 actions=module.params["actions"],
411 principals=BucketAccessPolicyRulePrincipal(
412 all=module.params["principals"]
413 ),
414 ),
415 context_names=[module.params["context"]],
416 )
417 else:
418 res = blade.post_buckets_bucket_access_policies_rules(
419 bucket_names=[module.params["name"]],
420 names=module.params["rule"],
421 rule=BucketAccessPolicyRulePost(
422 effect=module.params["effect"],
423 resources=all_resources,
424 actions=module.params["actions"],
425 principals=BucketAccessPolicyRulePrincipal(
426 all=module.params["principals"]
427 ),
428 ),
429 )
430 if res.status_code != 200:
431 module.fail_json(
432 msg="Failed to create access policy rule {0} "
433 "in policy {1}. Error: {2}".format(
434 module.params["rule"], module.params["name"], res.errors[0].message
435 )
436 )
437 module.exit_json(changed=changed)
438
439
440 def create_cors_policy(module, blade):
441 """Create CORS policy or rule"""
442 changed = False
443 api_version = list(blade.get_versions().items)
444 if CONTEXT_API_VERSION in api_version:
445 res = blade.get_buckets_cross_origin_resource_sharing_policies(
446 bucket_names=[module.params["name"]],
447 context_names=[module.params["context"]],
448 )
449 else:
450 res = blade.get_buckets_cross_origin_resource_sharing_policies(
451 bucket_names=[module.params["name"]]
452 )
453 if res.total_item_count == 0:
454 # Need to create the policy with its first rule
455 changed = True
456 if not module.check_mode:
457 if CONTEXT_API_VERSION in api_version:
458 res = blade.post_buckets_cross_origin_resource_sharing_policies(
459 bucket_names=[module.params["name"]],
460 context_names=[module.params["context"]],
461 )
462 else:
463 res = blade.post_buckets_cross_origin_resource_sharing_policies(
464 bucket_names=[module.params["name"]]
465 )
466 if res.status_code != 200:
467 module.fail_json(
468 msg="Failed to create initial CORS policy for {0}. Error: {1}".format(
469 module.params["name"], res.errors[0].message
470 )
471 )
472 # Create a new rule for the policy
473 if not module.check_mode:
474 changed = True
475 if CONTEXT_API_VERSION in api_version:
476 res = blade.get_buckets_cross_origin_resource_sharing_policies_rules(
477 bucket_names=[module.params["name"]],
478 names=[module.params["rule"]],
479 context_names=[module.params["context"]],
480 )
481 else:
482 res = blade.get_buckets_cross_origin_resource_sharing_policies_rules(
483 bucket_names=[module.params["name"]], names=[module.params["rule"]]
484 )
485 if res.status_code == 200:
486 changed = False
487 module.exit_json(changed=changed)
488
489 if CONTEXT_API_VERSION in api_version:
490 res = blade.post_buckets_cross_origin_resource_sharing_policies_rules(
491 bucket_names=[module.params["name"]],
492 names=module.params["rule"],
493 rule=CrossOriginResourceSharingPolicyRulePost(
494 allowed_methods=module.params["methods"],
495 allowed_origins=module.params["origins"],
496 allowed_headers=module.params["headers"],
497 ),
498 context_names=[module.params["context"]],
499 )
500 else:
501 res = blade.post_buckets_cross_origin_resource_sharing_policies_rules(
502 bucket_names=[module.params["name"]],
503 names=module.params["rule"],
504 rule=CrossOriginResourceSharingPolicyRulePost(
505 allowed_methods=module.params["methods"],
506 allowed_origins=module.params["origins"],
507 allowed_headers=module.params["headers"],
508 ),
509 )
510 if res.status_code != 200:
511 module.fail_json(
512 msg="Failed to create CORS policy rule {0} "
513 "in policy {1}. Error: {2}".format(
514 module.params["rule"], module.params["name"], res.errors[0].message
515 )
516 )
517 module.exit_json(changed=changed)
518
519
520 def main():
521 argument_spec = purefb_argument_spec()
522 argument_spec.update(
523 dict(
524 state=dict(type="str", default="present", choices=["absent", "present"]),
525 context=dict(type="str", default=""),
526 policy_type=dict(
527 type="str",
528 default="access",
529 choices=[
530 "access",
531 "cors",
532 ],
533 ),
534 name=dict(type="str", required=True),
535 rule=dict(type="str"),
536 resources=dict(type="list", elements="str", default=["*"]),
537 effect=dict(
538 type="str",
539 default="allow",
540 choices=[
541 "allow",
542 "deny",
543 ],
544 ),
545 methods=dict(
546 type="list",
547 elements="str",
548 default=["GET", "PUT", "HEAD", "POST", "DELETE"],
549 choices=[
550 # Only currently allowed option is:
551 # GET, PUT, HEAD, POST, DELETE
552 "GET",
553 "PUT",
554 "HEAD",
555 "POST",
556 "DELETE",
557 ],
558 ),
559 principals=dict(type="bool", default="True"),
560 origins=dict(type="list", elements="str", default=["*"]),
561 headers=dict(type="list", elements="str", default=["*"]),
562 actions=dict(
563 type="list",
564 elements="str",
565 default="s3:GetObject",
566 choices=[
567 "s3:*",
568 "s3:AbortMultipartUpload",
569 "s3:BypassGovernanceRetention",
570 "s3:CreateBucket",
571 "s3:DeleteBucket",
572 "s3:DeleteObject",
573 "s3:DeleteObjectVersion",
574 "s3:ExtendSafemodeRetentionPeriod",
575 "s3:GetBucketAcl",
576 "s3:GetBucketLocation",
577 "s3:GetBucketVersioning",
578 "s3:GetLifecycleConfiguration",
579 "s3:GetObject",
580 "s3:GetObjectAcl",
581 "s3:GetObjectLegalHold",
582 "s3:GetObjectLockConfiguration",
583 "s3:GetObjectRetention",
584 "s3:GetObjectTagging",
585 "s3:GetObjectVersion",
586 "s3:GetObjectVersionTagging",
587 "s3:ListAllMyBuckets",
588 "s3:ListBucket",
589 "s3:ListBucketMultipartUploads",
590 "s3:ListBucketVersions",
591 "s3:ListMultipartUploadParts",
592 "s3:PutBucketVersioning",
593 "s3:PutLifecycleConfiguration",
594 "s3:PutObject",
595 "s3:PutObjectLegalHold",
596 "s3:PutObjectLockConfiguration",
597 "s3:PutObjectRetention",
598 "s3:ResolveSafemodeConflicts",
599 ],
600 ),
601 )
602 )
603
604 required_if = [["state", "present", ["rule"]]]
605 module = AnsibleModule(
606 argument_spec,
607 required_if=required_if,
608 supports_check_mode=True,
609 )
610
611 state = module.params["state"]
612 if not HAS_PYPURECLIENT:
613 module.fail_json(msg="py-pure-client sdk is required for this module")
614 blade = get_system(module)
615 api_version = list(blade.get_versions().items)
616 if MIN_API_VERSION not in api_version:
617 module.fail_json(
618 msg=(
619 "Minimum FlashBlade REST version required: {0}".format(MIN_API_VERSION)
620 )
621 )
622 if CONTEXT_API_VERSION in api_version:
623 res = blade.get_buckets(
624 names=[module.params["name"]],
625 destroyed=False,
626 context_names=[module.params["context"]],
627 )
628 else:
629 res = blade.get_buckets(names=[module.params["name"]], destroyed=False)
630 if res.status_code != 200:
631 module.fail_json(
632 msg="Bucket {0} does not exist, or is destroyed.".format(
633 module.params["name"]
634 )
635 )
636 if module.params["policy_type"] == "access":
637 if state == "present":
638 create_access_policy(module, blade)
639 else:
640 delete_access_policy(module, blade)
641 else:
642 if state == "present":
643 create_cors_policy(module, blade)
644 else:
645 delete_cors_policy(module, blade)
646
647 module.exit_json(changed=False)
648
649
650 if __name__ == "__main__":
651 main()