Sitemap
Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Using external secrets in Kubernetes

5 min readJan 31, 2024

--

Press enter or click to view image in full size

Background

In a previous article in which I deployed Airflow almost in a production environment with Kubernetes, at some point I mentioned that creating the necessary secrets as standard Kubernetes secrets isn't the best practice and one alternative is to use the External Secrets Operator. With that said, in this article I'll try to make a overview and a hello world with this operator.

Why use External Secrets?

Kubernetes gives us the tools for creating secrets, but these secrets are encrypted only with base64, which means that if you want to version the secret manifest in a git repository, for instance, anyone can run the following command echo -n "<SECRET_IN_BASE64>" | base64 --decode to get access to the secrets unencrypted value.

To avoid this kind of issue and to provide one more layer of security, external secrets comes with another approach, to use cloud providers and secret vaults as the source of truth for the secrets values and sync these sources with a standard Kubernetes secret.

With the access control of cloud providers and secrets vaults in combination of Kubernete's RBAC, we are able to limit to only the ones who need to see the unencrypted value of the secret. To avoid versioning the base64 value of the secret in the git repository.

Hands on!

To start our lab, I should point that I'll not provision any cloud infrastructure, mostly because of the cost. With that said, I'll use Kind to have a local Kubernetes cluster to play with and cover some AWS specific concepts. In order to deploy the External Secrets Operator, I'll use Helm and it's helm chart.

According to the External Secrets documentation, we need to configure a policy and attach it to a role. The policy must contain the following permissions:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetResourcePolicy",
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret",
"secretsmanager:ListSecretVersionIds"
],
"Resource": [
"arn:aws:secretsmanager:<REGION>:<ACCOUNT_ID>:secret:dev-*"
]
}
]
}

Note that in the Resource block we can use a wild card, in this example, we gave permission only to see the secrets starting with dev-. With that we can ensure that the external secrets operator doesn't see unnecessary secrets in our AWS account.

One extra step we must take in our AWS account is to create a role and attach the created policy to it.

Once the policy was created and attached to a role, we can install the external secrets helm chart in our environment, to achieve that we can use the following command:

helm repo add external-secrets-operator https://charts.external-secrets.io/ && \
helm install my-external-secrets external-secrets-operator/external-secrets --version 0.9.11

With the helm chart installed, we can write our helm values. In the end, it'll be something like this:

It is important to set the role arn we recently created in the .serviceAccount.annotations to make helm able to create the needed service accounts with the permissions to see the our secrets within AWS secrets manager.

To install the external secrets in our cluster we can use the following command:

helm upgrade --install external-secrets external-secrets --repo=https://charts.external-secrets.io \
--version 0.9.11 \
-n external-secrets \
--create-namespace \
--values ./manifests/values.yaml \
--set serviceAccount.annotations."eks\.amazonaws\.com/role-arn"=${EXTERNAL_SECRETS_ROLE_ARN}

Note that we can pass the value of our role arn to avoid versioning it in our values manifest. Since this code can be executed within a Makefile, you can customize it to get the role arn dynamically using AWS CLI or Terraform.

Get Victor Macedo’s stories in your inbox

Join Medium for free to get updates from this writer.

Once the installation is finished, we are presented the following output:

NAME: external-secrets
LAST DEPLOYED: Mon Jan 29 16:43:05 2024
NAMESPACE: external-secrets
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
external-secrets has been deployed successfully!

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: https://github.com/external-secrets/external-secrets

Now the resources ClusterSecretStore, SecretStore and ExternalSecrets will be available to us in our cluster. Let's create a ClusterSecretStore to sync it to the AWS secrets manager.

Considering you are using the Elastic Kubernetes Service (EKS) in the same account you're storing the secrets, we can create the ClusterSecretStore with the following manifest:

But, since I do not provisioned any infrastructure, I will go with the Fake provider, that allow us to create a fake provider just to understand how the external secret works. We can create it with the following manifest:

Now, in order to create our ExternalSecret we can apply the following manifest:

The ExternalSecret manifest is responsible to get the secret we want, in this case the /foo/baz and create a standard Kubernetes Secret with the values of it and name this Secret resource as secret-to-be-created .

After applying the manifests to our cluster, we can see if they are really there.

Since our secret /foo/baz is a JSON and it has two keys, we can see that our secret was created with two DATA=2. To see the value of the secret we can use: kubectl get secret secret-to-be-created -o yaml | yq .data.john | base64 --decode , this command will get the true unencrypted value that the john key contains.

Press enter or click to view image in full size

Wrap up

In this example, we installed the External Secrets Operator and created our first secret from a ExternalSecret resource. The operator has a lot of connections with almost any provider in the market, make sure to check external secrets docs!

Remember that if you want to use RBAC to limit who can see the Kubernetes Secrets that are created from the ExternalSecrets resource you should specify the namespace you are creating the ExternalSecrets and maybe will be more suitable to create as many SecretStore's as necessary instead of a ClusterSecretStore.

I hope this article helped you in some way!

Wanna connect?

--

--

Written by Victor Macedo

Machine Learning Engineer | MLOps | Desenvolvedor | DevOps | Java | Python | Go | Kubernetes

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.