PolicyWriter implements EntityWriter { @Override public void write(Bundle bundle, File rootFolder, Bundle rawBundle) { File policyFolder = new File(rootFolder, "policy"); documentFileUtils.createFolder(policyFolder.toPath()); bundle.getFolderTree().stream().forEach(folder -> { if (folder.getParentFolder() != null) { Path folderFile = policyFolder.toPath().resolve(bundle.getFolderTree().getPath(folder)); documentFileUtils.createFolder(folderFile); } }); Map<String, PolicyMetadata> policyMetadataMap = new HashMap<>(); Map<String, Service> services = bundle.getEntities(Service.class); services.values().parallelStream().forEach(serviceEntity -> { writePolicy(bundle, policyFolder, serviceEntity, serviceEntity.getPolicyXML()); final PolicyMetadata policyMetadata = createPolicyMetadata(bundle, rawBundle, null, serviceEntity); policyMetadataMap.put(policyMetadata.getFullPath(), policyMetadata); }); Stream.of( bundle.getEntities(Policy.class).values().stream(), bundle.getEntities(GlobalPolicy.class).values().stream().map(Policy.class::cast).collect(toList()).stream(), bundle.getEntities(AuditPolicy.class).values().stream().map(Policy.class::cast).collect(toList()).stream() ).flatMap(s -> s) .forEach(policyEntity -> { writePolicy(bundle, policyFolder, policyEntity, policyEntity.getPolicyDocument()); final PolicyMetadata policyMetadata = createPolicyMetadata(bundle, rawBundle, policyEntity, policyEntity); policyMetadataMap.put(policyMetadata.getFullPath(), policyMetadata); }); writePolicyMetadata(policyMetadataMap, rootFolder); } @Inject  PolicyWriter(PolicyConverterRegistry policyConverterRegistry, DocumentFileUtils documentFileUtils,
                 JsonFileUtils jsonFileUtils, EntityLinkerRegistry entityLinkerRegistry); @Override void write(Bundle bundle, File rootFolder, Bundle rawBundle);  }
