首页 > 解决方案 > 使用 spring.cloud.kubernetes.secrets.paths 属性将机密作为文件读取

问题描述

是否有一个如何使用spring.cloud.kubernetes.secrets.paths=/mnt/secrets-store属性将机密作为文件读取的示例(作为卷安装)?

我的 Pod.yaml


kind: Pod
apiVersion: v1
metadata:
  name: nginx-mounted-secrets
  namespace: default
  labels:
    aadpodidbinding: pod-mi
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: foo
          mountPath: "/mnt/secrets-store"
          readOnly: true
  volumes:
    - name: foo
      csi:
        driver: secrets-store.csi.k8s.io
        readOnly: true
        volumeAttributes:
          secretProviderClass: spc

当我运行 Pod 时,秘密已安装

kubectl -n default exec -it nginx-mounted-secrets -- bash
root@nginx-mounted-secrets:/# ls /mnt/secrets-store
service-one-secret
service-two-secret

我尝试了以下方法,但未填充字段:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties
@Data
public class ApplicationSecrets {

    private String serviceOneSecret;
    private String serviceTwoSecret;

}

我在用着

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-kubernetes-config</artifactId>
            <version>1.1.6.RELEASE</version>
        </dependency>

标签: springspring-cloud-kubernetes

解决方案


一般来说,spring 应用程序ConfigurationProperties属性的构建考虑到了关注点分离的前缀(即 spring 属性与应用程序属性..)。

按照您的 yml 结构,您的属性有一些不同的关注点:元数据、规范(分为容器和卷)

因此,为了使您的实现正常工作:

  1. @ConfigurationProperties为每个子树定义正确的前缀并相应地使用它们

2.[OR] 加载 yml 属性并通过 @Value 查找它们,使用自定义 propertyPlaceholder,如以下答案所示:How to use YamlPropertiesFactoryBean to load YAML files using Spring Framework 4.1? 例如,它们会自动装配地图中的所有属性(此解决方案@Value("#{${propertyname}}") private Map<String,String> propertyname;


推荐阅读