![](/img/trans.png)
[英]How to specify external properties files in application.properties in Spring Boot?
[英]How to handle Docker-Secrets in application.properties files
如何將 Docker 機密(來自 /run/secrets 的文件/數據)注入 application.properties 文件? 使用環境變量安全嗎?
在 application.properties 中使用 docker secrets 的最佳方式似乎是使用配置樹。
如果你的 docker secrets 掛載到 /run/secrets(默認),那么你只需要將spring.config.import=optional:configtree:/run/secrets/
放在 application.properties 的頂部。 然后你可以使用例如 docker secret db_password
作為${db_password}
。
首先,為application.properties
的秘密數據使用環境變量是不安全的。
在談論秘密時,您主要有兩種選擇。
如果您使用 Docker Secrets而沒有 Docker Swarm ,那么您可以直接將整個application.properties
加載到 secret 中,將其掛載在/run/secrets
下,並將其作為帶有 Z38008DD81C2F4D791ZCE8AF6E 標志的配置文件引用。
如果您將 Docker Secrets與 Docker Swarm 一起使用,那么您可以使用 Swarm 的配置模板將您感興趣並與它們相關的具體字段存儲為秘密。
例子:
echo -n "myUser" | docker secret create db_user -
echo -n "myPass" | docker secret create db_password -
echo -n "jdbc://..." | docker secret create db_url -
應用程序.properties.tmpl
spring.datasource.url={{ secret "db_url" }}
spring.datasource.user={{ secret "db_user" }}
spring.datasource.password={{ secret "db_password" }}
docker-compose.yml
version: '3.9'
services:
api:
image: yourapp:1.0.0
configs:
- source: application.properties
target: /usr/app/config/application.properties
secrets:
- db_url
- db_user
- db_password
configs:
application.properties:
template_driver: golang
file: ./application.properties.tmpl
name: myapp.application.properties
secrets:
db_url:
external: true
db_user:
external: true
db_password:
external: true
當您使用docker stack deploy -c docker-compose.yml myapp
進行部署時,它將自動使用機密內容填充配置並將其掛載到目標路徑中。
如果您訂閱了關於配置的十二要素應用程序哲學,那么環境變量是存儲應用程序機密的合適位置。
使用 Spring 引導,具體來說,可以按照映射到application.properties
或application.yml
文件中的鍵的UPPER_SNAKE_CASE
命名約定將它們設置為容器中的環境變量。 例如,如果您想設置數據庫密碼,就好像它在application.properties
文件中定義為database.password=i-am-the-password
但從版本控制中忽略它,您可以這樣做:
$ export DATABASE_PASSWORD=i-am-the-password
(或將 env var 注入容器運行時的另一種方法。)
然后可以在 Java 代碼中訪問數據庫密碼,如下所示:
import org.springframework.beans.factory.annotation.Value;
public class Example {
private final String databasePassword;
public Example(
@Value("${database.password}") String databasePassword) {
this.databasePassword = databasePassword;
}
}
目前在春天很容易。
首先讓我們創建 2 個秘密:
docker secret create magic magic.yml
docker secret create arctos arctos.yml
在 magic.yml 和 arctos.yml 之上是您希望作為 docker 秘密保留的配置文件,例如:
#arctos.yml
---
arctos:
cold: "Very cold!"
#magic.yml
---
magic:
value1: "Hello!"
value2: 20
現在讓我們創建 Spring Boot 簡單應用程序:
@SpringBootApplication
@EnableConfigurationProperties({MagicProperties.class, ArctosProperties.class})
public class SimpleWebServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleWebServiceApplication.class, args);
}
}
@ConfigurationProperties("arctos")
record ArctosProperties(String cold) {
}
@ConfigurationProperties("magic")
record MagicProperties(String value1, int value2) {
}
@RestController
@RequiredArgsConstructor // lombok annotation pure constructor can be used
class HelloController {
private final MagicProperties magicProperties;
private final ArctosProperties arctosProperties;
@GetMapping("/magic")
public MagicProperties magic() {
return magicProperties;
}
@GetMapping("/arctos")
public ArctosProperties arctos() {
return arctosProperties;
}
}
#application.yml
---
spring:
application:
name: hello-secret
config:
import:
- optional:file:/run/secrets/magic.yml
- optional:file:/run/secrets/arctos.yml
#docker-compose.yml
version: '3.9'
networks:
default:
driver: overlay
name: my-network
services:
hello-secret:
image: <your_image_here>
ports:
- 8080:8080
deploy:
replicas: 1
update_config:
order: start-first
monitor: 10s
delay: 5s
secrets:
- source: magic
target: magic.yml
- source: arctos
target: arctos.yml
secrets:
magic:
external: true
arctos:
external: true
現在運行:
docker stack deploy -c docker-compose.yml mystack
並致電:
http :8080/magic
http :8080/arctos
#Post Scriptum
事實上,我們可以讓我們的應用程序更加靈活。 例如,如果在開發/測試期間我們不想使用秘密,我們可以稍微擴展我們的應用程序
#application.yml 我們擴展了
# Properties
arctos:
cold: ${ARCTOS_COLD:}
magic:
value1: ${MAGIC_VALUE1:}
value2: ${MAGIC_VALUE2:}
#docker-compose.yml 我們擴展了
environment:
MAGIC_VALUE1: "env_used"
MAGIC_VALUE2: 0
ARCTOS_COLD: "In fact in env area is hot ;]"
在這種情況下,當秘密未與我們的服務相關聯時,將使用環境值。 (事實上 ,如果您提供環境值,它們將具有優先權,因此無論如何它們最終都會獲勝)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.