Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vault-injector race condition #711

Open
dberuben opened this issue Dec 5, 2024 · 0 comments
Open

Vault-injector race condition #711

dberuben opened this issue Dec 5, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@dberuben
Copy link

dberuben commented Dec 5, 2024

Vault agent injector race condition with environment variables

Describe the bug

There appears to be a race condition with vault-agent-init where the application starts before the environment variables from the injected secrets are properly loaded. While the secrets are successfully written to /vault/secrets/config, they are not present in the application's environment when the process starts, causing authentication failures with dependent services.

Specifically, when checking /proc/1/environ for the main application process, the injected secrets (e.g., REDIS_PASSWORD) are not present in the environment, even though they exist in the secrets file. This causes the application to fail with Redis authentication errors.

The race condition appears to occur in the following sequence:

  1. vault-agent-init writes the initial secrets file
  2. Two processes then start in parallel:
    • The application container executes: source /vault/secrets/config && ./app start
    • vault-agent continues updating /vault/secrets/config with fresh secrets
  3. Due to this parallel execution, sometimes when the source command runs, it encounters an empty or partially written secrets file, resulting in environment variables not being properly loaded

Environment

  • Kubernetes version: v1.29.10-eks-7f9249a
  • Distribution: Amazon EKS
  • vault-k8s version: helm chart vault-0.29.0
  • Vault-server: 1.17.2

To Reproduce

Steps to reproduce the behavior:

  1. Deploy application with vault-agent injection annotations
  2. Application pod starts and vault-agent injects secrets into /vault/secrets/config
  3. Application process starts and attempts to connect to Redis
  4. Connection fails with "NOAUTH Authentication required" because environment variables are not loaded
  5. Verify by checking /proc/1/environ - injected secrets are missing from environment

Application deployment annotations:

podAnnotations:
  vault.hashicorp.com/agent-cache-enable: "true"
  vault.hashicorp.com/agent-inject: "true"
  vault.hashicorp.com/role: "service-sa"
  vault.hashicorp.com/agent-inject-secret-config: path/${lsp.env}/service
  vault.hashicorp.com/agent-inject-template-config: |
    {{ with secret "path/${lsp.env}/service" -}}
     {{ range $k, $v := .Data.data }}
        export {{ $k | replaceAll "." "_" | toUpper }}='{{ $v }}'
      {{ end }}
    {{- end }}
command: ["sh", "-c", "source /vault/secrets/config && node index.js"]

Expected behavior

The secrets should be properly loaded into the application's environment before the application starts. The source /vault/secrets/config command should execute after the secrets file is fully written and before the application process begins.

Additional context

Verification steps performed:

  1. Checking /vault/secrets/config shows the secrets are properly written:
export REDIS_PASSWORD='Password'
  1. Checking process environment shows secrets are missing:
$ cat /proc/1/environ | grep REDIS_PASSWORD
(no output)
  1. Error logs from application:
{
  "level": "error",
  "time": 1733389377936,
  "err": {
    "type": "ReplyError",
    "message": "NOAUTH Authentication required.",
    "stack": "ReplyError: NOAUTH Authentication required.\n    at parseError (/usr/src/app/node_modules/redis-parser/lib/parser.js:179:12)\n    at parseType (/usr/src/app/node_modules/redis-parser/lib/parser.js:302:14)",
    "command": {
      "name": "info",
      "args": []
    }
  },
  "msg": "Redis error."
}

This suggests the application starts before the environment variables are properly loaded, despite using the source command in the container's command string.

Solution
We were able to mitigate the issue by adding this annotation

vault.hashicorp.com/agent-pre-populate-only: "true"
@dberuben dberuben added the bug Something isn't working label Dec 5, 2024
@dberuben dberuben changed the title Vault-injector race condition with environment variables Vault-injector race condition Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant