Skip to content

Commit

Permalink
Add possibility to reload secrets from properties file with JCasC reload
Browse files Browse the repository at this point in the history
  • Loading branch information
tempora-mutantur committed Jan 20, 2021
1 parent 9a40a90 commit f7dd5f4
Showing 1 changed file with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,26 @@ public class PropertiesSecretSource extends SecretSource {
*/
public static final String SECRETS_DEFAULT_PATH = "/run/secrets/secrets.properties";

private Properties secrets;
private Properties secrets = new Properties();

@Override
public Optional<String> reveal(String secret) {
// lazy initialization
if (secrets == null) {
secrets = new Properties();
final String secretsEnv = System.getenv("SECRETS_FILE");
final String secretsPath = secretsEnv == null ? SECRETS_DEFAULT_PATH : secretsEnv;
final File secretsFile = new File(secretsPath);
if (secretsFile.exists() && secretsFile.isFile()) {
try (InputStream input = new FileInputStream(secretsFile)) {
secrets.load(input);
} catch (IOException ioe) {
LOGGER.log(Level.WARNING,
"Source properties file " + secretsPath + " could not be loaded", ioe);
}
}
}
return Optional.ofNullable(secrets.getProperty(secret));
}

if (secrets.getProperty(secret) == null) {
return Optional.empty();
} else {
return Optional.of(secrets.getProperty(secret));
@Override
public void init() {
secrets.clear();
final String secretsEnv = System.getenv("SECRETS_FILE");
final String secretsPath = secretsEnv == null ? SECRETS_DEFAULT_PATH : secretsEnv;
final File secretsFile = new File(secretsPath);
if (secretsFile.exists() && secretsFile.isFile()) {
try (InputStream input = new FileInputStream(secretsFile)) {
secrets.load(input);
} catch (IOException ioe) {
LOGGER.log(Level.WARNING,
"Source properties file " + secretsPath + " could not be loaded", ioe);
}
}
}
}

0 comments on commit f7dd5f4

Please sign in to comment.