|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2019 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | + |
| 19 | +from google.cloud import storage |
| 20 | + |
| 21 | + |
| 22 | +def enable_bucket_policy_only(bucket_name): |
| 23 | + """Enable Bucket Policy Only for a bucket""" |
| 24 | + # [START storage_enable_bucket_policy_only] |
| 25 | + # bucket_name = "my-bucket" |
| 26 | + |
| 27 | + storage_client = storage.Client() |
| 28 | + bucket = storage_client.bucket(bucket_name) |
| 29 | + |
| 30 | + bucket.iam_configuration.bucket_policy_only_enabled = True |
| 31 | + bucket.patch() |
| 32 | + |
| 33 | + print('Bucket Policy Only was enabled for {}.'.format(bucket.name)) |
| 34 | + # [END storage_enable_bucket_policy_only] |
| 35 | + |
| 36 | + |
| 37 | +def disable_bucket_policy_only(bucket_name): |
| 38 | + """Disable Bucket Policy Only for a bucket""" |
| 39 | + # [START storage_disable_bucket_policy_only] |
| 40 | + # bucket_name = "my-bucket" |
| 41 | + |
| 42 | + storage_client = storage.Client() |
| 43 | + bucket = storage_client.bucket(bucket_name) |
| 44 | + |
| 45 | + bucket.iam_configuration.bucket_policy_only_enabled = False |
| 46 | + bucket.patch() |
| 47 | + |
| 48 | + print('Bucket Policy Only was disabled for {}.'.format(bucket.name)) |
| 49 | + # [END storage_disable_bucket_policy_only] |
| 50 | + |
| 51 | + |
| 52 | +def get_bucket_policy_only(bucket_name): |
| 53 | + """Get Bucket Policy Only for a bucket""" |
| 54 | + # [START storage_get_bucket_policy_only] |
| 55 | + # bucket_name = "my-bucket" |
| 56 | + |
| 57 | + storage_client = storage.Client() |
| 58 | + bucket = storage_client.get_bucket(bucket_name) |
| 59 | + iam_configuration = bucket.iam_configuration |
| 60 | + |
| 61 | + if iam_configuration.bucket_policy_only_enabled: |
| 62 | + print('Bucket Policy Only is enabled for {}.'.format(bucket.name)) |
| 63 | + print('Bucket will be locked on {}.'.format( |
| 64 | + iam_configuration.bucket_policy_only_locked_time)) |
| 65 | + else: |
| 66 | + print('Bucket Policy Only is disabled for {}.'.format(bucket.name)) |
| 67 | + # [END storage_get_bucket_policy_only] |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + |
| 72 | + parser = argparse.ArgumentParser( |
| 73 | + description=__doc__, |
| 74 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 75 | + subparsers = parser.add_subparsers(dest='command') |
| 76 | + |
| 77 | + enable_bucket_policy_only_parser = subparsers.add_parser( |
| 78 | + 'enable-bucket-policy-only', help=enable_bucket_policy_only.__doc__) |
| 79 | + enable_bucket_policy_only_parser.add_argument('bucket_name') |
| 80 | + |
| 81 | + disable_bucket_policy_only_parser = subparsers.add_parser( |
| 82 | + 'disable-bucket-policy-only', help=disable_bucket_policy_only.__doc__) |
| 83 | + disable_bucket_policy_only_parser.add_argument('bucket_name') |
| 84 | + |
| 85 | + get_bucket_policy_only_parser = subparsers.add_parser( |
| 86 | + 'get-bucket-policy-only', help=get_bucket_policy_only.__doc__) |
| 87 | + get_bucket_policy_only_parser.add_argument('bucket_name') |
| 88 | + |
| 89 | + args = parser.parse_args() |
| 90 | + |
| 91 | + if args.command == 'enable-bucket-policy-only': |
| 92 | + enable_bucket_policy_only(args.bucket_name) |
| 93 | + elif args.command == 'disable-bucket-policy-only': |
| 94 | + disable_bucket_policy_only(args.bucket_name) |
| 95 | + elif args.command == 'get-bucket-policy-only': |
| 96 | + get_bucket_policy_only(args.bucket_name) |
0 commit comments