aboutsummaryrefslogtreecommitdiff
path: root/main_test.go
blob: b4b0af2ffb6ff841d4de84a20b480d7470add27c (plain) (blame)
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
package main

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// The interception ca is added to the system roots and never substituted for
// them: the one feature that reaches the internet directly is oidc, and it
// verifies a real provider against the roots the image ships.
func TestCABundle(t *testing.T) {
	dir := t.TempDir()
	sys := filepath.Join(dir, "ca-certificates.crt")
	cf := filepath.Join(dir, "cloudflare.crt")
	out := filepath.Join(dir, "ca-bundle.crt")
	if err := os.WriteFile(sys, []byte("the system roots\n"), 0600); err != nil {
		t.Fatal(err)
	}

	// No interception certificate, which is what a bare docker run gets.
	if err := caBundle(sys, cf, out); err != nil {
		t.Fatalf("with no interception ca: %v", err)
	}
	if got, err := os.ReadFile(out); err != nil || string(got) != "the system roots\n" {
		t.Errorf("the bundle is %q: %v, want the system roots alone", got, err)
	}

	if err := os.WriteFile(cf, []byte("the interception ca\n"), 0600); err != nil {
		t.Fatal(err)
	}
	if err := caBundle(sys, cf, out); err != nil {
		t.Fatalf("with one: %v", err)
	}
	got, err := os.ReadFile(out)
	if err != nil {
		t.Fatal(err)
	}
	if !strings.Contains(string(got), "the system roots") {
		t.Errorf("the system roots were replaced rather than added to: %q", got)
	}
	if !strings.Contains(string(got), "the interception ca") {
		t.Errorf("the interception ca is not in the bundle: %q", got)
	}

	// A system store that is not there is a bundle that cannot be trusted, not
	// an empty one.
	if err := caBundle(filepath.Join(dir, "absent"), cf, out); err == nil {
		t.Error("a missing system store was accepted")
	}
}