@Test void testNoPolicies(final TemporaryFolder temporaryFolder) { PolicyWriter writer = new PolicyWriter(policyConverterRegistry, DocumentFileUtils.INSTANCE, JsonFileUtils.INSTANCE, new EntityLinkerRegistry(new HashSet<>())); Bundle bundle = new Bundle(); bundle.addEntity(ROOT_FOLDER); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); writer.write(bundle, temporaryFolder.getRoot(), bundle); File policyFolder = new File(temporaryFolder.getRoot(), "policy"); assertTrue(policyFolder.exists()); assertEquals(0, policyFolder.listFiles().length); }
@Test void testWriteAssertionJS(final TemporaryFolder temporaryFolder) throws DocumentParseException { PolicyWriter writer = new PolicyWriter(policyConverterRegistry, DocumentFileUtils.INSTANCE, JsonFileUtils.INSTANCE, new EntityLinkerRegistry(new HashSet<>())); Bundle bundle = new Bundle(); bundle.addEntity(ROOT_FOLDER); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); Policy policy = new Policy(); policy.setGuid("123"); policy.setPath("assertionPolicy"); policy.setParentFolder(ROOT_FOLDER); policy.setName("assertionPolicy"); policy.setId("asd"); policy.setPolicyXML("<wsp:Policy xmlns:wsp=\"http: " <wsp:All wsp:Usage=\"Required\"><L7p:JavaScript>\n" + " <L7p:ExecutionTimeout stringValue=\"\"/>\n" + " <L7p:Name stringValue=\"assertionPolicy\"/>\n" + " <L7p:Script stringValueReference=\"inline\"><![CDATA[var js = {};]]></L7p:Script>\n" + " </L7p:JavaScript></wsp:All>\n" + "</wsp:Policy>"); policy.setPolicyDocument(DocumentTools.INSTANCE.parse(policy.getPolicyXML()).getDocumentElement()); bundle.getPolicies().put("assertionPolicy", policy); writer.write(bundle, temporaryFolder.getRoot(), bundle); File policyFolder = new File(temporaryFolder.getRoot(), "policy"); assertTrue(policyFolder.exists()); File policyFile = new File(policyFolder, "assertionPolicy.assertion.js"); assertTrue(policyFile.exists()); File configFolder = new File(temporaryFolder.getRoot(), "config"); assertTrue(configFolder.exists()); File policyMetadataFile = new File(configFolder, "policies.yml"); assertTrue(policyMetadataFile.exists()); final Map<String, PolicyMetadata> policyMetadataMap = JsonFileUtils.INSTANCE.readPoliciesConfigFile(temporaryFolder.getRoot(), PolicyMetadata.class); assertEquals(1, policyMetadataMap.size()); assertFalse(policyMetadataMap.get("assertionPolicy").isHasRouting()); }
@Test void testWriteRoutingAssertion(final TemporaryFolder temporaryFolder) throws DocumentParseException { PolicyWriter writer = new PolicyWriter(policyConverterRegistry, DocumentFileUtils.INSTANCE, JsonFileUtils.INSTANCE, new EntityLinkerRegistry(new HashSet<>())); Bundle bundle = new Bundle(); bundle.addEntity(ROOT_FOLDER); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); Policy policy = new Policy(); policy.setGuid("123"); policy.setPath("assertionPolicy"); policy.setParentFolder(ROOT_FOLDER); policy.setName("assertionPolicy"); policy.setId("asd"); policy.setHasRouting(true); policy.setPolicyXML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<wsp:Policy xmlns:L7p=\"http: " xmlns:wsp=\"http: " <wsp:All wsp:Usage=\"Required\">\n" + " <L7p:Http2Routing>\n" + " <L7p:Http2ClientConfigName stringValue=\"&lt;Default Config>\"/>\n" + " <L7p:ProtectedServiceUrl stringValue=\"http: " </L7p:Http2Routing>\n" + " </wsp:All>\n" + "</wsp:Policy>"); policy.setPolicyDocument(DocumentTools.INSTANCE.parse(policy.getPolicyXML()).getDocumentElement()); bundle.getPolicies().put("assertionPolicy", policy); writer.write(bundle, temporaryFolder.getRoot(), bundle); File policyFolder = new File(temporaryFolder.getRoot(), "policy"); assertTrue(policyFolder.exists()); File configFolder = new File(temporaryFolder.getRoot(), "config"); assertTrue(configFolder.exists()); File policyMetadataFile = new File(configFolder, "policies.yml"); assertTrue(policyMetadataFile.exists()); final Map<String, PolicyMetadata> policyMetadataMap = JsonFileUtils.INSTANCE.readPoliciesConfigFile(temporaryFolder.getRoot(), PolicyMetadata.class); assertEquals(1, policyMetadataMap.size()); assertTrue(policyMetadataMap.get("assertionPolicy").isHasRouting()); }
@Test void testWritePolicyWithSubfolder(final TemporaryFolder temporaryFolder) throws DocumentParseException { PolicyWriter writer = new PolicyWriter(policyConverterRegistry, DocumentFileUtils.INSTANCE, JsonFileUtils.INSTANCE, new EntityLinkerRegistry(new HashSet<>())); Bundle bundle = new Bundle(); bundle.addEntity(ROOT_FOLDER); Folder folder = new Folder("0000000000000000ffffffffffff54", "Test"); folder.setParentFolder(ROOT_FOLDER); bundle.addEntity(folder); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); Policy policy = new Policy(); policy.setGuid("123"); policy.setPath("assertionPolicy"); policy.setParentFolder(folder); policy.setName("assertionPolicy"); policy.setId("asd"); policy.setPolicyXML("<wsp:Policy xmlns:wsp=\"http: " <wsp:All wsp:Usage=\"Required\"><L7p:JavaScript>\n" + " <L7p:ExecutionTimeout stringValue=\"\"/>\n" + " <L7p:Name stringValue=\"assertionPolicy\"/>\n" + " <L7p:Script stringValueReference=\"inline\"><![CDATA[var js = {};]]></L7p:Script>\n" + " </L7p:JavaScript></wsp:All>\n" + "</wsp:Policy>"); policy.setPolicyDocument(DocumentTools.INSTANCE.parse(policy.getPolicyXML()).getDocumentElement()); bundle.getPolicies().put("assertionPolicy", policy); writer.write(bundle, temporaryFolder.getRoot(), bundle); File policyFolder = new File(temporaryFolder.getRoot(), "policy"); assertTrue(policyFolder.exists()); File testFolder = new File(policyFolder, "Test"); assertTrue(testFolder.exists()); File policyFile = new File(testFolder, "assertionPolicy.assertion.js"); assertTrue(policyFile.exists()); File configFolder = new File(temporaryFolder.getRoot(), "config"); assertTrue(configFolder.exists()); File policyMetadataFile = new File(configFolder, "policies.yml"); assertTrue(policyMetadataFile.exists()); }
@Test void testWriteServicePolicy(final TemporaryFolder temporaryFolder) throws DocumentParseException { PolicyWriter writer = new PolicyWriter(policyConverterRegistry, DocumentFileUtils.INSTANCE, JsonFileUtils.INSTANCE, new EntityLinkerRegistry(new HashSet<>())); Bundle bundle = new Bundle(); bundle.addEntity(ROOT_FOLDER); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); Service service = new Service(); service.setPath("assertionPolicy"); service.setParentFolder(ROOT_FOLDER); service.setName("assertionPolicy"); service.setId("asd"); DocumentTools documentTools = DocumentTools.INSTANCE; Document document = documentTools.parse("<wsp:Policy xmlns:wsp=\"http: " <wsp:All wsp:Usage=\"Required\"><L7p:JavaScript>\n" + " <L7p:ExecutionTimeout stringValue=\"\"/>\n" + " <L7p:Name stringValue=\"assertionPolicy\"/>\n" + " <L7p:Script stringValueReference=\"inline\"><![CDATA[var js = {};]]></L7p:Script>\n" + " </L7p:JavaScript></wsp:All>\n" + "</wsp:Policy>"); service.setPolicyXML(document.getDocumentElement()); bundle.getServices().put("assertionPolicy", service); writer.write(bundle, temporaryFolder.getRoot(), bundle); File policyFolder = new File(temporaryFolder.getRoot(), "policy"); assertTrue(policyFolder.exists()); File policyFile = new File(policyFolder, "assertionPolicy.assertion.js"); assertTrue(policyFile.exists()); File configFolder = new File(temporaryFolder.getRoot(), "config"); assertTrue(configFolder.exists()); File policyMetadataFile = new File(configFolder, "policies.yml"); assertTrue(policyMetadataFile.exists()); }
@Test void testWritePolicyWithDependencies(final TemporaryFolder temporaryFolder) throws DocumentParseException { PolicyWriter writer = new PolicyWriter(policyConverterRegistry, DocumentFileUtils.INSTANCE, JsonFileUtils.INSTANCE, new EntityLinkerRegistry(new HashSet<>())); Bundle bundle = new Bundle(); bundle.addEntity(ROOT_FOLDER); bundle.setFolderTree(new FolderTree(bundle.getEntities(Folder.class).values())); Policy policy = new Policy(); policy.setGuid("123"); policy.setPath("assertionPolicy"); policy.setParentFolder(ROOT_FOLDER); policy.setName("assertionPolicy"); policy.setId("asd"); policy.setPolicyXML("<wsp:Policy xmlns:wsp=\"http: " <wsp:All wsp:Usage=\"Required\"><L7p:JavaScript>" + " <L7p:ExecutionTimeout stringValue=\"\"/>" + " <L7p:Name stringValue=\"assertionPolicy\"/>" + " <L7p:Script stringValueReference=\"inline\"><![CDATA[var js = {};]]></L7p:Script>" + " </L7p:JavaScript>" + " <L7p:Encapsulated encassName=\"encassDep\"/>" + "</wsp:All>" + "</wsp:Policy>"); policy.setPolicyDocument(DocumentTools.INSTANCE.parse(policy.getPolicyXML()).getDocumentElement()); bundle.getPolicies().put("assertionPolicy", policy); Policy depPolicy = new Policy(); depPolicy.setGuid("456"); depPolicy.setPath("assertionDepPolicy"); depPolicy.setParentFolder(Folder.ROOT_FOLDER); depPolicy.setName("assertionDepPolicy"); depPolicy.setId("qwe"); depPolicy.setPolicyXML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<wsp:Policy xmlns:L7p=\"http: " <wsp:All wsp:Usage=\"Required\">\n" + " <L7p:CommentAssertion>\n" + " <L7p:Comment stringValue=\"Policy Fragment: includedPolicy\"/>\n" + " </L7p:CommentAssertion>\n" + " </wsp:All>\n" + "</wsp:Policy>"); depPolicy.setPolicyDocument(DocumentTools.INSTANCE.parse(depPolicy.getPolicyXML()).getDocumentElement()); bundle.getPolicies().put("assertionDepPolicy", depPolicy); Encass encass = new Encass(); encass.setName("encassOne"); encass.setPolicy("assertionPolicy"); bundle.getEncasses().put("encassOne", encass); encass = new Encass(); encass.setName("encassDep"); encass.setPolicy("anotherPolicy"); bundle.getEncasses().put("encassDep", encass); encass = new Encass(); encass.setName("encassTwo"); encass.setPolicy("assertionPolicy"); bundle.getEncasses().put("encassTwo", encass); encass = new Encass(); encass.setName("encassThree"); encass.setPolicy("assertionDepPolicy"); bundle.getEncasses().put("encassThree", encass); JdbcConnection.Builder builder = new JdbcConnection.Builder(); builder.id("jdbcid"); builder.name("testjdbc"); builder.driverClass("testDriver"); builder.jdbcUrl("jdbc:localhost:3306"); JdbcConnection jdbcConnection = builder.build(); bundle.addEntity(jdbcConnection); Map<Dependency, List<Dependency>> dependencyListMap = new HashMap<>(); List<Dependency> dependencies = new ArrayList<>(); Dependency jdbcCon = new Dependency("jdbcid", JdbcConnection.class, "testjdbc", EntityTypes.JDBC_CONNECTION); dependencies.add(jdbcCon); Dependency encassDependency = new Dependency(null, null, "encassDep", EntityTypes.ENCAPSULATED_ASSERTION_TYPE); dependencies.add(encassDependency); Dependency encassOne = new Dependency(null, null, "encassOne", EntityTypes.ENCAPSULATED_ASSERTION_TYPE); dependencies.add(encassOne); Dependency encassTwo = new Dependency(null, null, "encassTwo", EntityTypes.ENCAPSULATED_ASSERTION_TYPE); dependencies.add(encassTwo); Dependency policyDep = new Dependency("qwe", Policy.class, "assertionDepPolicy", EntityTypes.POLICY_TYPE); dependencies.add(policyDep); dependencyListMap.put(new Dependency("asd", Policy.class, "assertionPolicy", EntityTypes.POLICY_TYPE), dependencies); List<Dependency> jdbcDependencies = new ArrayList<>(); Dependency jdncConPass = new Dependency(null, null, "jdbcpass", EntityTypes.STORED_PASSWORD_TYPE); jdbcDependencies.add(jdncConPass); dependencyListMap.put(jdbcCon, jdbcDependencies); List<Dependency> depPolicyDependencies = new ArrayList<>(); Dependency encassThree = new Dependency(null, null, "encassThree", EntityTypes.ENCAPSULATED_ASSERTION_TYPE); depPolicyDependencies.add(encassThree); dependencyListMap.put(policyDep, depPolicyDependencies); bundle.setDependencyMap(dependencyListMap); writer.write(bundle, temporaryFolder.getRoot(), bundle); File policyFolder = new File(temporaryFolder.getRoot(), "policy"); assertTrue(policyFolder.exists()); File configFolder = new File(temporaryFolder.getRoot(), "config"); assertTrue(configFolder.exists()); File policyMetadataFile = new File(configFolder, "policies.yml"); assertTrue(policyMetadataFile.exists()); Map<String, PolicyMetadata> policyMetadataMap = getPolicyMetadata(policyMetadataFile); PolicyMetadata policyMetadata = policyMetadataMap.get("assertionPolicy"); Set<Dependency> usedEntities = policyMetadata.getUsedEntities(); assertTrue(usedEntities.contains(encassDependency)); assertFalse(usedEntities.contains(encassOne)); assertFalse(usedEntities.contains(encassTwo)); assertTrue(usedEntities.contains(new Dependency(null, null, "testjdbc", EntityTypes.JDBC_CONNECTION))); assertTrue(usedEntities.contains(jdncConPass)); assertTrue(usedEntities.contains(new Dependency(null, null, "assertionDepPolicy", EntityTypes.POLICY_TYPE))); assertFalse(usedEntities.contains(encassThree)); }