spring-bootkubernetes-helmaws-secrets-managerrancher-rke

How to pull secrets from kubernetes and refer it as environment variable for spring boot application using helm charts


I am new to helm chart and spring boot application development.

The sensitive details of my spring boot application are stored in AWS secrets manager like below,

basic.auth-role.ROLE_API_PROXY_ADMIN.[0].username

basic.auth-role.ROLE_API_PROXY_ADMIN.[0].password

basic.auth-role.ROLE_API_PROXY_CONSUMER.[0].username

basic.auth-role.ROLE_API_PROXY_CONSUMER.[0].password

Using helm charts, I am retrieving these from AWS secrets manager and store it in kubernetes secrets which I am able to do so. PFB snippet,

Snippet from helm values.yaml

externalSecret:
  # Create an external secret resource.
  create: true
  # Name of the secret store.
  name: "integration-hub-service-external-secrets"
  # Target name for the external secret.
  targetName: &externalSecret-targetName integration-hub-service-secrets
  secrets:
    # Define the secrets to be pulled from AWS Secrets Manager.
    - secretKey: &basic_auth-role_ROLE_API_PROXY_ADMIN_0_username basic_auth-role_ROLE_API_PROXY_ADMIN_0_username
      remoteRef:
        key: "/DSC/UAT/BLUE/INTEGRATION-HUB-SERVICE"
        property: basic.auth-role.ROLE_API_PROXY_ADMIN.[0].username
    - secretKey: &basic_auth-role_ROLE_API_PROXY_ADMIN_0_password basic_auth-role_ROLE_API_PROXY_ADMIN_0_password
      remoteRef:
        key: "/DSC/UAT/BLUE/INTEGRATION-HUB-SERVICE"
        property: basic.auth-role.ROLE_API_PROXY_ADMIN.[0].password
    - secretKey: &basic_auth-role_ROLE_API_PROXY_CONSUMER_0_username basic_auth-role_ROLE_API_PROXY_CONSUMER_0_username
      remoteRef:
        key: "/DSC/UAT/BLUE/INTEGRATION-HUB-SERVICE"
        property: basic.auth-role.ROLE_API_PROXY_CONSUMER.[0].username
    - secretKey: &basic_auth-role_ROLE_API_PROXY_CONSUMER_0_password basic_auth-role_ROLE_API_PROXY_CONSUMER_0_password
      remoteRef:
        key: "/DSC/UAT/BLUE/INTEGRATION-HUB-SERVICE"
        property: basic.auth-role.ROLE_API_PROXY_CONSUMER.[0].password

Using helm anchor and alias, I am able to get the value from externalSecret and reference it in env as mentioned below.

Now, the issue here is, I have underscore in 'ROLE_API_PROXY_CONSUMER' and 'ROLE_API_PROXY_ADMIN'. So, could you please help me on how to mention the environment variable name in this case,

    env:
      - name: "SPRING_PROFILES_ACTIVE"
        value: "local"
      - name: "???"
        valueFrom:
          secretKeyRef:
            name: *externalSecret-targetName
            key: *basic_auth-role_ROLE_API_PROXY_ADMIN_0_username
      - name: "???"
        valueFrom:
          secretKeyRef:
            name: *externalSecret-targetName
            key: *basic_auth-role_ROLE_API_PROXY_ADMIN_0_password

Solution

  • Spring Boot's rules for binding environment variables to Spring properties are included in the Spring Boot documentation: given the property name, replace dots with underscores, remove any dashes, and convert to uppercase. For list-type values you'd include the index directly in the environment variable name without underscores.

    Given these rules, if your Spring property name is

    basic.auth-role.ROLE_API_PROXY_ADMIN.[0].username
    

    then it would be bound from an environment variable

    BASIC_AUTHROLE_ROLE_API_PROXY_ADMIN_0_USERNAME