|
| 1 | +package oidc |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "net/url" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "k8s.io/client-go/discovery" |
| 10 | + "k8s.io/client-go/rest" |
| 11 | +) |
| 12 | + |
| 13 | +func makeRESTClient(t *testing.T, ts *httptest.Server) rest.Interface { |
| 14 | + t.Helper() |
| 15 | + u, err := url.Parse(ts.URL) |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("parse server url: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + cfg := &rest.Config{ |
| 21 | + Host: u.Host, |
| 22 | + } |
| 23 | + |
| 24 | + discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(cfg, ts.Client()) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("new discovery client: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + return discoveryClient.RESTClient() |
| 30 | +} |
| 31 | + |
| 32 | +func TestFetch_Success(t *testing.T) { |
| 33 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 34 | + switch r.URL.Path { |
| 35 | + case "/.well-known/openid-configuration": |
| 36 | + w.Header().Set("Content-Type", "application/json") |
| 37 | + _, _ = w.Write([]byte(`{"issuer":"https://example"}`)) |
| 38 | + case "/openid/v1/jwks": |
| 39 | + w.Header().Set("Content-Type", "application/json") |
| 40 | + _, _ = w.Write([]byte(`{"keys":[]}`)) |
| 41 | + default: |
| 42 | + http.NotFound(w, r) |
| 43 | + } |
| 44 | + })) |
| 45 | + defer ts.Close() |
| 46 | + |
| 47 | + rc := makeRESTClient(t, ts) |
| 48 | + g := &DataGathererOIDC{cl: rc} |
| 49 | + |
| 50 | + anyRes, count, err := g.Fetch() |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("Fetch returned error: %v", err) |
| 53 | + } |
| 54 | + if count != 1 { |
| 55 | + t.Fatalf("expected count 1, got %d", count) |
| 56 | + } |
| 57 | + |
| 58 | + res, ok := anyRes.(OIDCDiscoveryData) |
| 59 | + if !ok { |
| 60 | + t.Fatalf("unexpected result type: %T", anyRes) |
| 61 | + } |
| 62 | + |
| 63 | + if res.OIDCConfig == nil { |
| 64 | + t.Fatalf("expected OIDCConfig, got nil") |
| 65 | + } |
| 66 | + if iss, _ := res.OIDCConfig["issuer"].(string); iss != "https://example" { |
| 67 | + t.Fatalf("unexpected issuer: %v", res.OIDCConfig["issuer"]) |
| 68 | + } |
| 69 | + |
| 70 | + if res.JWKS == nil { |
| 71 | + t.Fatalf("expected JWKS, got nil") |
| 72 | + } |
| 73 | + if _, ok := res.JWKS["keys"].([]any); !ok { |
| 74 | + t.Fatalf("expected keys to be a slice, got %#v", res.JWKS["keys"]) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestFetch_Errors(t *testing.T) { |
| 79 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 80 | + switch r.URL.Path { |
| 81 | + case "/.well-known/openid-configuration": |
| 82 | + // return server error |
| 83 | + http.Error(w, "boom", http.StatusInternalServerError) |
| 84 | + case "/openid/v1/jwks": |
| 85 | + // return invalid JSON |
| 86 | + w.Header().Set("Content-Type", "application/json") |
| 87 | + _, _ = w.Write([]byte(`}{`)) |
| 88 | + default: |
| 89 | + http.NotFound(w, r) |
| 90 | + } |
| 91 | + })) |
| 92 | + defer ts.Close() |
| 93 | + |
| 94 | + rc := makeRESTClient(t, ts) |
| 95 | + g := &DataGathererOIDC{cl: rc} |
| 96 | + |
| 97 | + anyRes, _, err := g.Fetch() |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Fetch returned error: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + res, ok := anyRes.(OIDCDiscoveryData) |
| 103 | + if !ok { |
| 104 | + t.Fatalf("unexpected result type: %T", anyRes) |
| 105 | + } |
| 106 | + |
| 107 | + if res.OIDCConfig != nil { |
| 108 | + t.Fatalf("expected nil OIDCConfig on error, got %#v", res.OIDCConfig) |
| 109 | + } |
| 110 | + if res.OIDCConfigError != "failed to get OIDC discovery document: an error on the server (\"boom\") has prevented the request from succeeding" { |
| 111 | + t.Fatalf("unexpected OIDCConfigError: %q", res.OIDCConfigError) |
| 112 | + } |
| 113 | + if res.JWKS != nil { |
| 114 | + t.Fatalf("expected nil JWKS on malformed JSON, got %#v", res.JWKS) |
| 115 | + } |
| 116 | + if res.JWKSError != "failed to unmarshal JWKS response: invalid character '}' looking for beginning of value" { |
| 117 | + t.Fatalf("unexpected JWKSError: %q", res.JWKSError) |
| 118 | + } |
| 119 | +} |
0 commit comments