Search in sources :

Example 26 with Binding

use of com.google.api.services.iam.v1.model.Binding in project java-docs-samples by GoogleCloudPlatform.

the class RemoveMember method removeMember.

// Removes member from a role; removes binding if binding contains 0 members.
public static void removeMember(Policy policy) {
    // policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
    String role = "roles/existing-role";
    String member = "user:member-to-remove@example.com";
    List<Binding> bindings = policy.getBindings();
    Binding binding = null;
    for (Binding b : bindings) {
        if (b.getRole().equals(role)) {
            binding = b;
        }
    }
    if (binding.getMembers().contains(member)) {
        binding.getMembers().remove(member);
        System.out.println("Member " + member + " removed from " + role);
        if (binding.getMembers().isEmpty()) {
            policy.getBindings().remove(binding);
        }
        return;
    }
    System.out.println("Role not found in policy; member not removed");
    return;
}
Also used : Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding)

Example 27 with Binding

use of com.google.api.services.iam.v1.model.Binding in project terra-workspace-manager by DataBiosphere.

the class GcpCloudSyncStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
    String gcpProjectId = flightContext.getWorkingMap().get(GCP_PROJECT_ID, String.class);
    FlightMap workingMap = flightContext.getWorkingMap();
    // Read Sam groups for each workspace role.
    Map<WsmIamRole, String> workspaceRoleGroupsMap = workingMap.get(WorkspaceFlightMapKeys.IAM_GROUP_EMAIL_MAP, new TypeReference<>() {
    });
    try {
        Policy currentPolicy = resourceManagerCow.projects().getIamPolicy(gcpProjectId, new GetIamPolicyRequest()).execute();
        List<Binding> newBindings = new ArrayList<>();
        // Add all existing bindings to ensure we don't accidentally clobber existing permissions.
        newBindings.addAll(currentPolicy.getBindings());
        // Add appropriate project-level roles for each WSM IAM role.
        workspaceRoleGroupsMap.forEach((role, email) -> newBindings.add(bindingForRole(role, email, gcpProjectId)));
        Policy newPolicy = new Policy().setVersion(currentPolicy.getVersion()).setBindings(newBindings).setEtag(currentPolicy.getEtag());
        SetIamPolicyRequest iamPolicyRequest = new SetIamPolicyRequest().setPolicy(newPolicy);
        logger.info("Setting new Cloud Context IAM policy: " + iamPolicyRequest.toPrettyString());
        resourceManagerCow.projects().setIamPolicy(gcpProjectId, iamPolicyRequest).execute();
    } catch (IOException e) {
        throw new RetryableCrlException("Error setting IAM permissions", e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding) SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest) RetryableCrlException(bio.terra.workspace.service.workspace.exceptions.RetryableCrlException) FlightMap(bio.terra.stairway.FlightMap) WsmIamRole(bio.terra.workspace.service.iam.model.WsmIamRole)

Example 28 with Binding

use of com.google.api.services.iam.v1.model.Binding in project terra-workspace-manager by DataBiosphere.

the class GrantWsmRoleAdminStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
    String wsmSaEmail = GcpUtils.getWsmSaEmail(crlService.getApplicationCredentials());
    String projectId = context.getWorkingMap().get(WorkspaceFlightMapKeys.GCP_PROJECT_ID, String.class);
    try {
        Policy policy = crlService.getCloudResourceManagerCow().projects().getIamPolicy(projectId, new GetIamPolicyRequest()).execute();
        Binding bindingToAdd = new Binding().setMembers(Collections.singletonList("serviceAccount:" + wsmSaEmail)).setRole("roles/iam.roleAdmin");
        List<Binding> bindingList = policy.getBindings();
        bindingList.add(bindingToAdd);
        policy.setBindings(bindingList);
        SetIamPolicyRequest iamPolicyRequest = new SetIamPolicyRequest().setPolicy(policy);
        crlService.getCloudResourceManagerCow().projects().setIamPolicy(projectId, iamPolicyRequest).execute();
    } catch (IOException e) {
        throw new InternalServerErrorException("Error while granting WSM SA the Role Admin role", e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding) SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest) InternalServerErrorException(bio.terra.common.exception.InternalServerErrorException) IOException(java.io.IOException) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)

Example 29 with Binding

use of com.google.api.services.iam.v1.model.Binding in project terra-cloud-resource-lib by DataBiosphere.

the class AIPlatformNotebooksCowTest method setGetTestIamPolicyNotebookInstance.

