|
| 1 | +--- |
| 2 | +title:"Using secrets with GitOps" |
| 3 | +description:"Store secrets in Git with Bitnami sealed secrets" |
| 4 | +group:yaml-examples |
| 5 | +sub_group:examples |
| 6 | +toc:true |
| 7 | +--- |
| 8 | + |
| 9 | +##Prerequisites |
| 10 | + |
| 11 | +- A[free Codefresh account](https://codefresh.io/docs/docs/getting-started/create-a-codefresh-account/) |
| 12 | +- A Kubernetes cluster |
| 13 | +- The[Codefresh GitOps agent]({{site.baseurl}}/docs/integrations/argo-cd/) installed on the cluster |
| 14 | + |
| 15 | +##Using the Bitnami Sealed secrets controller |
| 16 | + |
| 17 | +If you follow[GitOps](https://codefresh.io/gitops/), then you should already know that everything should be placed under source control, and Git is to be used as the single source of truth. |
| 18 | + |
| 19 | +This presents a challenge with secrets that are needed by the application, as they must never be stored in Git in clear text under any circumstance. |
| 20 | + |
| 21 | +To solve this issue, we can use the[Bitnami Sealed secrets controller](https://github.com/bitnami-labs/sealed-secrets). This is a Kubernetes controller |
| 22 | +that can be used to encrypt/decrypt your application secrets in a secure way. |
| 23 | + |
| 24 | +The order of events is the following: |
| 25 | + |
| 26 | +1. You install the Bitnami Sealed secrets controller in the cluster. It generates a public and private key. The private key stays in the cluster and never gets out |
| 27 | +1. You take a raw secret and use the`kubeseal` utility to encrypt it. Encryption happens with the public key of the cluster that you can give to anybody. |
| 28 | +1. The encrypted secrets are stored in Git. There are safe to be committed and nobody can decrypt them without direct access to the cluster |
| 29 | +1. During runtime you deploy the sealed secret like any other Kubernetes manifest. The controller converts them to[plain Kubernetes secrets](https://kubernetes.io/docs/concepts/configuration/secret/) on the fly using the private key of the cluster |
| 30 | +1. Your application reads the secrets like any other Kubernetes secret. Your application doesn't need to know anything about the sealed secrets controller or how the encryption decryption works. |
| 31 | + |
| 32 | + |
| 33 | +To use the controller first install it in your cluster |
| 34 | + |
| 35 | +``` |
| 36 | +helm repo add sealed-secrets https://bitnami-labs.github.io/sealed-secrets |
| 37 | +helm repo update |
| 38 | +helm install sealed-secrets-controller sealed-secrets/sealed-secrets |
| 39 | +``` |
| 40 | + |
| 41 | +By default the controller will be installed at the`kube-system` namespace. The namespace |
| 42 | +and release name are important, since if you change the defaults, you need to set them up |
| 43 | +with`kubeseal` as well as you work with secrets |
| 44 | + |
| 45 | +Download the`kubeseal` CLI. |
| 46 | + |
| 47 | +``` |
| 48 | +wget https://github.com/bitnami-labs/sealed-secrets/releases/download/v0.16.0/kubeseal-linux-amd64 -O kubeseal |
| 49 | +sudo install -m 755 kubeseal /usr/local/bin/kubeseal |
| 50 | +``` |
| 51 | + |
| 52 | +##The example application |
| 53 | + |
| 54 | +You can find the example project at[https://github.com/codefresh-contrib/gitops-secrets-sample-app](https://github.com/codefresh-contrib/gitops-secrets-sample-app). |
| 55 | + |
| 56 | +It is a web application that prints out several secrets which are[read from the filesystem](https://github.com/codefresh-contrib/gitops-secrets-sample-app/blob/main/settings.ini): |
| 57 | + |
| 58 | +`settings.ini` |
| 59 | +```ini |
| 60 | +[security] |
| 61 | +# Path to key pair |
| 62 | +private_key = /secrets/sign/key.private |
| 63 | +public_key= /secrets/sign/key.pub |
| 64 | + |
| 65 | +[paypal] |
| 66 | +paypal_url = https://development.paypal.example.com |
| 67 | +paypal_cert=/secrets/ssl/paypal.crt |
| 68 | + |
| 69 | +[mysql] |
| 70 | +db_con= /secrets/mysql/connection |
| 71 | +db_user = /secrets/mysql/username |
| 72 | +db_password = /secrets/mysql/password |
| 73 | +``` |
| 74 | + |
| 75 | +The application itself knows nothing about Kubernetes secrets, mounted volumes or any other cluster resource. It only reads its own filesystem at`/secrets` |
| 76 | + |
| 77 | +This folder is populated inside the pod with[secret mounting](https://github.com/codefresh-contrib/gitops-secrets-sample-app/blob/main/manifests/deployment.yml): |
| 78 | + |
| 79 | +```yaml |
| 80 | +--- |
| 81 | +apiVersion:apps/v1 |
| 82 | +kind:Deployment |
| 83 | +metadata: |
| 84 | +name:gitops-secrets-deploy |
| 85 | +spec: |
| 86 | +replicas:1 |
| 87 | +selector: |
| 88 | +matchLabels: |
| 89 | +app:gitops-secrets-app |
| 90 | +template: |
| 91 | +metadata: |
| 92 | +labels: |
| 93 | +app:gitops-secrets-app |
| 94 | +spec: |
| 95 | +containers: |
| 96 | + -name:gitops-secrets-app |
| 97 | +image:docker.io/kostiscodefresh/gitops-secrets-sample-app:latest |
| 98 | +imagePullPolicy:Always |
| 99 | +ports: |
| 100 | + -containerPort:8080 |
| 101 | +volumeMounts: |
| 102 | + -name:mysql |
| 103 | +mountPath:"/secrets/mysql" |
| 104 | +readOnly:true |
| 105 | + -name:paypal |
| 106 | +mountPath:"/secrets/ssl" |
| 107 | +readOnly:true |
| 108 | + -name:sign-keys |
| 109 | +mountPath:"/secrets/sign/" |
| 110 | +readOnly:true |
| 111 | +livenessProbe: |
| 112 | +httpGet: |
| 113 | +path:/health |
| 114 | +port:8080 |
| 115 | +readinessProbe: |
| 116 | +httpGet: |
| 117 | +path:/health |
| 118 | +port:8080 |
| 119 | +volumes: |
| 120 | + -name:mysql |
| 121 | +secret: |
| 122 | +secretName:mysql-credentials |
| 123 | + -name:paypal |
| 124 | +secret: |
| 125 | +secretName:paypal-cert |
| 126 | + -name:sign-keys |
| 127 | +projected: |
| 128 | +sources: |
| 129 | + -secret: |
| 130 | +name:key-private |
| 131 | + -secret: |
| 132 | +name:key-public |
| 133 | + |
| 134 | +``` |
| 135 | + |
| 136 | +This way there is a clear separation of concerns. |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +You can find the secrets themselves at[https://github.com/codefresh-contrib/gitops-secrets-sample-app/tree/main/never-commit-to-git/unsealed_secrets](https://github.com/codefresh-contrib/gitops-secrets-sample-app/tree/main/never-commit-to-git/unsealed_secrets). There are encoded with base64 so they are**NOT** safe to commit in Git. |
| 141 | + |
| 142 | +>Note that for demonstration reasons the Git repository contains raw secrets so that you can encrypt them yourself. In a production application the Git repository must only contain sealed/encrypted secrets |
| 143 | +
|
| 144 | +##Preparing the secrets |
| 145 | + |
| 146 | +The critical point of this application is to encrypt all the secrets and place them in Git. |
| 147 | +By default, the sealed secrets controller will encrypt a secret according to a specific namespace (this behavior is configurable) so you need to decide in advance what namespace wil host the application. |
| 148 | + |
| 149 | +Then encrypt all secrets as below: |
| 150 | + |
| 151 | +``` |
| 152 | +kubectl create ns git-secrets |
| 153 | +cd safe-to-commit/sealed_secrets |
| 154 | +kubeseal -n git-secrets < ../../never-commit-to-git/unsealed_secrets/db-creds.yml > db-creds.json |
| 155 | +kubeseal -n git-secrets < ../../never-commit-to-git/unsealed_secrets/key-private.yml > key-private.json |
| 156 | +kubeseal -n git-secrets < ../../never-commit-to-git/unsealed_secrets/key-public.yml > key-public.json |
| 157 | +kubeseal -n git-secrets < ../../never-commit-to-git/unsealed_secrets/paypal-cert.yml > paypal-cert.json |
| 158 | +kubectl apply -f . -n git-secrets |
| 159 | +
|
| 160 | +``` |
| 161 | + |
| 162 | +You now have encrypted your plain secrets. These files are safe to commit to Git. |
| 163 | +You can see that they have been converted automatically to plain secrets with the command |
| 164 | + |
| 165 | +``` |
| 166 | +kubectl get secrets -n git-secrets |
| 167 | +``` |
| 168 | + |
| 169 | +##Deploying manually the application |
| 170 | + |
| 171 | +Note that the application requires all secrets to be present: |
| 172 | + |
| 173 | +``` |
| 174 | +cd safe-to-commit/manifests |
| 175 | +kubectl apply -f . -n git-secrets |
| 176 | +``` |
| 177 | + |
| 178 | +You can now visit the application url to see how it has access to all the secrets. |
| 179 | + |
| 180 | + |
| 181 | +##Deploying the application with Codefresh GitOps |
| 182 | + |
| 183 | +Of course the big advantage of having everything committed into Git, is the ability to adopt GitOps |
| 184 | +for the whole application (including secrets). |
| 185 | + |
| 186 | +This means that you can simply[point Codefresh GitOps to your repository]({{site.baseurl}}/docs/integrations/argo-cd/#creating-argocd-applications) and have the application |
| 187 | +automatically deploy in the cluster. |
| 188 | + |
| 189 | +{% include image.html |
| 190 | +lightbox="true" |
| 191 | +file="/images/examples/sealed-secrets/add-app.png" |
| 192 | +url="/images/examples/sealed-secrets/add-app.png" |
| 193 | +alt="Creating a GitOps application" |
| 194 | +caption="Creating a GitOps application" |
| 195 | +max-width="50%" |
| 196 | +%} |
| 197 | + |
| 198 | +You can then see the application in the GitOps dashboard: |
| 199 | + |
| 200 | +{% include image.html |
| 201 | +lightbox="true" |
| 202 | +file="/images/examples/sealed-secrets/current-state.png" |
| 203 | +url="/images/examples/sealed-secrets/current-state.png" |
| 204 | +alt="GitOps dashboard" |
| 205 | +caption="GitOps dashboard" |
| 206 | +max-width="90%" |
| 207 | +%} |
| 208 | + |
| 209 | +If you visit its URL you will |
| 210 | +see the loading of secrets: |
| 211 | + |
| 212 | +{% include image.html |
| 213 | +lightbox="true" |
| 214 | +file="/images/examples/sealed-secrets/app-secrets.png" |
| 215 | +url="/images/examples/sealed-secrets/app-secrets.png" |
| 216 | +alt="Application using secrets" |
| 217 | +caption="Application using secrets" |
| 218 | +max-width="90%" |
| 219 | +%} |
| 220 | + |
| 221 | + |
| 222 | +>Note that for simplicity reasons the same Git repository holds both the application source code and its |
| 223 | +manifests. In a real application you should have two Git repositories (one of the source code only and one of the manifests). |
| 224 | + |
| 225 | + |
| 226 | +##What to Read Next |
| 227 | + |
| 228 | +-[Codefresh GitOps]({{site.baseurl}}/docs/ci-cd-guides/gitops-deployments/) |
| 229 | +-[Using secrets]({{site.baseurl}}/docs/configure-ci-cd-pipeline/secrets-store/) |
| 230 | +-[Secrets with Mozilla Sops]({{site.baseurl}}/docs/yaml-examples/examples/decryption-with-mozilla-sops/) |
| 231 | +-[Vault Secrets in the Pipeline]({{site.baseurl}}/docs/yaml-examples/examples/vault-secrets-in-the-pipeline/) |
| 232 | + |