Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-distributed-conifg used by parameter store #1060

Open
wants to merge 7 commits into
base: 5.0.x
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions aws-distributed-configuration/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies {
api project(":aws-common")
api('io.micronaut.discovery:micronaut-discovery-client')
testImplementation "io.micronaut:micronaut-http-server-netty"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2017-2021 original 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 io.micronaut.aws.distributedconfiguration;

import io.micronaut.core.annotation.NonNull;

/**
* Configuration for Distributed Configuration using AWS services such as AWS Parameter Store or Secrets Manager.
*
* @author Sergio del Amo
* @since 2.7.0
*/
public interface AwsDistributedConfiguration {

/**
*
* @return Prefix for AWS Distributed Configuration resources names.
*/
@NonNull
String getPrefix();

/**
*
* @return Delimiter for AWS Distributed Configuration resources names.
*/
@NonNull
String getDelimiter();

/**
*
* @return Leading Delimiter for AWS Distributed Configuration resources names.
*/
@NonNull
String getLeadingDelimiter();

/**
*
* @return Trailing Delimiter for AWS Distributed Configuration resources names.
*/
@NonNull
String getTrailingDelimiter();

/**
*
* @return Default shared configuration name.
*/
@NonNull
String getSharedConfigurationName();

/**
*
* @return Weather paths with active environment names should be searched or not.
*/
boolean isSearchActiveEnvironments();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2017-2021 original 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 io.micronaut.aws.distributedconfiguration;

import io.micronaut.context.env.Environment;
import io.micronaut.context.env.MapPropertySource;
import io.micronaut.context.env.PropertySource;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.discovery.config.ConfigurationClient;
import io.micronaut.runtime.ApplicationConfiguration;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
* Base implementation for AWS services contributing distributed configuration.
*
* @author Sergio del Amo
* @since 2.7.0
*/
public abstract class AwsDistributedConfigurationClient implements ConfigurationClient {
private static final Logger LOG = LoggerFactory.getLogger(AwsDistributedConfigurationClient.class);
private static final String UNDERSCORE = "_";
private final AwsDistributedConfiguration awsDistributedConfiguration;
private final KeyValueFetcher keyValueFetcher;

@Nullable
private final String applicationName;

/**
*
* @param awsDistributedConfiguration AWS Distributed Configuration
* @param keyValueFetcher a Key Value Fetcher
* @param applicationConfiguration Application Configuration
*/
public AwsDistributedConfigurationClient(AwsDistributedConfiguration awsDistributedConfiguration,
KeyValueFetcher keyValueFetcher,
@Nullable ApplicationConfiguration applicationConfiguration) {
this.awsDistributedConfiguration = awsDistributedConfiguration;
this.keyValueFetcher = keyValueFetcher;
this.applicationName = applicationConfiguration == null ? null : (applicationConfiguration.getName().orElse(null));
if (LOG.isTraceEnabled()) {
if (this.applicationName != null) {
LOG.trace("application name: {}", applicationName);
} else {
LOG.trace("application name not set");
}
}
}

@Override
public Publisher<PropertySource> getPropertySources(Environment environment) {
List<String> configurationResolutionPrefixes = generateConfigurationResolutionPrefixes(environment);

Map<String, Map> configurationResolutionPrefixesValues = new HashMap<>();

for (String prefix : configurationResolutionPrefixes) {
Optional<Map> keyValuesOptional = keyValueFetcher.keyValuesByPrefix(prefix);
if (keyValuesOptional.isPresent()) {
Map keyValues = keyValuesOptional.get();
configurationResolutionPrefixesValues.put(prefix, keyValues);
}
}
Set<String> allKeys = new HashSet<>();
for (Map m : configurationResolutionPrefixesValues.values()) {
allKeys.addAll(m.keySet());
}
Map<String, Object> result = new HashMap<>();
if (LOG.isTraceEnabled()) {
LOG.trace("evaluating {} keys", allKeys.size());
}
for (String k : allKeys) {
if (!result.containsKey(k)) {
for (String prefix : configurationResolutionPrefixes) {
if (configurationResolutionPrefixesValues.containsKey(prefix)) {
Map<String, ?> values = configurationResolutionPrefixesValues.get(prefix);
if (values.containsKey(k)) {
if (LOG.isTraceEnabled()) {
LOG.trace("adding property {} from prefix {}", k, prefix);
}
result.put(k, values.get(k));
break;
}
}
}
}
}
String propertySourceName = getPropertySourceName();
if (LOG.isDebugEnabled()) {
LOG.debug("Property source {} with #{} items", propertySourceName, result.size());
}
return Publishers.just(new MapPropertySource(propertySourceName, result));
}

/**
*
* @return The name of the property source
*/
@NonNull
protected abstract String getPropertySourceName();

/**
*
* @param environment Micronaut's Environment
* @return A List of prefixes ordered by precedence
*/
@NonNull
private List<String> generateConfigurationResolutionPrefixes(@NonNull Environment environment) {
List<String> configurationResolutionPrefixes = new ArrayList<>();
if (applicationName != null && awsDistributedConfiguration.isSearchActiveEnvironments()) {
for (String name : environment.getActiveNames()) {
configurationResolutionPrefixes.add(awsDistributedConfiguration.getLeadingDelimiter() + String.join(awsDistributedConfiguration.getDelimiter(), Arrays.asList(awsDistributedConfiguration.getPrefix(), applicationName + UNDERSCORE + name)) + awsDistributedConfiguration.getTrailingDelimiter());
}
}
if (applicationName != null) {
configurationResolutionPrefixes.add(awsDistributedConfiguration.getLeadingDelimiter() + String.join(awsDistributedConfiguration.getDelimiter(), Arrays.asList(awsDistributedConfiguration.getPrefix(), applicationName)) + awsDistributedConfiguration.getTrailingDelimiter());
}
if (awsDistributedConfiguration.isSearchActiveEnvironments()) {
for (String name : environment.getActiveNames()) {
configurationResolutionPrefixes.add(awsDistributedConfiguration.getLeadingDelimiter() + String.join(awsDistributedConfiguration.getDelimiter(), Arrays.asList(awsDistributedConfiguration.getPrefix(), awsDistributedConfiguration.getSharedConfigurationName() + UNDERSCORE + name)) + awsDistributedConfiguration.getTrailingDelimiter());
}
}
configurationResolutionPrefixes.add(awsDistributedConfiguration.getLeadingDelimiter() + String.join(awsDistributedConfiguration.getDelimiter(), Arrays.asList(awsDistributedConfiguration.getPrefix(), awsDistributedConfiguration.getSharedConfigurationName())) + awsDistributedConfiguration.getTrailingDelimiter());

return configurationResolutionPrefixes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright 2017-2021 original 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 io.micronaut.aws.distributedconfiguration;

import io.micronaut.aws.AWSConfiguration;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.annotation.NonNull;

/**
* {@link ConfigurationProperties} implementation of {@link AwsDistributedConfiguration}.
*
* @author Sergio del Amo
* @since 2.7.0
*/
@BootstrapContextCompatible
@ConfigurationProperties(AwsDistributedConfigurationProperties.PREFIX)
public class AwsDistributedConfigurationProperties implements AwsDistributedConfiguration {

public static final String PREFIX = AWSConfiguration.PREFIX + ".distributed-configuration";

public static final String DEFAULT_PREFIX = "config";

public static final String DEFAULT_SHARED_CONFIGURATION_NAME = "application";

public static final String DEFAULT_DELIMETER = "/";

public static final String DEFAULT_LEADING_DELIMETER = "/";

public static final String DEFAULT_TRAILING_DELIMETER = "/";

public static final boolean DEFAULT_SEARCH_ACTIVE_ENVIRONMENTS = true;

private boolean searchActiveEnvironments = DEFAULT_SEARCH_ACTIVE_ENVIRONMENTS;

@NonNull
String prefix = DEFAULT_PREFIX;

@NonNull
private String sharedConfigurationName = DEFAULT_SHARED_CONFIGURATION_NAME;

@NonNull
private String delimiter = DEFAULT_DELIMETER;

@NonNull
private String leadingDelimiter = DEFAULT_LEADING_DELIMETER;

@NonNull
private String trailingDelimiter = DEFAULT_TRAILING_DELIMETER;

@Override
@NonNull
public String getPrefix() {
return prefix;
}

/**
* Prefix for AWS Distributed Configuration resources names. Default value ({@value #DEFAULT_PREFIX}).
* @param prefix Prefix for AWS Distributed Configuration resources names.
*/
public void setPrefix(@NonNull String prefix) {
this.prefix = prefix;
}

@Override
@NonNull
public String getSharedConfigurationName() {
return sharedConfigurationName;
}

/**
* Default shared configuration name. Default value ({@value #DEFAULT_SHARED_CONFIGURATION_NAME}).
* @param sharedConfigurationName shared configuration name.
*/
public void setSharedConfigurationName(@NonNull String sharedConfigurationName) {
this.sharedConfigurationName = sharedConfigurationName;
}

@Override
@NonNull
public String getDelimiter() {
return delimiter;
}

/**
* Delimiter for AWS Distributed Configuration resources names. Default value ({@value #DEFAULT_DELIMETER}).
* @param delimiter Delimiter for AWS Distributed Configuration resources names.
*/
public void setDelimiter(@NonNull String delimiter) {
this.delimiter = delimiter;
}

@Override
@NonNull
public String getLeadingDelimiter() {
return leadingDelimiter;
}

/**
* Leading delimiter for AWS Distributed Configuration resources names. Default value ({@value #DEFAULT_LEADING_DELIMETER}).
* @param leadingDelimiter Leading Delimiter for AWS Distributed Configuration resources names.
*/
public void setLeadingDelimiter(@NonNull String leadingDelimiter) {
this.leadingDelimiter = leadingDelimiter;
}

@Override
@NonNull
public String getTrailingDelimiter() {
return trailingDelimiter;
}

/**
* Trailing delimiter for AWS Distributed Configuration resources names. Default value ({@value #DEFAULT_TRAILING_DELIMETER}).
* @param trailingDelimiter Trailing Delimiter for AWS Distributed Configuration resources names.
*/
public void setTrailingDelimiter(@NonNull String trailingDelimiter) {
this.trailingDelimiter = trailingDelimiter;
}

/**
* @return Search active environment paths
*/
@Override
public boolean isSearchActiveEnvironments() {
return searchActiveEnvironments;
}

/**
* Search additional paths suffixed with each active environment.
* e.g. /config/application_EC2
* Default value ({@value #DEFAULT_SEARCH_ACTIVE_ENVIRONMENTS}).
*
* @param searchActiveEnvironments True if paths suffixed with micronaut environments should be searched
*/
public void setSearchActiveEnvironments(boolean searchActiveEnvironments) {
this.searchActiveEnvironments = searchActiveEnvironments;
}
}
Loading