forked from hariohmprasath/spring-cloud-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add aws-secretsmanager: prefix config import (spring-attic/spring-clo…
…ud-aws#721) In `spring-boot` 2.4, `Volume Mounted Config Directory Trees` was added. This commit introduces the prefix `aws-secretsmanager:` which will resolve the values given the configuration properties supported by secrets manager integration. Also, if keys are added after the prefix then just these will be resolved. Use: `aws-secretsmanager:` or `aws-secretsmanager:my-secret-key` or `aws-secretsmanager:my-secret-key;my-anoter-secret-key` Closes spring-attic/spring-cloud-aws#655 Closes spring-attic/spring-cloud-aws#515 Co-authored-by: Maciej Walkowiak <walkowiak.maciej@yahoo.com>
- Loading branch information
1 parent
13b5559
commit c7d2b71
Showing
11 changed files
with
511 additions
and
49 deletions.
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
94 changes: 94 additions & 0 deletions
94
...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,94 @@ | ||
/* | ||
* 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.hasLength(prefix)) { | ||
return prefix + "/" + context; | ||
} | ||
return 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
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> { | ||
|
||
@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) { | ||
throw new ConfigDataResourceNotFoundException(resource, e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.