Skip to content
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

Iam condition samples #7127

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion google-cloud-clients/pom.xml
Expand Up @@ -219,7 +219,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core-bom</artifactId>
<version>1.92.2</version>
<version>1.92.3-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
6 changes: 6 additions & 0 deletions google-cloud-examples/pom.xml
Expand Up @@ -76,6 +76,12 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.103.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-core</artifactId>
<version>1.92.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
Expand Down
@@ -0,0 +1,56 @@
package com.google.cloud.examples.storage.buckets;

import com.google.cloud.Binding;
import com.google.cloud.Condition;
import com.google.cloud.Policy;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// [START storage_add_bucket_conditional_iam_binding]
public class AddBucketIamConditionalBinding {
/** Example of adding a conditional binding to the Bucket-level IAM */
public static void addBucketIamConditionalBinding(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();
Policy originalPolicy =
storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));

String role = "roles/storage.objectViewer";

String member = "group:example@google.com";

List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
Condition.Builder conditionBuilder = Condition.newBuilder();
conditionBuilder.setTitle("Title");
conditionBuilder.setDescription("Description");
conditionBuilder.setExpression(
"resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")");
bindings.add(
Binding.newBuilder()
.setRole(role)
.setMembers(Arrays.asList(member))
.setCondition(conditionBuilder.build())
.build());

Policy updatedPolicy =
storage.setIamPolicy(
bucketName, originalPolicy.toBuilder().setBindings(bindings).setVersion(3).build());
for (Binding binding : updatedPolicy.getBindingsList()) {
if (binding.getRole().equals(role)
&& binding.getMembers().contains(member)
&& conditionBuilder.build() == binding.getCondition()) {
System.out.printf(
"Added conditional binding with role %s to %s\n", member, role, bucketName);
}
}
}
}
// [END storage_add_bucket_conditional_iam_binding]
@@ -0,0 +1,59 @@
/*
* 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 storaoe_add_bucket_iam_member]
import com.google.cloud.Binding;
import com.google.cloud.Policy;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AddBucketIamMember {
/** Example of adding a member to the Bucket-level IAM */
public static void addBucketIamMember(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();
Policy originalPolicy =
storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));

String role = "roles/storage.objectViewer";

String member = "group:example@google.com";

List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
bindings.add(Binding.newBuilder().setRole(role).setMembers(Arrays.asList(member)).build());

Policy updatedPolicy =
storage.setIamPolicy(
bucketName, originalPolicy.toBuilder().setBindings(bindings).setVersion(3).build());
for (Binding binding : updatedPolicy.getBindingsList()) {
if (binding.getRole().equals(role)
&& binding.getMembers().contains(member)
&& null == binding.getCondition()) {
System.out.printf("Added %s with role %s to %s\n", member, role, bucketName);
}
}
}
}
// [END storage_add_bucket_iam_member]
@@ -0,0 +1,57 @@
/*
* 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_add_bucket_label]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

import java.util.HashMap;
import java.util.Map;

public class AddBucketLabel {
public static void addBucketLabel(String projectId, String bucketName, String labelKey, String labelValue) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The key of the label to add
// String labelKey = "label-key-to-add";

// The value of the label to add
// String labelValue = "label-value-to-add";

Map<String, String> labelsToAdd = new HashMap<>();
labelsToAdd.put(labelKey, labelValue);

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
Map<String, String> labels = bucket.getLabels();
if (labels == null) {
labels = labelsToAdd;
} else {
labels.putAll(labelsToAdd);
}
bucket.toBuilder().setLabels(labels).build().update();

System.out.println(
"Added label " + labelKey + " with value " + labelValue + " to bucket " + bucketName + ".");
}
}
// [END storage_add_bucket_label]
@@ -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_change_default_storage_class]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageOptions;

public class ChangeDefaultStorageClass {
public static void changeDefaultStorageClass(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";

// See the StorageClass documentation for other valid storage classes:
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
StorageClass storageClass = StorageClass.COLDLINE;

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
bucket = bucket.toBuilder().setStorageClass(storageClass).build().update();

System.out.println(
"Default storage class for bucket "
+ bucketName
+ " has been set to "
+ bucket.getStorageClass());
}
}
// [END storage_change_default_storage_class]
@@ -0,0 +1,71 @@
/*
* 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_cors_configuration]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Cors;
import com.google.cloud.storage.HttpMethod;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.ImmutableList;

public class ConfigureBucketCors {
public static void configureBucketCors(String projectId, String bucketName, String origin, String responseHeader,
Integer maxAgeSeconds) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The origin for this CORS config to allow requests from
// String origin = "http://example.appspot.com";

// The response header to share across origins
// String responseHeader = "Content-Type";

// The maximum amount of time the browser can make requests before it must repeat preflighted requests
// Integer maxAgeSeconds = 3600;

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);

// See the HttpMethod documentation for other HTTP methods available:
// https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/urlfetch/HTTPMethod
HttpMethod method = HttpMethod.GET;

Cors cors =
Cors.newBuilder()
.setOrigins(ImmutableList.of(Cors.Origin.of(origin)))
.setMethods(ImmutableList.of(method))
.setResponseHeaders(ImmutableList.of(responseHeader))
.setMaxAgeSeconds(maxAgeSeconds)
.build();

bucket.toBuilder().setCors(ImmutableList.of(cors)).build().update();

System.out.println(
"Bucket "
+ bucketName
+ " was updated with a CORS config to allow GET requests from "
+ origin
+ " sharing "
+ responseHeader
+ " responses across origins");
}
}
// [END storage_cors_configuration]
@@ -0,0 +1,59 @@
/*
* 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_create_bucket_class_location]
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageOptions;

public class CreateBucketWithStorageClassAndLocation {
public static void createBucketWithStorageClassAndLocation(String projectId, String bucketName) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The ID to give your GCS bucket
// String bucketName = "your-unique-bucket-name";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

// See the StorageClass documentation for other valid storage classes:
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
StorageClass storageClass = StorageClass.COLDLINE;

// See this documentation for other valid locations:
// http://g.co/cloud/storage/docs/bucket-locations#location-mr
String location = "asia";

Bucket bucket =
storage.create(
BucketInfo.newBuilder(bucketName)
.setStorageClass(storageClass)
.setLocation(location)
.build());

System.out.println(
"Created bucket "
+ bucket.getName()
+ " in "
+ bucket.getLocation()
+ " with storage class "
+ bucket.getStorageClass());
}
}
// [END storage_create_bucket_class_location]