Repository navigation
Expand file tree
/
Copy pathca_linux_test.go
More file actions
82 lines (71 loc) · 2.44 KB
/
Copy pathca_linux_test.go
File metadata and controls
82 lines (71 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//go:build linux
// +build linux
package docker
import (
"os"
"path/filepath"
"strings"
"testing"
)
// withTempTrustStore points the Linux anchor/bundle candidates at a temp dir so
// installHarnessCA can be exercised without touching the real system store.
func withTempTrustStore(t *testing.T) (anchorDir, bundlePath string) {
t.Helper()
dir := t.TempDir()
anchorDir = filepath.Join(dir, "anchors")
bundlePath = filepath.Join(dir, "ca-certificates.crt")
if err := os.MkdirAll(anchorDir, 0755); err != nil {
t.Fatal(err)
}
// Seed an existing bundle so the append path is exercised.
if err := os.WriteFile(bundlePath, []byte("# existing roots\n"), 0644); err != nil {
t.Fatal(err)
}
origAnchors := linuxAnchorCandidates
origBundles := linuxBundleCandidates
// No refresh tool on the anchor entry so the test doesn't shell out.
linuxAnchorCandidates = []anchorCandidate{{dir: anchorDir, refresh: []string{"harness-nonexistent-refresh-tool"}}}
linuxBundleCandidates = []string{bundlePath}
t.Cleanup(func() {
linuxAnchorCandidates = origAnchors
linuxBundleCandidates = origBundles
})
return anchorDir, bundlePath
}
func TestInstallHarnessCA_Linux(t *testing.T) {
anchorDir, bundlePath := withTempTrustStore(t)
installHarnessCA([]byte(testCAPEM))
// Anchor file should be written.
anchorFile := filepath.Join(anchorDir, "harness-egress-ca.crt")
got, err := os.ReadFile(anchorFile)
if err != nil {
t.Fatalf("expected anchor file at %s: %s", anchorFile, err)
}
if string(got) != testCAPEM {
t.Fatalf("anchor file = %q, want %q", string(got), testCAPEM)
}
// Bundle should have the CA appended (refresh tool is absent, so the
// direct-append fallback is what provides trust here).
bundle, err := os.ReadFile(bundlePath)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(bundle), strings.TrimSpace(testCAPEM)) {
t.Fatalf("bundle %q does not contain appended CA", string(bundle))
}
if !strings.HasPrefix(string(bundle), "# existing roots") {
t.Fatalf("bundle append clobbered existing roots: %q", string(bundle))
}
}
func TestInstallHarnessCA_Linux_Idempotent(t *testing.T) {
_, bundlePath := withTempTrustStore(t)
installHarnessCA([]byte(testCAPEM))
installHarnessCA([]byte(testCAPEM))
bundle, err := os.ReadFile(bundlePath)
if err != nil {
t.Fatal(err)
}
if n := strings.Count(string(bundle), strings.TrimSpace(testCAPEM)); n != 1 {
t.Fatalf("expected CA to appear exactly once after two installs, got %d", n)
}
}