|
15 | 15 | package main |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "encoding/json" |
19 | 18 | "encoding/pem" |
20 | 19 | "net" |
21 | 20 | "net/http" |
22 | 21 | "net/http/httptest" |
23 | 22 | "os" |
24 | 23 | "path/filepath" |
25 | 24 | "strings" |
26 | | - "sync/atomic" |
27 | 25 | "testing" |
28 | | - "time" |
29 | 26 |
|
30 | | - "go.ate.dev/substrate/internal/actoridjwt" |
31 | | - "go.ate.dev/substrate/internal/localjwtauthority" |
32 | | - "go.ate.dev/substrate/internal/objectstoreplugin/actorauthz" |
33 | 27 | exchangepb "go.ate.dev/substrate/internal/proto/objectstoreexchangepb" |
34 | 28 | "go.ate.dev/substrate/internal/testcert" |
35 | 29 | ) |
@@ -89,104 +83,6 @@ func TestNewExchangeService(t *testing.T) { |
89 | 83 | } |
90 | 84 | } |
91 | 85 |
|
92 | | -// TestCheckIssuerCertificate checks that errors other than an untrusted |
93 | | -// certificate let the exchange start. |
94 | | -func TestCheckIssuerCertificate(t *testing.T) { |
95 | | - trusted := httptest.NewTLSServer(http.NotFoundHandler()) |
96 | | - t.Cleanup(trusted.Close) |
97 | | - closed := httptest.NewTLSServer(http.NotFoundHandler()) |
98 | | - closed.Close() |
99 | | - for _, tc := range []struct { |
100 | | - name string |
101 | | - issuer string |
102 | | - client *http.Client |
103 | | - }{ |
104 | | - {"unreachable issuer", closed.URL, closed.Client()}, |
105 | | - {"issuer without discovery", trusted.URL, trusted.Client()}, |
106 | | - } { |
107 | | - t.Run(tc.name, func(t *testing.T) { |
108 | | - if err := checkIssuerCertificate(t.Context(), tc.issuer, "ca.pem", actorauthz.NewVerifier(tc.issuer, tc.client)); err != nil { |
109 | | - t.Fatalf("checkIssuerCertificate() = %v, want nil", err) |
110 | | - } |
111 | | - }) |
112 | | - } |
113 | | -} |
114 | | - |
115 | | -// TestNewActorVerifierWarmsCache checks that the start check fills the cache |
116 | | -// of the verifier the exchange uses, and that a failed start check leaves the |
117 | | -// first exchange to fetch right away. |
118 | | -func TestNewActorVerifierWarmsCache(t *testing.T) { |
119 | | - for _, tc := range []struct { |
120 | | - name string |
121 | | - // startFails makes the issuer answer 503 to the start check only. |
122 | | - startFails bool |
123 | | - // wantFetches is the JWKS fetch count after the start and one Verify. |
124 | | - wantFetches int32 |
125 | | - }{ |
126 | | - {"start check fills the cache", false, 1}, |
127 | | - {"failed start check retries on first use", true, 2}, |
128 | | - } { |
129 | | - t.Run(tc.name, func(t *testing.T) { |
130 | | - auth, err := localjwtauthority.GenerateAuthority("ES256", "k1") |
131 | | - if err != nil { |
132 | | - t.Fatal(err) |
133 | | - } |
134 | | - pool := &localjwtauthority.ConcretePool{Authorities: []*localjwtauthority.Authority{auth}, ActiveForSigning: "k1"} |
135 | | - keys, err := pool.VerificationKeys() |
136 | | - if err != nil { |
137 | | - t.Fatal(err) |
138 | | - } |
139 | | - jwks, err := localjwtauthority.JWKS(keys) |
140 | | - if err != nil { |
141 | | - t.Fatal(err) |
142 | | - } |
143 | | - var fetches atomic.Int32 |
144 | | - failNext := &atomic.Bool{} |
145 | | - failNext.Store(tc.startFails) |
146 | | - mux := http.NewServeMux() |
147 | | - var srv *httptest.Server |
148 | | - mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) { |
149 | | - _ = json.NewEncoder(w).Encode(map[string]string{"issuer": srv.URL, "jwks_uri": srv.URL + "/jwks"}) |
150 | | - }) |
151 | | - mux.HandleFunc("/jwks", func(w http.ResponseWriter, _ *http.Request) { |
152 | | - fetches.Add(1) |
153 | | - if failNext.Swap(false) { |
154 | | - http.Error(w, "down", http.StatusServiceUnavailable) |
155 | | - return |
156 | | - } |
157 | | - _, _ = w.Write(jwks) |
158 | | - }) |
159 | | - srv = httptest.NewTLSServer(mux) |
160 | | - t.Cleanup(srv.Close) |
161 | | - |
162 | | - verifier, err := newActorVerifier(t.Context(), srv.URL, writeServerCA(t, srv)) |
163 | | - if err != nil { |
164 | | - t.Fatal(err) |
165 | | - } |
166 | | - now := time.Now() |
167 | | - token, err := pool.SignJWT(&actoridjwt.Claims{ |
168 | | - Kind: actoridjwt.KindWorkerForActor, |
169 | | - Issuer: srv.URL, |
170 | | - Subject: actoridjwt.KindWorkerForActor.SubjectPrefix() + "/team-a/bot", |
171 | | - Audiences: []string{actorauthz.Audience}, |
172 | | - Expiration: now.Add(time.Hour), |
173 | | - NotBefore: now.Add(-time.Minute), |
174 | | - IssuedAt: now, |
175 | | - Substrate: actoridjwt.SubstrateClaims{Atespace: "team-a", ActorName: "bot", ActorUID: "uid", Node: "node-1"}, |
176 | | - }) |
177 | | - if err != nil { |
178 | | - t.Fatal(err) |
179 | | - } |
180 | | - if _, err := verifier.Verify(t.Context(), token, now); err != nil { |
181 | | - t.Fatalf("Verify() error = %v", err) |
182 | | - } |
183 | | - if got := fetches.Load(); got != tc.wantFetches { |
184 | | - t.Fatalf("JWKS fetches = %d, want %d", got, tc.wantFetches) |
185 | | - } |
186 | | - }) |
187 | | - } |
188 | | -} |
189 | | - |
190 | 86 | // writeServerCA writes srv's certificate to a PEM file and returns its path. |
191 | 87 | func writeServerCA(t *testing.T, srv *httptest.Server) string { |
192 | 88 | t.Helper() |
|
0 commit comments