Search in sources :

Example 16 with Binding

use of com.google.api.services.cloudresourcemanager.v3.model.Binding 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;
}
Also used : Policy(com.google.api.services.cloudkms.v1.model.Policy) Binding(com.google.api.services.cloudkms.v1.model.Binding) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) SetIamPolicyRequest(com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest)

Example 17 with Binding

use of com.google.api.services.cloudresourcemanager.v3.model.Binding 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));
}
Also used : Policy(com.google.api.services.iam.v1.model.Policy) Binding(com.google.api.services.iam.v1.model.Binding)

Example 18 with Binding

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

the class DatasetSetIamPolicy method datasetSetIamPolicy.

public static void datasetSetIamPolicy(String datasetName) throws IOException {
    // String datasetName =
    // String.format(DATASET_NAME, "your-project-id", "your-region-id", "your-dataset-id");
    // Initialize the client, which will be used to interact with the service.
    CloudHealthcare client = createClient();
    // Configure the IAMPolicy to apply to the dataset.
    // 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.datasetViewer").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.
    Datasets.SetIamPolicy request = client.projects().locations().datasets().setIamPolicy(datasetName, policyRequest);
    // Execute the request and process the results.
    Policy updatedPolicy = request.execute();
    System.out.println("Dataset policy has been updated: " + updatedPolicy.toPrettyString());
}
Also used : Binding(com.google.api.services.healthcare.v1.model.Binding) Policy(com.google.api.services.healthcare.v1.model.Policy) Datasets(com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets) SetIamPolicyRequest(com.google.api.services.healthcare.v1.model.SetIamPolicyRequest) CloudHealthcare(com.google.api.services.healthcare.v1.CloudHealthcare)

Example 19 with Binding

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

the class DicomStoreSetIamPolicy method dicomStoreSetIamPolicy.

public static void dicomStoreSetIamPolicy(String dicomStoreName) throws IOException {
    // String dicomStoreName =
    // String.format(
    // DICOM_NAME, "your-project-id", "your-region-id", "your-dataset-id", "your-dicom-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.dicomStoreAdmin").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.
    DicomStores.SetIamPolicy request = client.projects().locations().datasets().dicomStores().setIamPolicy(dicomStoreName, policyRequest);
    // Execute the request and process the results.
    Policy updatedPolicy = request.execute();
    System.out.println("DICOM policy has been updated: " + updatedPolicy.toPrettyString());
}
Also used : Binding(com.google.api.services.healthcare.v1.model.Binding) Policy(com.google.api.services.healthcare.v1.model.Policy) SetIamPolicyRequest(com.google.api.services.healthcare.v1.model.SetIamPolicyRequest) DicomStores(com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores) CloudHealthcare(com.google.api.services.healthcare.v1.CloudHealthcare)

Example 20 with Binding

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

the class AccessTests method beforeTest.

@Before
public void beforeTest() {
    bout = new ByteArrayOutputStream();
    System.setOut(new PrintStream(bout));
    policyMock = new Policy();
    List<String> members = new ArrayList<String>();
    members.add("user:member-to-remove@example.com");
    Binding binding = new Binding();
    binding.setRole("roles/existing-role");
    binding.setMembers(members);
    List<Binding> bindings = new ArrayList<Binding>();
    bindings.add(binding);
    policyMock.setBindings(bindings);
}
Also used : Policy(com.google.api.services.cloudresourcemanager.v3.model.Policy) Binding(com.google.api.services.cloudresourcemanager.v3.model.Binding) PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Before(org.junit.Before)

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 Policy (com.google.api.services.iam.v1.model.Policy)5 SetIamPolicyRequest (com.google.api.services.iam.v1.model.SetIamPolicyRequest)5 ArrayList (java.util.ArrayList)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 ServiceAccountName (bio.terra.cloudres.google.iam.ServiceAccountName)3 StepResult (bio.terra.stairway.StepResult)3 CreateServiceAccountRequest (com.google.api.services.iam.v1.model.CreateServiceAccountRequest)3