Coder Social home page Coder Social logo

chrismld / smartkey-kubernetes-webhook Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 2.0 20 KB

An alpha webhook that mutates Kubernetes secrets to encrypt its content using an encryption key from SmartKey. When a pod wants to use a secret, the mutate webhook injects an init container that decrypts the secrets with SmartKey and stores them in a shared volume in memory with the pod.

Dockerfile 2.98% Go 90.56% Shell 6.46%
kubernetes encryption-decryption kms smartkey security-tools

smartkey-kubernetes-webhook's Introduction

SmartKey Mutate Webhook for Kubernetes Secrets

This is an alpha webhook that mutates Kubernetes secrets to encrypt its content using an encryption key from SmartKey. When a pod wants to use a secret, the mutate webhook injects an init container that decrypts the secrets with SmartKey and stores them in a shared volume in memory with the pod. All data secrets are available under the /smk/secrets/ folder.


Requirements

  • An SmartKey account
  • Kubernetes v1.16 or higher
  • Ensure that the MutatingAdmissionWebhook controller is enabled
  • Ensure that the admissionregistration.k8s.io/v1 API is enabled

If you can run the following command without getting any error, you're good to go:

kubectl get mutatingwebhookconfigurations

How to Install the Webhook

Before you start, you need to get the SmartKey setting values for the credentials secret below:

  • Use the endpoint URL from your SmartKey account for the SMARTKEY_URL setting.
  • Generate a (or use an existing) API key from your account for the SMARTKEY_API_KEY setting.
  • Generate a (or use an existing) security object, and take note of the object UUID for the SMARTKEY_OBJECT_UUID setting.

Clone this repository locally, and run the following commands using the setting values from above:

cd smartkey-kubernetes-webhook
kubectl create namespace smartkey-vault
kubectl create secret generic smk-credentials \
 --from-literal=SMARTKEY_URL='https://eu.smartkey.io' \
 --from-literal=SMARTKEY_OBJECT_UUID='{YOUR_SECURITY_OBJECT_UUID}' \
 --from-literal=SMARTKEY_API_KEY='{YOUR_API_KEY}' \
 -n smartkey-vaul --dry-run=client -o yaml | kubectl apply -f -
./deploy.sh

Confirm that the webhook is running:

kubectl get mutatingwebhookconfigurations
kubectl describe mutatingwebhookconfiguration smartkey-webhook
kubectl get all -n smartkey-vault

How to Encrypt Secrets

Label a Namespace

The mutate webhook only works if your namespace has the proper label.

To label a namespace, run the following command:

kubectl label namespace default smartkey-vault=enabled

Generate a Secret

Creating a secret doesn't change, you simply run a command in the cluster like the following one:

kubectl create secret generic db-user-pass \
 --from-file=./username.txt --from-file=./password.txt \
 --dry-run=client -o yaml | kubectl apply -f -

If you try to get the values from the secret, they're still encoded in base64 but its content is encrypted.

kubectl get secret db-user-pass -o json
echo ENCODEDVALUE | base64 --decode

Include SmartKey Annotations

Once you create the secret, the only way to decrypt it is by including the annotations in the pod referencing the scret name, like this:

  annotations:
    smartkey.io/agent-secret-credentials: "db-user-pass"

Based on the above anotation, the name of the secret is db-user-pass and the name of the folder available in the container will be credentials.

What does that mean? When you create a POD like the following one:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
  annotations:
    smartkey.io/agent-secret-credentials: "db-user-pass"
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

You can inspect the init container logs in case something fails or you want to confirm that everything worked.

kubectl logs nginx -c smk-decrypt-init

You can get access to the secrets at /smk/secrets/credentials/, you can give it a try by running the following commands:

kubectl exec nginx -c nginx -- ls -la /smk/secrets
kubectl exec nginx -c nginx -- ls -la /smk/secrets/credentials
kubectl exec nginx -c nginx -- cat /smk/secrets/credentials/password.txt
kubectl exec nginx -c nginx -- cat /smk/secrets/credentials/username.txt

How to Uninstall the Webhook

Simply run the following command:

kubectl delete -f deployment/

Roadmap

  • Cover more use cases for secrets configuration besides the data type
  • Provide the option to use secrets in environment variables (although it's not recommended)
  • Support for automatically decrypt a secret when the data changes

smartkey-kubernetes-webhook's People

Contributors

chrismld avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

jmarhee wen-mei

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.