use of com.google.api.services.iam.v1.model.Binding in project terra-workspace-manager by DataBiosphere.
the class PetSaService method disablePetServiceAccountImpersonationWithEtag.
/**
* Revoke the permission to impersonate a pet service account granted by {@code
* enablePetServiceAccountImpersonation}. Unlike other operations, this does not run in a flight
* because it only requires one write operation. This operation is idempotent.
*
* <p>This method requires a user's pet service account email as input. As a transitive
* dependency, this also means the provided workspace must have a GCP context.
*
* <p>This method does not authenticate that the user should have access to impersonate their pet
* SA, callers should validate this first.
*
* @param workspaceUuid ID of the workspace to disable pet SA in
* @param userToDisableEmail The user losing access to pet SA
* @param userRequest This request's token will be used to authenticate SAM requests
* @param eTag GCP eTag which must match the pet SA's current policy. If null, this is ignored.
*/
public Optional<Policy> disablePetServiceAccountImpersonationWithEtag(UUID workspaceUuid, String userToDisableEmail, AuthenticatedUserRequest userRequest, @Nullable String eTag) {
String proxyGroupEmail = SamRethrow.onInterrupted(() -> samService.getProxyGroupEmail(userToDisableEmail, userRequest.getRequiredToken()), "disablePet");
String targetMember = "group:" + proxyGroupEmail;
String projectId = gcpCloudContextService.getRequiredGcpProject(workspaceUuid);
try {
Optional<ServiceAccountName> userToDisablePetSA = getUserPetSa(projectId, userToDisableEmail, userRequest);
if (userToDisablePetSA.isEmpty()) {
return Optional.empty();
}
Policy saPolicy = crlService.getIamCow().projects().serviceAccounts().getIamPolicy(userToDisablePetSA.get()).execute();
// clobbering other changes.
if (eTag != null && !saPolicy.getEtag().equals(eTag)) {
logger.warn("GCP IAM policy eTag did not match expected value when revoking pet SA access for user {} in workspace {}. This is normal for Step retries.", userToDisableEmail, workspaceUuid);
return Optional.empty();
}
// If the member is already not on the policy, we are done
// This handles the case where there are no bindings at all, so we don't
// need to worry about null binding later in the logic.
Optional<Binding> bindingToModify = findServiceAccountUserBinding(saPolicy);
if (bindingToModify.isEmpty() || !bindingToModify.get().getMembers().contains(targetMember)) {
return Optional.empty();
}
bindingToModify.get().getMembers().remove(targetMember);
SetIamPolicyRequest request = new SetIamPolicyRequest().setPolicy(saPolicy);
return Optional.of(crlService.getIamCow().projects().serviceAccounts().setIamPolicy(userToDisablePetSA.get(), request).execute());
} catch (IOException e) {
return handleProxyUpdateError(e, "disabling");
}
}
use of com.google.api.services.iam.v1.model.Binding in project java-docs-samples by GoogleCloudPlatform.
the class Hl7v2StoreSetIamPolicy method hl7v2StoreSetIamPolicy.
public static void hl7v2StoreSetIamPolicy(String hl7v2StoreName) throws IOException {
// String hl7v2StoreName =
// String.format(
// HL7v2_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-hl7v2-id");
// Initialize the client, which will be used to interact with the service.
CloudHealthcare client = createClient();
// Configure the IAMPolicy to apply to the store.
// For more information on understanding IAM roles, see the following:
// https://cloud.google.com/iam/docs/understanding-roles
Binding binding = new Binding().setRole("roles/healthcare.hl7V2Consumer").setMembers(Arrays.asList("domain:google.com"));
Policy policy = new Policy().setBindings(Arrays.asList(binding));
SetIamPolicyRequest policyRequest = new SetIamPolicyRequest().setPolicy(policy);
// Create request and configure any parameters.
Hl7V2Stores.SetIamPolicy request = client.projects().locations().datasets().hl7V2Stores().setIamPolicy(hl7v2StoreName, policyRequest);
// Execute the request and process the results.
Policy updatedPolicy = request.execute();
System.out.println("HL7v2 policy has been updated: " + updatedPolicy.toPrettyString());
}
use of com.google.api.services.iam.v1.model.Binding in project java-docs-samples by GoogleCloudPlatform.
the class QuickstartTests method testQuickstart.
@Test
public void testQuickstart() throws Exception {
String member = "serviceAccount:" + serviceAccount.getEmail();
String role = "roles/logging.logWriter";
// Tests initializeService()
CloudResourceManager crmService = Quickstart.initializeService();
// Tests addBinding()
Quickstart.addBinding(crmService, "projects/" + PROJECT_ID, member, role);
// Get the project's polcy and confirm that the member is in the policy
Policy policy = Quickstart.getPolicy(crmService, "projects/" + PROJECT_ID);
Binding binding = null;
List<Binding> bindings = policy.getBindings();
for (Binding b : bindings) {
if (b.getRole().equals(role)) {
binding = b;
break;
}
}
assertThat(binding.getMembers(), hasItem(member));
// Tests removeMember()
Quickstart.removeMember(crmService, "projects/" + PROJECT_ID, member, role);
// Confirm that the member has been removed
policy = Quickstart.getPolicy(crmService, "projects/" + PROJECT_ID);
binding = null;
bindings = policy.getBindings();
for (Binding b : bindings) {
if (b.getRole().equals(role)) {
binding = b;
break;
}
}
if (binding != null) {
assertThat(binding.getMembers(), not(hasItem(member)));
}
}
use of com.google.api.services.iam.v1.model.Binding in project java-docs-samples by GoogleCloudPlatform.
the class AddMember method addMember.
// Adds a member to a preexisting role.
public static void addMember(Policy policy) {
// policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
String role = "roles/existing-role";
String member = "user:member-to-add@example.com";
List<Binding> bindings = policy.getBindings();
for (Binding b : bindings) {
if (b.getRole().equals(role)) {
b.getMembers().add(member);
System.out.println("Member " + member + " added to role " + role);
return;
}
}
System.out.println("Role not found in policy; member not added");
}
use of com.google.api.services.iam.v1.model.Binding in project java-docs-samples by GoogleCloudPlatform.
the class Quickstart method removeMember.
public static void removeMember(CloudResourceManager crmService, String projectId, String member, String role) {
// Gets the project's policy.
Policy policy = getPolicy(crmService, projectId);
// Removes the member from the role.
Binding binding = null;
for (Binding b : policy.getBindings()) {
if (b.getRole().equals(role)) {
binding = b;
break;
}
}
if (binding.getMembers().contains(member)) {
binding.getMembers().remove(member);
if (binding.getMembers().isEmpty()) {
policy.getBindings().remove(binding);
}
}
// Sets the updated policy.
setPolicy(crmService, projectId, policy);
}
Aggregations