This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Cloud Kubernetes 3.1.4!spring-doc.cn

Secrets PropertySource

Kubernetes has the notion of Secrets for storing sensitive data such as passwords, OAuth tokens, and so on. This project provides integration with Secrets to make secrets accessible by Spring Boot applications. You can explicitly enable or disable This feature by setting the spring.cloud.kubernetes.secrets.enabled property.spring-doc.cn

When enabled, the Fabric8SecretsPropertySource looks up Kubernetes for Secrets from the following sources:spring-doc.cn

  1. Reading recursively from secrets mountsspring-doc.cn

  2. Named after the application (as defined by spring.application.name)spring-doc.cn

  3. Matching some labelsspring-doc.cn

By default, consuming Secrets through the API (points 2 and 3 above) is not enabled for security reasons. The permission 'list' on secrets allows clients to inspect secrets values in the specified namespace. Further, we recommend that containers share secrets through mounted volumes.spring-doc.cn

If you enable consuming Secrets through the API, we recommend that you limit access to Secrets by using an authorization policy, such as RBAC. For more information about risks and best practices when consuming Secrets through the API refer to this doc.spring-doc.cn

If the secrets are found, their data is made available to the application.spring-doc.cn

Assume that we have a spring boot application named demo that uses properties to read its database configuration. We can create a Kubernetes secret by using the following command:spring-doc.cn

kubectl create secret generic db-secret --from-literal=username=user --from-literal=password=p455w0rd

The preceding command would create the following secret (which you can see by using kubectl get secrets db-secret -o yaml):spring-doc.cn

apiVersion: v1
data:
  password: cDQ1NXcwcmQ=
  username: dXNlcg==
kind: Secret
metadata:
  creationTimestamp: 2017-07-04T09:15:57Z
  name: db-secret
  namespace: default
  resourceVersion: "357496"
  selfLink: /api/v1/namespaces/default/secrets/db-secret
  uid: 63c89263-6099-11e7-b3da-76d6186905a8
type: Opaque

Note that the data contains Base64-encoded versions of the literal provided by the create command.spring-doc.cn

Your application can then use this secret — for example, by exporting the secret’s value as environment variables:spring-doc.cn

apiVersion: v1
kind: Deployment
metadata:
  name: ${project.artifactId}
spec:
   template:
     spec:
       containers:
         - env:
            - name: DB_USERNAME
              valueFrom:
                 secretKeyRef:
                   name: db-secret
                   key: username
            - name: DB_PASSWORD
              valueFrom:
                 secretKeyRef:
                   name: db-secret
                   key: password

You can select the Secrets to consume in a number of ways:spring-doc.cn

  1. By listing the directories where secrets are mapped:spring-doc.cn

    -Dspring.cloud.kubernetes.secrets.paths=/etc/secrets/db-secret,etc/secrets/postgresql

    If you have all the secrets mapped to a common root, you can set them like:spring-doc.cn

    -Dspring.cloud.kubernetes.secrets.paths=/etc/secrets
  2. By setting a named secret:spring-doc.cn

    -Dspring.cloud.kubernetes.secrets.name=db-secret
  3. By defining a list of labels:spring-doc.cn

    -Dspring.cloud.kubernetes.secrets.labels.broker=activemq
    -Dspring.cloud.kubernetes.secrets.labels.db=postgresql

As the case with ConfigMap, more advanced configuration is also possible where you can use multiple Secret instances. The spring.cloud.kubernetes.secrets.sources list makes this possible. For example, you could define the following Secret instances:spring-doc.cn

spring:
  application:
    name: cloud-k8s-app
  cloud:
    kubernetes:
      secrets:
        name: default-name
        namespace: default-namespace
        sources:
         # Spring Cloud Kubernetes looks up a Secret named s1 in namespace default-namespace
         - name: s1
         # Spring Cloud Kubernetes looks up a Secret named default-name in namespace n2
         - namespace: n2
         # Spring Cloud Kubernetes looks up a Secret named s3 in namespace n3
         - namespace: n3
           name: s3

