Skip to content

Commit

Permalink
move decryption logic to Encryptor Config. handle no encryption prope…
Browse files Browse the repository at this point in the history
…rties
  • Loading branch information
Bruce Randall committed Dec 15, 2023
1 parent eb89313 commit 7b498b2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ public class ConfigClientRequestTemplateFactory {

private final Log log;

private EncryptorConfig encryptorConfig;

private final ConfigClientProperties properties;

public ConfigClientRequestTemplateFactory(Log log, ConfigClientProperties properties) {
this.log = log;
this.properties = properties;
this.encryptorConfig = properties.getEncryptorConfig();
}

public Log getLog() {
Expand Down Expand Up @@ -125,12 +128,13 @@ private Optional<AccessTokenResponse> getOAuthToken(RestTemplate template, Strin
return parseTokenResponse(tokenJson);
}

private String decryptProperty(String prop) {
if (prop.startsWith("ENC(")) {
prop = prop.substring(4, prop.lastIndexOf(")"));
return properties.getEncryptorConfig().getEncryptor().decrypt(prop);
private String decryptProperty(String property) {
if (encryptorConfig != null) {
return encryptorConfig.decryptProperty(property);
}
else {
return property;
}
return prop;
}

private Optional<AccessTokenResponse> parseTokenResponse(String tokenJson) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public StringEncryptor getEncryptor() {
return encryptor;
}

public String decryptProperty(String prop) {
if (prop.startsWith("ENC(")) {
prop = prop.substring(4, prop.lastIndexOf(")"));
return getEncryptor().decrypt(prop);
}
return prop;
}

@Override
public String toString() {
return "EncryptorConfig{" + "encryptorAlgorithm='" + encryptorAlgorithm + '\'' + ", encryptorIterations="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,32 @@ void whenCreate_givenBadTokenResponse_thenNoHeaderSet() {
void whenDecryptProperty_givenEncryptedProp_thenDecryptProp() {
// given
ConfigClientProperties properties = new ConfigClientProperties(new MockEnvironment());
System.setProperty(EncryptorConfig.ENCRYPTOR_SYSTEM_PROPERTY, "YaddaYaddaYadda");
EncryptorConfig encryptorConfig = new EncryptorConfig();
encryptorConfig.setEncryptorAlgorithm("PBEWITHHMACSHA512ANDAES_256");
properties.setEncryptorConfig(encryptorConfig);

properties.setConfigClientOauth2Properties(new ConfigClientOauth2Properties());
properties.getConfigClientOauth2Properties().setGrantType("client_credentials");
properties.getConfigClientOauth2Properties()
.setTokenUri(idpUrl + "/realms/test-realm/protocol/openid-connect/token");
properties.getConfigClientOauth2Properties().setOauthUsername("oauthUsername");
properties.getConfigClientOauth2Properties().setOauthPassword("oauthPassword");
System.setProperty(EncryptorConfig.ENCRYPTOR_SYSTEM_PROPERTY, "YaddaYaddaYadda");

StringEncryptor encryptor = encryptorConfig.getEncryptor();
String secret = UUID.randomUUID().toString();
String encryptedProp = encryptor.encrypt(secret);
properties.getConfigClientOauth2Properties().setClientSecret("ENC(" + encryptedProp + ")");
ConfigClientRequestTemplateFactory templateFactory = new ConfigClientRequestTemplateFactory(LOG, properties);
properties.getConfigClientOauth2Properties().setOauthPassword("PLAIN OLD TEXT");
// when
String actualSecret = ReflectionTestUtils.invokeMethod(templateFactory, "decryptProperty",
properties.getConfigClientOauth2Properties().getClientSecret());

String actualSecret = encryptorConfig
.decryptProperty(properties.getConfigClientOauth2Properties().getClientSecret());

// then
assertThat(secret).isEqualTo(actualSecret);
actualSecret = encryptorConfig.decryptProperty(properties.getConfigClientOauth2Properties().getOauthPassword());
assertThat("PLAIN OLD TEXT").isEqualTo(actualSecret);
}

}

0 comments on commit 7b498b2

Please sign in to comment.