You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ol...@apache.org on 2021/06/19 22:05:19 UTC
[sling-org-apache-sling-commons-crypto] 04/04: test component
lifecycle and security provider with Bouncy Castle
This is an automated email from the ASF dual-hosted git repository.
olli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-crypto.git
commit 4360451ab13a06b274e478cd71fa2fa962b09091
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Sun Jun 20 00:04:57 2021 +0200
test component lifecycle and security provider with Bouncy Castle
---
pom.xml | 6 ++
.../JasyptStandardPBEStringCryptoService.java | 14 +--
.../JasyptStandardPBEStringCryptoServiceTest.java | 119 +++++++++++++++++++++
3 files changed, 132 insertions(+), 7 deletions(-)
diff --git a/pom.xml b/pom.xml
index 61e608b..11f68f0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -153,6 +153,12 @@
<version>3.1.0</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.bouncycastle</groupId>
+ <artifactId>bcprov-jdk15on</artifactId>
+ <version>1.69</version>
+ <scope>test</scope>
+ </dependency>
<!-- Google -->
<dependency>
<groupId>com.google.guava</groupId>
diff --git a/src/main/java/org/apache/sling/commons/crypto/jasypt/internal/JasyptStandardPBEStringCryptoService.java b/src/main/java/org/apache/sling/commons/crypto/jasypt/internal/JasyptStandardPBEStringCryptoService.java
index 90f00d8..a887b25 100644
--- a/src/main/java/org/apache/sling/commons/crypto/jasypt/internal/JasyptStandardPBEStringCryptoService.java
+++ b/src/main/java/org/apache/sling/commons/crypto/jasypt/internal/JasyptStandardPBEStringCryptoService.java
@@ -55,20 +55,20 @@ import org.slf4j.LoggerFactory;
public class JasyptStandardPBEStringCryptoService implements CryptoService {
@Reference
- private volatile PasswordProvider passwordProvider;
+ protected volatile PasswordProvider passwordProvider;
@Reference
- private volatile IvGenerator ivGenerator;
+ protected volatile IvGenerator ivGenerator;
@Reference(
cardinality = ReferenceCardinality.OPTIONAL
)
- private volatile Provider securityProvider;
+ protected volatile Provider securityProvider;
@Reference(
cardinality = ReferenceCardinality.OPTIONAL
)
- private volatile SaltGenerator saltGenerator;
+ protected volatile SaltGenerator saltGenerator;
private StandardPBEStringEncryptor encryptor;
@@ -78,19 +78,19 @@ public class JasyptStandardPBEStringCryptoService implements CryptoService {
}
@Activate
- private void activate(final JasyptStandardPBEStringCryptoServiceConfiguration configuration) {
+ protected void activate(final JasyptStandardPBEStringCryptoServiceConfiguration configuration) {
logger.debug("activating");
setupEncryptor(configuration);
}
@Modified
- private void modified(final JasyptStandardPBEStringCryptoServiceConfiguration configuration) {
+ protected void modified(final JasyptStandardPBEStringCryptoServiceConfiguration configuration) {
logger.debug("modifying");
setupEncryptor(configuration);
}
@Deactivate
- private void deactivate() {
+ protected void deactivate() {
logger.debug("deactivating");
}
diff --git a/src/test/java/org/apache/sling/commons/crypto/jasypt/internal/JasyptStandardPBEStringCryptoServiceTest.java b/src/test/java/org/apache/sling/commons/crypto/jasypt/internal/JasyptStandardPBEStringCryptoServiceTest.java
new file mode 100644
index 0000000..e8a62fb
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/crypto/jasypt/internal/JasyptStandardPBEStringCryptoServiceTest.java
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.commons.crypto.jasypt.internal;
+
+import java.security.Provider;
+import java.security.Security;
+
+import org.apache.sling.commons.crypto.PasswordProvider;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.jasypt.iv.RandomIvGenerator;
+import org.junit.Test;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.jasypt.commons.CommonUtils.STRING_OUTPUT_TYPE_BASE64;
+import static org.jasypt.commons.CommonUtils.STRING_OUTPUT_TYPE_HEXADECIMAL;
+import static org.jasypt.encryption.pbe.StandardPBEByteEncryptor.DEFAULT_KEY_OBTENTION_ITERATIONS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class JasyptStandardPBEStringCryptoServiceTest {
+
+ private static final String MESSAGE = "Rudy, a Message to You";
+
+ @Test
+ public void testComponentLifecycle() {
+ final PasswordProvider passwordProvider = mock(PasswordProvider.class);
+ when(passwordProvider.getPassword()).thenReturn("+AQ?aDes!'DBMkrCi:FE6q\\sOn=Pbmn=PK8n=PK?".toCharArray());
+ final JasyptStandardPBEStringCryptoService service = new JasyptStandardPBEStringCryptoService();
+ service.passwordProvider = passwordProvider;
+ service.ivGenerator = new RandomIvGenerator();
+ { // activate
+ final JasyptStandardPBEStringCryptoServiceConfiguration configuration = mock(JasyptStandardPBEStringCryptoServiceConfiguration.class);
+ when(configuration.algorithm()).thenReturn("PBEWITHHMACSHA512ANDAES_256");
+ when(configuration.keyObtentionIterations()).thenReturn(DEFAULT_KEY_OBTENTION_ITERATIONS);
+ when(configuration.securityProviderName()).thenReturn(null);
+ when(configuration.stringOutputType()).thenReturn(STRING_OUTPUT_TYPE_BASE64);
+ service.activate(configuration);
+ final String ciphertext = service.encrypt(MESSAGE);
+ final String message = service.decrypt(ciphertext);
+ assertThat(message).isEqualTo(MESSAGE);
+ }
+ { // modified
+ final JasyptStandardPBEStringCryptoServiceConfiguration configuration = mock(JasyptStandardPBEStringCryptoServiceConfiguration.class);
+ when(configuration.algorithm()).thenReturn("PBEWITHHMACSHA512ANDAES_256");
+ when(configuration.keyObtentionIterations()).thenReturn(1);
+ when(configuration.securityProviderName()).thenReturn("");
+ when(configuration.stringOutputType()).thenReturn(STRING_OUTPUT_TYPE_HEXADECIMAL);
+ service.modified(configuration);
+ final String ciphertext = service.encrypt(MESSAGE);
+ final String message = service.decrypt(ciphertext);
+ assertThat(message).isEqualTo(MESSAGE);
+ }
+ { // deactivate
+ service.deactivate();
+ final String ciphertext = service.encrypt(MESSAGE);
+ final String message = service.decrypt(ciphertext);
+ assertThat(message).isEqualTo(MESSAGE);
+ }
+ }
+
+ @Test
+ public void testProviderName() {
+ final Provider securityProvider = new BouncyCastleProvider();
+ Security.addProvider(securityProvider);
+ final PasswordProvider passwordProvider = mock(PasswordProvider.class);
+ when(passwordProvider.getPassword()).thenReturn("+AQ?aDes!'DBMkrCi:FE6q\\sOn=Pbmn=PK8n=PK?".toCharArray());
+ final JasyptStandardPBEStringCryptoService service = new JasyptStandardPBEStringCryptoService();
+ service.passwordProvider = passwordProvider;
+ service.ivGenerator = new RandomIvGenerator();
+
+ final JasyptStandardPBEStringCryptoServiceConfiguration configuration = mock(JasyptStandardPBEStringCryptoServiceConfiguration.class);
+ when(configuration.algorithm()).thenReturn("PBEWITHSHA256AND128BITAES-CBC-BC");
+ when(configuration.keyObtentionIterations()).thenReturn(DEFAULT_KEY_OBTENTION_ITERATIONS);
+ when(configuration.securityProviderName()).thenReturn("BC");
+ when(configuration.stringOutputType()).thenReturn(STRING_OUTPUT_TYPE_BASE64);
+ service.activate(configuration);
+ final String ciphertext = service.encrypt(MESSAGE);
+ final String message = service.decrypt(ciphertext);
+ assertThat(message).isEqualTo(MESSAGE);
+ }
+
+ @Test
+ public void testProvider() {
+ final Provider securityProvider = new BouncyCastleProvider();
+ final PasswordProvider passwordProvider = mock(PasswordProvider.class);
+ when(passwordProvider.getPassword()).thenReturn("+AQ?aDes!'DBMkrCi:FE6q\\sOn=Pbmn=PK8n=PK?".toCharArray());
+ final JasyptStandardPBEStringCryptoService service = new JasyptStandardPBEStringCryptoService();
+ service.passwordProvider = passwordProvider;
+ service.ivGenerator = new RandomIvGenerator();
+ service.securityProvider = securityProvider;
+
+ final JasyptStandardPBEStringCryptoServiceConfiguration configuration = mock(JasyptStandardPBEStringCryptoServiceConfiguration.class);
+ when(configuration.algorithm()).thenReturn("PBEWITHSHA256AND128BITAES-CBC-BC");
+ when(configuration.keyObtentionIterations()).thenReturn(DEFAULT_KEY_OBTENTION_ITERATIONS);
+ when(configuration.securityProviderName()).thenReturn(null);
+ when(configuration.stringOutputType()).thenReturn(STRING_OUTPUT_TYPE_BASE64);
+ service.activate(configuration);
+ final String ciphertext = service.encrypt(MESSAGE);
+ final String message = service.decrypt(ciphertext);
+ assertThat(message).isEqualTo(MESSAGE);
+ }
+
+}