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

[Refactoring] Introduce RequestContext to easily pass new parameters to downstream methods #2408

Open
wants to merge 2 commits into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.util.CollectionUtils;

/**
Expand Down Expand Up @@ -74,8 +75,8 @@ protected void doHealthCheck(Health.Builder builder) {
String profiles = repository.getProfiles();

try {
Environment environment = this.environmentRepository.findOne(application, profiles,
repository.getLabel(), false);
Environment environment = this.environmentRepository.findOne(new RequestContext.Builder()
.name(application).profiles(profiles).label(repository.getLabel()).build());

HashMap<String, Object> detail = new HashMap<>();
detail.put("name", environment.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.AbstractScmAccessor;
import org.springframework.cloud.config.server.support.AbstractScmAccessorProperties;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

Expand Down Expand Up @@ -51,19 +52,14 @@ public AbstractScmEnvironmentRepository(ConfigurableEnvironment environment,
}

@Override
public synchronized Environment findOne(String application, String profile, String label) {
return findOne(application, profile, label, false);
}

@Override
public synchronized Environment findOne(String application, String profile, String label, boolean includeOrigin) {
public synchronized Environment findOne(RequestContext ctx) {
NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(getEnvironment(),
new NativeEnvironmentProperties(), this.observationRegistry);
Locations locations = getLocations(application, profile, label);
Locations locations = getLocations(ctx);
delegate.setSearchLocations(locations.getLocations());
Environment result = delegate.findOne(application, profile, "", includeOrigin);
Environment result = delegate.findOne(ctx.toBuilder().label("").build());
result.setVersion(locations.getVersion());
result.setLabel(label);
result.setLabel(ctx.getLabel());
return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(), getUri());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -77,13 +78,13 @@ public AbstractVaultEnvironmentRepository(ObjectProvider<HttpServletRequest> req
}

@Override
public Environment findOne(String application, String profile, String label) {
String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);
public Environment findOne(RequestContext ctx) {
String[] profiles = StringUtils.commaDelimitedListToStringArray(ctx.getProfiles());
List<String> scrubbedProfiles = scrubProfiles(profiles);

List<String> keys = findKeys(application, scrubbedProfiles);
List<String> keys = findKeys(ctx.getName(), scrubbedProfiles);

Environment environment = new Environment(application, profiles, label, null, getWatchState());
Environment environment = new Environment(ctx.getName(), profiles, ctx.getLabel(), null, getWatchState());

for (String key : keys) {
// read raw 'data' key from vault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -62,18 +63,15 @@ public AwsParameterStoreEnvironmentRepository(SsmClient awsSsmClient, ConfigServ
}

@Override
public Environment findOne(String application, String profile, String label) {
if (!StringUtils.hasLength(application)) {
application = configServerProperties.getDefaultApplicationName();
}

if (!StringUtils.hasLength(profile)) {
profile = configServerProperties.getDefaultProfile();
}
public Environment findOne(RequestContext ctx) {
String application = StringUtils.hasLength(ctx.getName()) ? ctx.getName()
: configServerProperties.getDefaultApplicationName();
String profile = StringUtils.hasLength(ctx.getProfiles()) ? ctx.getProfiles()
: configServerProperties.getDefaultProfile();

String[] profiles = StringUtils.commaDelimitedListToStringArray(profile);

Environment result = new Environment(application, profiles, label, null, null);
Environment result = new Environment(application, profiles, ctx.getLabel(), null, null);

Set<String> paths = buildParameterPaths(application, profiles);
List<PropertySource> propertySources = getPropertySources(paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.core.io.InputStreamResource;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -76,12 +77,12 @@ public void setOrder(int order) {
}

@Override
public Environment findOne(String specifiedApplication, String specifiedProfiles, String specifiedLabel) {
final String application = ObjectUtils.isEmpty(specifiedApplication)
? serverProperties.getDefaultApplicationName() : specifiedApplication;
final String profiles = ObjectUtils.isEmpty(specifiedProfiles) ? serverProperties.getDefaultProfile()
: specifiedProfiles;
final String label = ObjectUtils.isEmpty(specifiedLabel) ? serverProperties.getDefaultLabel() : specifiedLabel;
public Environment findOne(RequestContext ctx) {
final String application = ObjectUtils.isEmpty(ctx.getName()) ? serverProperties.getDefaultApplicationName()
: ctx.getName();
final String profiles = ObjectUtils.isEmpty(ctx.getProfiles()) ? serverProperties.getDefaultProfile()
: ctx.getProfiles();
final String label = ObjectUtils.isEmpty(ctx.getLabel()) ? serverProperties.getDefaultLabel() : ctx.getLabel();

String[] profileArray = parseProfiles(profiles);
List<String> apps = Arrays.asList(StringUtils.commaDelimitedListToStringArray(application.replace(" ", "")));
Expand Down Expand Up @@ -204,7 +205,9 @@ private ResponseInputStream<GetObjectResponse> getObject(String key) throws Exce
}

@Override
public Locations getLocations(String application, String profiles, String label) {
public Locations getLocations(RequestContext ctx) {
String label = ctx.getLabel();

StringBuilder baseLocation = new StringBuilder(AWS_S3_RESOURCE_SCHEME + bucketName + PATH_SEPARATOR);
if (!StringUtils.hasText(label) && StringUtils.hasText(serverProperties.getDefaultLabel())) {
label = serverProperties.getDefaultLabel();
Expand All @@ -214,7 +217,7 @@ public Locations getLocations(String application, String profiles, String label)
baseLocation.append(label);
}

return new Locations(application, profiles, label, null, new String[] { baseLocation.toString() });
return new Locations(ctx.getName(), ctx.getProfiles(), label, null, new String[] { baseLocation.toString() });
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -72,26 +73,15 @@ public AwsSecretsManagerEnvironmentRepository(SecretsManagerClient awsSmClient,
}

@Override
public Environment findOne(String application, String profileList, String label) {
public Environment findOne(RequestContext ctx) {
final String defaultApplication = configServerProperties.getDefaultApplicationName();
final String defaultProfile = configServerProperties.getDefaultProfile();
final String defaultLabel = environmentProperties.getDefaultLabel();
final boolean ignoreLabel = environmentProperties.isIgnoreLabel();

if (ObjectUtils.isEmpty(application)) {
application = defaultApplication;
}

if (ObjectUtils.isEmpty(profileList)) {
profileList = defaultProfile;
}

if (ignoreLabel) {
label = null;
}
else if (StringUtils.isEmpty(label)) {
label = defaultLabel;
}
String application = ObjectUtils.isEmpty(ctx.getName()) ? defaultApplication : ctx.getName();
String profileList = ObjectUtils.isEmpty(ctx.getProfiles()) ? defaultProfile : ctx.getProfiles();
String label = ignoreLabel ? null : (StringUtils.isEmpty(ctx.getLabel()) ? defaultLabel : ctx.getLabel());

String[] profiles = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(profileList));
Environment environment = new Environment(application, profiles, label, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.OrderComparator;

/**
Expand Down Expand Up @@ -71,24 +72,19 @@ public CompositeEnvironmentRepository(List<EnvironmentRepository> environmentRep
}

@Override
public Environment findOne(String application, String profile, String label) {
return findOne(application, profile, label, false);
}

@Override
public Environment findOne(String application, String profile, String label, boolean includeOrigin) {
Environment env = new Environment(application, new String[] { profile }, label, null, null);
public Environment findOne(RequestContext ctx) {
Environment env = new Environment(ctx.getName(), new String[] { ctx.getProfiles() }, ctx.getLabel(), null,
null);
if (this.environmentRepositories.size() == 1) {
Environment envRepo = this.environmentRepositories.get(0).findOne(application, profile, label,
includeOrigin);
Environment envRepo = this.environmentRepositories.get(0).findOne(ctx);
env.addAll(envRepo.getPropertySources());
env.setVersion(envRepo.getVersion());
env.setState(envRepo.getState());
}
else {
for (EnvironmentRepository repo : environmentRepositories) {
try {
env.addAll(repo.findOne(application, profile, label, includeOrigin).getPropertySources());
env.addAll(repo.findOne(ctx).getPropertySources());
}
catch (Exception e) {
if (failOnError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.credhub.core.CredHubOperations;
import org.springframework.credhub.support.CredentialDetails;
Expand Down Expand Up @@ -52,30 +53,26 @@ public CredhubEnvironmentRepository(CredHubOperations credHubOperations) {
}

@Override
public Environment findOne(String application, String profilesList, String label) {
if (ObjectUtils.isEmpty(profilesList)) {
profilesList = DEFAULT_PROFILE;
}
if (ObjectUtils.isEmpty(label)) {
label = DEFAULT_LABEL;
}
public Environment findOne(RequestContext ctx) {
String profilesList = ObjectUtils.isEmpty(ctx.getProfiles()) ? DEFAULT_PROFILE : ctx.getProfiles();
String label = ObjectUtils.isEmpty(ctx.getLabel()) ? DEFAULT_LABEL : ctx.getLabel();

String[] profiles = StringUtils.commaDelimitedListToStringArray(profilesList);

Environment environment = new Environment(application, profiles, label, null, null);
Environment environment = new Environment(ctx.getName(), profiles, label, null, null);
for (String profile : profiles) {
environment.add(new PropertySource("credhub-" + application + "-" + profile + "-" + label,
findProperties(application, profile, label)));
if (!DEFAULT_APPLICATION.equals(application)) {
environment.add(new PropertySource("credhub-" + ctx.getName() + "-" + profile + "-" + label,
findProperties(ctx.getName(), profile, label)));
if (!DEFAULT_APPLICATION.equals(ctx.getName())) {
addDefaultPropertySource(environment, DEFAULT_APPLICATION, profile, label);
}
}

if (!Arrays.asList(profiles).contains(DEFAULT_PROFILE)) {
addDefaultPropertySource(environment, application, DEFAULT_PROFILE, label);
addDefaultPropertySource(environment, ctx.getName(), DEFAULT_PROFILE, label);
}

if (!Arrays.asList(profiles).contains(DEFAULT_PROFILE) && !DEFAULT_APPLICATION.equals(application)) {
if (!Arrays.asList(profiles).contains(DEFAULT_PROFILE) && !DEFAULT_APPLICATION.equals(ctx.getName())) {
addDefaultPropertySource(environment, DEFAULT_APPLICATION, DEFAULT_PROFILE, label);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.cloud.config.environment.EnvironmentMediaType;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.support.PathUtils;
import org.springframework.cloud.config.server.support.RequestContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -107,39 +108,44 @@ public void setAcceptEmpty(boolean acceptEmpty) {
@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
produces = MediaType.APPLICATION_JSON_VALUE)
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles) {
return getEnvironment(name, profiles, null, false);
RequestContext ctx = new RequestContext.Builder().name(normalize(name)).profiles(profiles).build();
return getEnvironment(ctx);
}

@GetMapping(path = "/{name}/{profiles:(?!.*\\b\\.(?:ya?ml|properties|json)\\b).*}",
produces = EnvironmentMediaType.V2_JSON)
public Environment defaultLabelIncludeOrigin(@PathVariable String name, @PathVariable String profiles) {
return getEnvironment(name, profiles, null, true);
RequestContext ctx = new RequestContext.Builder().name(normalize(name)).profiles(profiles).includeOrigin(true)
.build();
return getEnvironment(ctx);
}

@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = MediaType.APPLICATION_JSON_VALUE)
public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label) {
return getEnvironment(name, profiles, label, false);
RequestContext ctx = new RequestContext.Builder().name(normalize(name)).profiles(profiles)
.label(normalize(label)).build();
return getEnvironment(ctx);
}

@GetMapping(path = "/{name}/{profiles}/{label:.*}", produces = EnvironmentMediaType.V2_JSON)
public Environment labelledIncludeOrigin(@PathVariable String name, @PathVariable String profiles,
@PathVariable String label) {
return getEnvironment(name, profiles, label, true);
RequestContext ctx = new RequestContext.Builder().name(normalize(name)).profiles(profiles)
.label(normalize(label)).includeOrigin(true).build();
return getEnvironment(ctx);
}

public Environment getEnvironment(String name, String profiles, String label, boolean includeOrigin) {
public Environment getEnvironment(RequestContext ctx) {
try {
name = normalize(name);
label = normalize(label);
Environment environment = this.repository.findOne(name, profiles, label, includeOrigin);
Environment environment = this.repository.findOne(ctx);
if (!this.acceptEmpty && (environment == null || environment.getPropertySources().isEmpty())) {
throw new EnvironmentNotFoundException("Profile Not found");
}
return environment;
}
catch (Exception e) {
LOG.warn(String.format("Error getting the Environment with name=%s profiles=%s label=%s includeOrigin=%b",
name, profiles, label, includeOrigin), e);
ctx.getName(), ctx.getProfiles(), ctx.getLabel(), ctx.getIncludeOrigin()), e);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.environment.PropertyValueDescriptor;
import org.springframework.cloud.config.server.encryption.EnvironmentEncryptor;
import org.springframework.cloud.config.server.support.RequestContext;

/**
* A delegating {@link EnvironmentRepository} that can decrypt the properties if an
Expand Down Expand Up @@ -55,20 +56,15 @@ public EnvironmentEncryptorEnvironmentRepository(EnvironmentRepository delegate,
}

@Override
public Environment findOne(String name, String profiles, String label) {
return findOne(name, profiles, label, false);
}

@Override
public Environment findOne(String name, String profiles, String label, boolean includeOrigin) {
Environment environment = this.delegate.findOne(name, profiles, label, includeOrigin);
public Environment findOne(RequestContext ctx) {
Environment environment = this.delegate.findOne(ctx);
if (this.environmentEncryptors != null) {
for (EnvironmentEncryptor environmentEncryptor : environmentEncryptors) {
environment = environmentEncryptor.decrypt(environment);
}
}
if (!this.overrides.isEmpty()) {
environment.addFirst(new PropertySource("overrides", getOverridesMap(includeOrigin)));
environment.addFirst(new PropertySource("overrides", getOverridesMap(ctx.getIncludeOrigin())));
}
return environment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
package org.springframework.cloud.config.server.environment;

import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.support.RequestContext;

/**
* @author Dave Syer
* @author Roy Clarkson
*/
public interface EnvironmentRepository {

Environment findOne(String application, String profile, String label);

default Environment findOne(String application, String profile, String label, boolean includeOrigin) {
return findOne(application, profile, label);
}
Environment findOne(RequestContext ctx);

}
Loading
Loading