Skip to content

Commit 1e9623a

Browse files
author
Daan Hoogland
committed
replace jasypt string encryptor by ACS generic code
1 parent 55fb559 commit 1e9623a

2 files changed

Lines changed: 44 additions & 36 deletions

File tree

utils/src/main/java/com/cloud/utils/crypt/EncryptablePropertyPlaceholderConfigurer.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,42 @@
1919

2020
package com.cloud.utils.crypt;
2121

22-
import org.jasypt.encryption.StringEncryptor;
2322
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
2423

2524
/**
2625
* Spring bean that resolves property placeholders, decrypting any value wrapped as
27-
* {@code ENC(...)} with the given jasypt {@link StringEncryptor}. Values that are not wrapped
28-
* are passed through unchanged.
26+
* {@code ENC(...)} with the management server's own secret key, via
27+
* {@link EncryptionSecretKeyChecker} (the same AES-GCM based mechanism already used to
28+
* encrypt {@code db.properties}). Values that are not wrapped are passed through unchanged,
29+
* and if encryption has not been configured on the management server, decryption is skipped
30+
* entirely and the raw (still-wrapped) value is returned.
2931
*
3032
* This replaces {@code org.jasypt.spring3.properties.EncryptablePropertyPlaceholderConfigurer}
3133
* (from the jasypt-spring3 artifact), which is not on the classpath and is incompatible with
32-
* Spring 5, so beans referencing it fail with a ClassNotFoundException.
34+
* Spring 5, so beans referencing it fail with a ClassNotFoundException. Unlike that class, this
35+
* one needs no separate {@code StringEncryptor}/algorithm bean wired in: declare it as
36+
* <pre>{@code
37+
* <bean id="propertyConfigurer" class="com.cloud.utils.crypt.EncryptablePropertyPlaceholderConfigurer">
38+
* <property name="location" value="classpath:/cred.properties" />
39+
* </bean>
40+
* }</pre>
41+
* and it reuses whichever secret key the management server was configured with (file/env/web,
42+
* see {@code password.encryption.type} in db.properties).
3343
*/
3444
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
3545

3646
private static final String ENC_PREFIX = "ENC(";
3747
private static final String ENC_SUFFIX = ")";
3848

39-
private final StringEncryptor encryptor;
40-
41-
public EncryptablePropertyPlaceholderConfigurer(StringEncryptor encryptor) {
42-
this.encryptor = encryptor;
43-
}
44-
4549
@Override
4650
protected String convertPropertyValue(String originalValue) {
47-
if (originalValue == null) {
48-
return null;
51+
if (originalValue == null || !EncryptionSecretKeyChecker.useEncryption()) {
52+
return originalValue;
4953
}
5054

5155
String trimmedValue = originalValue.trim();
5256
if (trimmedValue.startsWith(ENC_PREFIX) && trimmedValue.endsWith(ENC_SUFFIX)) {
53-
String encryptedValue = trimmedValue.substring(ENC_PREFIX.length(), trimmedValue.length() - ENC_SUFFIX.length());
54-
return encryptor.decrypt(encryptedValue);
57+
return EncryptionSecretKeyChecker.decryptPropertyIfNeeded(trimmedValue);
5558
}
5659

5760
return originalValue;

utils/src/test/java/com/cloud/utils/crypt/EncryptablePropertyPlaceholderConfigurerTest.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,60 @@
1919

2020
package com.cloud.utils.crypt;
2121

22-
import static org.mockito.Mockito.mock;
23-
import static org.mockito.Mockito.verifyNoInteractions;
24-
import static org.mockito.Mockito.when;
25-
26-
import org.jasypt.encryption.StringEncryptor;
22+
import org.junit.After;
2723
import org.junit.Assert;
24+
import org.junit.Before;
2825
import org.junit.Test;
2926

3027
public class EncryptablePropertyPlaceholderConfigurerTest {
3128

29+
private static final String ENCRYPTED_VALUE = "ENC(iYVsCZXiGiC6SzZLMNBvBL93hoUpntxkuRjyaqC8L+JYKXw=)";
30+
31+
private final EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer();
32+
33+
@After
34+
public void tearDown() {
35+
EncryptionSecretKeyChecker.resetEncryptor();
36+
}
37+
3238
@Test
33-
public void convertPropertyValueDecryptsWrappedValue() {
34-
StringEncryptor encryptor = mock(StringEncryptor.class);
35-
when(encryptor.decrypt("bmb2VaFdQb")).thenReturn("secret");
36-
EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor);
39+
public void convertPropertyValueDecryptsWrappedValueWhenEncryptionEnabled() {
40+
EncryptionSecretKeyChecker.initEncryptor("managementkey");
3741

38-
String result = configurer.convertPropertyValue("ENC(bmb2VaFdQb)");
42+
String result = configurer.convertPropertyValue(ENCRYPTED_VALUE);
3943

40-
Assert.assertEquals("secret", result);
44+
Assert.assertEquals("encthis", result);
4145
}
4246

4347
@Test
4448
public void convertPropertyValueTrimsSurroundingWhitespaceBeforeDecrypting() {
45-
StringEncryptor encryptor = mock(StringEncryptor.class);
46-
when(encryptor.decrypt("bmb2VaFdQb")).thenReturn("secret");
47-
EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor);
49+
EncryptionSecretKeyChecker.initEncryptor("managementkey");
4850

49-
String result = configurer.convertPropertyValue(" ENC(bmb2VaFdQb) ");
51+
String result = configurer.convertPropertyValue(" " + ENCRYPTED_VALUE + " ");
52+
53+
Assert.assertEquals("encthis", result);
54+
}
55+
56+
@Test
57+
public void convertPropertyValuePassesThroughWrappedValueWhenEncryptionNotConfigured() {
58+
String result = configurer.convertPropertyValue(ENCRYPTED_VALUE);
5059

51-
Assert.assertEquals("secret", result);
60+
Assert.assertEquals(ENCRYPTED_VALUE, result);
5261
}
5362

5463
@Test
5564
public void convertPropertyValuePassesThroughPlainValues() {
56-
StringEncryptor encryptor = mock(StringEncryptor.class);
57-
EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor);
65+
EncryptionSecretKeyChecker.initEncryptor("managementkey");
5866

5967
String result = configurer.convertPropertyValue("guest");
6068

6169
Assert.assertEquals("guest", result);
62-
verifyNoInteractions(encryptor);
6370
}
6471

6572
@Test
6673
public void convertPropertyValueHandlesNull() {
67-
StringEncryptor encryptor = mock(StringEncryptor.class);
68-
EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor);
74+
EncryptionSecretKeyChecker.initEncryptor("managementkey");
6975

7076
Assert.assertNull(configurer.convertPropertyValue(null));
71-
verifyNoInteractions(encryptor);
7277
}
7378
}

0 commit comments

Comments
 (0)