-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(storage): public access prevention #7423
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...es/src/main/java/com/google/cloud/examples/storage/buckets/GetPublicAccessPrevention.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright 2020 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. | ||
| */ | ||
| package com.google.cloud.examples.storage.buckets; | ||
|
|
||
| // [START storage_get_public_access_prevention] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class GetPublicAccessPrevention { | ||
| public static void getPublicAccessPrevention(String projectId, String bucketName) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
|
|
||
| // Gets Bucket Metadata and prints publicAccessPrevention value (either 'unspecified' or | ||
| // 'enforced'). | ||
| BucketInfo.PublicAccessPrevention publicAccessPrevention = | ||
| bucket.getIamConfiguration().getPublicAccessPrevention(); | ||
|
|
||
| System.out.println( | ||
| "Public access prevention is set to " | ||
| + publicAccessPrevention.getValue() | ||
| + " for " | ||
| + bucketName); | ||
| } | ||
| } | ||
| // [END storage_get_public_access_prevention] |
48 changes: 48 additions & 0 deletions
48
...ain/java/com/google/cloud/examples/storage/buckets/SetPublicAccessPreventionEnforced.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2020 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. | ||
| */ | ||
| package com.google.cloud.examples.storage.buckets; | ||
|
|
||
| // [START storage_set_public_access_prevention_enforced] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class SetPublicAccessPreventionEnforced { | ||
| public static void setPublicAccessPreventionEnforced(String projectId, String bucketName) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
|
|
||
| // Enforces public access prevention for the bucket | ||
| bucket | ||
| .toBuilder() | ||
| .setIamConfiguration( | ||
| BucketInfo.IamConfiguration.newBuilder() | ||
| .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.ENFORCED) | ||
| .build()) | ||
| .build() | ||
| .update(); | ||
|
|
||
| System.out.println("Public access prevention is set to enforced for " + bucketName); | ||
| } | ||
| } | ||
| // [END storage_set_public_access_prevention_enforced] |
48 changes: 48 additions & 0 deletions
48
.../java/com/google/cloud/examples/storage/buckets/SetPublicAccessPreventionUnspecified.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2020 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. | ||
| */ | ||
| package com.google.cloud.examples.storage.buckets; | ||
|
|
||
| // [START storage_set_public_access_prevention_unspecified] | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.BucketInfo; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
|
|
||
| public class SetPublicAccessPreventionUnspecified { | ||
| public static void setPublicAccessPreventionUnspecified(String projectId, String bucketName) { | ||
| // The ID of your GCP project | ||
| // String projectId = "your-project-id"; | ||
|
|
||
| // The ID of your GCS bucket | ||
| // String bucketName = "your-unique-bucket-name"; | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
| Bucket bucket = storage.get(bucketName); | ||
|
|
||
| // Sets public access prevention to 'unspecified' for the bucket | ||
| bucket | ||
| .toBuilder() | ||
| .setIamConfiguration( | ||
| BucketInfo.IamConfiguration.newBuilder() | ||
| .setPublicAccessPrevention(BucketInfo.PublicAccessPrevention.UNSPECIFIED) | ||
| .build()) | ||
| .build() | ||
| .update(); | ||
|
|
||
| System.out.println("Public access prevention is set to unspecified for " + bucketName); | ||
| } | ||
| } | ||
| // [END storage_set_public_access_prevention_unspecified] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should these tests be verifying public access prevention without the API call to
getPublicAccessPrevention? This is testing thesetPublicAccessPreventionUnspecifiedfunction, but also relies on the functionality ofgetPublicAccessPrevention. Not sure what practices are on how small a unit should be for each unit test.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.
Fair point, I'll decouple.
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.
Ah for these tests, I keep them simple for samples. This case is covered now by library integration tests.