Skip to content

Commit

Permalink
feat(config): Resolve configfiles using a custom PropertySource (spin…
Browse files Browse the repository at this point in the history
  • Loading branch information
scottfrederick authored and Jammy Louie committed Sep 3, 2019
1 parent 8ac83d1 commit e3d1996
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 210 deletions.
2 changes: 2 additions & 0 deletions kork-config/kork-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ dependencies {
implementation "commons-io:commons-io"
implementation "org.apache.commons:commons-lang3"

api project(':kork-secrets')

api "org.springframework.cloud:spring-cloud-context"
api "org.springframework.cloud:spring-cloud-config-server"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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
*
* http://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 com.netflix.spinnaker.kork.configserver;

import com.netflix.spinnaker.kork.secrets.SecretAwarePropertySource;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;

public class CloudConfigApplicationListener
implements ApplicationListener<ApplicationPreparedEvent> {
private static final String APPLICATION_CONFIG_PROPERTY_SOURCE_PREFIX = "applicationConfig:";
private static final String CONFIG_SERVER_PROPERTY_SOURCE_NAME = "bootstrapProperties";

@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
ConfigurableApplicationContext context = event.getApplicationContext();
ConfigurableEnvironment environment = context.getEnvironment();

for (PropertySource propertySource : environment.getPropertySources()) {
if (shouldWrap(propertySource)) {
CloudConfigAwarePropertySource wrapper =
new CloudConfigAwarePropertySource(propertySource, context);
environment.getPropertySources().replace(propertySource.getName(), wrapper);
}
}
}

private boolean shouldWrap(PropertySource propertySource) {
return (propertySource.getName().startsWith(APPLICATION_CONFIG_PROPERTY_SOURCE_PREFIX)
|| propertySource.getName().equals(CONFIG_SERVER_PROPERTY_SOURCE_NAME))
&& !((propertySource instanceof CloudConfigAwarePropertySource)
|| (propertySource instanceof SecretAwarePropertySource));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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
*
* http://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 com.netflix.spinnaker.kork.configserver;

import org.springframework.beans.BeansException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;

public class CloudConfigAwarePropertySource extends EnumerablePropertySource<PropertySource> {
private final ConfigurableApplicationContext context;
private CloudConfigResourceService resourceService;

CloudConfigAwarePropertySource(PropertySource source, ConfigurableApplicationContext context) {
super(source.getName(), source);
this.context = context;
}

@Override
public Object getProperty(String name) {
Object value = source.getProperty(name);
if (value instanceof String) {
String stringValue = (String) value;
if (CloudConfigResourceService.isCloudConfigResource(stringValue)) {
resolveResourceService(stringValue);
value = resourceService.getLocalPath(stringValue);
}
}
return value;
}

private void resolveResourceService(String path) {
if (resourceService == null) {
try {
resourceService = context.getBean(CloudConfigResourceService.class);
} catch (BeansException e) {
throw new ConfigFileLoadingException(
"Config Server repository not configured for resource \"" + path + "\"");
}
}
}

@Override
public String[] getPropertyNames() {
if (source instanceof EnumerablePropertySource) {
return ((EnumerablePropertySource) source).getPropertyNames();
} else {
return new String[0];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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
*
* http://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 com.netflix.spinnaker.kork.configserver;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.resource.NoSuchResourceException;
import org.springframework.cloud.config.server.resource.ResourceRepository;
import org.springframework.cloud.config.server.support.EnvironmentPropertySource;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;

public class CloudConfigResourceService implements EnvironmentAware {
private static final String CONFIG_SERVER_RESOURCE_PREFIX = "configserver:";

private final ResourceRepository resourceRepository;
private final EnvironmentRepository environmentRepository;

@Value("${spring.application.name:application}")
private String applicationName = "application";

private String profiles;

public CloudConfigResourceService() {
this.resourceRepository = null;
this.environmentRepository = null;
}

public CloudConfigResourceService(
ResourceRepository resourceRepository, EnvironmentRepository environmentRepository) {
this.resourceRepository = resourceRepository;
this.environmentRepository = environmentRepository;
}

public String getLocalPath(String path) {
String contents = retrieveFromConfigServer(path);
return ConfigFileUtils.writeToTempFile(contents, getResourceName(path));
}

private String retrieveFromConfigServer(String path) {
if (resourceRepository == null || environmentRepository == null) {
throw new ConfigFileLoadingException(
"Config Server repository not configured for resource \"" + path + "\"");
}

try {
String fileName = getResourceName(path);
Resource resource =
this.resourceRepository.findOne(applicationName, profiles, null, fileName);
try (InputStream inputStream = resource.getInputStream()) {
Environment environment =
this.environmentRepository.findOne(applicationName, profiles, null);

String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
StandardEnvironment preparedEnvironment =
EnvironmentPropertySource.prepareEnvironment(environment);
return EnvironmentPropertySource.resolvePlaceholders(preparedEnvironment, text);
}
} catch (NoSuchResourceException e) {
throw new ConfigFileLoadingException(
"The resource \"" + path + "\" was not found in config server", e);
} catch (IOException e) {
throw new ConfigFileLoadingException(
"Exception reading config server resource \"" + path + "\"", e);
}
}

private String getResourceName(String path) {
return path.substring(CONFIG_SERVER_RESOURCE_PREFIX.length());
}

@Override
public void setEnvironment(org.springframework.core.env.Environment environment) {
profiles = StringUtils.join(environment.getActiveProfiles(), ",");
}

public static boolean isCloudConfigResource(String path) {
return path.startsWith(CONFIG_SERVER_RESOURCE_PREFIX);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Pivotal, Inc.
*
* 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
*
* http://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 com.netflix.spinnaker.kork.configserver;

public class ConfigFileLoadingException extends RuntimeException {
public ConfigFileLoadingException(String message) {
super(message);
}

public ConfigFileLoadingException(String message, Throwable cause) {
super(message, cause);
}

public ConfigFileLoadingException(Throwable cause) {
super(cause);
}
}
Loading

0 comments on commit e3d1996

Please sign in to comment.