forked from spinnaker/kork
-
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.
feat(config): Resolve configfiles using a custom PropertySource (spin…
- Loading branch information
1 parent
8ac83d1
commit e3d1996
Showing
11 changed files
with
437 additions
and
210 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
51 changes: 51 additions & 0 deletions
51
...src/main/java/com/netflix/spinnaker/kork/configserver/CloudConfigApplicationListener.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,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)); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...src/main/java/com/netflix/spinnaker/kork/configserver/CloudConfigAwarePropertySource.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,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]; | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
...fig/src/main/java/com/netflix/spinnaker/kork/configserver/CloudConfigResourceService.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,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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...fig/src/main/java/com/netflix/spinnaker/kork/configserver/ConfigFileLoadingException.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,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); | ||
} | ||
} |
Oops, something went wrong.