Skip to content

bug: Environment variable with common prefix returns incorrect value in init_by_lua #13055

@ChuanFF

Description

@ChuanFF

Current Behavior

Description:
When reading environment variables in the init_by_lua phase, if two variables share a common prefix (e.g., KUBERNETES_CLIENT_TOKEN and KUBERNETES_CLIENT_TOKEN_FILE), the value of the shorter-named variable may be erroneously returned when the longer-named one is requested. The issue appears to be sensitive to the order in which the variables are defined in the configuration.

Expected Behavior

Expected behavior:
os.getenv("KUBERNETES_CLIENT_TOKEN_FILE") should always return the value of the environment variable named exactly KUBERNETES_CLIENT_TOKEN_FILE, regardless of the definition order of the env directives.

Error Logs

No response

Steps to Reproduce

Steps to reproduce:

  1. Create a test configuration similar to t/kubernetes/discovery/kubernetes2.t that defines two environment variables with a common prefix, e.g.:
    env KUBERNETES_CLIENT_TOKEN=some-token;
    env KUBERNETES_CLIENT_TOKEN_FILE=/path/to/token;
    
  2. In init_by_lua, attempt to read KUBERNETES_CLIENT_TOKEN_FILE using os.getenv("KUBERNETES_CLIENT_TOKEN_FILE").
  3. Observe that the returned value is some-token (the value of the shorter variable) instead of /path/to/token.
  4. Swap the order of the two env directives so that the longer name (KUBERNETES_CLIENT_TOKEN_FILE) appears first:
    env KUBERNETES_CLIENT_TOKEN_FILE=/path/to/token;
    env KUBERNETES_CLIENT_TOKEN=some-token;
    
  5. In init_by_lua, again read KUBERNETES_CLIENT_TOKEN_FILE – this time it returns the correct value /path/to/token.

Environment

master branch

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    📋 Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions