use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method removeMemberFromKeyRingPolicy.
// [END kms_remove_member_from_cryptokey_policy]
// [START kms_remove_member_from_keyring_policy]
/**
* Removes the given member from the given policy.
*/
public static Policy removeMemberFromKeyRingPolicy(String projectId, String locationId, String keyRingId, String member, String role) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the cryptoKey
String cryptoKey = String.format("projects/%s/locations/%s/keyRings/%s", projectId, locationId, keyRingId);
// Get the current IAM policy and add the new account to it.
Policy iamPolicy = getKeyRingPolicy(projectId, locationId, keyRingId);
// Filter out the given member
for (Binding b : iamPolicy.getBindings()) {
if (role.equals(b.getRole()) && b.getMembers().contains(member)) {
b.getMembers().remove(member);
break;
}
}
// Set the new IAM Policy.
Policy newIamPolicy = kms.projects().locations().keyRings().setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy)).execute();
System.out.println("Response: " + newIamPolicy);
return newIamPolicy;
}
use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project terra-workspace-manager by DataBiosphere.
the class GrantPetUsagePermissionStep method doStep.
@Override
public StepResult doStep(FlightContext context) throws InterruptedException, RetryException {
String userEmail = SamRethrow.onInterrupted(() -> samService.getUserEmailFromSam(userRequest), "enablePet");
Policy modifiedPolicy;
try {
modifiedPolicy = petSaService.enablePetServiceAccountImpersonation(workspaceUuid, userEmail, userRequest);
} catch (ConflictException e) {
// There was a conflict enabling the service account. Request retry.
return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
}
// Store the eTag value of the modified policy in case this step needs to be undone.
FlightMap workingMap = context.getWorkingMap();
workingMap.put(PetSaKeys.MODIFIED_PET_SA_POLICY_ETAG, modifiedPolicy.getEtag());
return StepResult.getStepResultSuccess();
}
use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project terra-workspace-manager by DataBiosphere.
the class NotebookCloudSyncStep method doStep.
@Override
public StepResult doStep(FlightContext flightContext) throws InterruptedException, RetryException {
FlightMap workingMap = flightContext.getWorkingMap();
FlightUtils.validateRequiredEntries(workingMap, ControlledResourceKeys.GCP_CLOUD_CONTEXT);
GcpCloudContext cloudContext = workingMap.get(ControlledResourceKeys.GCP_CLOUD_CONTEXT, GcpCloudContext.class);
List<Binding> newBindings = createBindings(cloudContext, flightContext.getWorkingMap());
AIPlatformNotebooksCow notebooks = crlService.getAIPlatformNotebooksCow();
InstanceName instanceName = resource.toInstanceName(cloudContext.getGcpProjectId());
try {
Policy policy = notebooks.instances().getIamPolicy(instanceName).execute();
// Duplicating bindings is harmless (e.g. on retry). GCP de-duplicates.
Optional.ofNullable(policy.getBindings()).ifPresent(newBindings::addAll);
policy.setBindings(newBindings);
notebooks.instances().setIamPolicy(instanceName, new SetIamPolicyRequest().setPolicy(policy)).execute();
} catch (IOException e) {
return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
}
return StepResult.getStepResultSuccess();
}
use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project terra-workspace-manager by DataBiosphere.
the class CreateGcpContextFlightV2Test method successCreatesProjectAndContext.
@Test
@DisabledIfEnvironmentVariable(named = "TEST_ENV", matches = BUFFER_SERVICE_DISABLED_ENVS_REG_EX)
void successCreatesProjectAndContext() throws Exception {
UUID workspaceUuid = createWorkspace(spendUtils.defaultSpendId());
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
assertTrue(testUtils.getAuthorizedGcpCloudContext(workspaceUuid, userRequest).isEmpty());
// Retry steps once to validate idempotency.
Map<String, StepStatus> retrySteps = getStepNameToStepStatusMap();
FlightDebugInfo debugInfo = FlightDebugInfo.newBuilder().doStepFailures(retrySteps).build();
FlightState flightState = StairwayTestUtils.blockUntilFlightCompletes(jobService.getStairway(), CreateGcpContextFlightV2.class, createInputParameters(workspaceUuid, userRequest), STAIRWAY_FLIGHT_TIMEOUT, debugInfo);
assertEquals(FlightStatus.SUCCESS, flightState.getFlightStatus());
String projectId = flightState.getResultMap().get().get(WorkspaceFlightMapKeys.GCP_PROJECT_ID, String.class);
assertTrue(testUtils.getAuthorizedGcpCloudContext(workspaceUuid, userRequest).isPresent());
String contextProjectId = workspaceService.getAuthorizedRequiredGcpProject(workspaceUuid, userRequest);
assertEquals(projectId, contextProjectId);
// Verify that the policies were properly stored
Optional<GcpCloudContext> optionalCloudContext = testUtils.getAuthorizedGcpCloudContext(workspaceUuid, userRequest);
assertTrue(optionalCloudContext.isPresent(), "has cloud context");
GcpCloudContext cloudContext = optionalCloudContext.get();
assertTrue(cloudContext.getSamPolicyOwner().isPresent(), "has owner policy");
assertTrue(cloudContext.getSamPolicyWriter().isPresent(), "has writer policy");
assertTrue(cloudContext.getSamPolicyReader().isPresent(), "has reader policy");
assertTrue(cloudContext.getSamPolicyApplication().isPresent(), "has application policy");
Project project = crl.getCloudResourceManagerCow().projects().get(projectId).execute();
assertEquals(projectId, project.getProjectId());
assertEquals("billingAccounts/" + spendUtils.defaultBillingAccountId(), crl.getCloudBillingClientCow().getProjectBillingInfo("projects/" + projectId).getBillingAccountName());
assertRolesExist(project);
assertPolicyGroupsSynced(workspaceUuid, project);
}
use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project terra-workspace-manager by DataBiosphere.
the class CreateGcpContextFlightV2Test method assertPolicyGroupsSynced.
/**
* Asserts that Sam groups are granted their appropriate IAM roles on a GCP project.
*/
private void assertPolicyGroupsSynced(UUID workspaceUuid, Project project) throws Exception {
Map<WsmIamRole, String> roleToSamGroup = Arrays.stream(WsmIamRole.values()).collect(Collectors.toMap(Function.identity(), role -> "group:" + SamRethrow.onInterrupted(() -> mockSamService.syncWorkspacePolicy(workspaceUuid, role, userAccessUtils.defaultUserAuthRequest()), "syncWorkspacePolicy")));
Policy currentPolicy = crl.getCloudResourceManagerCow().projects().getIamPolicy(project.getProjectId(), new GetIamPolicyRequest()).execute();
for (WsmIamRole role : WsmIamRole.values()) {
// Don't check MANAGER role, which isn't synced to GCP.
if (role.equals(WsmIamRole.MANAGER)) {
continue;
}
assertRoleBindingInPolicy(role, roleToSamGroup.get(role), currentPolicy, project.getProjectId());
}
}
Aggregations