Search in sources :

Example 56 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class EnvironmentController method labelledProperties.

@RequestMapping("/{label}/{name}-{profiles}.properties")
public ResponseEntity<String> labelledProperties(@PathVariable String name, @PathVariable String profiles, @PathVariable String label, @RequestParam(defaultValue = "true") boolean resolvePlaceholders) throws IOException {
    validateProfiles(profiles);
    Environment environment = labelled(name, profiles, label);
    Map<String, Object> properties = convertToProperties(environment);
    String propertiesString = getPropertiesString(properties);
    if (resolvePlaceholders) {
        propertiesString = resolvePlaceholders(prepareEnvironment(environment), propertiesString);
    }
    return getSuccess(propertiesString);
}
Also used : EnvironmentPropertySource.prepareEnvironment(org.springframework.cloud.config.server.support.EnvironmentPropertySource.prepareEnvironment) Environment(org.springframework.cloud.config.environment.Environment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 57 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class CipherEnvironmentEncryptor method decrypt.

private Environment decrypt(Environment environment, TextEncryptorLocator encryptor) {
    Environment result = new Environment(environment);
    for (PropertySource source : environment.getPropertySources()) {
        Map<Object, Object> map = new LinkedHashMap<Object, Object>(source.getSource());
        for (Map.Entry<Object, Object> entry : new LinkedHashSet<>(map.entrySet())) {
            Object key = entry.getKey();
            String name = key.toString();
            String value = entry.getValue().toString();
            if (value.startsWith("{cipher}")) {
                map.remove(key);
                try {
                    value = value.substring("{cipher}".length());
                    value = encryptor.locate(this.helper.getEncryptorKeys(name, StringUtils.arrayToCommaDelimitedString(environment.getProfiles()), value)).decrypt(this.helper.stripPrefix(value));
                } catch (Exception e) {
                    value = "<n/a>";
                    name = "invalid." + name;
                    String message = "Cannot decrypt key: " + key + " (" + e.getClass() + ": " + e.getMessage() + ")";
                    if (logger.isDebugEnabled()) {
                        logger.debug(message, e);
                    } else if (logger.isWarnEnabled()) {
                        logger.warn(message);
                    }
                }
                map.put(name, value);
            }
        }
        result.add(new PropertySource(source.getName(), map));
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Environment(org.springframework.cloud.config.environment.Environment) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PropertySource(org.springframework.cloud.config.environment.PropertySource) LinkedHashMap(java.util.LinkedHashMap)

Example 58 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class NativeEnvironmentRepository method clean.

protected Environment clean(Environment value) {
    Environment result = new Environment(value.getName(), value.getProfiles(), value.getLabel(), this.version, value.getState());
    for (PropertySource source : value.getPropertySources()) {
        String name = source.getName();
        if (this.environment.getPropertySources().contains(name)) {
            continue;
        }
        name = name.replace("applicationConfig: [", "");
        name = name.replace("]", "");
        if (this.searchLocations != null) {
            boolean matches = false;
            String normal = name;
            if (normal.startsWith("file:")) {
                normal = StringUtils.cleanPath(new File(normal.substring("file:".length())).getAbsolutePath());
            }
            String profile = result.getProfiles() == null ? null : StringUtils.arrayToCommaDelimitedString(result.getProfiles());
            for (String pattern : getLocations(result.getName(), profile, result.getLabel()).getLocations()) {
                if (!pattern.contains(":")) {
                    pattern = "file:" + pattern;
                }
                if (pattern.startsWith("file:")) {
                    pattern = StringUtils.cleanPath(new File(pattern.substring("file:".length())).getAbsolutePath()) + "/";
                }
                if (logger.isTraceEnabled()) {
                    logger.trace("Testing pattern: " + pattern + " with property source: " + name);
                }
                if (normal.startsWith(pattern) && !normal.substring(pattern.length()).contains("/")) {
                    matches = true;
                    break;
                }
            }
            if (!matches) {
                // Don't include this one: it wasn't matched by our search locations
                if (logger.isDebugEnabled()) {
                    logger.debug("Not adding property source: " + name);
                }
                continue;
            }
        }
        logger.info("Adding property source: " + name);
        result.add(new PropertySource(name, source.getSource()));
    }
    return result;
}
Also used : StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.cloud.config.environment.Environment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) File(java.io.File) MapPropertySource(org.springframework.core.env.MapPropertySource) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 59 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class ResourceController method retrieve.

synchronized String retrieve(String name, String profile, String label, String path, boolean resolvePlaceholders) throws IOException {
    if (name != null && name.contains("(_)")) {
        // "(_)" is uncommon in a git repo name, but "/" cannot be matched
        // by Spring MVC
        name = name.replace("(_)", "/");
    }
    if (label != null && label.contains("(_)")) {
        // "(_)" is uncommon in a git branch name, but "/" cannot be matched
        // by Spring MVC
        label = label.replace("(_)", "/");
    }
    // ensure InputStream will be closed to prevent file locks on Windows
    try (InputStream is = this.resourceRepository.findOne(name, profile, label, path).getInputStream()) {
        String text = StreamUtils.copyToString(is, Charset.forName("UTF-8"));
        if (resolvePlaceholders) {
            Environment environment = this.environmentRepository.findOne(name, profile, label);
            text = resolvePlaceholders(prepareEnvironment(environment), text);
        }
        return text;
    }
}
Also used : InputStream(java.io.InputStream) EnvironmentPropertySource.prepareEnvironment(org.springframework.cloud.config.server.support.EnvironmentPropertySource.prepareEnvironment) Environment(org.springframework.cloud.config.environment.Environment)

Example 60 with Environment

use of org.springframework.cloud.config.environment.Environment in project spring-cloud-config by spring-cloud.

the class AbstractScmEnvironmentRepository method findOne.

@Override
public synchronized Environment findOne(String application, String profile, String label) {
    NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(getEnvironment(), new NativeEnvironmentProperties());
    Locations locations = getLocations(application, profile, label);
    delegate.setSearchLocations(locations.getLocations());
    Environment result = delegate.findOne(application, profile, "");
    result.setVersion(locations.getVersion());
    result.setLabel(label);
    return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(), getUri());
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Environment(org.springframework.cloud.config.environment.Environment)

Aggregations

Environment (org.springframework.cloud.config.environment.Environment)118 Test (org.junit.Test)104 StandardEnvironment (org.springframework.core.env.StandardEnvironment)37 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)20 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)19 PropertySource (org.springframework.cloud.config.environment.PropertySource)12 LinkedHashMap (java.util.LinkedHashMap)9 File (java.io.File)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)7 FileOutputStream (java.io.FileOutputStream)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)4 EnvironmentPropertySource.prepareEnvironment (org.springframework.cloud.config.server.support.EnvironmentPropertySource.prepareEnvironment)4 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)4 HttpEntity (org.springframework.http.HttpEntity)4 RestTemplate (org.springframework.web.client.RestTemplate)4 Git (org.eclipse.jgit.api.Git)3 Matchers.anyString (org.mockito.Matchers.anyString)3