Search in sources :

Example 1 with Identity

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

the class ModifyPolicy method main.

public static void main(String... args) {
    // Create Resource Manager service object
    // By default, credentials are inferred from the runtime environment.
    ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
    // Get a project from the server
    // Use an existing project's ID
    String projectId = "some-project-id";
    Project project = resourceManager.get(projectId);
    // Get the project's policy
    Policy policy = project.getPolicy();
    // Add a viewer
    Policy.Builder modifiedPolicy = policy.toBuilder();
    Identity newViewer = Identity.user("<insert user's email address here>");
    modifiedPolicy.addIdentity(Role.viewer(), newViewer);
    // Write policy
    Policy updatedPolicy = project.replacePolicy(modifiedPolicy.build());
    // Print policy
    System.out.printf("Updated policy for %s: %n%s%n", projectId, updatedPolicy);
}
Also used : Policy(com.google.cloud.Policy) Project(com.google.cloud.resourcemanager.Project) ResourceManager(com.google.cloud.resourcemanager.ResourceManager) Identity(com.google.cloud.Identity)

Example 2 with Identity

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

the class PolicyMarshaller method toPb.

@Override
protected com.google.api.services.cloudresourcemanager.model.Policy toPb(Policy policy) {
    com.google.api.services.cloudresourcemanager.model.Policy policyPb = new com.google.api.services.cloudresourcemanager.model.Policy();
    List<Binding> bindingPbList = new LinkedList<>();
    for (Map.Entry<Role, Set<Identity>> binding : policy.getBindings().entrySet()) {
        Binding bindingPb = new Binding();
        bindingPb.setRole(binding.getKey().getValue());
        bindingPb.setMembers(Lists.transform(new ArrayList<>(binding.getValue()), new Function<Identity, String>() {

            @Override
            public String apply(Identity identity) {
                return IDENTITY_STR_VALUE_FUNCTION.apply(identity);
            }
        }));
        bindingPbList.add(bindingPb);
    }
    policyPb.setBindings(bindingPbList);
    policyPb.setEtag(policy.getEtag());
    policyPb.setVersion(policy.getVersion());
    return policyPb;
}
Also used : Policy(com.google.cloud.Policy) Binding(com.google.api.services.cloudresourcemanager.model.Binding) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Role(com.google.cloud.Role) Function(com.google.common.base.Function) Identity(com.google.cloud.Identity) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Identity

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

the class ITStorageTest method testBucketPolicy.

@Test
public void testBucketPolicy() {
    String projectId = remoteStorageHelper.getOptions().getProjectId();
    Identity projectOwner = Identity.projectOwner(projectId);
    Identity projectEditor = Identity.projectEditor(projectId);
    Identity projectViewer = Identity.projectViewer(projectId);
    Map<com.google.cloud.Role, Set<Identity>> bindingsWithoutPublicRead = ImmutableMap.of(StorageRoles.legacyBucketOwner(), (Set<Identity>) newHashSet(projectOwner, projectEditor), StorageRoles.legacyBucketReader(), newHashSet(projectViewer));
    Map<com.google.cloud.Role, Set<Identity>> bindingsWithPublicRead = ImmutableMap.of(StorageRoles.legacyBucketOwner(), (Set<Identity>) newHashSet(projectOwner, projectEditor), StorageRoles.legacyBucketReader(), newHashSet(projectViewer), StorageRoles.legacyObjectReader(), newHashSet(Identity.allUsers()));
    // Validate getting policy.
    Policy currentPolicy = storage.getIamPolicy(BUCKET);
    assertEquals(bindingsWithoutPublicRead, currentPolicy.getBindings());
    // Validate updating policy.
    Policy updatedPolicy = storage.setIamPolicy(BUCKET, currentPolicy.toBuilder().addIdentity(StorageRoles.legacyObjectReader(), Identity.allUsers()).build());
    assertEquals(bindingsWithPublicRead, updatedPolicy.getBindings());
    Policy revertedPolicy = storage.setIamPolicy(BUCKET, updatedPolicy.toBuilder().removeIdentity(StorageRoles.legacyObjectReader(), Identity.allUsers()).build());
    assertEquals(bindingsWithoutPublicRead, revertedPolicy.getBindings());
    // Validate testing permissions.
    List<Boolean> expectedPermissions = ImmutableList.of(true, true);
    assertEquals(expectedPermissions, storage.testIamPermissions(BUCKET, ImmutableList.of("storage.buckets.getIamPolicy", "storage.buckets.setIamPolicy")));
}
Also used : Role(com.google.cloud.storage.Acl.Role) Policy(com.google.cloud.Policy) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Identity(com.google.cloud.Identity) Test(org.junit.Test)

Example 4 with Identity

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

the class PolicyHelper method convertToApiPolicy.

static com.google.api.services.storage.model.Policy convertToApiPolicy(Policy policy) {
    List<Bindings> bindings = new ArrayList<>(policy.getBindings().size());
    for (Map.Entry<Role, Set<Identity>> entry : policy.getBindings().entrySet()) {
        List<String> members = new ArrayList<>(entry.getValue().size());
        for (Identity identity : entry.getValue()) {
            members.add(identity.strValue());
        }
        bindings.add(new Bindings().setMembers(members).setRole(entry.getKey().getValue()));
    }
    return new com.google.api.services.storage.model.Policy().setBindings(bindings).setEtag(policy.getEtag());
}
Also used : Policy(com.google.cloud.Policy) Set(java.util.Set) ArrayList(java.util.ArrayList) Bindings(com.google.api.services.storage.model.Policy.Bindings) Role(com.google.cloud.Role) Identity(com.google.cloud.Identity) Map(java.util.Map)

Aggregations

Identity (com.google.cloud.Identity)4 Policy (com.google.cloud.Policy)4 Set (java.util.Set)3 Role (com.google.cloud.Role)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Binding (com.google.api.services.cloudresourcemanager.model.Binding)1 Bindings (com.google.api.services.storage.model.Policy.Bindings)1 Project (com.google.cloud.resourcemanager.Project)1 ResourceManager (com.google.cloud.resourcemanager.ResourceManager)1 Role (com.google.cloud.storage.Acl.Role)1 Function (com.google.common.base.Function)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1