|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package installdefaults |
| 16 | + |
| 17 | +import ( |
| 18 | + "go/ast" |
| 19 | + "go/parser" |
| 20 | + "go/token" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "runtime" |
| 24 | + "strconv" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | +) |
| 28 | + |
| 29 | +// This guard exists because hardcoded install-layout assumptions fail closed |
| 30 | +// in a relocated or renamed install, in ways that do not point at the naming |
| 31 | +// (see the ateletauth, ateletdial and NetworkPolicy histories). New namespace |
| 32 | +// or SPIFFE-identity literals belong in this package, in a flag default, or — |
| 33 | +// deliberately — in the allowlist below. |
| 34 | + |
| 35 | +// excludedTrees are directories whose tooling targets the canonical install |
| 36 | +// layout by design and is not expected to work against a relocated one. |
| 37 | +var excludedTrees = []string{ |
| 38 | + "cmd/ate-setup", // canonical GCP installer |
| 39 | + "cmd/benchmarking", // canonical load-testing workers |
| 40 | + "internal/benchmarking", // canonical load-testing harness |
| 41 | +} |
| 42 | + |
| 43 | +// allowedLiterals maps a repo-relative file to the canonical-layout string |
| 44 | +// literals it may carry. Everything here is either a flag/env default that an |
| 45 | +// operator overrides (the sanctioned pattern: default to the canonical |
| 46 | +// layout, configure the rest), or a string nothing verifies against. |
| 47 | +var allowedLiterals = map[string][]string{ |
| 48 | + // Flag defaults, overridden by the chart or the e2e manifest templates. |
| 49 | + "cmd/atecontroller/main.go": {"k8s:///api.ate-system.svc:443"}, |
| 50 | + "cmd/atelet/main.go": {"k8s:///api.ate-system.svc:443", "api.ate-system.svc"}, |
| 51 | + "cmd/atenet/internal/router/cmd.go": {"k8s:///api.ate-system.svc:443", "spiffe://cluster.local/"}, |
| 52 | + "internal/e2e/fixtures/testserver/egressprobe.go": {"atenet-egress.ate-system.svc:443"}, |
| 53 | + // Env-var defaults, overridden by ATE_* / E2E_* variables. |
| 54 | + "internal/ateclient/builder.go": {"api.ate-system.svc"}, |
| 55 | + // Inert: the actor JWT issuer is an upstream TODO nothing validates, and |
| 56 | + // an x509 template's Issuer field is overwritten by the signer. |
| 57 | + "cmd/ateapi/internal/actoridentity/actoridentity.go": {"https://api.ate-system.svc", "api.ate-system.svc.cluster.local"}, |
| 58 | +} |
| 59 | + |
| 60 | +// suspect reports whether a string literal encodes canonical install layout. |
| 61 | +func suspect(s string) bool { |
| 62 | + return strings.Contains(s, "ate-system") || strings.Contains(s, "spiffe://cluster.local") |
| 63 | +} |
| 64 | + |
| 65 | +func repoRoot(t *testing.T) string { |
| 66 | + t.Helper() |
| 67 | + _, thisFile, _, ok := runtime.Caller(0) |
| 68 | + if !ok { |
| 69 | + t.Fatal("cannot locate this test file") |
| 70 | + } |
| 71 | + return filepath.Dir(filepath.Dir(filepath.Dir(thisFile))) |
| 72 | +} |
| 73 | + |
| 74 | +func allowed(relPath, lit string) bool { |
| 75 | + for _, tree := range excludedTrees { |
| 76 | + if strings.HasPrefix(relPath, tree+string(filepath.Separator)) { |
| 77 | + return true |
| 78 | + } |
| 79 | + } |
| 80 | + for _, a := range allowedLiterals[filepath.ToSlash(relPath)] { |
| 81 | + if a == lit { |
| 82 | + return true |
| 83 | + } |
| 84 | + } |
| 85 | + return false |
| 86 | +} |
| 87 | + |
| 88 | +// TestNoNewHardcodedInstallLayoutInGo walks every non-test Go file under cmd/ |
| 89 | +// and internal/ and fails on string literals that name the canonical |
| 90 | +// namespace or a full SPIFFE identity outside this package. Comments are not |
| 91 | +// scanned, so kubebuilder markers (which must name a literal namespace) pass. |
| 92 | +func TestNoNewHardcodedInstallLayoutInGo(t *testing.T) { |
| 93 | + root := repoRoot(t) |
| 94 | + selfDir := filepath.Join("internal", "installdefaults") |
| 95 | + |
| 96 | + var offenders []string |
| 97 | + for _, tree := range []string{"cmd", "internal"} { |
| 98 | + err := filepath.WalkDir(filepath.Join(root, tree), func(path string, d os.DirEntry, err error) error { |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + if d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { |
| 103 | + return nil |
| 104 | + } |
| 105 | + rel, err := filepath.Rel(root, path) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + if strings.HasPrefix(rel, selfDir) { |
| 110 | + return nil |
| 111 | + } |
| 112 | + fset := token.NewFileSet() |
| 113 | + f, err := parser.ParseFile(fset, path, nil, 0) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + ast.Inspect(f, func(n ast.Node) bool { |
| 118 | + lit, ok := n.(*ast.BasicLit) |
| 119 | + if !ok || lit.Kind != token.STRING { |
| 120 | + return true |
| 121 | + } |
| 122 | + val, err := strconv.Unquote(lit.Value) |
| 123 | + if err != nil { |
| 124 | + return true |
| 125 | + } |
| 126 | + if suspect(val) && !allowed(rel, val) { |
| 127 | + offenders = append(offenders, fset.Position(lit.Pos()).String()+": "+lit.Value) |
| 128 | + } |
| 129 | + return true |
| 130 | + }) |
| 131 | + return nil |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if len(offenders) > 0 { |
| 139 | + t.Errorf("hardcoded install-layout strings outside internal/installdefaults; "+ |
| 140 | + "derive them from installdefaults or a configured value, or allowlist them "+ |
| 141 | + "in this test with a justification:\n %s", strings.Join(offenders, "\n ")) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// TestNoNewHardcodedInstallLayoutInChart holds the chart to a stricter bar: |
| 146 | +// it renders every name, so it has no reason to mention the canonical |
| 147 | +// namespace or a SPIFFE identity at all. |
| 148 | +func TestNoNewHardcodedInstallLayoutInChart(t *testing.T) { |
| 149 | + root := repoRoot(t) |
| 150 | + |
| 151 | + var offenders []string |
| 152 | + err := filepath.WalkDir(filepath.Join(root, "charts", "substrate"), func(path string, d os.DirEntry, err error) error { |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + if d.IsDir() || (!strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".tpl")) { |
| 157 | + return nil |
| 158 | + } |
| 159 | + data, err := os.ReadFile(path) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + rel, _ := filepath.Rel(root, path) |
| 164 | + for i, line := range strings.Split(string(data), "\n") { |
| 165 | + if strings.HasPrefix(strings.TrimSpace(line), "#") { |
| 166 | + continue // comments may name the defaults they explain |
| 167 | + } |
| 168 | + if suspect(line) { |
| 169 | + offenders = append(offenders, rel+":"+strconv.Itoa(i+1)+": "+strings.TrimSpace(line)) |
| 170 | + } |
| 171 | + } |
| 172 | + return nil |
| 173 | + }) |
| 174 | + if err != nil { |
| 175 | + t.Fatal(err) |
| 176 | + } |
| 177 | + |
| 178 | + if len(offenders) > 0 { |
| 179 | + t.Errorf("the chart must render names from the release, never hardcode the canonical layout:\n %s", |
| 180 | + strings.Join(offenders, "\n ")) |
| 181 | + } |
| 182 | +} |
0 commit comments