Kubernetes Secrets Management Using Kubernetes Sealed Secrets
Security is a major concern in continuous integration (CI), especially when managing sensitive information like API keys, passwords, and other secrets. For Kubernetes resources, Sealed Secrets offer an effective solution for securely managing sensitive information within your repository. In this blog we will explore what are Sealed Secrets, how to use them and some common management tasks around Sealed Secrets.
What are Sealed Secrets?
Sealed Secrets is a set of Kubernetes resources, controller and custom resource definition, that enables secure storage of secrets in your version control system (VCS). Unlike standard Kubernetes Secrets, which are base64-encoded and easily decoded, Sealed Secrets use asymmetric encryption to ensure your secrets remain encrypted until deployed to your Kubernetes cluster. Once deployed, the Sealed Secrets controller decrypts them into regular Kubernetes secrets, making them accessible to your applications.
Key Benefits of Sealed Secrets
- Security: Encrypted secrets can only be decrypted by the controller running in your Kubernetes cluster.
- Version Control: Encrypted secrets can be safely stored in your VCS.
- Automation: Simplifies the management of secrets in CI/CD pipelines.
Working with Sealed Secrets
Sealed Secrets leverage public and private key pairs. The public key, used by the kubeseal utility, encrypts the secrets and creates the corresponding Sealed Secret.
Note that Sealed Secrets are not a substitute for proper access control in the cluster. Once decrypted by the controller, anyone with appropriate access can view the secret’s content.
If the cluster is compromised or deleted, decrypting Sealed Secrets may become impossible. Later, we will discuss a proper key backup strategy to remedy this problem.
High-Level Overview of Working with Sealed Secrets:
- Encrypt Secrets: Admin uses the kubeseal CLI tool to encrypt the secrets.
- Store Encrypted Secrets: Admin sends encrypted secrets, known as Sealed Secrets, to the developers, and they are stored in the repository.
- Deploy Sealed Secrets: Sealed Secrets are deployed like any other Kubernetes object as part of your CI/CD workflow.
Installing Sealed Secrets
Before starting, ensure you have:
- A running Kubernetes cluster with proper access.
kubectl
installed and configured.- Helm installed and configured.
Install the kubeseal
utility using your package manager. Next, find a version of Sealed Secrets compatible with your Kubernetes cluster.
|
|
Install Sealed Secrets resources. Setting fullnameOverride
parameter to the given value is expected by kubeseal
utility.
|
|
Verify the Sealed Secrets controller is properly deployed and a new encryption key is generated.
|
|
Encrypting Secrets
The easiest way to create Sealed Secrets is from a secrets file. Create an example secret file:
|
|
Then, create a Sealed Secret file:
|
|
An example of a Sealed Secrets file is shown below:
|
|
Once created, the Sealed Secret is safe to share publicly. Store it in your repository with the rest of your code. Deploy them like any other Kubernetes object.
|
|
Verify that the Sealed Secret is decrypted correctly and mysecret is created:
|
|
Understanding Key Management
When the controller is first deployed, it generates encryption keys. It generates a new key every 30 days thereafter. The latest key is the active one used to encrypt new secrets, while old keys are retained for decrypting old Sealed Secrets.
To see all encryption keys, use:
|
|
It’s good practice to re-encrypt your Sealed Secrets with the latest key, either for compliance reasons or to maintain good security posture.
|
|
How to Backup Encryption Keys
Backing up the private key is crucial for disaster recovery. If you lose the key, the Sealed Secrets in your repository will become unreadable, and you won’t be able to recover the plaintext secrets.
Backup your encryption key pairs with the following command:
|
|
To restore key pairs to a new cluster, deploy the Sealed Secrets Controller and apply the encryption keys definition file. Then restart the Sealed Secrets controller.
|
|
Verify that you can deploy sealed secret from previous step:
|
|
Conclusion
In this post, we explored how to use Sealed Secrets to simplify the CI/CD process without compromising security. We covered how to encrypt secrets and create Sealed Secrets, the role of the Sealed Secrets controller, key rotations, and re-encryption of secrets. Finally, we discussed how to properly back up and restore encryption keys.
Happy engineering!