This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 372
Add aws-secretsmanager: prefix config import #721
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dfdc5c9
Add aws-secretsmanager: prefix config import
eddumelendez 4ca9ada
Add header
eddumelendez 8ff2f2c
Add docs
eddumelendez 1cdbda1
Fix call to secrestsmanager service
eddumelendez bb11c77
Remove public from test
eddumelendez f6477ca
Update spring-cloud-starter-aws-secrets-manager-config/src/main/java/…
eddumelendez 22615d3
Take advantage of optional
eddumelendez 1a5d5c9
Fallback name with spring.application.name
eddumelendez 89890b2
Remove dependency
eddumelendez 028213a
Polish
eddumelendez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...n/java/org/springframework/cloud/aws/secretsmanager/AwsSecretsManagerPropertySources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2013-2020 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 java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.amazonaws.services.secretsmanager.AWSSecretsManager; | ||
import org.apache.commons.logging.Log; | ||
|
||
import org.springframework.util.StringUtils; | ||
|
||
public class AwsSecretsManagerPropertySources { | ||
|
||
private final AwsSecretsManagerProperties properties; | ||
|
||
private final Log log; | ||
|
||
public AwsSecretsManagerPropertySources(AwsSecretsManagerProperties properties, Log log) { | ||
this.properties = properties; | ||
this.log = log; | ||
} | ||
|
||
public List<String> getAutomaticContexts(List<String> profiles) { | ||
List<String> contexts = new ArrayList<>(); | ||
String prefix = this.properties.getPrefix(); | ||
String defaultContext = getContext(prefix, this.properties.getDefaultContext()); | ||
|
||
String appName = this.properties.getName(); | ||
|
||
String appContext = prefix + "/" + appName; | ||
addProfiles(contexts, appContext, profiles); | ||
contexts.add(appContext); | ||
|
||
addProfiles(contexts, defaultContext, profiles); | ||
contexts.add(defaultContext); | ||
return contexts; | ||
} | ||
|
||
protected String getContext(String prefix, String context) { | ||
if (StringUtils.isEmpty(prefix)) { | ||
return context; | ||
} | ||
else { | ||
return prefix + "/" + context; | ||
} | ||
} | ||
|
||
private void addProfiles(List<String> contexts, String baseContext, List<String> profiles) { | ||
for (String profile : profiles) { | ||
contexts.add(baseContext + this.properties.getProfileSeparator() + profile); | ||
} | ||
} | ||
|
||
public AwsSecretsManagerPropertySource createPropertySource(String context, boolean optional, | ||
AWSSecretsManager client) { | ||
try { | ||
AwsSecretsManagerPropertySource propertySource = new AwsSecretsManagerPropertySource(context, client); | ||
propertySource.init(); | ||
return propertySource; | ||
// TODO: howto call close when /refresh | ||
} | ||
catch (Exception e) { | ||
if (this.properties.isFailFast() || !optional) { | ||
throw new AwsSecretsManagerPropertySourceNotFoundException(e); | ||
} | ||
else { | ||
log.warn("Unable to load AWS secret from " + context, e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
static class AwsSecretsManagerPropertySourceNotFoundException extends RuntimeException { | ||
|
||
AwsSecretsManagerPropertySourceNotFoundException(Exception source) { | ||
super(source); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ngframework/cloud/aws/autoconfigure/secretsmanager/AwsSecretsManagerConfigDataLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2013-2020 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.autoconfigure.secretsmanager; | ||
|
||
import java.util.Collections; | ||
|
||
import com.amazonaws.services.secretsmanager.AWSSecretsManager; | ||
|
||
import org.springframework.boot.context.config.ConfigData; | ||
import org.springframework.boot.context.config.ConfigDataLoader; | ||
import org.springframework.boot.context.config.ConfigDataLoaderContext; | ||
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException; | ||
import org.springframework.cloud.aws.secretsmanager.AwsSecretsManagerPropertySource; | ||
|
||
/** | ||
* @author Eddú Meléndez | ||
* @since 2.3.0 | ||
*/ | ||
public class AwsSecretsManagerConfigDataLoader implements ConfigDataLoader<AwsSecretsManagerConfigDataResource> { | ||
maciejwalkowiak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public ConfigData load(ConfigDataLoaderContext context, AwsSecretsManagerConfigDataResource resource) { | ||
try { | ||
AWSSecretsManager ssm = context.getBootstrapContext().get(AWSSecretsManager.class); | ||
AwsSecretsManagerPropertySource propertySource = resource.getPropertySources() | ||
.createPropertySource(resource.getContext(), resource.isOptional(), ssm); | ||
return new ConfigData(Collections.singletonList(propertySource)); | ||
} | ||
catch (Exception e) { | ||
maciejwalkowiak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new ConfigDataResourceNotFoundException(resource, e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to document that it can be used with
optional
prefix.