Created
June 10, 2021 14:49
-
-
Save superzjn/2ce89f5ba136f9f7c41d03723e51b551 to your computer and use it in GitHub Desktop.
[Pull credentials from AWS parameter store] #AWS
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
package com.morningstar.uim.util; | |
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; | |
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement; | |
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient; | |
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClientBuilder; | |
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterRequest; | |
import com.amazonaws.services.simplesystemsmanagement.model.GetParameterResult; | |
import java.util.concurrent.ConcurrentHashMap; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.env.Environment; | |
@Configuration | |
public class AppConfigParam { | |
private final Environment env; | |
private static final ConcurrentHashMap<String, String> map = new ConcurrentHashMap(); | |
public AppConfigParam(final Environment env) { | |
this.env = env; | |
} | |
public String get(String paramName) { | |
paramName = "appConfigParam." + paramName; | |
String value = (String)map.get(paramName); | |
if (value == null) { | |
String lookupKeys = this.env.getProperty(paramName); | |
if (lookupKeys != null) { | |
String[] lookupKeyValues = lookupKeys.split("\\|"); | |
value = System.getenv(lookupKeyValues[0].trim()); | |
if (value != null) { | |
map.put(paramName, value); | |
} else { | |
AWSSimpleSystemsManagement ssm = (AWSSimpleSystemsManagement)((AWSSimpleSystemsManagementClientBuilder)AWSSimpleSystemsManagementClient.builder().withCredentials(DefaultAWSCredentialsProviderChain.getInstance())).build(); | |
GetParameterRequest request = (new GetParameterRequest()).withName(lookupKeyValues[1].trim()).withWithDecryption(true); | |
GetParameterResult response = ssm.getParameter(request); | |
if (response != null && response.getParameter() != null && response.getParameter().getValue() != null) { | |
value = response.getParameter().getValue(); | |
map.put(paramName, value); | |
} | |
} | |
} | |
} | |
return value; | |
} | |
@Bean | |
public AppConfigParam getAppConfigParam() { | |
return this; | |
} | |
} | |
// To use it, put the following in properties file | |
// appConfigParam.tokenizer-apiKey = TOKENIZER_API_KEY|tokenizerApikey_qa | |
// And need to login via AWS CLI before running | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment