-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support config yaml files embedded in env vars via spring.config.import
- Loading branch information
1 parent
51d15c7
commit 3b513af
Showing
13 changed files
with
208 additions
and
5 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
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
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
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
35 changes: 35 additions & 0 deletions
35
...g-boot/src/main/java/org/springframework/boot/io/EnvironmentVariableProtocolResolver.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,35 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.boot.io; | ||
|
||
import org.springframework.core.io.ProtocolResolver; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.ResourceLoader; | ||
|
||
/** | ||
* {@link ProtocolResolver} for resources contained in environment variables. | ||
* | ||
* @author Francisco Bento | ||
*/ | ||
class EnvironmentVariableProtocolResolver implements ProtocolResolver { | ||
|
||
@Override | ||
public Resource resolve(String location, ResourceLoader resourceLoader) { | ||
return EnvironmentVariableResource.fromUri(location); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...ct/spring-boot/src/main/java/org/springframework/boot/io/EnvironmentVariableResource.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,87 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.boot.io; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
import org.springframework.core.io.AbstractResource; | ||
import org.springframework.core.io.Resource; | ||
|
||
/** | ||
* {@link Resource} implementation for system environment variables. | ||
* | ||
* @author Francisco Bento | ||
* @since 3.5.0 | ||
*/ | ||
public class EnvironmentVariableResource extends AbstractResource { | ||
|
||
/** Pseudo URL prefix for loading from an environment variable: "env:". */ | ||
public static final String PSEUDO_URL_PREFIX = "env:"; | ||
|
||
/** Pseudo URL prefix indicating that the environment variable is base64-encoded. */ | ||
public static final String BASE64_ENCODED_PREFIX = "base64:"; | ||
|
||
private final String envVar; | ||
|
||
private final boolean isBase64; | ||
|
||
public EnvironmentVariableResource(final String envVar, final boolean isBase64) { | ||
this.envVar = envVar; | ||
this.isBase64 = isBase64; | ||
} | ||
|
||
public static EnvironmentVariableResource fromUri(String url) { | ||
if (url.startsWith(PSEUDO_URL_PREFIX)) { | ||
String envVar = url.substring(PSEUDO_URL_PREFIX.length()); | ||
boolean isBase64 = false; | ||
if (envVar.startsWith(BASE64_ENCODED_PREFIX)) { | ||
envVar = envVar.substring(BASE64_ENCODED_PREFIX.length()); | ||
isBase64 = true; | ||
} | ||
return new EnvironmentVariableResource(envVar, isBase64); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean exists() { | ||
return System.getenv(this.envVar) != null; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Environment variable '" + this.envVar + "'"; | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
return new ByteArrayInputStream(getContents()); | ||
} | ||
|
||
protected byte[] getContents() { | ||
String value = System.getenv(this.envVar); | ||
if (this.isBase64) { | ||
return Base64.getDecoder().decode(value); | ||
} | ||
return value.getBytes(StandardCharsets.UTF_8); | ||
} | ||
|
||
} |
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
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
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
2 changes: 2 additions & 0 deletions
2
...spring-boot/src/test/resources/application-import-base64-yaml-from-environment.properties
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,2 @@ | ||
my.value=application-import-base64-yaml-from-environment | ||
spring.config.import=env:base64:MY_CONFIG_BASE64_YAML[.yaml] |
2 changes: 2 additions & 0 deletions
2
.../spring-boot/src/test/resources/application-import-properties-from-environment.properties
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,2 @@ | ||
my.value=application-import-properties-from-environment | ||
spring.config.import=env:MY_CONFIG_PROPERTIES[.properties] |
2 changes: 2 additions & 0 deletions
2
...c/test/resources/application-import-yaml-from-environment-with-profile-variant.properties
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,2 @@ | ||
spring.config.import=env:MY_CONFIG_YAML[.yaml] | ||
my.value=application-import-from-environment-with-profile-variant |
2 changes: 2 additions & 0 deletions
2
...roject/spring-boot/src/test/resources/application-import-yaml-from-environment.properties
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,2 @@ | ||
my.value=application-import-yaml-from-environment | ||
spring.config.import=env:MY_CONFIG_YAML[.yaml] |