~bigbes/lethe

ref: ff633decd7949efd3dea9e056b06a3a659406494 lethe/internal/config/config_test.go -rw-r--r-- 9.5 KiB
ff633dec — Eugene Blikh auth: integrate oidcstub as opt-in dev OP under auth.oidc.dev_stub a month ago
                                                                                
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package config_test

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

	"sourcecraft.dev/bigbes/lethe/internal/config"
)

// validForwardAuthYAML is a baseline valid configuration with forward-auth
// enabled. Tests typically clone and tweak this string for negative cases.
const validForwardAuthYAML = `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice", "bob"]
  admins: ["alice"]
  forward_auth:
    enabled: true
  oidc:
    enabled: false
logging:
  level: "info"
  format: "tint"
ingest: {}
`

const validOIDCYAML = `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: false
  oidc:
    enabled: true
    issuer: "https://auth.example.com"
    audience: "lethe"
logging:
  level: "info"
  format: "tint"
ingest: {}
`

const validOIDCDevStubYAML = `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: false
  oidc:
    enabled: true
    issuer: "https://auth.example.com"
    audience: "lethe"
    dev_stub:
      enabled: true
      bind: "127.0.0.1:8181"
      token_ttl: "1h"
logging:
  level: "info"
  format: "tint"
ingest: {}
`

const validBothEnabledYAML = `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: true
  oidc:
    enabled: true
    issuer: "https://auth.example.com"
    audience: "lethe"
logging:
  level: "info"
  format: "tint"
ingest: {}
`

func writeYAML(t *testing.T, body string) string {
	t.Helper()
	dir := t.TempDir()
	path := filepath.Join(dir, "config.yaml")
	if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
		t.Fatalf("write yaml: %v", err)
	}
	return path
}

func TestLoad_ValidForwardAuth(t *testing.T) {
	path := writeYAML(t, validForwardAuthYAML)
	cfg, err := config.Load(path)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	if cfg.Server.Bind != "127.0.0.1:8080" {
		t.Errorf("Server.Bind = %q", cfg.Server.Bind)
	}
	if !cfg.Auth.ForwardAuth.Enabled {
		t.Error("ForwardAuth.Enabled = false, want true")
	}
}

func TestLoad_ValidOIDC(t *testing.T) {
	path := writeYAML(t, validOIDCYAML)
	cfg, err := config.Load(path)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	if !cfg.Auth.OIDC.Enabled {
		t.Error("OIDC.Enabled = false, want true")
	}
	if cfg.Auth.OIDC.Issuer != "https://auth.example.com" {
		t.Errorf("OIDC.Issuer = %q", cfg.Auth.OIDC.Issuer)
	}
}

func TestLoad_ValidBothEnabled(t *testing.T) {
	path := writeYAML(t, validBothEnabledYAML)
	if _, err := config.Load(path); err != nil {
		t.Fatalf("Load: %v", err)
	}
}

func TestLoad_ValidOIDCDevStub(t *testing.T) {
	path := writeYAML(t, validOIDCDevStubYAML)
	cfg, err := config.Load(path)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	if !cfg.Auth.OIDC.DevStub.Enabled {
		t.Error("DevStub.Enabled = false, want true")
	}
	if cfg.Auth.OIDC.DevStub.Bind != "127.0.0.1:8181" {
		t.Errorf("DevStub.Bind = %q, want 127.0.0.1:8181", cfg.Auth.OIDC.DevStub.Bind)
	}
	if cfg.Auth.OIDC.DevStub.TokenTTL != time.Hour {
		t.Errorf("DevStub.TokenTTL = %s, want 1h", cfg.Auth.OIDC.DevStub.TokenTTL)
	}
}

func TestLoad_DevStubBindNonLoopback_Rejects(t *testing.T) {
	body := strings.Replace(validOIDCDevStubYAML, `"127.0.0.1:8181"`, `"0.0.0.0:8181"`, 1)
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error for non-loopback dev_stub bind, got nil")
	}
}

