From 1345ff02e33973849c255ced17670326246b9f78 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 20 May 2025 01:54:17 +0400 Subject: [PATCH 1/5] docs: k8s agent injector --- .../integrations/platforms/kubernetes-csi.mdx | 2 +- .../platforms/kubernetes-injector.mdx | 297 ++++++++++++++++++ 2 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 docs/integrations/platforms/kubernetes-injector.mdx diff --git a/docs/integrations/platforms/kubernetes-csi.mdx b/docs/integrations/platforms/kubernetes-csi.mdx index 88df9585c..a38a37404 100644 --- a/docs/integrations/platforms/kubernetes-csi.mdx +++ b/docs/integrations/platforms/kubernetes-csi.mdx @@ -1,6 +1,6 @@ --- title: "Kubernetes CSI" -description: "How to use Infisical to inject secrets directly into Kubernetes pods." +description: "How to use the Infisical Kubernetes CSI provider to inject secrets directly into Kubernetes pods." --- ## Overview diff --git a/docs/integrations/platforms/kubernetes-injector.mdx b/docs/integrations/platforms/kubernetes-injector.mdx new file mode 100644 index 000000000..175408ed1 --- /dev/null +++ b/docs/integrations/platforms/kubernetes-injector.mdx @@ -0,0 +1,297 @@ +--- +title: "Kubernetes Agent Injector" +description: "How to use the Infisical Kubernetes Agent Injector to inject secrets directly into Kubernetes pods." +--- + +## Overview + +The Infisical Kubernetes Agent Injector allows you to inject secrets directly into your Kubernetes pods. The Injector will create a [Infisical Agent](/integrations/platforms/infisical-agent) container within your pod that syncs secrets from Infisical into a shared volume mount within your pod. + + +The Infisical Agent Injector will patch and modify your pod's deployment to contain an [Infisical Agent](/integrations/platforms/infisical-agent) container which renders your Infisical secrets into a shared volume mount within your pod. + +The Infisical Agent Injector is built on [Kubernetes Mutating Admission Webhooks](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers), and will watch for `CREATE` and `UPDATE` events on pods in your cluster. +The injector is namespace-agnostic, and will watch for pods in any namespace, but will only patch pods that have the `org.infisical.com/inject` annotation set to `true`. + +## Install the Infisical Agent Injector + +To install the Infisical Agent Injector, you will need to install our helm charts using [Helm](https://helm.sh/). + +```bash +helm repo add infisical-helm-charts 'https://dl.cloudsmith.io/public/infisical/helm-charts/helm/charts/' +helm repo update +helm install --generate-name infisical-helm-charts/infisical-agent-injector +``` + +After installing the helm chart you can verify that the injector is running and working as intended by checking the logs of the injector pod. +```bash +$ kubectl logs deployment/infisical-agent-injector +2025/05/19 14:20:05 Starting infisical-agent-injector... +2025/05/19 14:20:05 Generating self-signed certificate... +2025/05/19 14:20:06 Creating directory: /tmp/tls +2025/05/19 14:20:06 Writing cert to: /tmp/tls/tls.crt +2025/05/19 14:20:06 Writing key to: /tmp/tls/tls.key +2025/05/19 14:20:06 Starting HTTPS server on port 8585... +2025/05/19 14:20:06 Attempting to update webhook config (attempt 1)... +2025/05/19 14:20:06 Successfully updated webhook configuration with CA bundle +``` + +## Supported annotations + +The Infisical Agent Injector supports the following annotations: + + + The inject annotation is used to enable the injector on a pod. Set the value to `true` and the pod will be patched with an Infisical Agent container on update or create. + + + The inject mode annotation is used to specify the mode to use to inject the secrets into the pod. Currently only `init` mode is supported. + + - `init`: The init method will create an init container for the pod that will render the secrets into a shared volume mount within the pod. The agent init container will run before any other containers in the pod runs, including other init containers. + + + The agent config map annotation is used to specify the name of the config map that contains the configuration for the injector. The config map must be in the same namespace as the pod. + + +## ConfigMap Configuration + +### Supported Fields + +When you are configuring a pod to use the injector, you must create a config map in the same namespace as the pod you want to inject secrets into. +The entire config needs to be of string format and needs to be assigned to the `config.yaml` key in the config map. You can find a full example of the config at the end of this section. + + + The address of your Infisical instance. This field is optional and will default to `https://app.infisical.com` if not provided. + + + + The authentication type to use to connect to Infisical. Currently only the `kubernetes` authentication type is supported. + You can refer to our [Kubernetes Auth](/documentation/platform/identities/kubernetes-auth) documentation for more information on how to create a machine identity for Kubernetes Auth. + Please note that the pod's default service account will be used to authenticate with Infisical. + + + + The ID of the machine identity to use to connect to Infisical. This field is required if the `infisical.auth.type` is set to `kubernetes`. + + + +The templates hold an array of templates that will be rendered and injected into the pod. + + + + The path to inject the secrets into within the pod. + If not specified, this will default to `/shared/infisical-secrets`. If you have multiple templates and don't provide a destination path, the destination paths will default to `/shared/infisical-secrets-1`, `/shared/infisical-secrets-2`, etc. + + + + The content of the template to render. + This will be rendered as a [Go Template](https://pkg.go.dev/text/template) and will have access to the following variables. + It follows the templating format and supports the same functions as the [Infisical Agent](/integrations/platforms/infisical-agent#quick-start-infisical-agent) + + + +### Authentication +The Infisical Agent Injector only supports Machine Identity [Kubernetes Auth](/documentation/platform/identities/kubernetes-auth) authentication at the moment. + +To configure Kubernetes Auth, you need to set the `auth.type` field to `kubernetes` and set the `auth.config.identity-id` to the ID of the machine identity you wish to use for authentication. + +```yaml +auth: + type: "kubernetes" + config: + identity-id: "" +``` + +### Example ConfigMap +```yaml config-map.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: demo-config-map +data: + config.yaml: | + infisical: + address: "https://app.infisical.com" + auth: + type: "kubernetes" + config: + identity-id: "" + templates: + - destination-path: "/path/to/save/secrets/file.txt" + template-content: | + {{- with secret "" "dev" "/" }} + {{- range . }} + {{ .Key }}={{ .Value }} + {{- end }} + {{- end }} +``` + +```bash +kubectl apply -f config-map.yaml +``` + +To use the config map in your pod, you will need to add the `org.infisical.com/agent-config-map` annotation to your pod's deployment. The value of the annotation is the name of the config map you created above. +```yaml +apiVersion: v1 +kind: Pod +metadata: + name: demo + labels: + app: demo + annotations: + org.infisical.com/inject: "true" # Set to true for the injector to patch the pod on create/update events + org.infisical.com/inject-mode: "init" # The mode to use to inject the secrets into the pod. Currently only `init` mode is supported. + org.infisical.com/agent-config-map: "name-of-config-map" # The name of the config map that you created above, which contains all the settings for injecting the secrets into the pod +spec: + # ... +``` + + + +## Quick Start +In this section we'll walk through a full example of how to inject secrets into a pod using the Infisical Agent Injector. +In this example we'll create a PostgreSQL database deployment and use the Infisical Agent injector to inject the database credentials to be used for the deployment. + +### Create secrets in Infisical +First you'll need to create the secrets in Infisical. + +- `POSTGRES_USER`: The username of the database user. +- `POSTGRES_PASSWORD`: The password to use for the user. +- `POSTGRES_DB`: The name of the database to create. + +Once you've created the secrets, save your project ID, environment slug, and secret path, as these will be used in the next step. + +### Configuration +To use the injector you must create a config map in the same namespace as the pod you want to inject secrets into. In this example we'll create a config map in the `test-namespace` namespace. + +The agent injector will authenticate with Infisical using a [Kubernetes Auth](/documentation/platform/identities/kubernetes-auth) machine identity. Please follow the [instructions](/documentation/platform/identities/kubernetes-auth) to create a machine identity configured for Kubernetes Auth. +The agent injector will use the service account token of the pod to authenticate with Infisical. + +The `template-content` will be rendered as a [Go Template](https://pkg.go.dev/text/template) and will have access to the following variables. It follows the templating format and supports the same functions as the [Infisical Agent](/integrations/platforms/infisical-agent#quick-start-infisical-agent) +The `destination-path` refers to the path within the pod that the secrets will be injected into. In this case we're injecting the secrets into a file called `/infisical/postgres-secrets`. + + +Replace the ``, ``, with your project ID and the environment slug of where you created your secrets in Infisical. Replace `` with the ID of your machine identity configured for Kubernetes Auth. +```yaml config-map.yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: postgres-deployment-infisical-config-map + namespace: test-namespace +data: + config.yaml: | + infisical: + address: "https://app.infisical.com" + auth: + type: "kubernetes" + config: + identity-id: "" + templates: + - destination-path: "/infisical/postgres-secrets" + template-content: | + {{- with secret "" "" "/" }} + {{- range . }} + {{ .Key }}={{ .Value }} + {{- end }} + {{- end }} +``` + +### Injecting secrets into your pod + +To inject secrets into your pod, you will need to add the `org.infisical.com/inject: "true"` annotation to your pod's deployment. + +The `org.infisical.com/agent-config-map` annotation will point to the config map we created in the previous step. It's important that the config map is in the same namespace as the pod. + +We are creating a Postgres deployment with a PVC to store the database data. + +```yaml postgres-deployment.yaml +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: postgres-pvc + namespace: test-namespace +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgres-deployment + namespace: test-namespace + labels: + app: postgres +spec: + replicas: 1 + selector: + matchLabels: + app: postgres + template: + metadata: + labels: + app: postgres + annotations: + org.infisical.com/inject: "true" + org.infisical.com/inject-mode: "init" + org.infisical.com/agent-config-map: "postgres-deployment-infisical-config-map" + spec: + containers: + - name: postgres + image: postgres:latest + command: ["/bin/bash", "-c"] + args: + - | + export $(cat /infisical/postgres-secrets | xargs) + exec docker-entrypoint.sh postgres + ports: + - containerPort: 5432 + volumeMounts: + - name: postgres-data + mountPath: /var/lib/postgresql/data + volumes: + - name: postgres-data + persistentVolumeClaim: + claimName: postgres-pvc +``` + +### Applying the deployment + +To apply the deployment, you can use the following command: + +```bash +kubectl apply -f postgres-deployment.yaml +``` +It may take a few minutes for the pod to be ready and for the secrets to be injected. You can check the status of the pod by running: + +```bash +kubectl get pods -n test-namespace +``` + +### Verifying the secrets are injected + +To verify the secrets are injected, you can check the pod's logs: + +```bash +$ kubectl exec -it deployment/postgres-deployment -n test-namespace -- cat /infisical/postgres-secrets + +Defaulted container "postgres" out of: postgres, infisical-agent-init (init) +POSTGRES_USER=infisical-db-user +POSTGRES_PASSWORD=demo-database-password +POSTGRES_DB=demo-database +``` + +You can also try connecting to the database using the credentials: +```bash +$ kubectl exec -it deployment/postgres-deployment -n test-namespace -- bash -c "PGPASSWORD='' psql -U -d " + +Defaulted container "postgres" out of: postgres, infisical-agent-init (init) +psql (17.5 (Debian 17.5-1.pgdg120+1)) +Type "help" for help. + +infisical-db=# +``` + +If you're able to connect to the database using the credentials from Infisical, then you've successfully injected the secrets into your pod. On each pod update, the secrets will be injected again with the latest values from Infisical. \ No newline at end of file From 7ffa0ef8f593d18275b81960affbeadd455276ef Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 20 May 2025 12:36:14 +0400 Subject: [PATCH 2/5] Update deployment.yaml --- docs/mint.json | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/mint.json b/docs/mint.json index 5335c7423..a81c9db67 100644 --- a/docs/mint.json +++ b/docs/mint.json @@ -441,6 +441,7 @@ "integrations/platforms/kubernetes/infisical-dynamic-secret-crd" ] }, + "integrations/platforms/kubernetes-injector", "integrations/platforms/kubernetes-csi", "integrations/platforms/docker-swarm-with-agent", "integrations/platforms/ecs-with-agent" From 4355fd09cc6d5374607445af3756a6ea4a09db25 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 20 May 2025 16:57:11 +0400 Subject: [PATCH 3/5] requested changes --- .../platforms/kubernetes-injector.mdx | 146 +++++++++--------- 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/docs/integrations/platforms/kubernetes-injector.mdx b/docs/integrations/platforms/kubernetes-injector.mdx index 175408ed1..a94e6e9cf 100644 --- a/docs/integrations/platforms/kubernetes-injector.mdx +++ b/docs/integrations/platforms/kubernetes-injector.mdx @@ -146,19 +146,16 @@ spec: ``` - ## Quick Start In this section we'll walk through a full example of how to inject secrets into a pod using the Infisical Agent Injector. -In this example we'll create a PostgreSQL database deployment and use the Infisical Agent injector to inject the database credentials to be used for the deployment. +In this example we'll create a basic nginx deployment and print a Infisical secret called `API_KEY` to the container logs. ### Create secrets in Infisical -First you'll need to create the secrets in Infisical. +First you'll need to create the secret in Infisical. -- `POSTGRES_USER`: The username of the database user. -- `POSTGRES_PASSWORD`: The password to use for the user. -- `POSTGRES_DB`: The name of the database to create. +- `API_KEY`: The API key to use for the nginx deployment. -Once you've created the secrets, save your project ID, environment slug, and secret path, as these will be used in the next step. +Once you've created the secret, save your project ID, environment slug, and secret path, as these will be used in the next step. ### Configuration To use the injector you must create a config map in the same namespace as the pod you want to inject secrets into. In this example we'll create a config map in the `test-namespace` namespace. @@ -167,7 +164,7 @@ The agent injector will authenticate with Infisical using a [Kubernetes Auth](/d The agent injector will use the service account token of the pod to authenticate with Infisical. The `template-content` will be rendered as a [Go Template](https://pkg.go.dev/text/template) and will have access to the following variables. It follows the templating format and supports the same functions as the [Infisical Agent](/integrations/platforms/infisical-agent#quick-start-infisical-agent) -The `destination-path` refers to the path within the pod that the secrets will be injected into. In this case we're injecting the secrets into a file called `/infisical/postgres-secrets`. +The `destination-path` refers to the path within the pod that the secrets will be injected into. In this case we're injecting the secrets into a file called `/infisical/secrets`. Replace the ``, ``, with your project ID and the environment slug of where you created your secrets in Infisical. Replace `` with the ID of your machine identity configured for Kubernetes Auth. @@ -175,7 +172,7 @@ Replace the ``, ``, with your project ID apiVersion: v1 kind: ConfigMap metadata: - name: postgres-deployment-infisical-config-map + name: nginx-infisical-config-map namespace: test-namespace data: config.yaml: | @@ -186,7 +183,7 @@ data: config: identity-id: "" templates: - - destination-path: "/infisical/postgres-secrets" + - destination-path: "/infisical/secrets" template-content: | {{- with secret "" "" "/" }} {{- range . }} @@ -201,60 +198,31 @@ To inject secrets into your pod, you will need to add the `org.infisical.com/inj The `org.infisical.com/agent-config-map` annotation will point to the config map we created in the previous step. It's important that the config map is in the same namespace as the pod. -We are creating a Postgres deployment with a PVC to store the database data. +We are creating a nginx deployment with a PVC to store the database data. -```yaml postgres-deployment.yaml +```yaml nginx.yaml --- apiVersion: v1 -kind: PersistentVolumeClaim +kind: Pod metadata: - name: postgres-pvc - namespace: test-namespace -spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 1Gi ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: postgres-deployment + name: nginx-pod namespace: test-namespace labels: - app: postgres + app: nginx + annotations: + org.infisical.com/inject: "true" + org.infisical.com/inject-mode: "init" + org.infisical.com/agent-config-map: "nginx-infisical-config-map" spec: - replicas: 1 - selector: - matchLabels: - app: postgres - template: - metadata: - labels: - app: postgres - annotations: - org.infisical.com/inject: "true" - org.infisical.com/inject-mode: "init" - org.infisical.com/agent-config-map: "postgres-deployment-infisical-config-map" - spec: - containers: - - name: postgres - image: postgres:latest - command: ["/bin/bash", "-c"] - args: - - | - export $(cat /infisical/postgres-secrets | xargs) - exec docker-entrypoint.sh postgres - ports: - - containerPort: 5432 - volumeMounts: - - name: postgres-data - mountPath: /var/lib/postgresql/data - volumes: - - name: postgres-data - persistentVolumeClaim: - claimName: postgres-pvc + containers: + - name: simple-app-demo + image: nginx:alpine + command: ["/bin/sh", "-c"] + args: + - | + export $(cat /infisical/secrets | xargs) + echo "API_KEY is set to: $API_KEY" + nginx -g "daemon off;" ``` ### Applying the deployment @@ -262,9 +230,9 @@ spec: To apply the deployment, you can use the following command: ```bash -kubectl apply -f postgres-deployment.yaml +kubectl apply -f nginx.yaml ``` -It may take a few minutes for the pod to be ready and for the secrets to be injected. You can check the status of the pod by running: +It may take a few minutes for the pod to be ready and for the Infisical secrets to be injected. You can check the status of the pod by running: ```bash kubectl get pods -n test-namespace @@ -275,23 +243,55 @@ kubectl get pods -n test-namespace To verify the secrets are injected, you can check the pod's logs: ```bash -$ kubectl exec -it deployment/postgres-deployment -n test-namespace -- cat /infisical/postgres-secrets +$ kubectl exec -it pod/nginx-pod -n test-namespace -- cat /infisical/secrets -Defaulted container "postgres" out of: postgres, infisical-agent-init (init) -POSTGRES_USER=infisical-db-user -POSTGRES_PASSWORD=demo-database-password -POSTGRES_DB=demo-database +Defaulted container "simple-app-demo" out of: simple-app-demo, infisical-agent-init (init) + +API_KEY=sk_api_... # The secret you created in Infisical ``` -You can also try connecting to the database using the credentials: +Additionally you can now check that the `API_KEY` secret is being logged to the nginx container logs: ```bash -$ kubectl exec -it deployment/postgres-deployment -n test-namespace -- bash -c "PGPASSWORD='' psql -U -d " - -Defaulted container "postgres" out of: postgres, infisical-agent-init (init) -psql (17.5 (Debian 17.5-1.pgdg120+1)) -Type "help" for help. - -infisical-db=# +$ kubectl logs pod/nginx-pod -n test-namespace +Defaulted container "simple-app-demo" out of: simple-app-demo, infisical-agent-init (init) +API_KEY is set to: sk_api_... # The secret you created in Infisical ``` -If you're able to connect to the database using the credentials from Infisical, then you've successfully injected the secrets into your pod. On each pod update, the secrets will be injected again with the latest values from Infisical. \ No newline at end of file + +## Troubleshooting + + + + If the pod is stuck in `Init` state, it means the Agent init container is failing to start or is stuck in a restart loop. + This could be due to a number of reasons, such as the machine identity not having the correct permissions, or trying to fetch secrets from a non-existent project/environment. + + You can check the logs of the infisical init container by running: + ```bash + # For deployments + kubectl logs deployment/your-deployment-name -c infisical-agent-init -n "" + + # For pods + kubectl logs pod/your-pod-name -c infisical-agent-init -n "" + ``` + + You can also check the logs of the pod by running: + ```bash + kubectl logs deployment/postgres-deployment -n test-namespace + ``` + + When checking the logs of the agent init container, you may see something like the following: + ```bash + Starting infisical agent... + 11:10AM INF starting Infisical agent... + 11:10AM INF Infisical instance address set to https://daniel1.tunn.dev + 11:10AM INF template engine started for template 1... + 11:10AM INF attempting to authenticate... + 11:10AM INF new access token saved to file at path '/home/infisical/config/identity-access-token' + 11:10AM ERR unable to process template because template: literalTemplate:1:9: executing "literalTemplate" at : error calling secret: CallGetRawSecretsV3: Unsuccessful response [GET https://daniel1.tunn.dev/api/v3/secrets/raw?environment=dev&expandSecretReferences=true&include_imports=true&secretPath=%2F&workspaceId=3c0d3ff6-165c-4dc9-b52c-ff3ffaedfce311111] [status-code=404] [response={"reqId":"req-ljqNq567jchFrK","statusCode":404,"message":"Project with ID '3c0d3ff6-165c-4dc9-b52c-ff3ffaedfce311111' not found during bot lookup. Are you sure you are using the correct project ID?","error":"NotFound"}] + + echo 'Agent failed with exit code 1' + + exit 1 + Agent failed with exit code 1 + ``` + + In the above error, the project ID was invalid in the config map. + \ No newline at end of file From 483850441d666a919bf37485e5c4caf1b78a4d08 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 20 May 2025 16:58:19 +0400 Subject: [PATCH 4/5] Update kubernetes-injector.mdx --- docs/integrations/platforms/kubernetes-injector.mdx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/integrations/platforms/kubernetes-injector.mdx b/docs/integrations/platforms/kubernetes-injector.mdx index a94e6e9cf..ade93aef2 100644 --- a/docs/integrations/platforms/kubernetes-injector.mdx +++ b/docs/integrations/platforms/kubernetes-injector.mdx @@ -192,6 +192,11 @@ data: {{- end }} ``` +Now apply the config map: +```bash +kubectl apply -f config-map.yaml +``` + ### Injecting secrets into your pod To inject secrets into your pod, you will need to add the `org.infisical.com/inject: "true"` annotation to your pod's deployment. From bc4fc9a1ca5178d0328eea031f125314290a5763 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 20 May 2025 17:20:54 +0400 Subject: [PATCH 5/5] docs: injector diagram --- docs/integrations/platforms/kubernetes-csi.mdx | 4 ++-- .../platforms/kubernetes-injector.mdx | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/integrations/platforms/kubernetes-csi.mdx b/docs/integrations/platforms/kubernetes-csi.mdx index a38a37404..da1d4d019 100644 --- a/docs/integrations/platforms/kubernetes-csi.mdx +++ b/docs/integrations/platforms/kubernetes-csi.mdx @@ -15,9 +15,9 @@ flowchart LR CSP --> CSD(Secrets Store CSI Driver) end - subgraph Application + subgraph Pod CSD --> V(Volume) - V <--> P(Pod) + V <--> P(Application) end ``` diff --git a/docs/integrations/platforms/kubernetes-injector.mdx b/docs/integrations/platforms/kubernetes-injector.mdx index ade93aef2..aacdcfbbb 100644 --- a/docs/integrations/platforms/kubernetes-injector.mdx +++ b/docs/integrations/platforms/kubernetes-injector.mdx @@ -13,6 +13,21 @@ The Infisical Agent Injector will patch and modify your pod's deployment to cont The Infisical Agent Injector is built on [Kubernetes Mutating Admission Webhooks](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers), and will watch for `CREATE` and `UPDATE` events on pods in your cluster. The injector is namespace-agnostic, and will watch for pods in any namespace, but will only patch pods that have the `org.infisical.com/inject` annotation set to `true`. + +```mermaid +flowchart LR + subgraph Secrets Management + SS(Infisical) --> INJ(Infisical Injector) + end + + subgraph Pod + INJ --> INIT(Agent Init Container) + INIT --> V(Volume) + V <--> P(Application) + end + +``` + ## Install the Infisical Agent Injector To install the Infisical Agent Injector, you will need to install our helm charts using [Helm](https://helm.sh/).