|
| 1 | +--- |
| 2 | +id: vault |
| 3 | +sidebar_label: Vault |
| 4 | +--- |
| 5 | +# Vault |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +This guide will show you that KCL solves the secret management problem by integrating [Vault](https://developer.hashicorp.com/vault) and [Vals](https://github.com/helmfile/vals). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | ++ Install [KCL](/docs/user_docs/getting-started/install) |
| 14 | ++ Prepare a [Kubernetes Cluster](https://kubernetes.io/) |
| 15 | ++ Install [Vault](https://developer.hashicorp.com/vault/downloads) |
| 16 | ++ Install [Vals](https://github.com/helmfile/vals) |
| 17 | + |
| 18 | +## How to |
| 19 | + |
| 20 | +### 1. Get the Example |
| 21 | + |
| 22 | +We put the application source code and infrastructure deployment code in different repos, which can be maintained by different roles to achieve the separation of concerns. |
| 23 | + |
| 24 | ++ Get the application code |
| 25 | + |
| 26 | +```shell |
| 27 | +git clone https://github.com/kcl-lang/kcl-lang.io.git/ |
| 28 | +cd ./kcl-lang.io/examples/secret-management/vault |
| 29 | +``` |
| 30 | + |
| 31 | ++ Show the config |
| 32 | + |
| 33 | +```shell |
| 34 | +cat main.k |
| 35 | +``` |
| 36 | + |
| 37 | +The output is |
| 38 | + |
| 39 | +```python |
| 40 | +# Secret Management using Vault and Vals |
| 41 | + |
| 42 | +apiVersion = "apps/v1" |
| 43 | +kind = "Deployment" |
| 44 | +metadata = { |
| 45 | + name = "nginx" |
| 46 | + labels.app = "nginx" |
| 47 | + annotations: { |
| 48 | + "secret-store": "vault" |
| 49 | + # Valid format: |
| 50 | + # "ref+vault://PATH/TO/KV_BACKEND#/KEY" |
| 51 | + "foo": "ref+vault://secret/foo#/foo" |
| 52 | + "bar": "ref+vault://secret/bar#/bar" |
| 53 | + } |
| 54 | +} |
| 55 | +spec = { |
| 56 | + replicas = 3 |
| 57 | + selector.matchLabels = metadata.labels |
| 58 | + template.metadata.labels = metadata.labels |
| 59 | + template.spec.containers = [ |
| 60 | + { |
| 61 | + name = metadata.name |
| 62 | + image = "${metadata.name}:1.14.2" |
| 63 | + ports = [{ containerPort = 80 }] |
| 64 | + } |
| 65 | + ] |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +The main.k file extends the configuration of the Nginx application and customizes annotations. Among them, the value of annotation `foo` and `bar` follow secret reference format (`ref+vault://PATH/TO/KV_BACKEND#/KEY`): |
| 70 | + |
| 71 | ++ `ref+vault`: indicates that this is a secret reference, and the external storage service is `Vault`. |
| 72 | ++ `PATH/TO/KV_BACKEND`: specifies the path where a secret is stored. |
| 73 | ++ `KEY`: specifies the key to reading secret. |
| 74 | + |
| 75 | +The complete format is concatenated using a style similar to URI expressions, which can retrieve a secret stored externally. |
| 76 | + |
| 77 | +### 2. Pre-store Secrets |
| 78 | + |
| 79 | +Start the Vault Server |
| 80 | + |
| 81 | +```shell |
| 82 | +vault server -dev |
| 83 | +export VAULT_ADDR='http://127.0.0.1:8200' |
| 84 | +# Note: Replace with your token |
| 85 | +export VAULT_TOKEN=yourtoken |
| 86 | +``` |
| 87 | + |
| 88 | +After Vault is started in development mode and unpacked, secrets are pre-stored, and the path and keys are consistent with `main.k`: |
| 89 | + |
| 90 | +```shell |
| 91 | +vault kv put secret/foo foo=foo |
| 92 | +vault kv put secret/bar bar=bar |
| 93 | +``` |
| 94 | + |
| 95 | +### 3. Deploy Configuration |
| 96 | + |
| 97 | +Using the following command to apply the deployment manifest. |
| 98 | + |
| 99 | +```shell |
| 100 | +kcl main.k | vals eval -f - | kubectl apply -f - |
| 101 | +``` |
| 102 | + |
| 103 | +The expect output is |
| 104 | + |
| 105 | +```shell |
| 106 | +deployment.apps/nginx created |
| 107 | +``` |
| 108 | + |
| 109 | +### 4. Verify Secrets |
| 110 | + |
| 111 | +Next, verify that the secrets have been retrieved from Vault and replace the values of annotations of Nginx: |
| 112 | + |
| 113 | ++ Verify the `foo` annotation |
| 114 | + |
| 115 | +```shell |
| 116 | +kubectl get deploy nginx -o yaml | grep 'foo:' |
| 117 | +``` |
| 118 | + |
| 119 | +The output is |
| 120 | + |
| 121 | +```yaml |
| 122 | + foo: foo |
| 123 | +``` |
| 124 | +
|
| 125 | ++ Verify the `bar` annotation |
| 126 | + |
| 127 | +```shell |
| 128 | +kubectl get deploy nginx -o yaml | grep 'bar:' |
| 129 | +``` |
| 130 | + |
| 131 | +The output is |
| 132 | + |
| 133 | +```yaml |
| 134 | + bar: bar |
| 135 | +``` |
| 136 | + |
| 137 | +So far, we have retrieved the secrets hosted in `Vault` and put them into use. |
| 138 | + |
| 139 | +## Summary |
| 140 | + |
| 141 | +This guide introduces how KCL solves the secret management by integrating Vault and Vals. By following these steps, we can retrieve the secrets hosted in Vault and utilize them. |
0 commit comments