PolicyUpdater { static String getEtagForExistingPolicy(ZTSClient zts, PolicyUpdaterConfiguration configuration, String domain) { if (domain == null) { throw new IllegalArgumentException("getEtagForExistingPolicy: null parameters are not valid arguments"); } String policyDir = configuration.getPolicyFileDir(); if (policyDir == null) { throw new IllegalArgumentException("getEtagForExistingPolicy: Invalid configuration: no policy directory path"); } String policyDirPath; if (policyDir.length() - 1 != policyDir.lastIndexOf(File.separator)) { policyDirPath = policyDir + File.separator; } else { policyDirPath = policyDir; } String etag = null; String policyFile = policyDirPath + domain + POLICY_FILE_EXTENSION; LOG.info("Decoding " + policyFile + " to retrieve eTag from policy file."); File file = new File(policyFile); if (file.exists() == false) { LOG.info("Policy file not found."); return etag; } DomainSignedPolicyData domainSignedPolicyData = null; try { domainSignedPolicyData = JSON.fromBytes(Files.readAllBytes(file.toPath()), DomainSignedPolicyData.class); } catch (Exception ex) { LOG.info("Unable to parse domain signed policy file: " + policyFile); return etag; } if (validateSignedPolicies(zts, configuration, domainSignedPolicyData, domain) == false) { LOG.info("Unable to validate domain signed policy file: " + policyFile); return etag; } LOG.info("Checking expiration time for: " + domain); long now = System.currentTimeMillis() / 1000; Timestamp expires = domainSignedPolicyData.getSignedPolicyData().getExpires(); long startupDelayInterval = configuration.getStartupDelayIntervalInSecs(); LOG.info("Expiration time for " + domain + " is: " + (expires.millis() / 1000)); LOG.info("Startup delay: " + startupDelayInterval); LOG.info("Current time: " + now); if (((expires.millis() / 1000) - now) < (startupDelayInterval)) { LOG.info("Signed policies for domain:" + domain + " are expired, returning null."); return null; } if (domainSignedPolicyData.getSignedPolicyData().getModified() != null) { etag = "\"" + domainSignedPolicyData.getSignedPolicyData().getModified().toString() + "\""; LOG.info("ETag: " + etag); } else { LOG.info("No ETag found."); } return etag; }  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 TestGetEtagForExistingPolicy() throws Exception, IOException { String expectedEtag = SignPoliciesUtility.signPolicies("./src/test/resources/unit_test_zts_private_k0.pem", "./src/test/resources/unit_test_zms_private_k0.pem", "./src/test/resources/sys.auth.pol", "./src/test/resources/sys.auth.new.pol"); Map<String, String> newenv = new HashMap<String, String>(); newenv.put("STARTUP_DELAY", Integer.toString(TEST_STARTUP_DELAY)); setEnvironmentVar(newenv); PolicyUpdaterConfiguration configuration = new PolicyUpdaterConfiguration(); configuration.init(pathToAthenzConfigFile, pathToZPUConfigFile); String matchingTag = PolicyUpdater.getEtagForExistingPolicy(null, configuration, "sys.auth.new"); Assert.assertEquals(matchingTag, expectedEtag); matchingTag = PolicyUpdater.getEtagForExistingPolicy(null, configuration, "sys.auth.new"); Assert.assertEquals(matchingTag, expectedEtag); }
@Test public void TestGetEtagForExistingPolicy_ExpirationLessThanStartUpDelay() throws Exception, IOException { Map<String, String> newenv = new HashMap<String, String>(); newenv.put("STARTUP_DELAY", Integer.toString(TEST_STARTUP_DELAY)); setEnvironmentVar(newenv); PolicyUpdaterConfiguration configuration = new PolicyUpdaterConfiguration(); configuration.init(pathToAthenzConfigFile, pathToZPUConfigFile); String matchingTag = PolicyUpdater.getEtagForExistingPolicy(null, configuration, "sys.auth"); Assert.assertNull(matchingTag); matchingTag = PolicyUpdater.getEtagForExistingPolicy(null, configuration, "sys.auth"); Assert.assertNull(matchingTag); }
@Test public void TestGetEtagForExistingPolicy_NoPolicyFile() throws Exception { PolicyUpdaterConfiguration configuration = new PolicyUpdaterConfiguration(); configuration.init(pathToAthenzConfigFile, pathToZPUConfigFile); String matchingTag = PolicyUpdater.getEtagForExistingPolicy(null, configuration, "testDomain"); Assert.assertNull(matchingTag); }
@Test public void TestGetEtagForExistingPolicy_IllegalArgs() throws Exception { PolicyUpdaterConfiguration configuration = new PolicyUpdaterConfiguration(); configuration.init(pathToAthenzConfigFile, pathToZPUConfigFile); boolean exceptionCaught = false; try { PolicyUpdater.getEtagForExistingPolicy(null, configuration, null); } catch (IllegalArgumentException ex) { exceptionCaught = true; } Assert.assertTrue(exceptionCaught); }
@Test public void TestGetEtagForExistingPolicy_BadPolicyFileDir_IllegalArgs() throws Exception { PolicyUpdaterConfiguration configMock = Mockito.mock(PolicyUpdaterConfiguration.class); Mockito.when(configMock.getPolicyFileDir()).thenReturn(null); boolean exceptionCaught = false; try { PolicyUpdater.getEtagForExistingPolicy(null, configMock, "testDomain"); } catch (IllegalArgumentException ex) { exceptionCaught = true; } Assert.assertTrue(exceptionCaught); }