Search in sources :

Example 21 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project java-functions by googleapis.

the class CloudFunctionsServiceClientTest method getIamPolicyTest.

@Test
public void getIamPolicyTest() throws Exception {
    Policy expectedResponse = Policy.newBuilder().setVersion(351608024).addAllBindings(new ArrayList<Binding>()).setEtag(ByteString.EMPTY).build();
    mockCloudFunctionsService.addResponse(expectedResponse);
    GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder().setResource(CloudFunctionName.of("[PROJECT]", "[LOCATION]", "[FUNCTION]").toString()).setOptions(GetPolicyOptions.newBuilder().build()).build();
    Policy actualResponse = client.getIamPolicy(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockCloudFunctionsService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetIamPolicyRequest actualRequest = ((GetIamPolicyRequest) actualRequests.get(0));
    Assert.assertEquals(request.getResource(), actualRequest.getResource());
    Assert.assertEquals(request.getOptions(), actualRequest.getOptions());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) AbstractMessage(com.google.protobuf.AbstractMessage) GetIamPolicyRequest(com.google.iam.v1.GetIamPolicyRequest) Test(org.junit.Test)

Example 22 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project java-storage by googleapis.

the class StorageClientTest method getIamPolicyTest2.

@Test
public void getIamPolicyTest2() throws Exception {
    Policy expectedResponse = Policy.newBuilder().setVersion(351608024).addAllBindings(new ArrayList<Binding>()).setEtag(ByteString.EMPTY).build();
    mockStorage.addResponse(expectedResponse);
    String resource = "resource-341064690";
    Policy actualResponse = client.getIamPolicy(resource);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockStorage.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetIamPolicyRequest actualRequest = ((GetIamPolicyRequest) actualRequests.get(0));
    Assert.assertEquals(resource, actualRequest.getResource());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) AbstractMessage(com.google.protobuf.AbstractMessage) ByteString(com.google.protobuf.ByteString) GetIamPolicyRequest(com.google.iam.v1.GetIamPolicyRequest) Test(org.junit.Test)

Example 23 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project java-storage by googleapis.

the class StorageClientTest method setIamPolicyExceptionTest2.

@Test
public void setIamPolicyExceptionTest2() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockStorage.addException(exception);
    try {
        String resource = "resource-341064690";
        Policy policy = Policy.newBuilder().build();
        client.setIamPolicy(resource, policy);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : Policy(com.google.iam.v1.Policy) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 24 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project java-storage by googleapis.

the class StorageClientTest method setIamPolicyTest.

@Test
public void setIamPolicyTest() throws Exception {
    Policy expectedResponse = Policy.newBuilder().setVersion(351608024).addAllBindings(new ArrayList<Binding>()).setEtag(ByteString.EMPTY).build();
    mockStorage.addResponse(expectedResponse);
    ResourceName resource = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]");
    Policy policy = Policy.newBuilder().build();
    Policy actualResponse = client.setIamPolicy(resource, policy);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockStorage.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    SetIamPolicyRequest actualRequest = ((SetIamPolicyRequest) actualRequests.get(0));
    Assert.assertEquals(resource.toString(), actualRequest.getResource());
    Assert.assertEquals(policy, actualRequest.getPolicy());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Policy(com.google.iam.v1.Policy) Binding(com.google.iam.v1.Binding) AbstractMessage(com.google.protobuf.AbstractMessage) SetIamPolicyRequest(com.google.iam.v1.SetIamPolicyRequest) ResourceName(com.google.api.resourcenames.ResourceName) Test(org.junit.Test)

Example 25 with Policy

use of oasis.names.tc.xacml._3_0.core.schema.wd_17.Policy in project core by authzforce.

the class XacmlXmlPdpTest method test.

@Test
public void test() throws IllegalArgumentException, IOException, JAXBException {
    LOGGER.debug("******************************");
    LOGGER.debug("Starting PDP test of directory '{}'", testDirPath);
    final String testResourceLocationPrefix = testDirPath + "/";
    // Parse request
    final Request request;
    // if no Request file, it is just a static policy syntax error check
    final Path reqFilepath = Paths.get(testResourceLocationPrefix + REQUEST_FILENAME);
    final XmlnsFilteringParser unmarshaller = XACML_PARSER_FACTORY.getInstance();
    if (Files.exists(reqFilepath)) {
        request = TestUtils.createRequest(reqFilepath, unmarshaller);
        LOGGER.debug("XACML Request sent to the PDP: {}", request);
    } else {
        request = null;
        // do nothing except logging -> request = null
        LOGGER.debug("Request file '{}' does not exist -> Static policy syntax error check (Request/Response ignored)", reqFilepath);
    }
    /*
		 * Policies
		 * 
		 * If there is a "$TEST_DIR/$POLICIES_DIR_NAME" directory, then load all policies from there, including root policy from "$TEST_DIR/$POLICIES_DIR_NAME/$ROOT_POLICY_FILENAME" Else load only the
		 * root policy from "$TEST_DIR/$ROOT_POLICY_FILENAME"
		 */
    final Path policiesDir = Paths.get(testResourceLocationPrefix + POLICIES_DIR_NAME);
    final Optional<Path> optPoliciesDir;
    final Path rootPolicyFile;
    if (Files.isDirectory(policiesDir)) {
        optPoliciesDir = Optional.of(policiesDir);
        rootPolicyFile = policiesDir.resolve(ROOT_POLICY_FILENAME);
    } else {
        optPoliciesDir = Optional.empty();
        rootPolicyFile = Paths.get(testResourceLocationPrefix + ROOT_POLICY_FILENAME);
    }
    /*
		 * Create PDP
		 */
    PdpEngineInoutAdapter<Request, Response> pdp = null;
    final Path pdpConfFile = Paths.get(testResourceLocationPrefix + PDP_CONF_FILENAME);
    try {
        final PdpEngineConfiguration pdpEngineConf;
        if (Files.notExists(pdpConfFile)) {
            LOGGER.debug("No PDP configuration file found at location: '{}'. Using minimal PDP instead (returned by TestUtils.getPDPNewInstance(policy) ).", pdpConfFile);
            pdpEngineConf = optPoliciesDir.isPresent() ? TestUtils.newPdpEngineConfiguration(TestUtils.getPolicyRef(rootPolicyFile), optPoliciesDir.get(), false, Optional.empty(), null, null) : TestUtils.newPdpEngineConfiguration(rootPolicyFile, false, Optional.empty(), null, null);
        } else {
            /*
				 * PDP configuration filename found in test directory -> create PDP from it
				 */
            // final String pdpExtXsdLocation = testResourceLocationPrefix + PDP_EXTENSION_XSD_FILENAME;
            File pdpExtXsdFile = null;
            try {
                pdpExtXsdFile = ResourceUtils.getFile(PDP_EXTENSION_XSD_LOCATION);
            } catch (final FileNotFoundException e) {
                LOGGER.debug("No PDP extension configuration file '{}' found -> JAXB-bound PDP extensions not allowed.", PDP_EXTENSION_XSD_LOCATION);
            }
            try {
                /*
					 * Load the PDP configuration from the configuration, and optionally, the PDP extension XSD if this file exists, and the XML catalog required to resolve these extension XSDs
					 */
                pdpEngineConf = pdpExtXsdFile == null ? PdpEngineConfiguration.getInstance(pdpConfFile.toString()) : PdpEngineConfiguration.getInstance(pdpConfFile.toString(), XML_CATALOG_LOCATION, PDP_EXTENSION_XSD_LOCATION);
            } catch (final IOException e) {
                throw new RuntimeException("Error parsing PDP configuration from file '" + pdpConfFile + "' with extension XSD '" + PDP_EXTENSION_XSD_LOCATION + "' and XML catalog file '" + XML_CATALOG_LOCATION + "'", e);
            }
        }
        pdp = PdpEngineAdapters.newXacmlJaxbInoutAdapter(pdpEngineConf);
        if (request == null) {
            /*
				 * This is a policy syntax error check, and we didn't find the syntax error as expected
				 */
            Assert.fail("Failed to find syntax error as expected in policy(ies) located in directory: " + testDirPath);
        } else {
            // Parse expected response
            final Response expectedResponse = TestUtils.createResponse(Paths.get(testResourceLocationPrefix + EXPECTED_RESPONSE_FILENAME), unmarshaller);
            final Response response = pdp.evaluate(request, null);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("XACML Response received from the PDP: {}", TestUtils.printResponse(response));
            }
            TestUtils.assertNormalizedEquals(testResourceLocationPrefix, expectedResponse, response);
            LOGGER.debug("Finished PDP test of directory '{}'", testDirPath);
        }
    } catch (final IllegalArgumentException e) {
        // we found syntax error in policy
        if (request == null) {
            // this is a policy syntax error check, and we found the syntax error as
            // expected -> success
            LOGGER.debug("Successfully found syntax error as expected in policy(ies) located in directory: {}", testDirPath, e);
        } else {
            throw e;
        }
    } finally {
        if (pdp != null) {
            pdp.close();
        }
    }
}
Also used : XmlnsFilteringParser(org.ow2.authzforce.core.pdp.api.XmlUtils.XmlnsFilteringParser) PdpEngineConfiguration(org.ow2.authzforce.core.pdp.impl.PdpEngineConfiguration) Request(oasis.names.tc.xacml._3_0.core.schema.wd_17.Request) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Response(oasis.names.tc.xacml._3_0.core.schema.wd_17.Response) File(java.io.File) Test(org.junit.Test)

Aggregations

Policy (com.google.iam.v1.Policy)217 Test (org.junit.Test)177 Binding (com.google.iam.v1.Binding)133 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