Search in sources :

Example 21 with SetIamPolicyRequest

use of com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest in project terra-cloud-resource-lib by DataBiosphere.

the class IamCowTest method setIamPolicyServiceAccountSerialize.

@Test
public void setIamPolicyServiceAccountSerialize() throws Exception {
    IamCow.Projects.ServiceAccounts.SetIamPolicy setIamPolicy = defaultIam().projects().serviceAccounts().setIamPolicy(ServiceAccountName.builder().projectId("projectId").email("saEmail").build(), new SetIamPolicyRequest().setPolicy(new Policy().setEtag("myEtag")));
    assertEquals("{\"resource\":\"projects/projectId/serviceAccounts/saEmail\"," + "\"content\":{\"policy\":{\"etag\":\"myEtag\"}}}", setIamPolicy.serialize().toString());
}
Also used : Policy(com.google.api.services.iam.v1.model.Policy) SetIamPolicyRequest(com.google.api.services.iam.v1.model.SetIamPolicyRequest) Test(org.junit.jupiter.api.Test)

Example 22 with SetIamPolicyRequest

use of com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest 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 23 with SetIamPolicyRequest

use of com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest 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)

Example 24 with SetIamPolicyRequest

use of com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest in project terra-cli by DataBiosphere.

the class Workspace method grantBreakGlass.

/**
 * Grant break-glass access to a user of this workspace. The Editor and Project IAM Admin roles
 * are granted to the user's proxy group.
 *
 * @param granteeEmail email of the workspace user requesting break-glass access
 * @param userProjectsAdminCredentials credentials for a SA that has permission to set IAM policy
 *     on workspace projects in this WSM deployment (e.g. WSM application SA)
 * @return the proxy group email of the workspace user that was granted break-glass access
 */
public String grantBreakGlass(String granteeEmail, ServiceAccountCredentials userProjectsAdminCredentials) {
    // fetch the user's proxy group email from SAM
    String granteeProxyGroupEmail = SamService.fromContext().getProxyGroupEmail(granteeEmail);
    logger.debug("granteeProxyGroupEmail: {}", granteeProxyGroupEmail);
    // grant the Editor role to the user's proxy group email on the workspace project
    CloudResourceManagerCow resourceManagerCow = CrlUtils.createCloudResourceManagerCow(userProjectsAdminCredentials);
    try {
        Policy policy = resourceManagerCow.projects().getIamPolicy(googleProjectId, new GetIamPolicyRequest()).execute();
        List<Binding> updatedBindings = Optional.ofNullable(policy.getBindings()).orElse(new ArrayList<>());
        updatedBindings.add(new Binding().setRole("roles/editor").setMembers(ImmutableList.of("group:" + granteeProxyGroupEmail)));
        updatedBindings.add(new Binding().setRole("roles/resourcemanager.projectIamAdmin").setMembers(ImmutableList.of("group:" + granteeProxyGroupEmail)));
        policy.setBindings(updatedBindings);
        resourceManagerCow.projects().setIamPolicy(googleProjectId, new SetIamPolicyRequest().setPolicy(policy)).execute();
    } catch (IOException ioEx) {
        throw new SystemException("Error granting the Editor and Project IAM Admin roles to the user's proxy group.", ioEx);
    }
    return granteeProxyGroupEmail;
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding) CloudResourceManagerCow(bio.terra.cloudres.google.cloudresourcemanager.CloudResourceManagerCow) SystemException(bio.terra.cli.exception.SystemException) SetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest) IOException(java.io.IOException) GetIamPolicyRequest(com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)

Example 25 with SetIamPolicyRequest

use of com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest in project terra-resource-buffer by DataBiosphere.

the class SetIamPolicyStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws RetryException {
    // Skip if IAM binding is not set.
    if (gcpProjectConfig.getIamBindings() == null || gcpProjectConfig.getIamBindings().isEmpty()) {
        return StepResult.getStepResultSuccess();
    }
    String projectId = flightContext.getWorkingMap().get(GOOGLE_PROJECT_ID, String.class);
    try {
        Policy policy = rmCow.projects().getIamPolicy(projectId, new GetIamPolicyRequest()).execute();
        gcpProjectConfig.getIamBindings().stream().map(iamBinding -> new Binding().setRole(iamBinding.getRole()).setMembers(iamBinding.getMembers())).forEach(policy.getBindings()::add);
        rmCow.projects().setIamPolicy(projectId, new SetIamPolicyRequest().setPolicy(policy)).execute();
    } catch (IOException e) {
        logger.info("Error when setting IAM policy", e);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : StepResult(bio.terra.stairway.StepResult) Logger(org.slf4j.Logger) Step(bio.terra.stairway.Step) RetryException(bio.terra.stairway.exception.RetryException) com.google.api.services.cloudresourcemanager.v3.model(com.google.api.services.cloudresourcemanager.v3.model) GcpProjectConfig(bio.terra.buffer.generated.model.GcpProjectConfig) LoggerFactory(org.slf4j.LoggerFactory) CloudResourceManagerCow(bio.terra.cloudres.google.cloudresourcemanager.CloudResourceManagerCow) IOException(java.io.IOException) StepStatus(bio.terra.stairway.StepStatus) GOOGLE_PROJECT_ID(bio.terra.buffer.service.resource.FlightMapKeys.GOOGLE_PROJECT_ID) FlightContext(bio.terra.stairway.FlightContext) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult)

Aggregations

IOException (java.io.IOException)11 SetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.SetIamPolicyRequest)6 SetIamPolicyRequest (com.google.api.services.iam.v1.model.SetIamPolicyRequest)6 Policy (com.google.api.services.cloudresourcemanager.v3.model.Policy)5 Binding (com.google.api.services.iam.v1.model.Binding)5 Policy (com.google.api.services.iam.v1.model.Policy)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 Binding (com.google.api.services.cloudresourcemanager.v3.model.Binding)4 GetIamPolicyRequest (com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest)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 ArrayList (java.util.ArrayList)4 ServiceAccountName (bio.terra.cloudres.google.iam.ServiceAccountName)3 StepResult (bio.terra.stairway.StepResult)3 CreateServiceAccountRequest (com.google.api.services.iam.v1.model.CreateServiceAccountRequest)3