carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1// internal/appconfig/materialize_test.go
2package appconfig
3
4import (
5 "strings"
6 "testing"
7)
8
9func TestRenderEnvMergesPlainAndDecryptedSecrets(t *testing.T) {
10 priv, pub, err := GenerateKey()
11 if err != nil {
12 t.Fatalf("GenerateKey: %v", err)
13 }
14 privKey, err := ParsePrivateKey(priv, "test")
15 if err != nil {
16 t.Fatalf("ParsePrivateKey: %v", err)
17 }
18 pubKey, err := ParsePublicKey(pub, "test")
19 if err != nil {
20 t.Fatalf("ParsePublicKey: %v", err)
21 }
22 envelope, err := Seal(pubKey, Binding{Account: "bab", App: "keymail"}, []byte("s3cr3t"))
23 if err != nil {
24 t.Fatalf("Seal: %v", err)
25 }
26 m := &Manifest{Schema: 1, App: "keymail", Vars: map[string]Var{
27 "LOG_LEVEL": {Kind: "env", Value: "debug"},
28 "SES_SECRET": {Kind: "secret", Envelope: envelope},
29 }}
30 got, err := RenderEnv(m, privKey, Binding{Account: "bab", App: "keymail"})
31 if err != nil {
32 t.Fatalf("RenderEnv: %v", err)
33 }
34 // Sorted by key, so the diff Sync computes against what's on disk is
35 // deterministic — an unrelated key added or removed elsewhere in the
36 // map must not reorder every line and force a spurious restart.
37 want := "LOG_LEVEL=debug\nSES_SECRET=s3cr3t\n"
38 if string(got) != want {
39 t.Fatalf("RenderEnv = %q, want %q", got, want)
40 }
41}
42
43func TestRenderEnvRejectsValuesSystemdWouldMangle(t *testing.T) {
44 // systemd's EnvironmentFile parser is shell-like: it eats backslash
45 // escapes and quotes, and drops a line outright on an unbalanced quote.
46 // A value carrying any of those would be silently corrupted on the host,
47 // so reject it here, where an operator still sees the error.
48 for name, val := range map[string]string{
49 "newline": "line1\nline2",
50 "nul": "a\x00b",
51 "backslash": `C:\Users\svc`,
52 "singlequote": "it's",
53 "doublequote": `say "hi"`,
54 "unbalanced": `p4ss"word`,
55 "mixed": `a\'b"c`,
56 } {
57 m := &Manifest{Schema: 1, App: "keymail", Vars: map[string]Var{
58 "BAD": {Kind: "env", Value: val},
59 }}
60 _, err := RenderEnv(m, nil, Binding{Account: "bab", App: "keymail"})
61 if err == nil {
62 t.Errorf("%s: RenderEnv accepted %q, want an error", name, val)
63 continue
64 }
65 if !strings.Contains(err.Error(), "BAD") {
66 t.Errorf("%s: error %q does not name the offending var", name, err)
67 }
68 }
69}
70
71func TestRenderEnvAcceptsOrdinaryPunctuation(t *testing.T) {
72 // The rejection must stay narrow: everything systemd passes through
73 // verbatim still renders, including the characters common in URLs and
74 // base64 payloads.
75 val := "postgres://u:p%40ss@host:5432/db?sslmode=require&x=a+b/c=="
76 m := &Manifest{Schema: 1, App: "keymail", Vars: map[string]Var{
77 "DATABASE_URL": {Kind: "env", Value: val},
78 }}
79 got, err := RenderEnv(m, nil, Binding{Account: "bab", App: "keymail"})
80 if err != nil {
81 t.Fatalf("RenderEnv: %v", err)
82 }
83 if string(got) != "DATABASE_URL="+val+"\n" {
84 t.Fatalf("RenderEnv = %q, want DATABASE_URL=%s", got, val)
85 }
86}
87