@Test
public void setGetTestIamPolicyNotebookInstance() throws Exception {
    InstanceName instanceName = defaultInstanceName().instanceId("set-get-iam").build();
    createInstance(instanceName);
    String userEmail = IntegrationCredentials.getUserGoogleCredentialsOrDie().getClientEmail();
    Binding binding = new Binding().setRole("roles/notebooks.viewer").setMembers(ImmutableList.of("serviceAccount:" + userEmail));
    Policy policy = notebooks.instances().getIamPolicy(instanceName).execute();
    policy.setBindings(ImmutableList.of(binding));
    Policy updatedPolicy = notebooks.instances().setIamPolicy(instanceName, new SetIamPolicyRequest().setPolicy(policy)).execute();
    assertThat(updatedPolicy.getBindings(), Matchers.hasItem(binding));
    Policy secondRetrieval = notebooks.instances().getIamPolicy(instanceName).execute();
    assertThat(secondRetrieval.getBindings(), Matchers.hasItem(binding));
    // Test the permissions of the user for which the IAM policy was set.
    AIPlatformNotebooksCow userNotebooks = AIPlatformNotebooksCow.create(IntegrationUtils.DEFAULT_CLIENT_CONFIG, IntegrationCredentials.getUserGoogleCredentialsOrDie());
    // Notebook get permission from "roles/notebooks.viewer".
    String getNotebookPermission = "notebooks.instances.get";
    TestIamPermissionsResponse iamResponse = userNotebooks.instances().testIamPermissions(instanceName, new TestIamPermissionsRequest().setPermissions(ImmutableList.of(getNotebookPermission))).execute();
    assertThat(iamResponse.getPermissions(), Matchers.contains(getNotebookPermission));
    notebooks.instances().delete(instanceName).execute();
}
Also used : Binding(com.google.api.services.notebooks.v1.model.Binding) Policy(com.google.api.services.notebooks.v1.model.Policy) SetIamPolicyRequest(com.google.api.services.notebooks.v1.model.SetIamPolicyRequest) TestIamPermissionsResponse(com.google.api.services.notebooks.v1.model.TestIamPermissionsResponse) TestIamPermissionsRequest(com.google.api.services.notebooks.v1.model.TestIamPermissionsRequest) Test(org.junit.jupiter.api.Test)

Example 30 with Binding

use of com.google.api.services.iam.v1.model.Binding in project terra-cloud-resource-lib by DataBiosphere.

the class AIPlatformNotebooksCowTest method instanceSetIamPolicySerialize.

@Test
public void instanceSetIamPolicySerialize() throws Exception {
    Binding binding = new Binding().setRole("roles/notebooks.viewer").setMembers(ImmutableList.of("userEmail:foo@gmail.com"));
    SetIamPolicyRequest request = new SetIamPolicyRequest().setPolicy(new Policy().setBindings(ImmutableList.of(binding)));
    assertEquals("{\"projectId\":\"my-project\",\"locations\":\"us-east1-b\"," + "\"instanceId\":\"my-id\",\"content\":{\"policy\":{" + "\"bindings\":[{\"members\":[\"userEmail:foo@gmail.com\"]," + "\"role\":\"roles/notebooks.viewer\"}]}}}", notebooks.instances().setIamPolicy(InstanceName.builder().projectId("my-project").location("us-east1-b").instanceId("my-id").build(), request).serialize().toString());
}
Also used : Binding(com.google.api.services.notebooks.v1.model.Binding) Policy(com.google.api.services.notebooks.v1.model.Policy) SetIamPolicyRequest(com.google.api.services.notebooks.v1.model.SetIamPolicyRequest) Test(org.junit.jupiter.api.Test)

Aggregations

Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)12 IOException (java.io.IOException)10 Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)9 Binding (com.google.api.services.iam.v1.model.Binding)6 ArrayList (java.util.ArrayList)6 Policy (com.google.api.services.iam.v1.model.Policy)5 ServiceAccount (com.google.api.services.iam.v1.model.ServiceAccount)5 SetIamPolicyRequest (com.google.api.services.iam.v1.model.SetIamPolicyRequest)5 CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)4 Binding (com.google.api.services.cloudkms.v1.model.Binding)4 Policy (com.google.api.services.cloudkms.v1.model.Policy)4 SetIamPolicyRequest (com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest)4 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)4 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)4 CloudHealthcare (com.google.api.services.healthcare.v1.CloudHealthcare)4 Binding (com.google.api.services.healthcare.v1.model.Binding)4 Policy (com.google.api.services.healthcare.v1.model.Policy)4 SetIamPolicyRequest (com.google.api.services.healthcare.v1.model.SetIamPolicyRequest)4 CreateServiceAccountRequest (com.google.api.services.iam.v1.model.CreateServiceAccountRequest)4 ServiceAccountName (bio.terra.cloudres.google.iam.ServiceAccountName)3