Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pkg/service/secretfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"os"
"runtime"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -43,4 +44,42 @@ func TestResolveSecretFileInputs(t *testing.T) {
require.Equal(t, "app-secret", resolved[0].Name)
require.Equal(t, "top-secret", resolved[0].Content)
})

t.Run("reads the file whose name ends in whitespace, not its trimmed neighbor", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows does not support these literal filenames")
}
dir := t.TempDir()
require.NoError(t, os.WriteFile(dir+"/neighbor.txt", []byte("NEIGHBOR"), 0o600))
for _, suffix := range []string{" ", "\t", "\n"} {
require.NoError(t, os.WriteFile(dir+"/neighbor.txt"+suffix, []byte("INTENDED"+suffix), 0o600))
resolved, err := ResolveSecretFileInputs([]string{"app-secret:" + dir + "/neighbor.txt" + suffix})
require.NoError(t, err)
require.Equal(t, "INTENDED"+suffix, resolved[0].Content, "suffix %q", suffix)
}
})

t.Run("a missing whitespace-suffixed file is not replaced by its neighbor", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows does not support these literal filenames")
}
dir := t.TempDir()
require.NoError(t, os.WriteFile(dir+"/standalone.txt", []byte("NEIGHBOR"), 0o600))
_, err := ResolveSecretFileInputs([]string{"app-secret:" + dir + "/standalone.txt "})
require.Error(t, err)
require.Contains(t, err.Error(), "failed to read --secret-file")
})

t.Run("keeps interior spaces and colons in the path", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows does not support these literal filenames")
}
dir := t.TempDir()
path := dir + "/a b:c.txt"
require.NoError(t, os.WriteFile(path, []byte("INTERIOR"), 0o600))
resolved, err := ResolveSecretFileInputs([]string{" app-secret :" + path})
require.NoError(t, err)
require.Equal(t, "app-secret", resolved[0].Name)
require.Equal(t, "INTERIOR", resolved[0].Content)
})
}
6 changes: 4 additions & 2 deletions pkg/types/service/secretfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func ParseSecretFileRef(raw string) (SecretFileRef, error) {
return SecretFileRef{}, fmt.Errorf("invalid --secret-file %q: expected NAME:LOCAL_PATH", raw)
}

// The name is formatting, so surrounding whitespace is dropped. The path
// names a file: whitespace there is part of the name the shell passed, and
// trimming it would read a different file that happens to exist.
name = strings.TrimSpace(name)
localPath = strings.TrimSpace(localPath)
if name == "" || localPath == "" {
if name == "" || strings.TrimSpace(localPath) == "" {
return SecretFileRef{}, fmt.Errorf("invalid --secret-file %q: expected NAME:LOCAL_PATH", raw)
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/types/service/secretfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ func TestParseSecretFileRef(t *testing.T) {
require.Contains(t, err.Error(), "expected NAME:LOCAL_PATH")
})

t.Run("trims whitespace from name and path", func(t *testing.T) {
parsed, err := servicetypes.ParseSecretFileRef(" my-secret : /tmp/secret.txt ")
t.Run("trims whitespace from the name but keeps the path exact", func(t *testing.T) {
parsed, err := servicetypes.ParseSecretFileRef(" my-secret :/tmp/secret.txt \t\n")
require.NoError(t, err)
require.Equal(t, "my-secret", parsed.Name)
require.Equal(t, "/tmp/secret.txt", parsed.Path)
require.Equal(t, "/tmp/secret.txt \t\n", parsed.Path)
})

t.Run("rejects a whitespace-only path", func(t *testing.T) {
_, err := servicetypes.ParseSecretFileRef("name: \t")
require.Error(t, err)
require.Contains(t, err.Error(), "expected NAME:LOCAL_PATH")
})

t.Run("handles multiple colons by using first as delimiter", func(t *testing.T) {
Expand Down