Skip to content

feat(secret): add a file-based secret manager - #14250

Open
cvas22 wants to merge 3 commits into
apache:masterfrom
cvas22:feat/14099-file-secret
Open

cvas22 wants to merge 3 commits into
apache:masterfrom
cvas22:feat/14099-file-secret

Conversation

@cvas22

@cvas22 cvas22 commented Nov 23, 2026

Copy link
Copy Markdown

Description

Adds a file secret manager so APISIX can read secrets that are mounted as files, like a Kubernetes Secret volume, instead of going through environment variables or an external manager. You add /apisix/admin/secrets/file/<id> with a base_path, and $secret://file/<id>/<key> resolves to the content of <base_path>/<key>.

The key can point into subdirectories, but it can't start with / or contain .. segments, so a reference can't read outside base_path. One trailing newline is dropped since most tools add one. Values go through the normal secret LRU cache, so a rotated Secret is picked up after the TTL without a restart.

Tests are in t/secret/file.t (schema, key checks, reads, newline handling, fetch_by_uri, and key-auth with a file-backed key). Docs are in both en and zh terminology/secret.md, and the Admin API spec has a FileSecret schema with admin-api.json regenerated by make bundle-admin-api-spec. validate-admin-api.py and t/openapi pass.

Which issue(s) this PR fixes:

Fixes #14099

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

Reads $secret://file/<id>/<key> from <base_path>/<key>, for secrets mounted as files like a Kubernetes Secret volume. Keys can't be absolute or contain '..' segments, and one trailing newline is dropped.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
Comment thread docs/zh/latest/terminology/secret.md Outdated
- [HashiCorp Vault](#使用-vault-管理密钥)
- [AWS Secrets Manager](#使用-aws-secrets-manager-管理密钥)
- [GCP Secrets Manager](#使用-gcp-secrets-manager-管理密钥)
- [文件](#使用文件管理密钥)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [文件](#使用文件管理密钥)
- [File Secrets Manager](#使用文件管理密钥)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed in b633577. thx

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation and comments in English please

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code and comments are in English. The zh file is the Chinese mirror of the English secret doc, like the other secret managers have, so I kept it in Chinese. Happy to drop it if you'd rather.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, as I could not read it, it was hard to determine if it contained info that was relevant.

Comment thread apisix/secret/file.lua
end

local path = base_path .. key
local content, err = core.io.get_file(path)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty file will lead to en empty string in LUA. So the get() will return succes. The content var will be "".

The if not content is a nill check not a empty string check. Adding a check for a emtpty string is an improvement for safety.

local content, err = core.io.get_file(path)
if not content then
return nil, "failed to read secret file: " .. (err or path .. ": no content")
end

if sub(content, -2) == "\r\n" then
content = sub(content, 1, -3)
elseif sub(content, -1) == "\n" then
content = sub(content, 1, -2)
end

if content == "" then
return nil, "secret file is empty: " .. path
end

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, an empty file now returns an error (500cad9).

Comment thread t/secret/file.t
two-lf: YWJjCg==
multi-line: bGluZTEKbGluZTI=


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test for empty files

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 500cad9, empty file and a file with only a newline.

Comment thread t/secret/file.t

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some extra test could be nice for completenes

  • a test for a kubernetes symlink
  • empty file see inline
  • pointing a key to a directory
  • file without permissions

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added cases for a Kubernetes style symlink, an empty file and a directory in 500cad9. I skipped the no-permission one since CI usually runs as root and it wouldn't fail there. I couldn't run the Test::Nginx suite locally, so CI will be the first run.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we consider a empty file with a line ending as an empty file?

if sub(content, -2) == "\r\n" then
content = sub(content, 1, -3)
elseif sub(content, -1) == "\n" then
content = sub(content, 1, -2)
end

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the trailing newline is stripped first, so a file with just "\n" or "\r\n" ends up empty and is rejected the same way. The only-newline case in TEST 4b covers that.

@jjansenvr jjansenvr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, looks solid!
Some minor remarks made but thanks for the work

…and directories

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22

cvas22 commented Nov 27, 2026

Copy link
Copy Markdown
Author

Yes, the newline gets stripped first and then the empty check runs, so a file with only a line ending is treated as empty. There's a test for that case too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: support file-based secrets provider for Kubernetes file-mounted secrets

2 participants