我有一个 Spring Boot 应用程序,已部署在 Google Compute Engine 上的 docker 容器上,该容器使用 TrustStore 和 KeyStore 进行 TLS。目前,这些 p12 文件直接位于 jar 文件中的 /src/main/resources/ 路径上。我想将它们外部化并在应用程序加载时从 Google SecretManager 中提取它们。该应用程序利用 Undertow。
我有以下设置:
server:
ssl:
enabled: true
trust-store-password: change-me
#trust-store: # to be set programmatically during launch
client-auth: need
#key-store: # to be set programmatically during launch
key-store-type: PKCS12
key-alias: my-alias
key-store-password: change-me
key-password: change-me
trust-store-type: PKCS12
我正在尝试按照这个思路做一些事情来设置信任库属性。
@Configuration
class SSLStoreConfig {
private final String trustStore = "my-trust-store.p12"
@Autowired
SecretManagerTemplate secretManagerTemplate
@PostConstruct
private void configureSSL() {
byte[] secretBytes = secretManagerTemplate.getSecretBytes("sm://trust-store")
Path filePath = Paths.get(trustStore)
try {
Files.write(filePath, secretBytes)
} catch (IOException e) {
System.out.println("Failed to write bytes" + e.toString())
}
String value = filePath.toAbsolutePath().toString()
System.setProperty("server.ssl.trust-store", value)
}
@PreDestroy
private void removeStores() {
Path filePath = Paths.get(trustStore)
Files.delete(filePath)
}
}
显然,这仅适用于信任库。一旦我能够成功设置它,我也可以添加 KeyStore。
如何从 GCP SecretManager 中提取这些文件并成功为 server.ssl 配置属性引用它们?