Search in sources :

Example 6 with Binding

use of org.flyte.api.v1.Binding in project google-cloud-java by GoogleCloudPlatform.

the class SourceSnippets method setIamPolicySource.

// [END securitycenter_get_source]
/**
 * Set IAM policy for a source.
 *
 * @param sourceName The source to set IAM Policy for.
 */
// [START securitycenter_set_source_iam]
static Policy setIamPolicySource(SourceName sourceName, String userEmail) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // userEmail = "someuser@domain.com"
        // Set up IAM Policy for the user userMail to use the role findingsEditor.
        // The user must be a valid google account.
        Policy oldPolicy = client.getIamPolicy(sourceName.toString());
        Binding bindings = Binding.newBuilder().setRole("roles/securitycenter.findingsEditor").addMembers("user:" + userEmail).build();
        Policy policy = oldPolicy.toBuilder().addBindings(bindings).build();
        // Start setting up a request to set IAM policy for a source.
        // SourceName sourceName = SourceName.of("123234324", "423432321");
        SetIamPolicyRequest.Builder request = SetIamPolicyRequest.newBuilder().setPolicy(policy).setResource(sourceName.toString());
        // Call the API.
        Policy response = client.setIamPolicy(request.build());
        System.out.println("Policy: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) SetIamPolicyRequest(com.google.iam.v1.SetIamPolicyRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 7 with Binding

use of org.flyte.api.v1.Binding in project java-docs-samples by GoogleCloudPlatform.

the class DeviceRegistryExample method setIamPermissions.

// [END iot_get_iam_policy]
// [START iot_set_iam_policy]
/**
 * Sets IAM permissions for the given registry.
 */
protected static void setIamPermissions(String projectId, String cloudRegion, String registryName, String member, String role) throws GeneralSecurityException, IOException {
    GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
    final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
    com.google.api.services.cloudiot.v1.model.Policy policy = service.projects().locations().registries().getIamPolicy(registryPath, new GetIamPolicyRequest()).execute();
    List<com.google.api.services.cloudiot.v1.model.Binding> bindings = policy.getBindings();
    boolean addNewRole = true;
    if (bindings != null) {
        for (com.google.api.services.cloudiot.v1.model.Binding binding : bindings) {
            if (binding.getRole().equals(role)) {
                List<String> members = binding.getMembers();
                members.add(member);
                binding.setMembers(members);
                addNewRole = false;
            }
        }
    } else {
        bindings = new ArrayList<>();
    }
    if (addNewRole) {
        com.google.api.services.cloudiot.v1.model.Binding bind = new com.google.api.services.cloudiot.v1.model.Binding();
        bind.setRole(role);
        List<String> members = new ArrayList<>();
        members.add(member);
        bind.setMembers(members);
        bindings.add(bind);
    }
    policy.setBindings(bindings);
    SetIamPolicyRequest req = new SetIamPolicyRequest().setPolicy(policy);
    policy = service.projects().locations().registries().setIamPolicy(registryPath, req).execute();
    System.out.println("Policy ETAG: " + policy.getEtag());
    for (com.google.api.services.cloudiot.v1.model.Binding binding : policy.getBindings()) {
        System.out.println(String.format("Role: %s", binding.getRole()));
        System.out.println("Binding members: ");
        for (String mem : binding.getMembers()) {
            System.out.println(String.format("\t%s", mem));
        }
    }
}
Also used : Binding(com.google.iam.v1.Binding) CloudIot(com.google.api.services.cloudiot.v1.CloudIot) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) SetIamPolicyRequest(com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest) JsonFactory(com.google.api.client.json.JsonFactory) ArrayList(java.util.ArrayList) GetIamPolicyRequest(com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 8 with Binding

use of org.flyte.api.v1.Binding in project java-docs-samples by GoogleCloudPlatform.

the class IamAddMember method iamAddMember.

// Add the given IAM member to the key.
public void iamAddMember(String projectId, String locationId, String keyRingId, String keyId, String member) throws IOException {
    // safely clean up any remaining background resources.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        // Build the key version name from the project, location, key ring, key,
        // and key version.
        CryptoKeyName resourceName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
        // The resource name could also be a key ring.
        // KeyRingName resourceName = KeyRingName.of(projectId, locationId, keyRingId);
        // Get the current policy.
        Policy policy = client.getIamPolicy(resourceName);
        // Create a new IAM binding for the member and role.
        Binding binding = Binding.newBuilder().setRole("roles/cloudkms.cryptoKeyEncrypterDecrypter").addMembers(member).build();
        // Add the binding to the policy.
        Policy newPolicy = policy.toBuilder().addBindings(binding).build();
        client.setIamPolicy(resourceName, newPolicy);
        System.out.printf("Updated IAM policy for %s%n", resourceName.toString());
    }
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 9 with Binding

use of org.flyte.api.v1.Binding in project java-docs-samples by GoogleCloudPlatform.

the class IamGetPolicy method iamGetPolicy.

// Get the IAM policy for the given key.
public void iamGetPolicy(String projectId, String locationId, String keyRingId, String keyId) throws IOException {
    // safely clean up any remaining background resources.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
        // Build the key version name from the project, location, key ring, key,
        // and key version.
        CryptoKeyName resourceName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
        // The resource name could also be a key ring.
        // KeyRingName resourceName = KeyRingName.of(projectId, locationId, keyRingId);
        // Get the current policy.
        Policy policy = client.getIamPolicy(resourceName);
        // Print the policy.
        System.out.printf("IAM policy:%n");
        for (Binding binding : policy.getBindingsList()) {
            System.out.printf("%s%n", binding.getRole());
            for (String member : binding.getMembersList()) {
                System.out.printf("- %s%n", member);
            }
        }
    }
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 10 with Binding

use of org.flyte.api.v1.Binding in project java-docs-samples by GoogleCloudPlatform.

the class IamGrantAccess method iamGrantAccess.

// Grant a member access to a particular secret.
public static void iamGrantAccess(String projectId, String secretId, String member) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name from the version.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Request the current IAM policy.
        Policy currentPolicy = client.getIamPolicy(GetIamPolicyRequest.newBuilder().setResource(secretName.toString()).build());
        // Build the new binding.
        Binding binding = Binding.newBuilder().setRole("roles/secretmanager.secretAccessor").addMembers(member).build();
        // Create a new IAM policy from the current policy, adding the binding.
        Policy newPolicy = Policy.newBuilder().mergeFrom(currentPolicy).addBindings(binding).build();
        // Save the updated IAM policy.
        client.setIamPolicy(SetIamPolicyRequest.newBuilder().setResource(secretName.toString()).setPolicy(newPolicy).build());
        System.out.printf("Updated IAM policy for %s\n", secretId);
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Aggregations

Binding (com.google.iam.v1.Binding)104 Policy (com.google.iam.v1.Policy)101 Test (org.junit.Test)87 AbstractMessage (com.google.protobuf.AbstractMessage)82 SetIamPolicyRequest (com.google.iam.v1.SetIamPolicyRequest)47 GetIamPolicyRequest (com.google.iam.v1.GetIamPolicyRequest)43 ResourceName (com.google.api.resourcenames.ResourceName)32 ByteString (com.google.protobuf.ByteString)32 Node (org.flyte.api.v1.Node)8 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)6 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)6 MockIAMPolicy (com.google.iam.v1.MockIAMPolicy)6 TaskNode (org.flyte.api.v1.TaskNode)6 Binding (org.kie.workbench.common.dmn.api.definition.v1_1.Binding)6 Test (org.junit.jupiter.api.Test)5 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)4 SecretName (com.google.cloud.secretmanager.v1.SecretName)4 ArrayList (java.util.ArrayList)4 Binding (org.flyte.api.v1.Binding)4 WorkflowTemplate (org.flyte.api.v1.WorkflowTemplate)4