Skip to content

Commit

Permalink
fix(core): add secrets cache
Browse files Browse the repository at this point in the history
closes #1679
  • Loading branch information
brian-mulier-p committed Jul 3, 2023
1 parent dd4e958 commit 1c44030
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions core/src/main/java/io/kestra/core/secret/SecretService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@
import jakarta.inject.Singleton;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Singleton
public class SecretService {
private Map<String, String> decodedSecrets;

public String findSecret(String key) throws IllegalVariableEvaluationException {
String environmentVariable = Optional.ofNullable(System.getenv("SECRETS_" + key.toUpperCase()))
.orElseThrow(() -> new IllegalVariableEvaluationException("Unable to find secret '" + key + "'. " +
"You should add it in your environment variables as 'SECRETS_" + key.toUpperCase()+"' with base64-encoded value."));
return new String(Base64.getDecoder().decode(environmentVariable));
if(decodedSecrets == null) {
decodedSecrets = new HashMap<>();
}

String decodedSecret = decodedSecrets.computeIfAbsent(
key,
(k) -> Optional.ofNullable(System.getenv("SECRETS_" + k.toUpperCase()))
.map(environmentVariable -> new String(Base64.getDecoder().decode(environmentVariable)))
.orElse(null)
);

if(decodedSecret == null){
throw new IllegalVariableEvaluationException("Unable to find secret '" + key + "'. " +
"You should add it in your environment variables as 'SECRETS_" + key.toUpperCase()+"' with base64-encoded value.");
}

return decodedSecret;
}
}

0 comments on commit 1c44030

Please sign in to comment.