|
1 | | -# How to Automate Kubernetes Secrets Encryption with Runme |
| 1 | +# Securing Kubernetes Secrets with Sealed Secrets and Runme |
2 | 2 |
|
3 | | -The default [Kubernetes Secrets](https://kubernetes.io/docs/concepts/configuration/secret/) are stored either as plaintext or base64 encoded. These secrets should not be sent to version control systems like git due to the security risk of exposing sensitive data such as API keys, passwords, or tokens. |
| 3 | +Runme provides a platform for documenting processes and instructions. It integrates swiftly with cloud infrastructures, including Kubernetes and its resources. |
4 | 4 |
|
5 | | -In cases where a developer or an infrastructure-focused engineer needs to add secrets to the cluster or version control, they should use encryption-first tools. These tools securely share and push these secrets to version control. Tools like [sops](https://fluxcd.io/flux/guides/mozilla-sops/) and [sealed-secrets](https://archive.eksworkshop.com/beginner/200_secrets/installing-sealed-secrets/) are commonly used by DevOps and SREs. |
| 5 | +This guide will explore one such resource, Kubernetes secrets, with a specific focus on Sealed Secrets. |
6 | 6 |
|
7 | | -With Runme, Platform Engineers can document how secrets are added to a Kubernetes cluster, ensuring the commands and guides are repeatable, consistent, and executable. Runme also enables engineers to create runbooks as documentation. This allows other team members, including new ones, to add and encrypt secrets correctly without leaking data and causing a significant security breach. |
| 7 | +[Sealed Secrets](https://archive.eksworkshop.com/beginner/200_secrets/installing-sealed-secrets/) is an open-source project that helps encrypt Kubernetes secrets, which can then be securely stored in your version control. Runme makes securing these secrets easier. |
8 | 8 |
|
9 | | -The guide below explains how to use [Runme](https://github.com/stateful/blog-examples/blob/main/kubernetes/k8s-secret/sops/sops-mac.md) as your central knowledge hub for automating Kubernetes secrets encryption. It will make your documentation the go-to source for all infrastructure operations. |
| 9 | +In this guide, we will demonstrate the steps required to encrypt Kubernetes secrets with Sealed Secrets in Runme successfully. |
10 | 10 |
|
11 | | -## **Prerequisites** |
| 11 | +## **Prerequisites**[](https://docs-runme-55rq3q1vz-stateful.vercel.app/guide/k8s-secret#prerequisites) |
12 | 12 |
|
13 | | -- An AWS account with privileges to create an [IAM User](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users.html) and a [KMS Key](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#kms_keys). |
14 | | -- [AWS CLI](https://docs.aws.amazon.com/cli/v1/userguide/cli-chap-install.html) installed and configured. |
15 | | -- Kubernetes Cluster: A running Kubernetes cluster is required. For this guide, we will use [kind](https://kind.sigs.k8s.io/docs/user/quick-start/) for our Kubernetes cluster. |
16 | | -- [Kubectl](https://kubernetes.io/docs/tasks/tools/): The Kubernetes command-line tool should be installed on your machine. |
| 13 | +To get started, ensure you have the following: |
17 | 14 |
|
18 | | -Here is a [notebook](https://github.com/stateful/blog-examples/blob/main/kubernetes/k8s-secret/sops/sops-mac.md) to help you install these prerequisites. For this guide, we are using either Linux or Mac OS. |
| 15 | +- **Clone the repository**: We created a [notebook repository](https://github.com/stateful/blog-examples/tree/main/kubernetes/k8s-secret/sealed-secret) containing all the instructions and commands required for this guide. |
| 16 | +- **Install Runme**: Install the [Runme extension on VS Code](https://marketplace.visualstudio.com/items?itemName=stateful.runme) and set it as your [default Markdown viewer.](https://docs.runme.dev/installation/installrunme#how-to-set-vs-code-as-your-default-markdown-viewer) |
19 | 17 |
|
20 | | -## **Securing Secrets with SOPS** |
| 18 | +This guide will focus on using the Mac specifications. If you use a Linux OS, follow the instructions in the [Linux Markdown files.](https://github.com/stateful/blog-examples/blob/main/kubernetes/k8s-secret/sealed-secret/linux-sealedsecrets.md) |
21 | 19 |
|
22 | | -You need enhanced security and access to a cloud provider to encrypt your Kubernetes secrets using SOPS. It also requires a [Key Management Service](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#kms_keys) (KMS); for this guide, we will use an AWS KMS key. This AWS service offers secure key management, key rotation, access control, auditing, and compatibility with cloud platforms. |
| 20 | +### Install all Dependencies |
23 | 21 |
|
24 | | -Here is an example [notebook](https://github.com/stateful/blog-examples/blob/main/kubernetes/k8s-secret/sops/sops-mac.md) to follow this tutorial. |
25 | | - |
26 | | -### **Installation of SOPS** |
27 | | - |
28 | | -**Step 1: Download SOPS Binary** |
29 | | - |
30 | | -To download SOPS binary to your environment, click the execute cell button in your Runme Markdown file. |
| 22 | +To follow up on securing your secrets using Sealed Secrets, ensure you install the necessary dependencies in the notebook's prerequisite section. In your Runme cell, run the commands below to install all dependencies required for this guide. |
31 | 23 |
|
32 | 24 | <video autoPlay loop muted playsInline controls> |
33 | | - <source src="/videos/runme-envprompt-k8s.mp4" type="video/mp4" /> |
34 | | - <source src="/videos/runme-envprompt-k8s.webm" type="video/webm" /> |
| 25 | + <source src="/videos/sealed-secrets-runme.mp4" type="video/mp4" /> |
| 26 | + <source src="/videos/sealed-secrets-runme.webm" type="video/webm" /> |
35 | 27 | </video> |
36 | 28 |
|
37 | | -You can also use the Runme [environment prompt](https://docs.runme.dev/getting-started/features#environment-variable-prompts) feature to download this; all you need to do is replace `{version}` with your current SOPS version and `{platform}` with your platform (e.g., *Darwin* for macOS, *Linux* for Linux). |
38 | | - |
39 | | -The env prompt uses the export commands available in Unix-like operating systems (such as Linux and macOS) to set the environment variable. |
40 | | - |
41 | | -<br /> |
42 | | -<Infobox type="sidenote" title="Note"> |
43 | | - |
44 | | -You don’t need to input the environment variable again once the values have been inputted; other code cells within the notebook can use this data unless you reset the session. |
45 | | - |
46 | | -</Infobox> |
47 | | - |
48 | | -### **Create a KMS Key** |
49 | | - |
50 | | -You need to create a [KMS key](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#kms_keys) to encrypt and decrypt your secrets. To do this, we will create a KMS key in AWS. You can create a KMS key with a specific name by using the `—-description` option followed by the key's name, for example, `runme-key`. |
| 29 | +### **Encrypt a Secret in Runme**[](https://docs-runme-55rq3q1vz-stateful.vercel.app/guide/k8s-secret#encrypt-a-secret) |
51 | 30 |
|
52 | | -To extract the value associated with the `KeyId` field, use `jq -r` to parse the JSON input from the `aws kms create-key` command. |
| 31 | +To encrypt a secret, you must create a Kubernetes secret using `kubeseal`. Navigate to your cloned repo's ‘Encrypt a Secret’ section and [choose your preferred encryption method from the options provided.](https://github.com/stateful/blog-examples/blob/main/kubernetes/k8s-secret/sealed-secret/Mac-sealedsecret.md#encrypt-a-secret) Once that is done, run the command below in your Runme cell. |
53 | 32 |
|
54 | | -You can transfer the stdout result of the last execution to your subsequent execution using the [Chain Cell Output](https://docs.runme.dev/getting-started/features#chain-cell-output) feature. To create an alias, refer to the image below: |
| 33 | +Runme will automatically create a Sealed Secret resource containing the encrypted data, the **`mysealedsecret.yaml`** |
55 | 34 |
|
56 | | - |
| 35 | +### Adding New Value to A Secret |
57 | 36 |
|
58 | | -Lastly, you can save it to the Runme cloud for future use or reference using the Runme [auto-save](https://docs.runme.dev/configuration/auto-save) feature. |
| 37 | +You can add a new value to your secret by navigating to [this section](https://github.com/stateful/blog-examples/blob/main/kubernetes/k8s-secret/sealed-secret/Mac-sealedsecret.md#adding-a-new-value-to-a-sealed-secret) and executing the commands. You only need to update your manifest file with the new values, re-encrypt the secrets, and then reapply them to the cluster. All of these can be executed in your Runme cell. The image below shows a visual representation of this process. |
59 | 38 |
|
60 | | -### **Configure SOPS** |
| 39 | +From the output gotten, you can see that your new value has successfully been added. |
61 | 40 |
|
62 | | -To configure `SOPS`, you must create a configuration file specifying how `SOPS` should encrypt your secrets and what encryption keys to use. Here's how you can do it: |
| 41 | + |
63 | 42 |
|
64 | | -1. Use the first code block to set the variable and input the value. |
65 | | -2. Use the second block to create the `sops.yaml` file. |
66 | | -3. Finally, use the third block to confirm and check if the file was created correctly. |
| 43 | +### Decrypt a Secret[](https://docs-runme-55rq3q1vz-stateful.vercel.app/guide/k8s-secret#decrypt-a-secret) |
67 | 44 |
|
68 | | -The image below provides a visual representation of these processes: |
| 45 | +To retrieve the original version of `runme-secrets.yaml`, you can decrypt the encrypted secret, `mysealedsecret.yaml`. Run the command in the ‘Decrypt a Secret’ section of the repo and run it in your Runme cell. |
69 | 46 |
|
70 | | - |
71 | | - |
72 | | -### **Encrypt Your Secrets** |
73 | | - |
74 | | -To encrypt your sensitive information with ease using SOPS and AWS KMS, follow these steps below: |
75 | | - |
76 | | -1. Set your environment variable |
77 | | -2. Ensure you have your manifest `runme-secrets.yaml` file containing your secrets, if not create one using the following YAML manifest |
78 | | - |
79 | | -```yaml {"id":"01HSTXRCH7SXS2QJRMENCHJ81Y"} |
80 | | -apiVersion: v1 |
81 | | -kind: Secret |
82 | | -metadata: |
83 | | - creationTimestamp: null |
84 | | - name: runme |
85 | | -stringData: |
86 | | - username: runme |
87 | | - password: stateful234 |
| 47 | +```sh |
| 48 | +kubeseal --controller-name=sealed-secrets-controller --controller-namespace=kube-system < mysealedsecret.yaml > mysecrets.yaml |
88 | 49 | ``` |
89 | 50 |
|
90 | | -3. Click the execute cell button |
91 | | -
|
92 | | - |
93 | | -
|
94 | | -Your secrets within `runme-secrets.yaml` will be encrypted and transferred to `runme-secrets-enc.yaml`. |
95 | | - |
96 | | -### **Decrypt Your Secrets** |
97 | | - |
98 | | -Runme Notebook provides clear and concise instructions, ensuring the decryption process is as smooth as encrypting. |
| 51 | +### Delete a Secret |
99 | 52 |
|
100 | | - |
| 53 | +To delete the secret, use the `kubectl` command to delete the resource and run it in your Runme cell. |
101 | 54 |
|
102 | | -Decrypt the secret from a SOPS encrypted secret `runme-secrets-enc.yaml` into the original version before it was encrypted `runme-secrets.yaml` |
103 | | - |
104 | | -### **Apply Encrypted Secret** |
105 | | - |
106 | | -```sh {"id":"01HRSMKKZDED03HJNEATG1D3W9"} |
107 | | -sops -d runme-secrets-enc.yaml | kubectl apply -f - |
| 55 | +```sh |
| 56 | +kubectl delete -f mysealedsecret.yaml |
108 | 57 | ``` |
109 | 58 |
|
110 | | -## **Securing Secrets with Sealed Secrets** |
111 | | - |
112 | | -[Sealed Secrets](https://archive.eksworkshop.com/beginner/200_secrets/installing-sealed-secrets/) is an open-source project developed by Bitnami that helps encrypt Kubernetes secrets, which can then be securely stored in your version control. This allows for the implementation of DevOps practices without compromising sensitive data. It is important to note that only the Sealed Secrets controller can decrypt these encrypted secrets. To use Sealed Secrets, you need to install the Sealed Secrets Controller, also known as ___Kubeseal___. |
113 | | - |
114 | | -To input the value of `version` or `platform`, you can use the [env prompt](https://docs.runme.dev/getting-started/features#environment-variable-prompts). |
115 | | - |
116 | | - |
117 | | - |
118 | | -### **Encrypt a Secret** |
119 | | - |
120 | | -Create a Kubernetes Secret and use **`kubeseal`** to encrypt it |
121 | | - |
122 | | - |
123 | | - |
124 | | -This creates a SealedSecret resource (**`mysealedsecret.yaml`**) containing the encrypted data. |
| 59 | +### **Deploy the Sealed Secret**[](https://docs-runme-55rq3q1vz-stateful.vercel.app/guide/k8s-secret#deploy-the-sealed-secret) |
125 | 60 |
|
126 | | -### Decrypt a Secret |
| 61 | +To deploy your secret, execute the code below: |
127 | 62 |
|
128 | | -To retrieve the original version of `runme-secrets.yaml`, you can decrypt the encrypted secret `mysealedsecret.yaml`. |
129 | | - |
130 | | - |
131 | | - |
132 | | -### **Deploy the Sealed Secret:** |
133 | | - |
134 | | -```sh {"id":"01HRSMKKZDFG16ASX22PJ26RP7"} |
| 63 | +```sh |
135 | 64 | kubectl apply -f mysealedsecret.yaml |
136 | 65 | ``` |
137 | 66 |
|
138 | | -The Sealed Secrets controller will decrypt the SealedSecret and create a regular Kubernetes Secret with the decrypted data. |
139 | | - |
140 | | -Make sure to replace placeholders like **`mysecret.yaml`** and **`mysealedsecret.yaml`** with your secret and sealed secret filenames. Adjust controller-specific details such as the namespace and name according to your environment. |
141 | | - |
142 | | -## **Challenges with Manual Execution** |
143 | | - |
144 | | -While the above processes help you secure your Kubernetes key, manually carrying out these operations can be take time and effort when you have to do this operation occasionally. This is where Runme comes in. |
145 | | - |
146 | | -Runme is a documentation tool that automates manual processes and gives you the time to jump right into a task, execute it, and save time. |
147 | | - |
148 | | -You can quickly eliminate the hassle of learning and implementing secret encryption in Kubernetes every time you need it, Runme will help you document your process |
| 67 | + |
149 | 68 |
|
150 | | -## **Improve Documentation Experience with Runme Notebook** |
| 69 | +The Sealed Secrets controller will decrypt the Sealed Secret and create a Kubernetes Secret with the decrypted data. |
151 | 70 |
|
152 | | -Previously, we explored securing your Kubernetes secrets using sealed secrets and SOPS. Now, we will walk you through how to automate these processes with a single click right inside your Markdown file. |
| 71 | +Make sure to replace placeholders like **`mysecret.yaml`** and **`mysealedsecret.yaml`** with your secret and Sealed Secret filenames. Adjust controller-specific details such as the namespace and name according to your environment. |
153 | 72 |
|
154 | | -1. Open VS Code on your local machine. Navigate to the extensions tab and search for “Runme” Now, click Install. |
155 | | -2. Create a README file. |
156 | | -3. To execute each of your commands, paste them into the code block in Runme and click the run cell button beside the code block. |
| 73 | +## How Runme Improves Your Documentation Experience |
157 | 74 |
|
158 | | -The image below illustrates how easy it is to download the SOPS binary using Runme. |
| 75 | +In this guide, we explored how to encrypt, decrypt, delete, deploy a Kubernetes secrtes using sealed secrets and Runme. Runme made the process of carrying all these operations out swiftly by utilizing its features. |
159 | 76 |
|
160 | | - |
| 77 | +Some [key features](https://docs.runme.dev/getting-started/features) of Runme that make it stand out include: |
161 | 78 |
|
162 | | -To have a full view of these processes, you can clone this [repo](https://github.com/stateful/blog-examples/blob/main/kubernetes/k8s-secret/sops/sops-mac.md), open it with VS Code on your local machine and click the run cell button to complete your tasks. However, ensure you have installed [Runme](../installation/index.md) first. |
| 79 | +- Its ability to simplify the copying and pasting of commands and codes, |
| 80 | +- Run tasks in the background, interact with your terminal, |
| 81 | +- Render images and tables inside your markdown files, |
| 82 | +- Run code in your preferred language. |
163 | 83 |
|
164 | | -Embrace the Runme Notebook experience to secure your secrets and effortlessly enhance your Kubernetes knowledge. Visit [Runme Documentation](https://docs.runme.dev/) to embark on a guided journey to a more secure Kubernetes environment. |
| 84 | +These are just a few of the things you can achieve in Runme. To explore Runme more, visit the [Runme Documentation](https://docs.runme.dev/), where you can embark on a guided journey to a more secure Kubernetes environment. |
0 commit comments