Search in sources :

Example 1 with Condition

use of com.google.cloud.Condition in project google-cloud-java by GoogleCloudPlatform.

the class RemoveBucketIamConditionalBinding method removeBucketIamConditionalBinding.

/**
 * Example of removing a conditional binding to the Bucket-level IAM
 */
public static void removeBucketIamConditionalBinding(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";
    // For more information please read:
    // https://cloud.google.com/storage/docs/access-control/iam
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Policy originalPolicy = storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));
    String role = "roles/storage.objectViewer";
    // getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's mutable.
    List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
    // Create a condition to compare against
    Condition.Builder conditionBuilder = Condition.newBuilder();
    conditionBuilder.setTitle("Title");
    conditionBuilder.setDescription("Description");
    conditionBuilder.setExpression("resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")");
    Iterator iterator = bindings.iterator();
    while (iterator.hasNext()) {
        Binding binding = (Binding) iterator.next();
        boolean foundRole = binding.getRole().equals(role);
        boolean conditionsEqual = conditionBuilder.build().equals(binding.getCondition());
        // Remove condition when the role and condition are equal
        if (foundRole && conditionsEqual) {
            iterator.remove();
            break;
        }
    }
    // Update policy to remove conditional binding
    Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
    updatedPolicyBuilder.setBindings(bindings).setVersion(3);
    Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
    System.out.println("Conditional Binding was removed.");
}
Also used : Policy(com.google.cloud.Policy) Binding(com.google.cloud.Binding) Condition(com.google.cloud.Condition) Storage(com.google.cloud.storage.Storage) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator)

Example 2 with Condition

use of com.google.cloud.Condition in project google-cloud-java by GoogleCloudPlatform.

the class AddBucketIamConditionalBinding method 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";
    // For more information please read:
    // https://cloud.google.com/storage/docs/access-control/iam
    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";
    // getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's mutable.
    List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
    // Create a condition
    String conditionTitle = "Title";
    String conditionDescription = "Description";
    String conditionExpression = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")";
    Condition.Builder conditionBuilder = Condition.newBuilder();
    conditionBuilder.setTitle(conditionTitle);
    conditionBuilder.setDescription(conditionDescription);
    conditionBuilder.setExpression(conditionExpression);
    // Add condition to a binding
    Binding.Builder newBindingBuilder = Binding.newBuilder().setRole(role).setMembers(Arrays.asList(member)).setCondition(conditionBuilder.build());
    bindings.add(newBindingBuilder.build());
    // Update policy with new conditional binding
    Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
    updatedPolicyBuilder.setBindings(bindings).setVersion(3);
    Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
    System.out.printf("Added %s with role %s to %s with condition %s %s %s\n", member, role, bucketName, conditionTitle, conditionDescription, conditionExpression);
}
Also used : Policy(com.google.cloud.Policy) Binding(com.google.cloud.Binding) Condition(com.google.cloud.Condition) Storage(com.google.cloud.storage.Storage) ArrayList(java.util.ArrayList)

Aggregations

Binding (com.google.cloud.Binding)2 Condition (com.google.cloud.Condition)2 Policy (com.google.cloud.Policy)2 Storage (com.google.cloud.storage.Storage)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)1