In the preceding example, if spring.cloud.kubernetes.secrets.namespace had not been set, the Secret named s1 would be looked up in the namespace that the application runs. See namespace-resolution to get a better understanding of how the namespace of the application is resolved.spring-doc.cn

Similar to the ConfigMaps; if you want your application to fail to start when it is unable to load Secrets property sources, you can set spring.cloud.kubernetes.secrets.fail-fast=true.spring-doc.cn

It is also possible to enable retry for Secret property sources like the ConfigMaps. As with the ConfigMap property sources, first you need to set spring.cloud.kubernetes.secrets.fail-fast=true. Then you need to add spring-retry and spring-boot-starter-aop to your classpath. Retry behavior of the Secret property sources can be configured by setting the spring.cloud.kubernetes.secrets.retry.* properties.spring-doc.cn

If you already have spring-retry and spring-boot-starter-aop on the classpath for some reason and want to enable fail-fast, but do not want retry to be enabled; you can disable retry for Secrets PropertySources by setting spring.cloud.kubernetes.secrets.retry.enabled=false.

Since data coming from Secrets is usually treated as sensitive, endpoints of the actuator /env and /configprops can be made to sanitize data, so that it is not displayed in plain text. In order to do that, you need to set:spring-doc.cn

spring.cloud.kubernetes.sanitize.secrets=true

This setting is supported since 3.0.6 and upwards.spring-doc.cn

Table 1. Properties:
Name Type Default Description

spring.cloud.kubernetes.secrets.enabledspring-doc.cn

Booleanspring-doc.cn

truespring-doc.cn

Enable Secrets PropertySourcespring-doc.cn

spring.cloud.kubernetes.secrets.namespring-doc.cn

Stringspring-doc.cn

${spring.application.name}spring-doc.cn

Sets the name of the secret to look upspring-doc.cn

spring.cloud.kubernetes.secrets.namespacespring-doc.cn

Stringspring-doc.cn

Client namespacespring-doc.cn

Sets the Kubernetes namespace where to look upspring-doc.cn

spring.cloud.kubernetes.secrets.labelsspring-doc.cn

Mapspring-doc.cn

nullspring-doc.cn

Sets the labels used to lookup secretsspring-doc.cn

spring.cloud.kubernetes.secrets.pathsspring-doc.cn

Listspring-doc.cn

nullspring-doc.cn

Sets the paths where secrets are mounted (example 1)spring-doc.cn

spring.cloud.kubernetes.secrets.enableApispring-doc.cn

Booleanspring-doc.cn

falsespring-doc.cn

Enables or disables consuming secrets through APIs (examples 2 and 3)spring-doc.cn

spring.cloud.kubernetes.secrets.fail-fastspring-doc.cn

Booleanspring-doc.cn

falsespring-doc.cn

Enable or disable failing the application start-up when an error occurred while loading a Secretspring-doc.cn

spring.cloud.kubernetes.secrets.retry.enabledspring-doc.cn

Booleanspring-doc.cn

truespring-doc.cn

Enable or disable secrets retry.spring-doc.cn

spring.cloud.kubernetes.secrets.retry.initial-intervalspring-doc.cn

Longspring-doc.cn

1000spring-doc.cn

Initial retry interval in milliseconds.spring-doc.cn

spring.cloud.kubernetes.secrets.retry.max-attemptsspring-doc.cn

Integerspring-doc.cn

6spring-doc.cn

Maximum number of attempts.spring-doc.cn

spring.cloud.kubernetes.secrets.retry.max-intervalspring-doc.cn

Longspring-doc.cn

2000spring-doc.cn

Maximum interval for backoff.spring-doc.cn

spring.cloud.kubernetes.secrets.retry.multiplierspring-doc.cn

Doublespring-doc.cn

1.1spring-doc.cn

Multiplier for next interval.spring-doc.cn

You can find an example of an application that uses secrets (though it has not been updated to use the new spring-cloud-kubernetes project) at spring-boot-camel-configspring-doc.cn