Sealed Secrets are a way to encrypt Kubernetes Secrets value that can be created by anyone, but can only be decrypted by the controller running in the target cluster recovering the original object. This is a good way if we want to store our sensitive configuration values into a git repository, especially when doing a gitops approach.
First is we need to install helm and add sealed-secret repo to it,
$ brew install helm $ helm repo add sealed-secrets https://bitnami-labs.github.io/sealed-secrets
Next is we need to create a specific Namespace and install our sealed-secret there,
$ oc project sealed-secrets
$ helm install my-sealed-secret \
--set containerSecurityContext.enabled=false \
--set podSecurityContext.enabled=false \
sealed-secrets/sealed-secrets
Lets try to create a simple Kubernetes secret
$ oc create secret generic app-cred-secret \ --from-literal=username=username123 \ --from-literal=password=password123 \ --dry-run=client -n edwin-ns -o yaml > secret.yaml
Where the result would be like this,
apiVersion: v1 data: password: cGFzc3dvcmQxMjM= username: dXNlcm5hbWUxMjM= kind: Secret metadata: creationTimestamp: null name: app-cred-secret namespace: edwin-ns
Now lets try to use Kubeseal to generate a secret which is being encrypted. We can specify “controller-name” based on generated service name within “sealed-secrets” namespace.
$ brew install kubeseal
$ kubeseal --controller-name=my-sealed-secret-sealed-secrets \
--controller-namespace=sealed-secrets -o yaml < secret.yaml > secret.sealed.yaml
We can see the result of the encrypted yaml,
apiVersion: bitnami.com/v1alpha1
kind: SealedSecret
metadata:
creationTimestamp: null
name: app-cred-secret
namespace: edwin-ns
spec:
encryptedData:
password: AgBXkADvKsjAHS31UwWFT+........eJtODYDQw==
username: AgAP40ssm84PhmNYDKPfDf/Cf......JDBQDtQ==
template:
metadata:
creationTimestamp: null
name: app-cred-secret
namespace: edwin-ns
After that, we can implement it directly using “oc apply” command
$ oc apply -f secret.sealed.yaml -n edwin-ns
and we can validate by running below command,
$ oc get sealedsecrets NAME AGE app-cred-secret 53m
We can see that our secret is succesfully created in our namespace
$ oc get secret app-cred-secret -n edwin-ns NAME TYPE DATA AGE app-cred-secret Opaque 2 55m