Skip to content
This repository was archived by the owner on Jan 19, 2022. It is now read-only.

Allows to specify PropertySource name returned by AwsSecretsManagerPropertySourceLocator #473

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
*/
public class AwsSecretsManagerPropertySourceLocator implements PropertySourceLocator {

private String propertySourceName;

private AWSSecretsManager smClient;

private AwsSecretsManagerProperties properties;
Expand All @@ -51,12 +53,16 @@ public class AwsSecretsManagerPropertySourceLocator implements PropertySourceLoc

private Log logger = LogFactory.getLog(getClass());

public AwsSecretsManagerPropertySourceLocator(AWSSecretsManager smClient,
AwsSecretsManagerProperties properties) {
public AwsSecretsManagerPropertySourceLocator(String propertySourceName, AWSSecretsManager smClient, AwsSecretsManagerProperties properties) {
this.propertySourceName = propertySourceName;
this.smClient = smClient;
this.properties = properties;
}

public AwsSecretsManagerPropertySourceLocator(AWSSecretsManager smClient, AwsSecretsManagerProperties properties) {
this("aws-secrets-manager", smClient, properties);
}

public List<String> getContexts() {
return contexts;
}
Expand Down Expand Up @@ -89,8 +95,7 @@ public PropertySource<?> locate(Environment environment) {

Collections.reverse(this.contexts);

CompositePropertySource composite = new CompositePropertySource(
"aws-secrets-manager");
CompositePropertySource composite = new CompositePropertySource(this.propertySourceName);

for (String propertySourceContext : this.contexts) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.cloud.aws.secretsmanager;

import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest;
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
import org.junit.Test;

import org.springframework.core.env.PropertySource;
import org.springframework.mock.env.MockEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Unit test for {@link AwsSecretsManagerPropertySourceLocator}.
*
* @author Anthony Foulfoin
*/
public class AwsSecretsManagerPropertySourceLocatorTest {

private AWSSecretsManager smClient = mock(AWSSecretsManager.class);

private MockEnvironment env = new MockEnvironment();

@Test
public void locate_nameSpecifiedInConstructor_returnsPropertySourceWithSpecifiedName() {
GetSecretValueResult secretValueResult = new GetSecretValueResult();
secretValueResult.setSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}");
when(smClient.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);

AwsSecretsManagerProperties properties = new AwsSecretsManagerProperties();
AwsSecretsManagerPropertySourceLocator locator = new AwsSecretsManagerPropertySourceLocator("my-name", smClient, properties);

PropertySource propertySource = locator.locate(env);

assertThat(propertySource.getName()).isEqualTo("my-name");
}

@Test
public void locate_nameNotSpecifiedInConstructor_returnsPropertySourceWithDefaultName() {
GetSecretValueResult secretValueResult = new GetSecretValueResult();
secretValueResult.setSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}");
when(smClient.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);

AwsSecretsManagerProperties properties = new AwsSecretsManagerProperties();
AwsSecretsManagerPropertySourceLocator locator = new AwsSecretsManagerPropertySourceLocator(smClient, properties);

PropertySource propertySource = locator.locate(env);

assertThat(propertySource.getName()).isEqualTo("aws-secrets-manager");
}

}