use of org.ow2.authzforce.core.pdp.impl.PdpEngineConfiguration 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();
}
}
}
use of org.ow2.authzforce.core.pdp.impl.PdpEngineConfiguration in project core by authzforce.
the class XacmlJsonTest method test.
@Test(dataProvider = "getTestDirectories")
public void test(final Path testDirectoryPath, final String reqFilterId) throws Exception {
LOGGER.debug("******************************");
LOGGER.debug("Starting PDP test in directory '{}'", testDirectoryPath);
// Response file
final Path expectedRespFilepath = testDirectoryPath.resolve(EXPECTED_RESPONSE_FILENAME_SUFFIX);
// If no Response file, it is just a static policy or request syntax error check
final JSONObject expectedResponse;
if (Files.exists(expectedRespFilepath)) {
try (final BufferedReader reader = Files.newBufferedReader(expectedRespFilepath, StandardCharsets.UTF_8)) {
expectedResponse = new LimitsCheckingJSONObject(reader, MAX_JSON_STRING_LENGTH, MAX_JSON_CHILDREN_COUNT, MAX_JSON_DEPTH);
if (!expectedResponse.has("Response")) {
throw new IllegalArgumentException("Invalid XACML JSON Response file: " + expectedRespFilepath + ". Expected root key: \"Response\"");
}
XacmlJsonUtils.RESPONSE_SCHEMA.validate(expectedResponse);
}
} else {
expectedResponse = null;
// Do nothing except logging -> request = null
LOGGER.debug("Response file '{}' does not exist -> Static Policy/Request syntax error check", expectedRespFilepath);
}
// Request file
final Path reqFilepath = testDirectoryPath.resolve(REQUEST_FILENAME_SUFFIX);
// If no Request file, it is just a static policy syntax error check
final JSONObject request;
if (Files.exists(reqFilepath)) {
try (InputStream inputStream = new FileInputStream(reqFilepath.toFile())) {
request = new JSONObject(new JSONTokener(inputStream));
if (!request.has("Request")) {
throw new IllegalArgumentException("Invalid XACML JSON Request file: " + reqFilepath + ". Expected root key: \"Request\"");
}
try {
XacmlJsonUtils.REQUEST_SCHEMA.validate(request);
} catch (ValidationException e) {
// we found a syntax error in request
if (expectedResponse == null) {
// this is a Request syntax error check and we found the syntax error as
// expected -> success
LOGGER.debug("Successfully found syntax error as expected in Request located at: {}", reqFilepath);
return;
}
// Unexpected error
throw e;
}
}
} 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);
}
/*
* Create PDP
*/
final PdpEngineConfiguration pdpEngineConf;
final Path pdpConfFile = testDirectoryPath.resolve(PDP_CONF_FILENAME);
if (Files.notExists(pdpConfFile)) {
/*
* Policies directory. If it exists, root Policy file is expected to be in there. This is the case for IIE*** conformance tests
*/
final Path policiesDir = testDirectoryPath.resolve(POLICIES_DIRNAME_SUFFIX);
/*
Attribute Provider config
*/
final Path attributeProviderConfFile = testDirectoryPath.resolve(ATTRIBUTE_PROVIDER_FILENAME_SUFFIX);
final Optional<Path> optAttributeProviderConfFile = Files.isRegularFile(attributeProviderConfFile) ? Optional.of(attributeProviderConfFile) : Optional.empty();
try {
if (Files.isDirectory(policiesDir)) {
final Path rootPolicyFile = policiesDir.resolve(ROOT_POLICY_FILENAME_SUFFIX);
pdpEngineConf = TestUtils.newPdpEngineConfiguration(TestUtils.getPolicyRef(rootPolicyFile), policiesDir, ENABLE_XPATH, optAttributeProviderConfFile, reqFilterId, BaseXacmlJsonResultPostprocessor.DefaultFactory.ID);
} else {
final Path rootPolicyFile = testDirectoryPath.resolve(ROOT_POLICY_FILENAME_SUFFIX);
pdpEngineConf = TestUtils.newPdpEngineConfiguration(rootPolicyFile, ENABLE_XPATH, optAttributeProviderConfFile, reqFilterId, BaseXacmlJsonResultPostprocessor.DefaultFactory.ID);
}
} 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) with path: {}*", testDirectoryPath);
return;
}
// Unexpected error
throw e;
}
} 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);
}
}
try (final PdpEngineInoutAdapter<JSONObject, JSONObject> pdp = PdpEngineXacmlJsonAdapters.newXacmlJsonInoutAdapter(pdpEngineConf)) {
if (request == null) {
// this is a policy syntax error check and we didn't found the syntax error as
// expected
org.junit.Assert.fail("Failed to find syntax error as expected in policy(ies) with path: " + testDirectoryPath + "*");
} else if (expectedResponse == null) {
/*
* No expected response, so it is not a PDP evaluation test, but request or policy syntax error check. We got here, so request and policy OK. This is unexpected.
*/
org.junit.Assert.fail("Missing response file '" + expectedRespFilepath + "' or failed to find syntax error as expected in either request located at '" + reqFilepath + "' or policy(ies) with path '" + testDirectoryPath + "*'");
} else {
// this is an evaluation test with request/response (not a policy syntax check)
LOGGER.debug("Request that is sent to the PDP: {}", request);
final JSONObject actualResponse = pdp.evaluate(request);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Response that is received from the PDP : {}", actualResponse);
}
assertNormalizedEquals("Test failed for directory " + testDirectoryPath, expectedResponse, actualResponse);
}
} 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) with path: {}*", testDirectoryPath);
return;
}
// Unexpected error
throw e;
}
}
use of org.ow2.authzforce.core.pdp.impl.PdpEngineConfiguration in project core by authzforce.
the class PdpGetStaticApplicablePoliciesTest method test.
@Test
public void test() throws IllegalArgumentException, IOException, URISyntaxException, JAXBException {
final String testResourceLocationPrefix = TEST_RESOURCES_DIRECTORY_LOCATION + "/";
/*
* 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 + XacmlXmlPdpTest.POLICIES_DIR_NAME);
final Optional<Path> optPoliciesDir;
final Path rootPolicyFile;
if (Files.isDirectory(policiesDir)) {
optPoliciesDir = Optional.of(policiesDir);
rootPolicyFile = policiesDir.resolve(XacmlXmlPdpTest.ROOT_POLICY_FILENAME);
} else {
optPoliciesDir = Optional.empty();
rootPolicyFile = Paths.get(testResourceLocationPrefix + XacmlXmlPdpTest.ROOT_POLICY_FILENAME);
}
/*
* Create PDP
*/
final PdpEngineConfiguration pdpEngineConf = optPoliciesDir.isPresent() ? TestUtils.newPdpEngineConfiguration(TestUtils.getPolicyRef(rootPolicyFile), optPoliciesDir.get(), false, Optional.empty(), null, null) : TestUtils.newPdpEngineConfiguration(rootPolicyFile, false, Optional.empty(), null, null);
try (final PdpEngineInoutAdapter<Request, Response> pdp = PdpEngineAdapters.newXacmlJaxbInoutAdapter(pdpEngineConf)) {
final Iterable<PrimaryPolicyMetadata> staticApplicablePolicies = pdp.getApplicablePolicies();
assertNotNull("One of the policies may not be statically resolved", staticApplicablePolicies);
final Iterator<PrimaryPolicyMetadata> staticApplicablePoliciesIterator = pdp.getApplicablePolicies().iterator();
assertTrue("No root policy in PDP's applicable policies (statically resolved)", staticApplicablePoliciesIterator.hasNext());
assertEquals("Invalid root policy in PDP's applicable policies (statically resolved)", ROOT_POLICYSET_METADATA, staticApplicablePoliciesIterator.next());
for (final PrimaryPolicyMetadata expectedRefPolicyMeta : REF_POLICYSET_METADATA_SET) {
assertTrue("No (more) referenced policy in PDP's applicable policies (statically resolved) although expected", staticApplicablePoliciesIterator.hasNext());
assertEquals("Invalid referenced policy in PDP's applicable policies (statically resolved)", expectedRefPolicyMeta, staticApplicablePoliciesIterator.next());
}
}
}
use of org.ow2.authzforce.core.pdp.impl.PdpEngineConfiguration in project restful-pdp by authzforce.
the class XacmlRestProfileJaxRsTest method startServer.
private static void startServer(String pdpConfigLocation) throws Exception {
final PdpEngineConfiguration pdpConf = PdpEngineConfiguration.getInstance(pdpConfigLocation, "src/test/resources/catalog.xml", "src/test/resources/pdp-ext.xsd");
/*
* See also http://cxf.apache.org/docs/secure-jax-rs-services.html
*/
final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(XacmlPdpResource.class);
sf.setResourceProvider(XacmlPdpResource.class, new SingletonResourceProvider(new XacmlPdpResource(pdpConf)));
// add custom providers if any
sf.setProviders(Collections.singletonList(new JsonRiJaxrsProvider()));
final LoggingFeature loggingFeature = new LoggingFeature();
loggingFeature.setPrettyLogging(true);
loggingFeature.setVerbose(true);
sf.setFeatures(Collections.singletonList(loggingFeature));
sf.setAddress(ENDPOINT_ADDRESS);
server = sf.create();
}
use of org.ow2.authzforce.core.pdp.impl.PdpEngineConfiguration in project core by authzforce.
the class ConformanceV3FromV2 method test.
@Test
public void test() throws Exception {
LOGGER.debug("******************************");
LOGGER.debug("Starting PDP test in directory: '{}'", testDirectoryPath);
// Response file
final XmlnsFilteringParser respUnmarshaller = xacmlParserFactory.getInstance();
final Path expectedRespFilepath = testDirectoryPath.resolve(EXPECTED_RESPONSE_FILENAME_SUFFIX);
// If no Response file, it is just a static policy or request syntax error check
final Response expectedResponse;
if (Files.exists(expectedRespFilepath)) {
expectedResponse = TestUtils.createResponse(expectedRespFilepath, respUnmarshaller);
} else {
expectedResponse = null;
// Do nothing except logging -> request = null
LOGGER.debug("Response file '{}' does not exist -> Static Policy/Request syntax error check", expectedRespFilepath);
}
// Request file
final XmlnsFilteringParser reqUnmarshaller = xacmlParserFactory.getInstance();
final Path reqFilepath = testDirectoryPath.resolve(REQUEST_FILENAME_SUFFIX);
// If no Request file, it is just a static policy syntax error check
final Request request;
if (Files.exists(reqFilepath)) {
try {
request = TestUtils.createRequest(reqFilepath, reqUnmarshaller);
} catch (final JAXBException e) {
// we found a syntax error in request
if (expectedResponse == null) {
// this is a Request syntax error check and we found the syntax error as
// expected -> success
LOGGER.debug("Successfully found syntax error as expected in Request located at: {}", reqFilepath);
return;
}
// Unexpected error
throw e;
}
} 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);
}
/*
* Create PDP
*/
final PdpEngineConfiguration pdpEngineConf;
final Path pdpConfFile = testDirectoryPath.resolve(PDP_CONF_FILENAME);
if (Files.notExists(pdpConfFile)) {
/*
* Policies directory. If it exists, root Policy file is expected to be in there. This is the case for IIE*** conformance tests
*/
final Path policiesDir = testDirectoryPath.resolve(POLICIES_DIRNAME_SUFFIX);
/*
Attribute Provider config
*/
final Path attributeProviderConfFile = testDirectoryPath.resolve(ATTRIBUTE_PROVIDER_FILENAME_SUFFIX);
final Optional<Path> optAttributeProviderConfFile = Files.isRegularFile(attributeProviderConfFile) ? Optional.of(attributeProviderConfFile) : Optional.empty();
try {
if (Files.isDirectory(policiesDir)) {
final Path rootPolicyFile = policiesDir.resolve(ROOT_POLICY_FILENAME_SUFFIX);
pdpEngineConf = TestUtils.newPdpEngineConfiguration(TestUtils.getPolicyRef(rootPolicyFile), policiesDir, enableXPath, optAttributeProviderConfFile, this.reqFilterId, null);
} else {
final Path rootPolicyFile = testDirectoryPath.resolve(ROOT_POLICY_FILENAME_SUFFIX);
pdpEngineConf = TestUtils.newPdpEngineConfiguration(rootPolicyFile, enableXPath, optAttributeProviderConfFile, this.reqFilterId, null);
}
} 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) with path: {}*", testDirectoryPath);
return;
}
// Unexpected error
throw e;
}
} 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);
}
}
try (PdpEngineInoutAdapter<Request, Response> pdp = PdpEngineAdapters.newXacmlJaxbInoutAdapter(pdpEngineConf)) {
if (request == null) {
// this is a policy syntax error check and we didn't found the syntax error as
// expected
Assert.fail("Failed to find syntax error as expected in policy(ies) with path: " + testDirectoryPath + "*");
} else if (expectedResponse == null) {
/*
* No expected response, so it is not a PDP evaluation test, but request or policy syntax error check. We got here, so request and policy OK. This is unexpected.
*/
Assert.fail("Missing response file '" + expectedRespFilepath + "' or failed to find syntax error as expected in either request located at '" + reqFilepath + "' or policy(ies) with path '" + testDirectoryPath + "*'");
} else {
// this is an evaluation test with request/response (not a policy syntax check)
LOGGER.debug("Request that is sent to the PDP: {}", request);
final Response actualResponse = pdp.evaluate(request, reqUnmarshaller.getNamespacePrefixUriMap());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Response that is received from the PDP : {}", TestUtils.printResponse(actualResponse));
}
TestUtils.assertNormalizedEquals("Test failed for directory " + testDirectoryPath, expectedResponse, actualResponse);
}
} 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) with path: {}*", testDirectoryPath);
return;
}
// Unexpected error
throw e;
}
}
Aggregations