use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project terra-cloud-resource-lib by DataBiosphere.
the class IamCowTest method getAndSetAndTestIamOnServiceAccount.
@Test
public void getAndSetAndTestIamOnServiceAccount() throws Exception {
IamCow.Projects.ServiceAccounts serviceAccounts = defaultIam().projects().serviceAccounts();
String projectName = "projects/" + reusableProject.getProjectId();
ServiceAccount serviceAccount = serviceAccounts.create(projectName, new CreateServiceAccountRequest().setAccountId(randomServiceAccountId())).execute();
ServiceAccountName serviceAccountName = ServiceAccountName.builder().projectId(serviceAccount.getProjectId()).email(serviceAccount.getEmail()).build();
Policy policy = serviceAccounts.getIamPolicy(serviceAccountName).execute();
assertNotNull(policy);
List<Binding> bindingList = new ArrayList<>();
String member = String.format("serviceAccount:%s", IntegrationCredentials.getUserGoogleCredentialsOrDie().getClientEmail());
Binding newBinding = new Binding().setRole("roles/iam.serviceAccountUser").setMembers(Collections.singletonList(member));
bindingList.add(newBinding);
policy.setBindings(bindingList);
Policy updatedPolicy = serviceAccounts.setIamPolicy(serviceAccountName, new SetIamPolicyRequest().setPolicy(policy)).execute();
assertThat(updatedPolicy.getBindings(), hasItem(newBinding));
assertEquals(updatedPolicy, serviceAccounts.getIamPolicy(serviceAccountName).execute());
// Test the permissions of the user for which the IAM policy was set.
IamCow.Projects.ServiceAccounts userIamServiceAccounts = IamCow.create(IntegrationUtils.DEFAULT_CLIENT_CONFIG, IntegrationCredentials.getUserGoogleCredentialsOrDie()).projects().serviceAccounts();
// The "actAs" permission associated with "roles/iam.serviceAccountUser".
String actAsPermission = "iam.serviceAccounts.actAs";
TestIamPermissionsResponse iamResponse = userIamServiceAccounts.testIamPermissions(serviceAccountName, new TestIamPermissionsRequest().setPermissions(ImmutableList.of(actAsPermission))).execute();
assertThat(iamResponse.getPermissions(), Matchers.contains(actAsPermission));
}
use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method getKeyRingPolicy.
// [END kms_get_cryptokey_policy]
// [START kms_get_keyring_policy]
/**
* Retrieves the IAM policy for the given crypto key.
*/
public static Policy getKeyRingPolicy(String projectId, String locationId, String keyRingId) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the keyring
String keyring = 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 = kms.projects().locations().keyRings().getIamPolicy(keyring).execute();
System.out.println(iamPolicy.getBindings());
return iamPolicy;
}
use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method getCryptoKeyPolicy.
// [END kms_restore_cryptokey_version]
// [START kms_get_cryptokey_policy]
/**
* Retrieves the IAM policy for the given crypto key.
*/
public static Policy getCryptoKeyPolicy(String projectId, String locationId, String keyRingId, String cryptoKeyId) 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/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
// Get the current IAM policy and add the new account to it.
Policy iamPolicy = kms.projects().locations().keyRings().cryptoKeys().getIamPolicy(cryptoKey).execute();
System.out.println(iamPolicy.getBindings());
return iamPolicy;
}
use of com.google.api.services.cloudresourcemanager.v3.model.Policy in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method removeMemberFromCryptoKeyPolicy.
// [END kms_add_member_to_keyring_policy]
// [START kms_remove_member_from_cryptokey_policy]
/**
* Removes the given member from the given policy.
*/
public static Policy removeMemberFromCryptoKeyPolicy(String projectId, String locationId, String keyRingId, String cryptoKeyId, 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/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
// Get the current IAM policy and add the new account to it.
Policy iamPolicy = getCryptoKeyPolicy(projectId, locationId, keyRingId, cryptoKeyId);
if (null == iamPolicy.getBindings()) {
// Nothing to remove
return null;
}
// Filter out the given member
for (Binding b : iamPolicy.getBindings()) {
if (role.equals(b.getRole()) && b.getMembers().contains(member)) {
b.getMembers().removeAll(Collections.singletonList(member));
break;
}
}
// Set the new IAM Policy.
Policy newIamPolicy = kms.projects().locations().keyRings().cryptoKeys().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 workbench by all-of-us.
the class IamServiceImpl method grantServiceAccountUserRole.
private void grantServiceAccountUserRole(String googleProject, String petServiceAccount) {
Policy policy = cloudIamClient.getServiceAccountIamPolicy(googleProject, petServiceAccount);
List<Binding> bindingList = Optional.ofNullable(policy.getBindings()).orElse(new ArrayList<>());
bindingList.add(new Binding().setRole(SERVICE_ACCOUNT_USER_ROLE).setMembers(Collections.singletonList("serviceAccount:" + petServiceAccount)));
cloudIamClient.setServiceAccountIamPolicy(googleProject, petServiceAccount, policy.setBindings(bindingList));
}
Aggregations