API keys, credentials, and other types of sensitive information are the primary way your application calls external services and interacts with the world outside its own runtime. Ensuring that your secrets are secure is one of the most important operational tasks that you need to address, but it's not just rigorous security that's important—providing an ergonomic, audited, and maintainable flow for the management of those secrets is crucial as well. If managing credentials is confusing or difficult, mistakes can undo all of the careful work that goes into keeping secrets secure.
Container orchestrators like Kubernetes offer helpful abstractions to address the need for sensitive values with native secrets. However, a Kubernetes Secret is a somewhat rudimentary object—it lacks encryption by default and normally exists as a plain key/value within etcd. By contrast, services like Vault or Doppler are intentionally designed to provide strong guarantees and well-defined access controls. Can we combine the operational power of Kubernetes with the assurance of a fully managed secret provider?
Yes! The External Secrets Operator is a Kubernetes operator that bridges the gap between Kubernetes' native secret support and external systems that provide a canonical source of truth for secret storage. It does this by leveraging custom resources that define how to retrieve external secrets in order to manage the lifecycle of secret resources in your Kubernetes cluster. In this tutorial, we'll use Doppler as the external secret provider to illustrate using external secrets in a real-world application.
The end goal of this guide is to leverage well-managed secrets in an application deployed in Kubernetes. To achieve this, we'll use:
When finished, you'll understand how to store and manage secrets in Doppler, use those same secrets as native Kubernetes secrets, and ultimately use them in a running application. Let's begin!
This article assumes that you'll follow along with your own Kubernetes cluster. You may choose to operate in an existing cluster (potentially within a sandboxed namespace), but if you'd like to use a sandbox instead, we suggest using either minikube or kind to create a local Kubernetes installation which will provide a safe environment for testing.
In order to provide a clean and pristine environment, we'll use minikube as the Kubernetes target.
In addition to a functional Kubernetes cluster, ensure that the following command-line tools are installed:
Once installed, confirm that your environment is ready:
The kubectl version command should confirm that kubectl is present and communicating with the Kubernetes API successfully. If you aren't using minikube, simply use the plain kubectl command for the remainder of the tutorial.
The helm and doppler commands should be available:
With the prerequisites installed, let's proceed with building and running our program.
Before we start creating secrets, let's begin by illustrating the external secrets workflow with a simple example service. The following guide will assume the use of minikube to build and load an application container image, so if your Kubernetes environment is different, you may need to adapt the instructions to work with your specific container registry strategy.
First, perform some initial steps to bootstrap a python Flask application:
Create a file named app.py that contains the following simple web application:
Define a small Dockerfile that defines how to build a container image for our application:
You can run this application locally with:
Try accessing the running application at http://localhost:5000 to see a response. The root route (/) will render a static string when no secret is present in the environment but will print the secret when the indicated environment variable is defined. Don't print secrets in production! This application is just a demonstration of how to read the value in a real program.
We're ready to load this application into Kubernetes. Assuming that you're following along with minikube, proceed to build the application container in the Kubernetes node under the name "app":
Create a new Kubernetes Deployment file named deployment.yaml:
Take note of a few assumptions in this Deployment:
Load this Deployment into your running cluster:
Finally, forward the port in another terminal window in order to access the running application in a simple tunnel.
Send a request to your Kubernetes application to see it in action:
Fantastic! Note that we've received the response that indicates no secret value has been injected into the environment. How can we add a secret to the application and see the secret sauce?
By using Doppler, we can achieve a great deal of control over application secrets and manage them at each step of our application's lifecycle, from in our local development environment to within a Kubernetes workload.
If you haven't signed up with Doppler, you can do so now. Once you have an account, proceed to use the login command to set up your account locally:
Setup Doppler for the demo application by defining a doppler-template.yaml file in the root of the application directory. This YAML file defines a template that we can import to create a new Doppler project easily from the command line:
Enter the directory in your shell and run the following doppler command in order to bootstrap your Doppler project. This will create a new project called secret-sauce, set up a few different environments, and load an initial secret value for SECRET_SAUCE:
You'll see output similar to the following:
View the secrets for this project with doppler secrets:
In addition to some default variables that begin with DOPPLER, you'll also find the secret value we'd like to inject, SECRETSAUCE:
Run a quick test to confirm that our application behaves as expected when a secret is present in its environment variables. To do this, we can use the doppler run command to easily inject the variable into our application.
Then issue a request to the listening port to see whether the response has changed:
Great! Let's continue on to install the External Secrets Operator.
The External Secrets Operator provides the translation layer between Kubernetes' native secrets and external secrets. The operator leverages custom resources in order to model external secrets, which it then retrieves as necessary and translates into native Kubernetes secrets that your workload can easily consume.
The operator is provided as a Helm chart, so first add the upstream external-secrets Helm repository to access the chart:
helm repo add external-secrets https://charts.external-secrets.io
# >> external-secrets has been added to your repositories
Next, install the chart into your Kubernetes cluster. The following command:
You should see output similar to the following:
In order to begin using ExternalSecrets, you will need to set up a SecretStore or ClusterSecretStore resource (for example, by creating a 'vault' SecretStore).
More information on the different types of SecretStores and how to configure them can be found in our GitHub repository.
With this operator installed, we're now ready to set up Doppler to serve as the source for external secrets.
The first step is to create a service token that serves as the authentication mechanism for the external secrets operator against Doppler. It should be stored inside of a generic Kubernetes secret that the operator will consume.
You can generate and store the token in a single step with some fancy footwork in the shell to avoid copying and pasting the token around. The following command creates a new Doppler token and then immediately loads it into Kubernetes:
Note that we're passing the --config prd flag to doppler in order to create a token scoped to the prd (production) configuration of our Doppler project. In our application directory, we defaulted to dev, which has its own secrets. With this technique, we're only interacting with development secrets locally while loading production secrets into our container runtime, which keeps the risk of exposure for production secrets minimal.
Next, create a SecretStore CRD that points the operator at your Doppler service token secret. This step sets up Doppler as a source for external secrets that we can call upon later.
Create the SecretStore in Kubernetes:
We're ready to inject our secret sauce! A new ExternalSecret is the necessary resource that enables us to synchronize one of our Doppler secrets to a related generic Kubernetes secret. Create a new file called externalsecret.yaml with the following YAML:
Load it into Kubernetes:
You can confirm that the secret was loaded by the operator by listing it with kubectl:
Success! Note that if you do not see the new secret, you can also inspect the External Secret Operator's logs to debug any issues:
Generic secrets can be seen in the Kubernetes dashboard as well. If you're using minikube, you can quickly install and view the Kubernetes dashboard with the following command:
Your browser will open to the dashboard landing page. You may use the left sidebar to navigate to the "Secrets" link to view your new secret:
The only task left to do is to consume the secret from our application. Here's an updated Deployment that references the newly-created Secret. Note the new env property, which injects the variable SECRET_SAUCE drawn from a secret named secret-sauce under the key SECRET_SAUCE:
Apply the updated Deployment:
The changed Deployment manifest will recreate any necessary pods, so invoke a new forwarded port once the pods have restarted to forward requests to the new containers:
Finally, Issue an HTTP request to the running container to see the injected Doppler secret in action:
Note that the content of this secret differs from the one we saw in our local development environment. The --config prd flag to the doppler command loaded a token with rights to the prd configuration in Doppler, which injects the correct secret for the configuration that the Doppler token has been scoped to.
Congratulations! You've successfully:
There's even more you can do with the External Secrets Operator and Doppler, so to learn more, dive into the documentation to learn how to use features like JSON processing, filtering, and more.
If you're actively using Kubernetes secrets stored in etcd today, you should also configure encryption at rest so that your secrets are secure no matter where they're stored. Whether you load them with kubectl or the External Secrets Operator, configuring encryption at rest is still important to address at every step of your secret management lifecycle.