PolicyUpdater { static void policyUpdater(PolicyUpdaterConfiguration configuration, ZTSClientFactory ztsFactory) throws Exception { try (ZTSClient zts = ztsFactory.create()) { List<String> domainList = configuration.getDomainList(); LOG.info("policyUpdater: Number of domains to process:" + (domainList == null ? 0 : domainList.size())); if (domainList == null) { LOG.error("policyUpdater: no domain list to process from configuration"); throw new Exception("no configured domains to process"); } for (String domain : domainList) { LOG.info("Fetching signed policies for domain:" + domain); String matchingTag = getEtagForExistingPolicy(zts, configuration, domain); Map<String, List<String>> responseHeaders = null; DomainSignedPolicyData domainSignedPolicyData = null; try { domainSignedPolicyData = zts.getDomainSignedPolicyData(domain, matchingTag, responseHeaders); } catch (Exception exc) { domainSignedPolicyData = null; LOG.error("PolicyUpdater: Unable to retrieve policies from zts for domain=" + domain, exc); } if (domainSignedPolicyData == null) { if (matchingTag != null && !matchingTag.isEmpty()) { LOG.info("PolicyUpdater: Policies not updated since last fetch time"); } } else if (validateSignedPolicies(zts, configuration, domainSignedPolicyData, domain)) { writePolicies(configuration, domain, domainSignedPolicyData); } } postDomainMetrics(zts); } }  static void main(String[] args); static void postDomainMetrics(ZTSClient zts); static final String ZPE_METRIC_FILE_PATH; static final String ZPE_PROP_METRIC_FILE_PATH; }
@Test public void TestPolicyUpdater() throws Exception { PolicyUpdaterConfiguration configuration = new PolicyUpdaterConfiguration(); configuration.init(pathToAthenzConfigFile, pathToZPUTestConfigFile); configuration.setPolicyFileDir(configuration.getRootDir() + TEST_POLICY_DIR); configuration.setPolicyFileTmpDir(configuration.getRootDir() + TEST_POLICY_TEMP_DIR); DebugZTSClientFactory ztsFactory = new DebugZTSClientFactory(); ztsFactory.setPublicKeyId("0"); PolicyUpdater.policyUpdater(configuration, ztsFactory); Path path = Paths.get(configuration.getRootDir() + TEST_POLICY_DIR + File.separator + "sports.pol"); DomainSignedPolicyData domainPolicySignedData = JSON.fromBytes(Files.readAllBytes(path), DomainSignedPolicyData.class); Assert.assertTrue(PolicyUpdater.validateSignedPolicies(null, configuration, domainPolicySignedData, "sports")); Files.delete(path); path = Paths.get(configuration.getRootDir() + TEST_POLICY_DIR + File.separator + "sys.auth.pol"); domainPolicySignedData = JSON.fromBytes(Files.readAllBytes(path), DomainSignedPolicyData.class); Assert.assertTrue(PolicyUpdater.validateSignedPolicies(null, configuration, domainPolicySignedData, "sys.auth.pol")); Files.delete(path); }
@Test public void TestPolicyUpdaterEmptyDomainList() throws Exception { PolicyUpdaterConfiguration configuration = new PolicyUpdaterConfiguration(); configuration.init(pathToAthenzConfigFile, pathToZPUEmptyConfigFile); configuration.setPolicyFileDir("./target/classes"); try { PolicyUpdater.policyUpdater(configuration, new DebugZTSClientFactory()); Assert.fail(); } catch (Exception ex) { Assert.assertTrue(ex.getMessage().contains("no configured domains")); } }
@Test public void TestPolicyUpdaterZTSException() throws Exception { PolicyUpdaterConfiguration configuration = new PolicyUpdaterConfiguration(); configuration.init(pathToAthenzConfigFile, pathToZPUConfigFile); configuration.setPolicyFileDir(configuration.getRootDir() + TEST_POLICY_DIR); configuration.setPolicyFileTmpDir(configuration.getRootDir() + TEST_POLICY_TEMP_DIR); DebugZTSClientFactory ztsFactory = new DebugZTSClientFactory(); ztsFactory.setPublicKeyId("4"); PolicyUpdater.policyUpdater(configuration, ztsFactory); }