Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6.7k
public access prevention samples & tests#4971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d1fb12e9487bf0d03c83fedcaee264f87526b5fe8731a34669a6d579ab1f5dc718afc7f89d88a97d697b0768ec491524fe9d660a511dfb51008c924dbdc95915d5ab3c8ac32060c89e38e31fa7a084d5835d9afb66782d228947e41caeee3060e71a052ce82f426b6c20f71e4ca4e5c10259738c9a5525d316181ad6f0be62abb5d44c9f72bfaaf07File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env python | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the 'License'); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import os | ||
| import time | ||
| import uuid | ||
| from google.cloud import storage | ||
| import pytest | ||
| @pytest.fixture(scope="function") | ||
| def bucket(): | ||
| """Yields a bucket that is deleted after the test completes.""" | ||
| # The new projects enforces uniform bucket level access, so | ||
| # we need to use the old main project for now. | ||
| original_value = os.environ['GOOGLE_CLOUD_PROJECT'] | ||
| os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['MAIN_GOOGLE_CLOUD_PROJECT'] | ||
| bucket = None | ||
| while bucket is None or bucket.exists(): | ||
| bucket_name = f"uniform-bucket-level-access-{uuid.uuid4().hex}" | ||
| bucket = storage.Client().bucket(bucket_name) | ||
| bucket.create() | ||
| yield bucket | ||
| time.sleep(3) | ||
| bucket.delete(force=True) | ||
| # Set the value back. | ||
| os.environ['GOOGLE_CLOUD_PROJECT'] = original_value | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import storage_get_public_access_prevention | ||
| import storage_set_public_access_prevention_enforced | ||
| import storage_set_public_access_prevention_unspecified | ||
| def test_get_public_access_prevention(bucket, capsys): | ||
| short_name = storage_get_public_access_prevention | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for declaring this as a variable? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I based these tests off of the UBLA tests and this is how those are written Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. supa long lines, was my rationale for using this lols... Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to either use the long name and let the autoformatter deal with it, or if that's really unacceptably long, to do the original import differently ("import storage_get_public_access_prevention as storage_get_pap" or something). Not a critical change since it's only in the test - up to you. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andrewsg , I wrote these tests to match the UBLA tests since they're similar features. Those are written with Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I didn't respond here earlier. Sure, fine to keep it for consistency. Thanks! | ||
| short_name.get_public_access_prevention( | ||
| bucket.name | ||
| ) | ||
| out, _ = capsys.readouterr() | ||
| assert ( | ||
| f"Public access prevention is unspecified for {bucket.name}." | ||
| in out | ||
| ) | ||
| def test_set_public_access_prevention_enforced(bucket, capsys): | ||
| short_name = storage_set_public_access_prevention_enforced | ||
| short_name.set_public_access_prevention_enforced( | ||
| bucket.name | ||
| ) | ||
| out, _ = capsys.readouterr() | ||
| assert ( | ||
| f"Public access prevention is set to enforced for {bucket.name}." | ||
| in out | ||
| ) | ||
| def test_set_public_access_prevention_unspecified(bucket, capsys): | ||
| short_name = storage_set_public_access_prevention_unspecified | ||
| short_name.set_public_access_prevention_unspecified( | ||
| bucket.name | ||
| ) | ||
| out, _ = capsys.readouterr() | ||
| assert ( | ||
| f"Public access prevention is 'unspecified' for {bucket.name}." | ||
| in out | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| google-cloud-pubsub==2.6.0 | ||
| google-cloud-storage==1.38.0 | ||
| google-cloud-storage==1.40.0 | ||
| google-api-python-client==2.7.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #!/usr/bin/env python | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the 'License'); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import sys | ||
| # [START storage_get_public_access_prevention] | ||
| from google.cloud import storage | ||
| def get_public_access_prevention(bucket_name): | ||
| """Gets the public access prevention setting (either 'unspecified' or 'enforced') for a bucket.""" | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "my-bucket" | ||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
| iam_configuration = bucket.iam_configuration | ||
| print( | ||
| f"Public access prevention is {iam_configuration.public_access_prevention} for {bucket.name}." | ||
| ) | ||
| # [END storage_get_public_access_prevention] | ||
| if __name__ == "__main__": | ||
| get_public_access_prevention(bucket_name=sys.argv[1]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/usr/bin/env python | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the 'License'); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import sys | ||
| # [START storage_set_public_access_prevention_enforced] | ||
| from google.cloud import storage | ||
| from google.cloud.storage.constants import PUBLIC_ACCESS_PREVENTION_ENFORCED | ||
| def set_public_access_prevention_enforced(bucket_name): | ||
| """Enforce public access prevention for a bucket.""" | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "my-bucket" | ||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
| bucket.iam_configuration.public_access_prevention = ( | ||
| PUBLIC_ACCESS_PREVENTION_ENFORCED | ||
| ) | ||
| bucket.patch() | ||
| print(f"Public access prevention is set to enforced for {bucket.name}.") | ||
| # [END storage_set_public_access_prevention_enforced] | ||
| if __name__ == "__main__": | ||
| set_public_access_prevention_enforced(bucket_name=sys.argv[1]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/usr/bin/env python | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the 'License'); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import sys | ||
| # [START storage_set_public_access_prevention_unspecified] | ||
| from google.cloud import storage | ||
| from google.cloud.storage.constants import PUBLIC_ACCESS_PREVENTION_UNSPECIFIED | ||
| def set_public_access_prevention_unspecified(bucket_name): | ||
| """Sets the public access prevention status to unspecified, so that the bucket inherits its setting from its parent project.""" | ||
| # The ID of your GCS bucket | ||
| # bucket_name = "my-bucket" | ||
| storage_client = storage.Client() | ||
| bucket = storage_client.get_bucket(bucket_name) | ||
| bucket.iam_configuration.public_access_prevention = ( | ||
| PUBLIC_ACCESS_PREVENTION_UNSPECIFIED | ||
| ) | ||
| bucket.patch() | ||
| print(f"Public access prevention is 'unspecified' for {bucket.name}.") | ||
| # [END storage_set_public_access_prevention_unspecified] | ||
| if __name__ == "__main__": | ||
| set_public_access_prevention_unspecified(bucket_name=sys.argv[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add license