-
Notifications
You must be signed in to change notification settings - Fork 28
/
common.go
632 lines (550 loc) · 16 KB
/
common.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
* Copyright 2018 The Service Manager Authors
*
* 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 common
import (
"context"
"github.com/Peripli/service-manager/pkg/multitenancy"
"github.com/tidwall/gjson"
"time"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/query"
"github.com/Peripli/service-manager/storage"
"github.com/Peripli/service-manager/pkg/web"
"github.com/onsi/ginkgo"
"github.com/spf13/pflag"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gofrs/uuid"
"strings"
"bytes"
"io"
"io/ioutil"
"github.com/Peripli/service-manager/pkg/types"
. "github.com/onsi/ginkgo"
)
type Object = map[string]interface{}
type Array = []interface{}
type closer interface {
Close()
}
type urler interface {
URL() string
}
type FakeServer interface {
closer
urler
}
type FlagValue struct {
pflagValue pflag.Value
}
func (f FlagValue) Set(s string) error {
return f.pflagValue.Set(s)
}
func (f FlagValue) String() string {
return f.pflagValue.String()
}
func RemoveNonNumericOrDateArgs(obj Object) Object {
return removeOnCondition(isNotNumericOrDate, obj)
}
func RemoveNumericAndDateArgs(obj Object) Object {
return removeOnCondition(isNumericOrDate, obj)
}
func RemoveBooleanArgs(obj Object) Object {
return removeOnCondition(isBoolean, obj)
}
func RemoveNonJSONArgs(obj Object) Object {
return removeOnCondition(isNotJSON, obj)
}
func removeOnCondition(condition func(arg interface{}) bool, obj Object) Object {
o := CopyObject(obj)
for k, v := range o {
if k == "labels" {
labels := v.(map[string]interface{})
for lKey, lValues := range labels {
lVals := lValues.([]interface{})
for index, lValue := range lVals {
if condition(lValue) {
labels[lKey] = append(lVals[:index], lVals[index+1:]...)
}
}
if len(lVals) == 0 {
delete(labels, lKey)
}
}
} else if condition(v) {
delete(o, k)
}
}
return o
}
func isJson(arg interface{}) bool {
if str, ok := arg.(string); ok {
var jsonStr map[string]interface{}
err := json.Unmarshal([]byte(str), &jsonStr)
return err == nil
}
if _, ok := arg.(map[string]interface{}); ok {
return true
}
if _, ok := arg.([]interface{}); ok {
return true
}
return false
}
func isNotJSON(arg interface{}) bool {
return !isJson(arg)
}
func isNumericOrDate(arg interface{}) bool {
if _, err := strconv.Atoi(fmt.Sprintf("%v", arg)); err == nil {
return true
}
if _, err := strconv.ParseFloat(fmt.Sprintf("%v", arg), 64); err == nil {
return true
}
if _, err := time.Parse(time.RFC3339Nano, fmt.Sprintf("%v", arg)); err == nil {
return true
}
return false
}
func isNotNumericOrDate(arg interface{}) bool {
return !isNumericOrDate(arg)
}
func isBoolean(arg interface{}) bool {
_, ok := arg.(bool)
return ok
}
func RemoveNotNullableFieldAndLabels(obj Object, objithMandatoryFields Object) Object {
o := CopyObject(obj)
for objField, objVal := range objithMandatoryFields {
if str, ok := objVal.(string); ok && len(str) == 0 {
//currently api returns empty string for nullable values
continue
}
delete(o, objField)
}
delete(o, "labels")
return o
}
func CopyLabels(obj Object) Object {
result := CopyObject(obj)
return (result["labels"]).(Object)
}
func CopyFields(obj Object) Object {
result := CopyObject(obj)
delete(result, "labels")
return result
}
func CopyObject(obj Object) Object {
o := Object{}
for k, v := range obj {
if k == "labels" {
l := map[string]interface{}{}
for lKey, lValues := range v.(map[string]interface{}) {
temp := []interface{}{}
for _, v := range lValues.([]interface{}) {
l[lKey] = append(temp, v)
}
}
o[k] = l
} else {
o[k] = v
}
}
return o
}
func MapContains(actual Object, expected Object) {
for k, v := range expected {
value, ok := actual[k]
if !ok {
Fail(fmt.Sprintf("Missing property '%s'", k), 1)
}
if value != v {
Fail(
fmt.Sprintf("For property '%s':\nExpected: %s\nActual: %s", k, v, value),
1)
}
}
}
func RemoveResourceByCriterion(testCtx *TestContext, criterion query.Criterion, resourceType types.ObjectType) error {
if err := testCtx.SMRepository.InTransaction(context.TODO(), func(ctx context.Context, storage storage.Repository) error {
if err := storage.Delete(ctx, resourceType, criterion); err != nil {
return err
}
return nil
}); err != nil {
if err != util.ErrNotFoundInStorage {
return err
}
}
return nil
}
func RemoveAllOperations(repository storage.TransactionalRepository) {
removeAll(repository, types.OperationType)
}
func RemoveAllNotifications(repository storage.TransactionalRepository) {
removeAll(repository, types.NotificationType)
}
func RemoveAllInstances(testCtx *TestContext) error {
if err := testCtx.SMRepository.InTransaction(context.TODO(), func(ctx context.Context, storage storage.Repository) error {
objectList, err := storage.List(context.TODO(), types.ServiceInstanceType)
if err != nil {
return err
}
for i := 0; i < objectList.Len(); i++ {
instance := objectList.ItemAt(i).(*types.ServiceInstance)
byID := query.ByField(query.EqualsOperator, "id", instance.ID)
if err := storage.Delete(ctx, types.ServiceInstanceType, byID); err != nil {
return err
}
}
return nil
}); err != nil {
if err != util.ErrNotFoundInStorage {
return err
}
}
return nil
}
func RemoveAllBindings(testCtx *TestContext) error {
if err := testCtx.SMRepository.InTransaction(context.TODO(), func(ctx context.Context, storage storage.Repository) error {
objectList, err := storage.List(context.TODO(), types.ServiceBindingType)
if err != nil {
return err
}
for i := 0; i < objectList.Len(); i++ {
binding := objectList.ItemAt(i).(*types.ServiceBinding)
byID := query.ByField(query.EqualsOperator, "id", binding.ID)
if err := storage.Delete(ctx, types.ServiceBindingType, byID); err != nil {
return err
}
}
return nil
}); err != nil {
if err != util.ErrNotFoundInStorage {
return err
}
}
return nil
}
func RemoveAllBrokers(repository storage.TransactionalRepository) {
removeAll(repository, types.ServiceBrokerType)
}
func RemoveAllPlatforms(repository storage.TransactionalRepository) {
removeAll(repository, types.PlatformType, query.ByField(query.NotEqualsOperator, "id", types.SMPlatform))
}
func RemoveAllVisibilities(repository storage.TransactionalRepository) {
removeAll(repository, types.VisibilityType)
}
func removeAll(repository storage.TransactionalRepository, entity types.ObjectType, queries ...query.Criterion) {
By("removing all " + entity.String())
if err := repository.InTransaction(context.TODO(), func(ctx context.Context, storage storage.Repository) error {
if len(queries) == 0 {
if err := storage.Delete(ctx, entity); err != nil {
return err
}
} else {
if err := storage.Delete(ctx, entity, queries...); err != nil {
return err
}
}
return nil
}); err != nil {
if err != util.ErrNotFoundInStorage {
panic(err)
}
}
}
func RegisterBrokerInSM(brokerJSON Object, SM *SMExpect, headers map[string]string, expectedStatus int) Object {
return SM.POST(web.ServiceBrokersURL).
WithHeaders(headers).
WithJSON(brokerJSON).Expect().Status(expectedStatus).JSON().Object().Raw()
}
func RegisterVisibilityForPlanAndPlatform(SM *SMExpect, planID, platformID string) string {
expect := SM.POST(web.VisibilitiesURL).WithJSON(Object{
"service_plan_id": planID,
"platform_id": platformID,
}).Expect()
return expect.Status(http.StatusCreated).JSON().Object().Value("id").String().Raw()
}
func CreateVisibilitiesForAllBrokerPlans(SM *SMExpect, brokerID string) {
offerings := SM.ListWithQuery(web.ServiceOfferingsURL, fmt.Sprintf("fieldQuery=broker_id eq '%s'", brokerID)).Iter()
offeringIDs := make([]string, 0, len(offerings))
for _, offering := range offerings {
offeringIDs = append(offeringIDs, offering.Object().Value("id").String().Raw())
}
plans := SM.ListWithQuery(web.ServicePlansURL, "fieldQuery="+fmt.Sprintf("service_offering_id in ('%s')", strings.Join(offeringIDs, "','"))).Iter()
for _, p := range plans {
SM.POST(web.VisibilitiesURL).WithJSON(Object{
"service_plan_id": p.Object().Value("id").String().Raw(),
}).Expect().Status(http.StatusCreated)
}
}
func RegisterPlatformInSMQuery(platformJSON Object, SM *SMExpect, headers map[string]string, query string) *types.Platform {
req := SM.POST(web.PlatformsURL).
WithHeaders(headers)
if query != "" {
req = req.WithQueryString(query)
}
reply := req.WithJSON(platformJSON).
Expect().Status(http.StatusCreated).JSON().Object().Raw()
return makePlatformFromResponse(reply)
}
func RegisterPlatformInSM(platformJSON Object, SM *SMExpect, headers map[string]string) *types.Platform {
reply := SM.POST(web.PlatformsURL).
WithHeaders(headers).WithJSON(platformJSON).
Expect().Status(http.StatusCreated).JSON().Object().Raw()
return makePlatformFromResponse(reply)
}
func makePlatformFromResponse(reply map[string]interface{}) *types.Platform {
createdAtString := reply["created_at"].(string)
updatedAtString := reply["updated_at"].(string)
createdAt, err := time.Parse(time.RFC3339Nano, createdAtString)
if err != nil {
panic(err)
}
updatedAt, err := time.Parse(time.RFC3339Nano, updatedAtString)
if err != nil {
panic(err)
}
platform := &types.Platform{
Base: types.Base{
ID: reply["id"].(string),
CreatedAt: createdAt,
UpdatedAt: updatedAt,
Ready: true,
},
Credentials: &types.Credentials{
Basic: &types.Basic{
Username: reply["credentials"].(map[string]interface{})["basic"].(map[string]interface{})["username"].(string),
Password: reply["credentials"].(map[string]interface{})["basic"].(map[string]interface{})["password"].(string),
},
},
Type: reply["type"].(string),
Description: reply["description"].(string),
Name: reply["name"].(string),
}
return platform
}
func generatePrivateKey() *rsa.PrivateKey {
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
return privateKey
}
func ExtractResourceIDs(entities []Object) []string {
result := make([]string, 0, 0)
if entities == nil {
return result
}
for _, value := range entities {
if _, ok := value["id"]; !ok {
panic(fmt.Sprintf("No id found for test resource %v", value))
}
result = append(result, value["id"].(string))
}
return result
}
type jwkResponse struct {
KeyType string `json:"kty"`
Use string `json:"sig"`
KeyID string `json:"kid"`
Algorithm string `json:"alg"`
Value string `json:"value"`
PublicKeyExponent string `json:"e"`
PublicKeyModulus string `json:"n"`
}
func newJwkResponse(keyID string, publicKey rsa.PublicKey) *jwkResponse {
modulus := base64.RawURLEncoding.EncodeToString(publicKey.N.Bytes())
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(publicKey.E))
data = data[:3]
exponent := base64.RawURLEncoding.EncodeToString(data)
return &jwkResponse{
KeyType: "RSA",
Use: "sig",
KeyID: keyID,
Algorithm: "RSA256",
Value: "",
PublicKeyModulus: modulus,
PublicKeyExponent: exponent,
}
}
func MakePlatform(id string, name string, atype string, description string) Object {
return Object{
"id": id,
"name": name,
"type": atype,
"description": description,
}
}
func GenerateRandomNotification() *types.Notification {
uid, err := uuid.NewV4()
if err != nil {
panic(err)
}
return &types.Notification{
Base: types.Base{
ID: uid.String(),
Ready: true,
},
PlatformID: "",
Resource: "notification",
Type: "CREATED",
}
}
func GenerateRandomPlatform() Object {
o := Object{}
for _, key := range []string{"id", "name", "type", "description"} {
UUID, err := uuid.NewV4()
if err != nil {
panic(err)
}
o[key] = UUID.String()
}
return o
}
func GenerateRandomBroker() Object {
o := Object{}
brokerServer := NewBrokerServer()
UUID, err := uuid.NewV4()
if err != nil {
panic(err)
}
UUID2, err := uuid.NewV4()
if err != nil {
panic(err)
}
o = Object{
"name": UUID.String(),
"broker_url": brokerServer.URL(),
"description": UUID2.String(),
"credentials": Object{
"basic": Object{
"username": brokerServer.Username,
"password": brokerServer.Password,
},
},
}
return o
}
func Print(message string, args ...interface{}) {
var err error
if len(args) == 0 {
_, err = fmt.Fprint(ginkgo.GinkgoWriter, "\n"+message+"\n")
} else {
_, err = fmt.Fprintf(ginkgo.GinkgoWriter, "\n"+message+"\n", args...)
}
if err != nil {
panic(err)
}
}
type HTTPReaction struct {
Status int
Body string
Err error
}
type HTTPExpectations struct {
URL string
Body string
Params map[string]string
Headers map[string]string
}
type NopCloser struct {
io.Reader
}
func (NopCloser) Close() error { return nil }
func Closer(s string) io.ReadCloser {
return NopCloser{bytes.NewBufferString(s)}
}
func httpCommonChecks(request *http.Request, reaction *HTTPReaction, checks *HTTPExpectations) (*http.Response, error) {
if checks != nil {
if len(checks.URL) > 0 && !strings.Contains(checks.URL, request.URL.Host) {
Fail(fmt.Sprintf("unexpected URL; expected %v, got %v", checks.URL, request.URL.Path))
}
for k, v := range checks.Headers {
actualValue := request.Header.Get(k)
if e, a := v, actualValue; e != a {
Fail(fmt.Sprintf("unexpected header value for key %q; expected %v, got %v", k, e, a))
}
}
for k, v := range checks.Params {
actualValue := request.URL.Query().Get(k)
if e, a := v, actualValue; e != a {
Fail(fmt.Sprintf("unexpected parameter value for key %q; expected %v, got %v", k, e, a))
}
}
var bodyBytes []byte
if request.Body != nil {
var err error
bodyBytes, err = ioutil.ReadAll(request.Body)
if err != nil {
Fail(fmt.Sprintf("error reading request Body bytes: %v", err))
}
}
if e, a := checks.Body, string(bodyBytes); e != a {
Fail(fmt.Sprintf("unexpected request Body: expected %v, got %v", e, a))
}
}
return &http.Response{
StatusCode: reaction.Status,
Body: Closer(reaction.Body),
Request: request,
}, reaction.Err
}
func DoHTTPWithClient(reaction *HTTPReaction, checks *HTTPExpectations) util.DoRequestWithClientFunc {
return func(request *http.Request, client *http.Client) (*http.Response, error) {
return httpCommonChecks(request, reaction, checks)
}
}
func DoHTTP(reaction *HTTPReaction, checks *HTTPExpectations) util.DoRequestFunc {
return func(request *http.Request) (*http.Response, error) {
return httpCommonChecks(request, reaction, checks)
}
}
type HTTPCouple struct {
Expectations *HTTPExpectations
Reaction *HTTPReaction
}
func DoHTTPSequence(sequence []HTTPCouple) util.DoRequestFunc {
i := 0
return func(request *http.Request) (*http.Response, error) {
r, err := DoHTTP(sequence[i].Reaction, sequence[i].Expectations)(request)
i++
return r, err
}
}
func ExtractTenantFunc(request *web.Request) (string, error) {
extractTenantFromToken := multitenancy.ExtractTenantFromTokenWrapperFunc("zid")
user, ok := web.UserFromContext(request.Context())
if !ok {
return "", nil
}
var userData json.RawMessage
if err := user.Data(&userData); err != nil {
return "", fmt.Errorf("could not unmarshal claims from token: %s", err)
}
clientIDFromToken := gjson.GetBytes([]byte(userData), "cid").String()
if "tenancyClient" != clientIDFromToken {
return "", nil
}
user.AccessLevel = web.TenantAccess
request.Request = request.WithContext(web.ContextWithUser(request.Context(), user))
return extractTenantFromToken(request)
}