2026-09-29 17:50:30 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestScriptoriumCredentialsComposeAndSerializeSafely(t *testing.T) {
|
|
|
|
|
t.Setenv("OPENROUTER_API_KEY", "synthetic-first-secret")
|
|
|
|
|
t.Setenv("CUSTOM_PROVIDER_KEY", "synthetic-second-secret")
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
root := writePipelineFixture(t, dir, "pipeline.yml", "whisperx:\n transcribe_url: https://example.test/transcribe\nscriptorium:\n credentials: [OPENROUTER_API_KEY]\n")
|
|
|
|
|
base, err := LoadPipeline(root)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for _, marshal := range []func() ([]byte, error){
|
|
|
|
|
func() ([]byte, error) { return yaml.Marshal(base) },
|
|
|
|
|
func() ([]byte, error) { return MarshalEffectivePipeline(base) },
|
|
|
|
|
} {
|
|
|
|
|
data, err := marshal()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(string(data), "OPENROUTER_API_KEY") || strings.Contains(string(data), "synthetic-first-secret") {
|
|
|
|
|
t.Fatal("unsafe or missing credential projection")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
t.Setenv("OPENROUTER_API_KEY", "rotated-secret")
|
|
|
|
|
rotated, err := LoadPipeline(root)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if EffectivePipelineDigest(base) != EffectivePipelineDigest(rotated) {
|
|
|
|
|
t.Fatal("rotation changed effective digest")
|
|
|
|
|
}
|
|
|
|
|
for _, tc := range []struct{ name, value, want string }{
|
|
|
|
|
{"replace", "[CUSTOM_PROVIDER_KEY]", "CUSTOM_PROVIDER_KEY"},
|
|
|
|
|
{"clear", "[]", ""}, {"null", "null", ""},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
writePipelineFixture(t, dir, "overlays/"+tc.name+".yml", "scriptorium:\n credentials: "+tc.value+"\n")
|
|
|
|
|
loaded, err := LoadPipelineWithOptions(root, PipelineLoadOptions{Overlay: &tc.name})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
creds := loaded.Scriptorium.Credentials
|
|
|
|
|
if tc.want == "" {
|
|
|
|
|
if len(creds) != 0 {
|
|
|
|
|
t.Fatal("credentials not cleared")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(creds) != 1 || creds[0].EnvName() != tc.want || creds[0].Value() != "synthetic-second-secret" {
|
|
|
|
|
t.Fatal("overlay did not replace resolved credentials")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
t.Setenv("OPENROUTER_API_KEY", "")
|
|
|
|
|
if _, err := LoadPipeline(root); err == nil {
|
|
|
|
|
t.Fatal("empty selected credential accepted")
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-04 14:41:34 +00:00
|
|
|
|
|
|
|
|
func TestScriptoriumInheritsResolvedTypedCredentialsSafely(t *testing.T) {
|
|
|
|
|
const envName = "NARRATIO_TEST_API_KEY"
|
|
|
|
|
const secret = "synthetic-inherited-secret"
|
|
|
|
|
t.Setenv(envName, secret)
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
root := writePipelineFixture(t, dir, "pipeline.yml", "whisperx:\n transcribe_url: https://example.test/transcribe\naudita:\n credentials: [NARRATIO_TEST_API_KEY]\nscriptorium:\n extends: audita\n")
|
|
|
|
|
cfg, err := LoadPipeline(root)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(cfg.Audita.Credentials) != 1 || cfg.Audita.Credentials[0].EnvName() != envName || !cfg.Audita.Credentials[0].IsResolved() || cfg.Audita.Credentials[0].Value() != secret {
|
|
|
|
|
t.Fatal("audita credential was not resolved")
|
|
|
|
|
}
|
|
|
|
|
if len(cfg.Scriptorium.Credentials) != 1 || cfg.Scriptorium.Credentials[0].EnvName() != envName || !cfg.Scriptorium.Credentials[0].IsResolved() || cfg.Scriptorium.Credentials[0].Value() != secret {
|
|
|
|
|
t.Fatal("scriptorium did not inherit the resolved typed credential")
|
|
|
|
|
}
|
|
|
|
|
for name, marshal := range map[string]func() ([]byte, error){
|
|
|
|
|
"config": func() ([]byte, error) { return yaml.Marshal(cfg) },
|
|
|
|
|
"effective": func() ([]byte, error) { return MarshalEffectivePipeline(cfg) },
|
|
|
|
|
} {
|
|
|
|
|
data, err := marshal()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(string(data), envName) || strings.Contains(string(data), secret) {
|
|
|
|
|
t.Fatalf("%s serialization did not safely expose credential identity", name)
|
|
|
|
|
}
|
|
|
|
|
var serialized struct {
|
|
|
|
|
Scriptorium struct {
|
|
|
|
|
Credentials []string `yaml:"credentials"`
|
|
|
|
|
} `yaml:"scriptorium"`
|
|
|
|
|
}
|
|
|
|
|
if err := yaml.Unmarshal(data, &serialized); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(serialized.Scriptorium.Credentials) != 1 || serialized.Scriptorium.Credentials[0] != envName {
|
|
|
|
|
t.Fatalf("%s serialization omitted the inherited Scriptorium credential", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
values, err := EffectivePipelineValues(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
inspection, err := yaml.Marshal(values)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
foundInheritedCredential := false
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
if value.Path == "scriptorium.credentials" && strings.Contains(value.Value, envName) {
|
|
|
|
|
foundInheritedCredential = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !foundInheritedCredential || strings.Contains(string(inspection), secret) {
|
|
|
|
|
t.Fatal("effective inspection did not safely expose inherited credential identity")
|
|
|
|
|
}
|
|
|
|
|
}
|