-
Notifications
You must be signed in to change notification settings - Fork 193
/
mock_http_roundtrip.go
375 lines (319 loc) · 12.3 KB
/
mock_http_roundtrip.go
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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mockgcp
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/workflows"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockaiplatform"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockalloydb"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockapikeys"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockartifactregistry"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockbigquery"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockbilling"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockcertificatemanager"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockcloudfunctions"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockcompute"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockcontainer"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockedgecontainer"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockedgenetwork"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockgkehub"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockgkemulticloud"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockiam"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocklogging"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmonitoring"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocknetworkservices"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockprivateca"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockpubsub"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockpubsublite"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockredis"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockresourcemanager"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocksecretmanager"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockservicenetworking"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockserviceusage"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocksql"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockstorage"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
)
type mockRoundTripper struct {
grpcConnection *grpc.ClientConn
grpcListener net.Listener
hosts map[string]http.Handler
iamPolicies *mockIAMPolicies
}
// MockService is the interface implemented by all services
type MockService interface {
// Register initializes the service, normally registering the GRPC service.
Register(grpcServer *grpc.Server)
// NewHTTPMux creates an HTTP mux for serving http traffic
NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error)
// ExpectedHost is the hostname we serve on e.g. foo.googleapis.com
ExpectedHost() string
}
func NewMockRoundTripper(t *testing.T, k8sClient client.Client, storage storage.Storage) *mockRoundTripper {
ctx := context.Background()
mockRoundTripper := &mockRoundTripper{}
mockHTTPClient := &http.Client{
Transport: mockRoundTripper,
}
env := &common.MockEnvironment{
KubeClient: k8sClient,
}
workflowEngine, err := workflows.NewEngine(mockHTTPClient)
if err != nil {
t.Fatalf("building workflow engine: %v", err)
}
env.Workflows = workflowEngine
resourcemanagerService := mockresourcemanager.New(env, storage)
env.Projects = resourcemanagerService.GetProjectStore()
var serverOpts []grpc.ServerOption
server := grpc.NewServer(serverOpts...)
mockRoundTripper.hosts = make(map[string]http.Handler)
var services []MockService
services = append(services, resourcemanagerService)
services = append(services, mockaiplatform.New(env, storage))
services = append(services, mockapikeys.New(env, storage))
services = append(services, mockbigquery.New(env, storage))
services = append(services, mockbilling.New(env, storage))
services = append(services, mockcontainer.New(env, storage))
services = append(services, mockcertificatemanager.New(env, storage))
services = append(services, mockcompute.New(env, storage))
services = append(services, mockgkemulticloud.New(env, storage))
services = append(services, mockiam.New(env, storage))
services = append(services, mocklogging.New(env, storage))
services = append(services, mocksecretmanager.New(env, storage))
services = append(services, mockprivateca.New(env, storage))
services = append(services, mockmonitoring.New(env, storage))
services = append(services, mockpubsublite.New(env, storage))
services = append(services, mocknetworkservices.New(env, storage))
services = append(services, mockpubsub.New(env, storage))
services = append(services, mockredis.New(env, storage))
services = append(services, mockservicenetworking.New(env, storage))
services = append(services, mockserviceusage.New(env, storage))
services = append(services, mocksql.New(env, storage))
services = append(services, mockstorage.New(env, storage))
services = append(services, mockcloudfunctions.New(env, storage))
services = append(services, mockedgenetwork.New(env, storage))
services = append(services, mockedgecontainer.New(env, storage))
services = append(services, mockartifactregistry.New(env, storage))
services = append(services, mockgkehub.New(env, storage))
services = append(services, mockalloydb.New(env, storage))
for _, service := range services {
service.Register(server)
}
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("net.Listen failed: %v", err)
}
mockRoundTripper.grpcListener = listener
go func() {
if err := server.Serve(listener); err != nil {
t.Errorf("error from grpc server: %v", err)
}
}()
t.Cleanup(func() {
server.Stop()
})
endpoint := listener.Addr().String()
var opts []grpc.DialOption
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.DialContext(ctx, endpoint, opts...)
if err != nil {
t.Fatalf("error dialing grpc endpoint %q: %v", endpoint, err)
}
mockRoundTripper.grpcConnection = conn
for _, service := range services {
mux, err := service.NewHTTPMux(ctx, conn)
if err != nil {
t.Fatalf("error building mux: %v", err)
}
mockRoundTripper.hosts[service.ExpectedHost()] = mux
}
mockRoundTripper.iamPolicies = newMockIAMPolicies()
return mockRoundTripper
}
func (m *mockRoundTripper) prefilterRequest(req *http.Request) error {
if req.Body != nil {
var requestBody bytes.Buffer
if _, err := io.Copy(&requestBody, req.Body); err != nil {
return fmt.Errorf("error reading request body: %w", err)
}
s := requestBody.String()
s, err := m.modifyUpdateMask(s)
if err != nil {
return err
}
req.Body = io.NopCloser(strings.NewReader(s))
}
return nil
}
// modifyUpdateMask fixes up the updateMask parameter, which is a proto FieldMask.
// Technically, when transported over JSON it should be passed as json fields (displayName),
// and when transported over proto is should be passed as proto fields (display_name).
// However, because GCP APIs seem to accept display_name or displayName over JSON.
// If we don't map display_name => displayName, the proto validation will reject it.
// e.g. https://github.com/grpc-ecosystem/grpc-gateway/issues/2239
func (m *mockRoundTripper) modifyUpdateMask(s string) (string, error) {
if len(s) == 0 {
return "", nil
}
o := make(map[string]any)
if err := json.Unmarshal([]byte(s), &o); err != nil {
return "", fmt.Errorf("parsing json: %w", err)
}
for k, v := range o {
switch k {
case "updateMask":
vString := v.(string)
tokens := strings.Split(vString, ",")
for i, token := range tokens {
switch token {
case "display_name":
tokens[i] = "displayName"
}
}
o[k] = strings.Join(tokens, ",")
}
}
b, err := json.Marshal(o)
if err != nil {
return "", fmt.Errorf("building json: %w", err)
}
return string(b), nil
}
// roundTripIAMPolicy serves the IAM policy verbs (e.g. :getIamPolicy)
// These are implemented on most resources, and rather than mock them
// per-resource, we implement them once here.
func (m *mockRoundTripper) roundTripIAMPolicy(req *http.Request) (*http.Response, error) {
requestPath := req.URL.Path
lastColon := strings.LastIndex(requestPath, ":")
verb := requestPath[lastColon+1:]
requestPath = strings.TrimSuffix(requestPath, ":"+verb)
switch verb {
case "getIamPolicy":
if req.Method == "GET" || req.Method == "POST" {
resourcePath := req.URL.Host + requestPath
return m.iamPolicies.serveGetIAMPolicy(resourcePath)
} else {
response := &http.Response{
StatusCode: http.StatusMethodNotAllowed,
Status: "method not supported",
Body: io.NopCloser(strings.NewReader("{}")),
}
return response, nil
}
case "setIamPolicy":
if req.Method == "POST" {
resourcePath := req.URL.Host + requestPath
return m.iamPolicies.serveSetIAMPolicy(resourcePath, req)
} else {
response := &http.Response{
StatusCode: http.StatusMethodNotAllowed,
Status: "method not supported",
Body: io.NopCloser(strings.NewReader("{}")),
}
return response, nil
}
default:
return &http.Response{
StatusCode: http.StatusNotFound,
Status: "not found",
Body: io.NopCloser(strings.NewReader("{}")),
}, nil
}
}
func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
log.Printf("request: %v %v", req.Method, req.URL)
requestPath := req.URL.Path
if strings.HasSuffix(requestPath, ":getIamPolicy") || strings.HasSuffix(requestPath, ":setIamPolicy") {
return m.roundTripIAMPolicy(req)
}
mux := m.hosts[req.Host]
if mux != nil {
if err := m.prefilterRequest(req); err != nil {
return nil, err
}
var body bytes.Buffer
w := &bufferedResponseWriter{body: &body, header: make(http.Header)}
mux.ServeHTTP(w, req)
response := &http.Response{}
response.Body = ioutil.NopCloser(&body)
response.Header = w.header
if w.statusCode == 0 {
w.statusCode = 200
}
response.Status = fmt.Sprintf("%d %s", w.statusCode, http.StatusText(w.statusCode))
response.StatusCode = w.statusCode
return response, nil
}
request := fmt.Sprintf("%s %s", req.Method, req.URL)
body := make(map[string]interface{})
response := &http.Response{
StatusCode: 403,
Status: "mockRoundTripper injecting fake response",
}
if request == "GET https://openidconnect.googleapis.com/v1/userinfo?alt=json" {
body["email"] = "test@example.com"
response.StatusCode = 200
} else {
log.Printf("host name %q not known. "+
"Please verify the ExpectedHost in service.go and retry.", req.Host)
}
if body != nil {
j, err := json.Marshal(body)
if err != nil {
panic("json.Marshal failed")
}
log.Printf("response: %d %s", response.StatusCode, string(j))
response.Body = ioutil.NopCloser(bytes.NewReader(j))
} else {
log.Printf("response: %d %s", response.StatusCode, "-")
}
return response, nil
}
// bufferedResponseWriter implements http.ResponseWriter and stores the response.
type bufferedResponseWriter struct {
statusCode int
body io.Writer
header http.Header
}
var _ http.ResponseWriter = &bufferedResponseWriter{}
// Header implements http.ResponseWriter
func (w *bufferedResponseWriter) Header() http.Header {
return w.header
}
// Write implements http.ResponseWriter
func (w *bufferedResponseWriter) Write(b []byte) (int, error) {
if w.statusCode == 0 {
w.statusCode = 200
}
return w.body.Write(b)
}
// WriteHeader implements http.ResponseWriter
func (w *bufferedResponseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
}