|
| 1 | +/* |
| 2 | + * Copyright 2016 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.endpoint |
| 18 | + |
| 19 | +import org.springframework.boot.context.properties.ConfigurationProperties |
| 20 | +import org.springframework.core.env.ConfigurableEnvironment |
| 21 | +import org.springframework.core.env.EnumerablePropertySource |
| 22 | +import org.springframework.core.env.Environment |
| 23 | +import org.springframework.core.env.MutablePropertySources |
| 24 | +import org.springframework.core.env.StandardEnvironment |
| 25 | + |
| 26 | +@ConfigurationProperties(prefix = "endpoints.resolvedEnv") |
| 27 | +class ResolvedEnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> { |
| 28 | + |
| 29 | + private final Sanitizer sanitizer = new Sanitizer(); |
| 30 | + |
| 31 | + public ResolvedEnvironmentEndpoint() { |
| 32 | + super("resolvedEnv") |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + Map<String, Object> invoke() { |
| 37 | + Environment environment = getEnvironment() |
| 38 | + return getPropertyKeys().collectEntries { |
| 39 | + [(it): sanitizer.sanitize(it, environment.getProperty(it))] |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Impl partially copied from |
| 45 | + * {@link org.springframework.boot.actuate.endpoint.EnvironmentEndpoint} |
| 46 | + * |
| 47 | + * This gathers all defined properties in the system (no matter the source) |
| 48 | + */ |
| 49 | + private SortedSet<String> getPropertyKeys() { |
| 50 | + SortedSet<String> result = new TreeSet<String>() |
| 51 | + Environment environment = getEnvironment() |
| 52 | + |
| 53 | + MutablePropertySources sources |
| 54 | + if (environment && environment instanceof ConfigurableEnvironment) { |
| 55 | + sources = ((ConfigurableEnvironment) environment).getPropertySources() |
| 56 | + } else { |
| 57 | + sources = new StandardEnvironment().getPropertySources() |
| 58 | + } |
| 59 | + |
| 60 | + sources.each { |
| 61 | + if (it instanceof EnumerablePropertySource) { |
| 62 | + ((EnumerablePropertySource) it).propertyNames.each { |
| 63 | + result.add(it) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return result |
| 69 | + } |
| 70 | + |
| 71 | + public void setKeysToSanitize(String... keysToSanitize) { |
| 72 | + this.sanitizer.setKeysToSanitize(keysToSanitize); |
| 73 | + } |
| 74 | +} |
0 commit comments