security: load Creator secrets from files
CI / test (pull_request) Successful in 10s
CI / test (push) Successful in 10s

This commit was merged in pull request #1.
This commit is contained in:
m
2026-08-16 17:57:32 +03:00
parent e9594306e5
commit 03dde8c196
7 changed files with 190 additions and 7 deletions
+58
View File
@@ -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")
}
})
}