SecretManager { public static void downloadSecrets() { List<? extends Config> downloads = AppSettings.getInstance().getConfigList(AppSettings.SECRET_MANAGER_DOWNLOADS); if (downloads.size() > 0) { for (Config download : downloads) { boolean required; try { required = download.getBoolean("required"); } catch (ConfigException.Missing e) { required = true; } downloadSecret(download.getString("secret"), download.getString("file"), required); } } }  static void downloadSecrets();  }
@Test public void testDownload() throws IOException { Object downloads = ConfigFactory.parseString( AppSettings.SECRET_MANAGER_DOWNLOADS + "=[" + "{" + "secret = \"projects/" + projectId + "/secrets/secretstuff/versions/latest\"," + "file = \"" + secretsDirectory.resolve("secretstuff.txt") + "\"" + "}" + "]").getAnyRef(AppSettings.SECRET_MANAGER_DOWNLOADS); try(SettingsOverride override = SettingsOverride.apply(Map.of(AppSettings.SECRET_MANAGER_DOWNLOADS, downloads))) { SecretManager.downloadSecrets(); String contents = Files.readString(secretsDirectory.resolve("secretstuff.txt")); assertEquals(contents, "This is secret stuff"); } }
@Test public void testFailMissingRequired() throws IOException { Object downloads = ConfigFactory.parseString( AppSettings.SECRET_MANAGER_DOWNLOADS + "=[" + "{" + "secret = \"projects/" + projectId + "/secrets/missing-required/versions/latest\"," + "file = \"" + secretsDirectory.resolve("missing-required.txt") + "\"" + "}" + "]").getAnyRef(AppSettings.SECRET_MANAGER_DOWNLOADS); try(SettingsOverride override = SettingsOverride.apply(Map.of(AppSettings.SECRET_MANAGER_DOWNLOADS, downloads))) { try { SecretManager.downloadSecrets(); fail(); } catch (RuntimeException e) { assertTrue(e.getCause().getClass().equals(NotFoundException.class)); } } }
@Test public void testFailMissingOptional() throws IOException { Object downloads = ConfigFactory.parseString( AppSettings.SECRET_MANAGER_DOWNLOADS + "=[" + "{" + "secret = \"projects/" + projectId + "/secrets/missing-optional/versions/latest\"," + "file = \"" + secretsDirectory.resolve("missing-optional.txt") + "\"," + "required = false" + "}" + "]").getAnyRef(AppSettings.SECRET_MANAGER_DOWNLOADS); try(SettingsOverride override = SettingsOverride.apply(Map.of(AppSettings.SECRET_MANAGER_DOWNLOADS, downloads))) { SecretManager.downloadSecrets(); assertFalse(Files.exists(secretsDirectory.resolve("missing-optional.txt"))); } }