security: load Creator secrets from files
This commit was merged in pull request #1.
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -67,3 +69,59 @@ func TestCreatorSessionHandlerPreservesBootstrapRecovery(t *testing.T) {
|
||||
t.Fatalf("bootstrap recovery status = %d", response.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadRequiredSecretFromEnvironment(t *testing.T) {
|
||||
t.Setenv("TEST_CREATOR_SECRET", "direct-value")
|
||||
value, err := readRequiredSecret("TEST_CREATOR_SECRET")
|
||||
if err != nil || value != "direct-value" {
|
||||
t.Fatalf("value=%q err=%v", value, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadRequiredSecretFromFile(t *testing.T) {
|
||||
fileName := filepath.Join(t.TempDir(), "secret")
|
||||
if err := os.WriteFile(fileName, []byte("file-value\r\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("TEST_CREATOR_SECRET_FILE", fileName)
|
||||
value, err := readRequiredSecret("TEST_CREATOR_SECRET")
|
||||
if err != nil || value != "file-value" {
|
||||
t.Fatalf("value=%q err=%v", value, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadRequiredSecretFailsClosed(t *testing.T) {
|
||||
t.Run("both sources", func(t *testing.T) {
|
||||
t.Setenv("TEST_CREATOR_SECRET", "direct")
|
||||
t.Setenv("TEST_CREATOR_SECRET_FILE", filepath.Join(t.TempDir(), "secret"))
|
||||
if _, err := readRequiredSecret("TEST_CREATOR_SECRET"); err == nil {
|
||||
t.Fatal("expected ambiguous configuration to fail")
|
||||
}
|
||||
})
|
||||
t.Run("empty file", func(t *testing.T) {
|
||||
fileName := filepath.Join(t.TempDir(), "secret")
|
||||
if err := os.WriteFile(fileName, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("TEST_CREATOR_SECRET_FILE", fileName)
|
||||
if _, err := readRequiredSecret("TEST_CREATOR_SECRET"); err == nil {
|
||||
t.Fatal("expected empty secret to fail")
|
||||
}
|
||||
})
|
||||
t.Run("multiline file", func(t *testing.T) {
|
||||
fileName := filepath.Join(t.TempDir(), "secret")
|
||||
if err := os.WriteFile(fileName, []byte("first\nsecond\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("TEST_CREATOR_SECRET_FILE", fileName)
|
||||
if _, err := readRequiredSecret("TEST_CREATOR_SECRET"); err == nil {
|
||||
t.Fatal("expected multiline secret to fail")
|
||||
}
|
||||
})
|
||||
t.Run("missing file", func(t *testing.T) {
|
||||
t.Setenv("TEST_CREATOR_SECRET_FILE", filepath.Join(t.TempDir(), "missing"))
|
||||
if _, err := readRequiredSecret("TEST_CREATOR_SECRET"); err == nil {
|
||||
t.Fatal("expected missing secret file to fail")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user