func TestLoad_Defaults(t *testing.T) {
	path := writeYAML(t, validForwardAuthYAML)
	cfg, err := config.Load(path)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	if cfg.Auth.ForwardAuth.UserHeader != "Remote-User" {
		t.Errorf("ForwardAuth.UserHeader = %q, want Remote-User", cfg.Auth.ForwardAuth.UserHeader)
	}
	if cfg.Auth.OIDC.UsernameClaim != "preferred_username" {
		t.Errorf("OIDC.UsernameClaim = %q, want preferred_username", cfg.Auth.OIDC.UsernameClaim)
	}
	if cfg.Ingest.MaxBodyBytes != 16*1024*1024 {
		t.Errorf("Ingest.MaxBodyBytes = %d, want %d", cfg.Ingest.MaxBodyBytes, 16*1024*1024)
	}
	if cfg.Ingest.MaxTurnContentBytes != 4*1024*1024 {
		t.Errorf("Ingest.MaxTurnContentBytes = %d, want %d", cfg.Ingest.MaxTurnContentBytes, 4*1024*1024)
	}
	if cfg.Ingest.ChunkSize != 500 {
		t.Errorf("Ingest.ChunkSize = %d, want 500", cfg.Ingest.ChunkSize)
	}
	if cfg.Database.BusyTimeout != 5*time.Second {
		t.Errorf("Database.BusyTimeout = %s, want 5s", cfg.Database.BusyTimeout)
	}
	if cfg.Server.ShutdownGrace != 10*time.Second {
		t.Errorf("Server.ShutdownGrace = %s, want 10s", cfg.Server.ShutdownGrace)
	}
	if cfg.Auth.OIDC.DevStub.TokenTTL != 24*time.Hour {
		t.Errorf("OIDC.DevStub.TokenTTL = %s, want 24h", cfg.Auth.OIDC.DevStub.TokenTTL)
	}
}

func TestLoad_EmptyAllowlistRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: []
  forward_auth:
    enabled: true
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error for empty allowed_users, got nil")
	}
}

func TestLoad_NonLoopbackBindRejected(t *testing.T) {
	body := strings.Replace(validForwardAuthYAML, `"127.0.0.1:8080"`, `"0.0.0.0:8080"`, 1)
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error for non-loopback bind, got nil")
	}
}

func TestLoad_BothAuthModesDisabledRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: false
  oidc:
    enabled: false
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error when both auth modes are disabled, got nil")
	}
}

func TestLoad_OIDCEnabledWithoutIssuerRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: false
  oidc:
    enabled: true
    audience: "lethe"
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error for OIDC enabled without issuer, got nil")
	}
}

func TestLoad_OIDCEnabledWithNonURLIssuerRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: false
  oidc:
    enabled: true
    issuer: "not a url"
    audience: "lethe"
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error for non-URL issuer, got nil")
	}
}

func TestLoad_AdminNotInAllowedUsersRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  admins: ["mallory"]
  forward_auth:
    enabled: true
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error when admin is not in allowed_users, got nil")
	}
}

func TestLoad_EmptyAdminsAccepted(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  admins: []
  forward_auth:
    enabled: true
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err != nil {
		t.Fatalf("expected empty admins to be accepted, got %v", err)
	}
}

func TestLoad_MissingDatabasePathRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database: {}
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: true
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error for missing database.path, got nil")
	}
}

func TestLoad_MaxTurnContentBytesGreaterThanMaxBodyBytesRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: true
logging:
  level: "info"
  format: "tint"
ingest:
  max_body_bytes: 1024
  max_turn_content_bytes: 4096
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error when max_turn_content_bytes > max_body_bytes, got nil")
	}
}

func TestLoad_UnknownYAMLKeyRejected(t *testing.T) {
	body := `
server:
  bind: "127.0.0.1:8080"
  totally_unknown_key: 42
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: true
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	if _, err := config.Load(writeYAML(t, body)); err == nil {
		t.Fatal("expected error for unknown YAML key, got nil")
	}
}

func TestLoad_EnvOverride(t *testing.T) {
	// Use a YAML body without admins so the override test only exercises
	// the slice-decoding path (LETHE_AUTH_ALLOWED_USERS overrides YAML).
	body := `
server:
  bind: "127.0.0.1:8080"
database:
  path: "./lethe.db"
auth:
  allowed_users: ["alice"]
  forward_auth:
    enabled: true
logging:
  level: "info"
  format: "tint"
ingest: {}
`
	path := writeYAML(t, body)
	t.Setenv("LETHE_AUTH_ALLOWED_USERS", "carol,dave")
	cfg, err := config.Load(path)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	got := cfg.Auth.AllowedUsers
	if len(got) != 2 || got[0] != "carol" || got[1] != "dave" {
		t.Errorf("AllowedUsers = %v, want [carol dave]", got)
	}
}

func TestMustLoad_PanicsOnError(t *testing.T) {
	defer func() {
		if r := recover(); r == nil {
			t.Fatal("expected MustLoad to panic on missing file")
		}
	}()
	config.MustLoad("/nonexistent/path/that/should/not/exist.yaml")
}