Search in sources :

Example 1 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method replaceTopicPolicy.

/** Example of replacing a topic policy. */
public Policy replaceTopicPolicy(String topicId) throws Exception {
    // [START pubsub_set_topic_policy]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        String topicName = TopicName.create(projectId, topicId).toString();
        Policy policy = topicAdminClient.getIamPolicy(topicName);
        // add role -> members binding
        Binding binding = Binding.newBuilder().setRole(Role.viewer().toString()).addMembers(Identity.allAuthenticatedUsers().toString()).build();
        // create updated policy
        Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build();
        updatedPolicy = topicAdminClient.setIamPolicy(topicName, updatedPolicy);
        return updatedPolicy;
    }
// [END pubsub_set_topic_policy]
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient)

Example 2 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method getTopicPolicy.

/** Example of getting a topic policy. */
public Policy getTopicPolicy(String topicId) throws Exception {
    // [START pubsub_get_topic_policy]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
        if (policy == null) {
        // topic iam policy was not found
        }
        return policy;
    }
// [END pubsub_get_topic_policy]
}
Also used : Policy(com.google.iam.v1.Policy) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) TopicName(com.google.pubsub.v1.TopicName)

Example 3 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project google-cloud-java by GoogleCloudPlatform.

the class ITPubSubTest method testTopicPolicy.

@Test
public void testTopicPolicy() {
    TopicName topicName = TopicName.create(projectId, formatForTest("testing-topic-policy"));
    topicAdminClient.createTopic(topicName);
    Policy policy = topicAdminClient.getIamPolicy(topicName.toString());
    Binding binding = Binding.newBuilder().setRole("roles/viewer").addMembers("allAuthenticatedUsers").build();
    Policy newPolicy = topicAdminClient.setIamPolicy(topicName.toString(), policy.toBuilder().addBindings(binding).build());
    assertTrue(newPolicy.getBindingsList().contains(binding));
    String permissionName = "pubsub.topics.get";
    List<String> permissions = topicAdminClient.testIamPermissions(topicName.toString(), Collections.singletonList(permissionName)).getPermissionsList();
    assertTrue(permissions.contains(permissionName));
    topicAdminClient.deleteTopic(topicName);
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) ByteString(com.google.protobuf.ByteString) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 4 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientTest method getIamPolicyTest.

@Test
@SuppressWarnings("all")
public void getIamPolicyTest() {
    int version = 351608024;
    ByteString etag = ByteString.copyFromUtf8("21");
    Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build();
    mockIAMPolicy.addResponse(expectedResponse);
    String formattedResource = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]").toString();
    Policy actualResponse = client.getIamPolicy(formattedResource);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockIAMPolicy.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetIamPolicyRequest actualRequest = (GetIamPolicyRequest) actualRequests.get(0);
    Assert.assertEquals(formattedResource, actualRequest.getResource());
}
Also used : Policy(com.google.iam.v1.Policy) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) GetIamPolicyRequest(com.google.iam.v1.GetIamPolicyRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 5 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientTest method setIamPolicyExceptionTest.

@Test
@SuppressWarnings("all")
public void setIamPolicyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockIAMPolicy.addException(exception);
    try {
        String formattedResource = TopicName.create("[PROJECT]", "[TOPIC]").toString();
        Policy policy = Policy.newBuilder().build();
        client.setIamPolicy(formattedResource, policy);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : Policy(com.google.iam.v1.Policy) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Aggregations

Policy (com.google.iam.v1.Policy)216 Test (org.junit.Test)176 Binding (com.google.iam.v1.Binding)132 AbstractMessage (com.google.protobuf.AbstractMessage)115 GetIamPolicyRequest (com.google.iam.v1.GetIamPolicyRequest)78 ByteString (com.google.protobuf.ByteString)76 SetIamPolicyRequest (com.google.iam.v1.SetIamPolicyRequest)69 ResourceName (com.google.api.resourcenames.ResourceName)66 StatusRuntimeException (io.grpc.StatusRuntimeException)43 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)41 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)8 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)8 DeadLetterPolicy (com.google.pubsub.v1.DeadLetterPolicy)8 StorageClient (com.google.storage.v2.StorageClient)8 MockIAMPolicy (com.google.iam.v1.MockIAMPolicy)7 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)6 TopicName (com.google.pubsub.v1.TopicName)6 IOException (java.io.IOException)6 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)4 SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)4