Repository navigation
feat: add constrained Docker Secret template support #178
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package hook | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "text/template" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultSecretDirectory = "/run/secrets" | ||
| templateDirectoryEnv = "WEBHOOK_SECRET_DIR" | ||
| maxSecretFileSize = int64(1 << 20) | ||
| ) | ||
|
|
||
| func templateFunctions() template.FuncMap { | ||
| return template.FuncMap{ | ||
| "getenv": getenv, | ||
| "getenvRequired": getenvRequired, | ||
| "getSecret": getSecret, | ||
| "trimSpace": strings.TrimSpace, | ||
| } | ||
| } | ||
|
|
||
| // getSecret reads one named secret from the configured secret directory. | ||
| // Names, rather than arbitrary paths, keep template file access confined to | ||
| // the directory supplied by the operator. | ||
| func getSecret(name string) (string, error) { | ||
| if name == "" || name == "." || name == ".." || filepath.IsAbs(name) || | ||
| strings.ContainsAny(name, `/\`) || filepath.Base(name) != name { | ||
| return "", fmt.Errorf("invalid secret name %q: use a single file name", name) | ||
| } | ||
|
|
||
| secretDirectory := os.Getenv(templateDirectoryEnv) | ||
| if secretDirectory == "" { | ||
| secretDirectory = defaultSecretDirectory | ||
| } | ||
| if !filepath.IsAbs(secretDirectory) { | ||
| return "", fmt.Errorf("%s must be an absolute path", templateDirectoryEnv) | ||
| } | ||
|
|
||
| secretRoot, err := os.OpenRoot(filepath.Clean(secretDirectory)) | ||
| if err != nil { | ||
| return "", fmt.Errorf("open secret directory: %w", err) | ||
| } | ||
| defer secretRoot.Close() | ||
|
|
||
| secretFile, err := secretRoot.Open(name) | ||
| if err != nil { | ||
| return "", fmt.Errorf("open secret %q: %w", name, err) | ||
| } | ||
| defer secretFile.Close() | ||
|
|
||
| secretInfo, err := secretFile.Stat() | ||
| if err != nil { | ||
| return "", fmt.Errorf("stat secret %q: %w", name, err) | ||
| } | ||
| if !secretInfo.Mode().IsRegular() { | ||
| return "", fmt.Errorf("secret %q is not a regular file", name) | ||
| } | ||
| if secretInfo.Size() > maxSecretFileSize { | ||
| return "", fmt.Errorf("secret %q exceeds the %d-byte size limit", name, maxSecretFileSize) | ||
| } | ||
|
|
||
| contents, err := io.ReadAll(io.LimitReader(secretFile, maxSecretFileSize+1)) | ||
| if err != nil { | ||
| return "", fmt.Errorf("read secret %q: %w", name, err) | ||
| } | ||
| if int64(len(contents)) > maxSecretFileSize { | ||
| return "", fmt.Errorf("secret %q exceeds the %d-byte size limit", name, maxSecretFileSize) | ||
| } | ||
|
|
||
| return string(contents), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package hook | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGetSecretReadsNamedFileWithoutChangingContents(t *testing.T) { | ||
| secretDirectory := t.TempDir() | ||
| t.Setenv(templateDirectoryEnv, secretDirectory) | ||
| require.NoError(t, os.WriteFile(filepath.Join(secretDirectory, "webhook-token"), []byte("secret-value\n"), 0o600)) | ||
|
|
||
| value, err := getSecret("webhook-token") | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "secret-value\n", value) | ||
| } | ||
|
|
||
| func TestGetSecretRejectsInvalidNames(t *testing.T) { | ||
| t.Setenv(templateDirectoryEnv, t.TempDir()) | ||
|
|
||
| for _, name := range []string{"", ".", "..", "../secret", "nested/secret", `nested\secret`, "/etc/passwd"} { | ||
| t.Run(name, func(t *testing.T) { | ||
| _, err := getSecret(name) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "invalid secret name") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetSecretRequiresAbsoluteDirectory(t *testing.T) { | ||
| t.Setenv(templateDirectoryEnv, "relative/secrets") | ||
|
|
||
| _, err := getSecret("webhook-token") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), templateDirectoryEnv+" must be an absolute path") | ||
| } | ||
|
|
||
| func TestGetSecretRejectsPathEscapingSymlink(t *testing.T) { | ||
| secretDirectory := t.TempDir() | ||
| t.Setenv(templateDirectoryEnv, secretDirectory) | ||
| outsideFile := filepath.Join(t.TempDir(), "outside-secret") | ||
| require.NoError(t, os.WriteFile(outsideFile, []byte("must-not-be-read"), 0o600)) | ||
| if err := os.Symlink(outsideFile, filepath.Join(secretDirectory, "webhook-token")); err != nil { | ||
| t.Skipf("cannot create symlink: %v", err) | ||
| } | ||
|
|
||
| _, err := getSecret("webhook-token") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), `open secret "webhook-token"`) | ||
| assert.NotContains(t, err.Error(), "must-not-be-read") | ||
| } | ||
|
|
||
| func TestGetSecretRejectsNonRegularAndOversizedFiles(t *testing.T) { | ||
| secretDirectory := t.TempDir() | ||
| t.Setenv(templateDirectoryEnv, secretDirectory) | ||
| require.NoError(t, os.Mkdir(filepath.Join(secretDirectory, "directory"), 0o700)) | ||
| require.NoError(t, os.WriteFile( | ||
| filepath.Join(secretDirectory, "oversized"), | ||
| bytes.Repeat([]byte("x"), int(maxSecretFileSize)+1), | ||
| 0o600, | ||
| )) | ||
|
|
||
| _, err := getSecret("directory") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "is not a regular file") | ||
|
|
||
| _, err = getSecret("oversized") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "exceeds the") | ||
| } | ||
|
|
||
| func TestGetSecretReportsMissingFileWithoutLeakingContents(t *testing.T) { | ||
| secretDirectory := t.TempDir() | ||
| t.Setenv(templateDirectoryEnv, secretDirectory) | ||
|
|
||
| _, err := getSecret("missing-secret") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), `secret "missing-secret"`) | ||
| } | ||
|
|
||
| func TestHooksTemplateLoadsSecretFile(t *testing.T) { | ||
| secretDirectory := t.TempDir() | ||
| t.Setenv(templateDirectoryEnv, secretDirectory) | ||
| require.NoError(t, os.WriteFile(filepath.Join(secretDirectory, "webhook-token"), []byte("secret-value\n"), 0o600)) | ||
|
|
||
| hooksPath := filepath.Join(t.TempDir(), "hooks.json.tmpl") | ||
| templateConfig := `[{"id":"secret-template","execute-command":"/bin/echo","response-message":"{{ getSecret "webhook-token" | trimSpace | js }}"}]` | ||
| require.NoError(t, os.WriteFile(hooksPath, []byte(templateConfig), 0o600)) | ||
|
|
||
| var hooks Hooks | ||
| require.NoError(t, hooks.LoadFromFile(hooksPath, true)) | ||
| require.Len(t, hooks, 1) | ||
| assert.Equal(t, "secret-value", hooks[0].ResponseMessage) | ||
| } | ||
|
|
||
| func TestHooksTemplateFailsClosedWhenSecretIsMissing(t *testing.T) { | ||
| t.Setenv(templateDirectoryEnv, t.TempDir()) | ||
| hooksPath := filepath.Join(t.TempDir(), "hooks.json.tmpl") | ||
| templateConfig := `[{"id":"secret-template","execute-command":"/bin/echo","response-message":"{{ getSecret "missing-secret" }}"}]` | ||
| require.NoError(t, os.WriteFile(hooksPath, []byte(templateConfig), 0o600)) | ||
|
|
||
| var hooks Hooks | ||
| err := hooks.LoadFromFile(hooksPath, true) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "missing-secret") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the selected secret is a FIFO with no writer,
secretRoot.Open(name)blocks indefinitely, so execution never reaches the later regular-file check. A hook configuration referencing such an entry can therefore hang validation or server startup instead of rejecting the documented unsupported file type; open the entry without blocking or otherwise verify its type before a potentially blocking read.Useful? React with 👍 / 👎.