|
15 | 15 | package ateapiauth |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "encoding/base64" |
18 | 19 | "os" |
19 | 20 | "path/filepath" |
20 | 21 | "strings" |
21 | 22 | "testing" |
22 | 23 | ) |
23 | 24 |
|
| 25 | +func TestLoadAuthenticationConfigDerivesIssuer(t *testing.T) { |
| 26 | + dir := t.TempDir() |
| 27 | + tokenPath := filepath.Join(dir, "token") |
| 28 | + payload := base64.RawURLEncoding.EncodeToString([]byte(`{"iss":"https://kubernetes.default.svc.cluster.local"}`)) |
| 29 | + if err := os.WriteFile(tokenPath, []byte("header."+payload+".signature"), 0o600); err != nil { |
| 30 | + t.Fatal(err) |
| 31 | + } |
| 32 | + configPath := filepath.Join(dir, "authentication.yaml") |
| 33 | + config := "actorIdentityJWTProvider: kubernetes\njwtProviders:\n- name: kubernetes\n audiences: [api.ate-system.svc]\n discoveryTokenFile: " + tokenPath + "\n" |
| 34 | + if err := os.WriteFile(configPath, []byte(config), 0o600); err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + cfg, err := LoadAuthenticationConfig(configPath) |
| 39 | + if err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + if got, want := cfg.JWTProviders[0].Issuer, "https://kubernetes.default.svc.cluster.local"; got != want { |
| 43 | + t.Fatalf("Issuer = %q, want %q", got, want) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestLoadAuthenticationConfigKeepsExplicitIssuer(t *testing.T) { |
| 48 | + path := filepath.Join(t.TempDir(), "authentication.yaml") |
| 49 | + if err := os.WriteFile(path, []byte(` |
| 50 | +actorIdentityJWTProvider: explicit |
| 51 | +jwtProviders: |
| 52 | +- name: explicit |
| 53 | + issuer: https://issuer.example |
| 54 | + audiences: [audience] |
| 55 | + certificateAuthorityFile: /does/not/exist |
| 56 | + discoveryTokenFile: /does/not/exist |
| 57 | +`), 0o600); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + cfg, err := LoadAuthenticationConfig(path) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + if got := cfg.JWTProviders[0].Issuer; got != "https://issuer.example" { |
| 65 | + t.Fatalf("Issuer = %q, want explicit issuer", got) |
| 66 | + } |
| 67 | +} |
| 68 | + |
24 | 69 | func TestLoadAuthenticationConfig(t *testing.T) { |
25 | 70 | path := filepath.Join(t.TempDir(), "authentication.yaml") |
26 | 71 | if err := os.WriteFile(path, []byte(` |
|
0 commit comments