-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcertificate_test.go
More file actions
247 lines (226 loc) · 8.89 KB
/
Copy pathcertificate_test.go
File metadata and controls
247 lines (226 loc) · 8.89 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package argocd
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/nebari-dev/nebari-infrastructure-core/pkg/config"
"github.com/nebari-dev/nebari-infrastructure-core/pkg/providers/cluster"
)
func TestNewTemplateData_Certificate(t *testing.T) {
settings := cluster.InfraSettings{}
tests := []struct {
name string
cert *config.CertificateConfig
wantSecretName string
wantSecretNS string
wantCrossNS bool
wantExisting bool
wantIssuerNonZero bool
}{
{
name: "no certificate uses defaults + selfsigned issuer",
cert: nil,
wantSecretName: "nebari-gateway-tls",
wantSecretNS: "envoy-gateway-system",
wantCrossNS: false,
wantExisting: false,
wantIssuerNonZero: true,
},
{
name: "existing files source",
cert: &config.CertificateConfig{Type: "existing", Files: &config.CertFiles{CertFile: "/c", KeyFile: "/k"}},
wantSecretName: "nebari-gateway-tls",
wantSecretNS: "envoy-gateway-system",
wantCrossNS: false,
wantExisting: true,
},
{
name: "existing files with custom secret name",
cert: &config.CertificateConfig{Type: "existing", SecretName: "custom-tls", Env: &config.CertEnv{CertEnv: "A", KeyEnv: "B"}},
wantSecretName: "custom-tls",
wantSecretNS: "envoy-gateway-system",
wantExisting: true,
},
{
name: "existing_secret same namespace",
cert: &config.CertificateConfig{Type: "existing", ExistingSecret: &config.ExistingSecretRef{Name: "user-tls"}},
wantSecretName: "user-tls",
wantSecretNS: "envoy-gateway-system",
wantCrossNS: false,
wantExisting: true,
},
{
name: "existing_secret cross namespace",
cert: &config.CertificateConfig{Type: "existing", ExistingSecret: &config.ExistingSecretRef{Name: "user-tls", Namespace: "user-ns"}},
wantSecretName: "user-tls",
wantSecretNS: "user-ns",
wantCrossNS: true,
wantExisting: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &config.NebariConfig{Domain: "test.example.com", Certificate: tt.cert}
data := NewTemplateData(cfg, nil, settings)
if data.GatewayTLSSecretName != tt.wantSecretName {
t.Errorf("GatewayTLSSecretName = %q, want %q", data.GatewayTLSSecretName, tt.wantSecretName)
}
if data.GatewayTLSSecretNamespace != tt.wantSecretNS {
t.Errorf("GatewayTLSSecretNamespace = %q, want %q", data.GatewayTLSSecretNamespace, tt.wantSecretNS)
}
if data.GatewayTLSCrossNamespace != tt.wantCrossNS {
t.Errorf("GatewayTLSCrossNamespace = %v, want %v", data.GatewayTLSCrossNamespace, tt.wantCrossNS)
}
if data.UseExistingCertificate != tt.wantExisting {
t.Errorf("UseExistingCertificate = %v, want %v", data.UseExistingCertificate, tt.wantExisting)
}
if tt.wantIssuerNonZero && data.CertificateIssuer == "" {
t.Error("CertificateIssuer should be set for non-existing types")
}
})
}
}
func TestGatewayTemplate_CertificateRefName(t *testing.T) {
content, err := templates.ReadFile("templates/manifests/networking/gateway.yaml")
if err != nil {
t.Fatalf("failed to read gateway template: %v", err)
}
t.Run("same namespace omits namespace field", func(t *testing.T) {
data := TemplateData{
Domain: "test.example.com",
HTTPSPort: 443,
GatewayTLSSecretName: "user-tls",
GatewayTLSSecretNamespace: "envoy-gateway-system",
GatewayTLSCrossNamespace: false,
}
processed, err := processTemplate("manifests/networking/gateway.yaml", content, data)
if err != nil {
t.Fatalf("processTemplate() error: %v", err)
}
output := string(processed)
if !strings.Contains(output, "name: user-tls") {
t.Errorf("expected certificateRef name user-tls, got:\n%s", output)
}
// The certificateRef namespace is indented under the ref (12 spaces);
// the Gateway's own metadata.namespace (2 spaces) is unrelated.
if strings.Contains(output, " namespace:") {
t.Errorf("same-namespace ref should not set namespace on certificateRefs, got:\n%s", output)
}
})
t.Run("cross namespace sets namespace field", func(t *testing.T) {
data := TemplateData{
Domain: "test.example.com",
HTTPSPort: 443,
GatewayTLSSecretName: "user-tls",
GatewayTLSSecretNamespace: "user-ns",
GatewayTLSCrossNamespace: true,
}
processed, err := processTemplate("manifests/networking/gateway.yaml", content, data)
if err != nil {
t.Fatalf("processTemplate() error: %v", err)
}
output := string(processed)
if !strings.Contains(output, "name: user-tls") {
t.Errorf("expected certificateRef name user-tls, got:\n%s", output)
}
if !strings.Contains(output, "namespace: user-ns") {
t.Errorf("cross-namespace ref should set namespace user-ns, got:\n%s", output)
}
})
}
func TestReferenceGrantTemplate(t *testing.T) {
content, err := templates.ReadFile("templates/manifests/networking/gateway-tls-referencegrant.yaml")
if err != nil {
t.Fatalf("failed to read referencegrant template: %v", err)
}
data := TemplateData{
GatewayTLSSecretName: "user-tls",
GatewayTLSSecretNamespace: "user-ns",
GatewayTLSCrossNamespace: true,
}
processed, err := processTemplate("manifests/networking/gateway-tls-referencegrant.yaml", content, data)
if err != nil {
t.Fatalf("processTemplate() error: %v", err)
}
output := string(processed)
if !strings.Contains(output, "kind: ReferenceGrant") {
t.Errorf("expected kind: ReferenceGrant, got:\n%s", output)
}
if !strings.Contains(output, "namespace: user-ns") {
t.Errorf("ReferenceGrant should live in user-ns, got:\n%s", output)
}
if !strings.Contains(output, "name: user-tls") {
t.Errorf("ReferenceGrant should grant access to user-tls, got:\n%s", output)
}
if !strings.Contains(output, "namespace: envoy-gateway-system") {
t.Errorf("ReferenceGrant from clause should reference envoy-gateway-system, got:\n%s", output)
}
}
func TestWriteAllToGit_SkipsCertManagerForExisting(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()
cfg := &config.NebariConfig{
Domain: "test.example.com",
Certificate: &config.CertificateConfig{
Type: "existing",
ExistingSecret: &config.ExistingSecretRef{Name: "user-tls"},
},
}
mock := &mockGitClient{workDir: tmpDir}
if err := WriteAllToGit(ctx, mock, cfg, nil, cluster.InfraSettings{}); err != nil {
t.Fatalf("WriteAllToGit() error: %v", err)
}
certPath := filepath.Join(tmpDir, "manifests", "security", "certificates", "gateway-certificate.yaml")
if _, err := os.Stat(certPath); !os.IsNotExist(err) {
t.Errorf("expected gateway-certificate.yaml to be skipped for type=existing, but it exists")
}
// The certificates Application must also be skipped, otherwise it would sync
// an empty directory (allowEmpty: false) and report as failed.
appPath := filepath.Join(tmpDir, "apps", "certificates.yaml")
if _, err := os.Stat(appPath); !os.IsNotExist(err) {
t.Errorf("expected apps/certificates.yaml to be skipped for type=existing, but it exists")
}
// Same-namespace existing_secret should not emit a ReferenceGrant.
rgPath := filepath.Join(tmpDir, "manifests", "networking", "gateway-tls-referencegrant.yaml")
if _, err := os.Stat(rgPath); !os.IsNotExist(err) {
t.Errorf("expected no ReferenceGrant for same-namespace secret, but it exists")
}
}
func TestWriteAllToGit_RendersCertManagerForSelfSigned(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()
cfg := &config.NebariConfig{Domain: "test.example.com"}
mock := &mockGitClient{workDir: tmpDir}
if err := WriteAllToGit(ctx, mock, cfg, nil, cluster.InfraSettings{}); err != nil {
t.Fatalf("WriteAllToGit() error: %v", err)
}
certPath := filepath.Join(tmpDir, "manifests", "security", "certificates", "gateway-certificate.yaml")
if _, err := os.Stat(certPath); os.IsNotExist(err) {
t.Error("expected gateway-certificate.yaml to be rendered for selfsigned default")
}
appPath := filepath.Join(tmpDir, "apps", "certificates.yaml")
if _, err := os.Stat(appPath); os.IsNotExist(err) {
t.Error("expected apps/certificates.yaml to be rendered for selfsigned default")
}
}
func TestWriteAllToGit_RendersReferenceGrantCrossNamespace(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()
cfg := &config.NebariConfig{
Domain: "test.example.com",
Certificate: &config.CertificateConfig{
Type: "existing",
ExistingSecret: &config.ExistingSecretRef{Name: "user-tls", Namespace: "user-ns"},
},
}
mock := &mockGitClient{workDir: tmpDir}
if err := WriteAllToGit(ctx, mock, cfg, nil, cluster.InfraSettings{}); err != nil {
t.Fatalf("WriteAllToGit() error: %v", err)
}
rgPath := filepath.Join(tmpDir, "manifests", "networking", "gateway-tls-referencegrant.yaml")
if _, err := os.Stat(rgPath); os.IsNotExist(err) {
t.Error("expected ReferenceGrant to be rendered for cross-namespace existing_secret")
}
}