|
| 1 | +package web |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/pem" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/forgekeep/nebula-mesh/internal/config" |
| 14 | +) |
| 15 | + |
| 16 | +// writeIDPCAPEM writes the TLS mock IdP's self-signed certificate to a PEM file |
| 17 | +// and returns its path, for use as oidc.tls_ca_cert. |
| 18 | +func writeIDPCAPEM(t *testing.T, idp *mockIDP) string { |
| 19 | + t.Helper() |
| 20 | + cert := idp.server.Certificate() |
| 21 | + if cert == nil { |
| 22 | + t.Fatal("TLS mock IdP has no certificate") |
| 23 | + } |
| 24 | + pemBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}) |
| 25 | + path := filepath.Join(t.TempDir(), "idp-ca.pem") |
| 26 | + if err := os.WriteFile(path, pemBytes, 0o600); err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + return path |
| 30 | +} |
| 31 | + |
| 32 | +// TestOIDCTLSPin_DiscoverySucceedsWithPin verifies that pinning the IdP's CA |
| 33 | +// lets discovery succeed against an otherwise-untrusted self-signed TLS IdP. |
| 34 | +func TestOIDCTLSPin_DiscoverySucceedsWithPin(t *testing.T) { |
| 35 | + _, s := newTestWeb(t) |
| 36 | + idp := setupOIDCServerTLS(t) |
| 37 | + caPath := writeIDPCAPEM(t, idp) |
| 38 | + |
| 39 | + o := newOIDCFromMock(t, idp, s, config.OIDCConfig{ |
| 40 | + AllowedEmails: []string{"alice@example.com"}, |
| 41 | + DefaultRole: "user", |
| 42 | + TLSCACert: caPath, |
| 43 | + }) |
| 44 | + if o == nil { |
| 45 | + t.Fatal("expected non-nil OIDC with valid CA pin") |
| 46 | + } |
| 47 | + if o.httpClient == nil { |
| 48 | + t.Error("expected pinned httpClient to be set when tls_ca_cert is configured") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// TestOIDCTLSPin_DiscoveryFailsWithoutPin verifies that without pinning, the |
| 53 | +// self-signed TLS IdP is rejected by the system trust store at discovery. |
| 54 | +func TestOIDCTLSPin_DiscoveryFailsWithoutPin(t *testing.T) { |
| 55 | + _, s := newTestWeb(t) |
| 56 | + idp := setupOIDCServerTLS(t) |
| 57 | + |
| 58 | + cfg := config.OIDCConfig{ |
| 59 | + Enabled: true, |
| 60 | + Issuer: idp.Issuer(), |
| 61 | + ClientID: "test-client", |
| 62 | + ClientSecret: "test-secret", |
| 63 | + RedirectURL: "https://nebula-mesh.test/ui/oidc/callback", |
| 64 | + AllowedEmails: []string{"alice@example.com"}, |
| 65 | + DefaultRole: "user", |
| 66 | + // No TLSCACert: the self-signed cert is not trusted. |
| 67 | + } |
| 68 | + sm := NewSessionManager(s) |
| 69 | + _, err := NewOIDC(context.Background(), &cfg, s, sm, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 70 | + if err == nil { |
| 71 | + t.Fatal("expected discovery to fail against untrusted self-signed IdP without pinning") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestOIDCTLSPin_BadCAFile verifies a missing or malformed CA file fails NewOIDC |
| 76 | +// loudly instead of silently falling back to the system store. |
| 77 | +func TestOIDCTLSPin_BadCAFile(t *testing.T) { |
| 78 | + _, s := newTestWeb(t) |
| 79 | + idp := setupOIDCServerTLS(t) |
| 80 | + sm := NewSessionManager(s) |
| 81 | + |
| 82 | + base := config.OIDCConfig{ |
| 83 | + Enabled: true, |
| 84 | + Issuer: idp.Issuer(), |
| 85 | + ClientID: "test-client", |
| 86 | + ClientSecret: "test-secret", |
| 87 | + RedirectURL: "https://nebula-mesh.test/ui/oidc/callback", |
| 88 | + AllowedEmails: []string{"alice@example.com"}, |
| 89 | + DefaultRole: "user", |
| 90 | + } |
| 91 | + |
| 92 | + t.Run("missing file", func(t *testing.T) { |
| 93 | + cfg := base |
| 94 | + cfg.TLSCACert = filepath.Join(t.TempDir(), "does-not-exist.pem") |
| 95 | + if _, err := NewOIDC(context.Background(), &cfg, s, sm, slog.New(slog.NewTextHandler(io.Discard, nil))); err == nil { |
| 96 | + t.Fatal("expected error for missing CA file") |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("garbage file", func(t *testing.T) { |
| 101 | + bad := filepath.Join(t.TempDir(), "garbage.pem") |
| 102 | + if err := os.WriteFile(bad, []byte("not a pem certificate"), 0o600); err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + cfg := base |
| 106 | + cfg.TLSCACert = bad |
| 107 | + if _, err := NewOIDC(context.Background(), &cfg, s, sm, slog.New(slog.NewTextHandler(io.Discard, nil))); err == nil { |
| 108 | + t.Fatal("expected error for non-PEM CA file") |
| 109 | + } |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +// TestOIDCTLSPin_FullCallback drives the full callback flow (token exchange + |
| 114 | +// id_token verification) against the TLS IdP with pinning, proving the pinned |
| 115 | +// client is used end to end, not just for discovery. |
| 116 | +func TestOIDCTLSPin_FullCallback(t *testing.T) { |
| 117 | + _, s := newTestWeb(t) |
| 118 | + idp := setupOIDCServerTLS(t) |
| 119 | + caPath := writeIDPCAPEM(t, idp) |
| 120 | + |
| 121 | + o := newOIDCFromMock(t, idp, s, config.OIDCConfig{ |
| 122 | + AllowedEmails: []string{"alice@example.com"}, |
| 123 | + DefaultRole: "user", |
| 124 | + TLSCACert: caPath, |
| 125 | + }) |
| 126 | + |
| 127 | + idp.NextIDToken(map[string]any{ |
| 128 | + "sub": "alice-sub", |
| 129 | + "aud": "test-client", |
| 130 | + "email": "alice@example.com", |
| 131 | + "email_verified": true, |
| 132 | + "preferred_username": "alice", |
| 133 | + "name": "Alice", |
| 134 | + }) |
| 135 | + |
| 136 | + rec := driveCallback(t, o, "state-tlspin", "code-1") |
| 137 | + if rec.Code != http.StatusSeeOther { |
| 138 | + t.Fatalf("status = %d, want 303; body=%s", rec.Code, rec.Body.String()) |
| 139 | + } |
| 140 | +} |
0 commit comments