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/17 10:34:36 UTC
[sling-org-apache-sling-commons-crypto] 04/07: SLING-10407 Provide
a password provider for environment variables
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 f6eddf352dd0c4effc160355f286569dedf6b34e
Author: Oliver Lietz <ol...@apache.org>
AuthorDate: Thu Jun 17 12:20:04 2021 +0200
SLING-10407 Provide a password provider for environment variables
* Do not deconstruct component, keep it in an usable state
* Test component lifecycle
---
.../EnvironmentVariablePasswordProvider.java | 8 +--
.../EnvironmentVariablePasswordProviderTest.java | 67 ++++++++++++++++++++++
.../EnvironmentVariablePasswordProviderIT.java | 2 +-
3 files changed, 72 insertions(+), 5 deletions(-)
diff --git a/src/main/java/org/apache/sling/commons/crypto/internal/EnvironmentVariablePasswordProvider.java b/src/main/java/org/apache/sling/commons/crypto/internal/EnvironmentVariablePasswordProvider.java
index 66ffcf2..bbaeeed 100644
--- a/src/main/java/org/apache/sling/commons/crypto/internal/EnvironmentVariablePasswordProvider.java
+++ b/src/main/java/org/apache/sling/commons/crypto/internal/EnvironmentVariablePasswordProvider.java
@@ -51,25 +51,25 @@ public class EnvironmentVariablePasswordProvider implements PasswordProvider {
}
@Activate
- private void activate(final EnvironmentVariablePasswordProviderConfiguration configuration) {
+ protected void activate(final EnvironmentVariablePasswordProviderConfiguration configuration) {
logger.debug("activating");
this.configuration = configuration;
}
@Modified
- private void modified(final EnvironmentVariablePasswordProviderConfiguration configuration) {
+ protected void modified(final EnvironmentVariablePasswordProviderConfiguration configuration) {
logger.debug("modifying");
this.configuration = configuration;
}
@Deactivate
- private void deactivate() {
+ protected void deactivate() {
logger.debug("deactivating");
- this.configuration = null;
}
@Override
public char @NotNull [] getPassword() {
+ Objects.requireNonNull(configuration, "Configuration must not be null");
final String name = configuration.name();
if (Objects.isNull(System.getenv(name))) {
final String message = String.format("environment variable '%s' not set", name);
diff --git a/src/test/java/org/apache/sling/commons/crypto/internal/EnvironmentVariablePasswordProviderTest.java b/src/test/java/org/apache/sling/commons/crypto/internal/EnvironmentVariablePasswordProviderTest.java
new file mode 100644
index 0000000..fd00acf
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/crypto/internal/EnvironmentVariablePasswordProviderTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.internal;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class EnvironmentVariablePasswordProviderTest {
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ @Test
+ public void testMissingConfiguration() {
+ final EnvironmentVariablePasswordProvider provider = new EnvironmentVariablePasswordProvider();
+ exception.expect(NullPointerException.class);
+ exception.expectMessage("Configuration must not be null");
+ provider.getPassword();
+ }
+
+ @Test
+ public void testComponentLifecycle() throws Exception {
+ final EnvironmentVariablePasswordProvider provider = new EnvironmentVariablePasswordProvider();
+ { // activate
+ final EnvironmentVariablePasswordProviderConfiguration configuration = mock(EnvironmentVariablePasswordProviderConfiguration.class);
+ when(configuration.name()).thenReturn("password_ascii85");
+ provider.activate(configuration);
+ final char[] password = withEnvironmentVariable("password_ascii85", "+AQ?aDes!'DBMkrCi:FE6q\\sOn=Pbmn=PK8n=PK?").execute(provider::getPassword);
+ assertThat(password).isEqualTo("+AQ?aDes!'DBMkrCi:FE6q\\sOn=Pbmn=PK8n=PK?".toCharArray());
+ }
+ { // modified
+ final EnvironmentVariablePasswordProviderConfiguration configuration = mock(EnvironmentVariablePasswordProviderConfiguration.class);
+ when(configuration.name()).thenReturn("password_utf8");
+ provider.modified(configuration);
+ final char[] password = withEnvironmentVariable("password_utf8", " Napøleøn Sølø (DK) 🏁🇩🇰").execute(provider::getPassword);
+ assertThat(password).isEqualTo(" Napøleøn Sølø (DK) 🏁🇩🇰".toCharArray());
+ }
+ { // deactivate
+ provider.deactivate();
+ final char[] password = withEnvironmentVariable("password_utf8", " Napøleøn Sølø (DK) 🏁🇩🇰").execute(provider::getPassword);
+ assertThat(password).isEqualTo(" Napøleøn Sølø (DK) 🏁🇩🇰".toCharArray());
+ }
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/commons/crypto/it/tests/EnvironmentVariablePasswordProviderIT.java b/src/test/java/org/apache/sling/commons/crypto/it/tests/EnvironmentVariablePasswordProviderIT.java
index 8ebd7de..51cd86e 100644
--- a/src/test/java/org/apache/sling/commons/crypto/it/tests/EnvironmentVariablePasswordProviderIT.java
+++ b/src/test/java/org/apache/sling/commons/crypto/it/tests/EnvironmentVariablePasswordProviderIT.java
@@ -76,7 +76,7 @@ public class EnvironmentVariablePasswordProviderIT extends CryptoTestSupport {
}
@Test(expected = RuntimeException.class)
- public void testEnvironmentVariableNotSet() throws Exception {
+ public void testEnvironmentVariableNotSet() {
passwordProvider.getPassword();
}