-
Notifications
You must be signed in to change notification settings - Fork 9
/
dam_integrity.go
754 lines (713 loc) · 32.6 KB
/
dam_integrity.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// Copyright 2019 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 dam
import (
"context"
"fmt"
"net/http"
"net/mail"
"regexp"
"sort"
"strconv"
"strings"
"time"
"google.golang.org/grpc/codes" /* copybara-comment */
"google.golang.org/grpc/status" /* copybara-comment */
"github.com/golang/protobuf/proto" /* copybara-comment */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/adapter" /* copybara-comment: adapter */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/check" /* copybara-comment: check */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/ga4gh" /* copybara-comment: ga4gh */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/httputils" /* copybara-comment: httputils */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/oathclients" /* copybara-comment: oathclients */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/persona" /* copybara-comment: persona */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/scim" /* copybara-comment: scim */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/storage" /* copybara-comment: storage */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/strutil" /* copybara-comment: strutil */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/validator" /* copybara-comment: validator */
cpb "github.com/GoogleCloudPlatform/healthcare-federated-access-services/proto/common/v1" /* copybara-comment: go_proto */
pb "github.com/GoogleCloudPlatform/healthcare-federated-access-services/proto/dam/v1" /* copybara-comment: go_proto */
)
const (
cfgVisaTypes = "VisaTypes"
cfgClients = "clients"
cfgOptions = "options"
cfgPolicies = "policies"
cfgResources = "resources"
cfgRoot = "cfg"
cfgServiceTemplates = "serviceTemplates"
cfgTestPersonas = "testPersonas"
cfgTrustedPassportIssuer = "trustedPassportIssuer"
cfgTrustedSources = "trustedSources"
)
var (
interfaceRE = regexp.MustCompile(`\$\{(.*)\}`)
)
// CheckIntegrity returns an error status if the config is invalid.
func (s *Service) CheckIntegrity(cfg *pb.DamConfig, realm string, tx storage.Tx) *status.Status {
return ValidateDAMConfig(cfg, s.ValidateCfgOpts(realm, tx))
}
// ValidateCfgOpts returns the options for checking validity of configuration.
func (s *Service) ValidateCfgOpts(realm string, tx storage.Tx) ValidateCfgOpts {
return ValidateCfgOpts{
Services: s.adapters,
DefaultBroker: s.defaultBroker,
RoleCategories: s.roleCategories,
HidePolicyBasis: s.hidePolicyBasis,
HideRejectDetail: s.hideRejectDetail,
Scim: s.scim,
Realm: realm,
Tx: tx,
}
}
// ValidateCfgOpts contains options for ValidateDAMConfig.
type ValidateCfgOpts struct {
Services *adapter.ServiceAdapters
DefaultBroker string
RoleCategories map[string]*pb.RoleCategory
HidePolicyBasis bool
HideRejectDetail bool
Scim *scim.Scim
Realm string
Tx storage.Tx
}
// ValidateDAMConfig checks that the provided config is valid.
func ValidateDAMConfig(cfg *pb.DamConfig, vopts ValidateCfgOpts) *status.Status {
if vopts.Services == nil {
return httputils.NewStatus(codes.Unavailable, "services not loaded")
}
if st := checkBasicIntegrity(cfg, vopts); st != nil {
return st
}
if st := checkExtraIntegrity(cfg, vopts); st != nil {
return st
}
return nil
}
func checkBasicIntegrity(cfg *pb.DamConfig, vopts ValidateCfgOpts) *status.Status {
for n, ti := range cfg.TrustedIssuers {
if err := checkName(n); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedPassportIssuer, n), err.Error())
}
if !httputils.IsHTTPS(ti.Issuer) && !httputils.IsLocalhost(ti.Issuer) {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedPassportIssuer, n, "issuer"), "trusted identity must have an issuer of type HTTPS")
}
if _, ok := translators[ti.TranslateUsing]; !ok && len(ti.TranslateUsing) > 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedPassportIssuer, n, "translateUsing"), fmt.Sprintf("trusted identity with unknown translator %q", ti.TranslateUsing))
}
if path, err := check.CheckUI(ti.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedPassportIssuer, n, path), fmt.Sprintf("trusted passport issuer UI settings: %v", err))
}
if stat := checkTrustedIssuerClientCredentials(n, vopts.DefaultBroker, ti, vopts); stat != nil {
return stat
}
}
for n, ts := range cfg.TrustedSources {
if err := checkName(n); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedSources, n), err.Error())
}
for i, source := range ts.Sources {
if !httputils.IsHTTPS(source) {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedSources, n, "sources", strconv.Itoa(i)), "trusted source URL must be HTTPS")
}
}
for i, visa := range ts.VisaTypes {
if _, ok := cfg.VisaTypes[visa]; !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedSources, n, "visaTypes", strconv.Itoa(i)), fmt.Sprintf("visa name %q not found in visa type definitions", visa))
}
}
if path, err := check.CheckUI(ts.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedSources, n, path), fmt.Sprintf("trusted sources UI settings: %v", err))
}
}
for n, policy := range cfg.Policies {
if err := checkName(n); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgPolicies, n), err.Error())
}
if path, err := validator.ValidatePolicy(policy, cfg.VisaTypes, cfg.TrustedSources, nil); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgPolicies, n, path), err.Error())
}
if path, err := check.CheckUI(policy.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgPolicies, n, path), fmt.Sprintf("policies UI settings: %v", err))
}
// Note: there is no requirement that built-in policies be present. But if they are, they must not be edited.
// Regular, non-built-in policies must not use reserved UI labels for built-in policies.
builtin, ok := BuiltinPolicies[n]
if ok && !proto.Equal(builtin, policy) {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgPolicies, n), fmt.Sprintf("built-in policy cannot be edited"))
}
if !ok && policy.Ui["source"] != "" {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgPolicies, n, "ui", "source"), fmt.Sprintf("%q label is reserved for built-in policies", "source"))
}
if !ok && policy.Ui["edit"] != "" {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgPolicies, n, "ui", "edit"), fmt.Sprintf("%q label is reserved for built-in policies", "edit"))
}
}
for n, st := range cfg.ServiceTemplates {
if stat := checkServiceTemplate(n, st, cfg, vopts); stat != nil {
return stat
}
}
for n, res := range cfg.Resources {
if err := checkName(n); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, n), err.Error())
}
for i, item := range res.Clients {
if _, ok := cfg.Clients[item]; !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, n, "clients", strconv.Itoa(i)), fmt.Sprintf("client %q does not exist", item))
}
}
if len(res.MaxTokenTtl) > 0 && !ttlRE.Match([]byte(res.MaxTokenTtl)) {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, n, "maxTokenTtl"), "max token TTL invalid format")
}
for vn, view := range res.Views {
if stat := checkViewIntegrity(vn, view, n, res, cfg, vopts); stat != nil {
return stat
}
}
if path, err := check.CheckUI(res.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, n, path), fmt.Sprintf("resource UI settings: %v", err))
}
}
for n, cl := range cfg.Clients {
if err := oathclients.CheckClientIntegrity(n, cl); err != nil {
return status.Convert(err)
}
}
for n, def := range cfg.VisaTypes {
if path, err := check.CheckUI(def.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgVisaTypes, n, path), fmt.Sprintf("claim definitions UI settings: %v", err))
}
}
personaEmail := make(map[string]string)
for n, tp := range cfg.TestPersonas {
if err := checkName(n); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n), err.Error())
}
if tp.Passport == nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "passport"), "persona requires a passport")
}
tid, err := persona.ToIdentity(context.Background(), n, tp, defaultPersonaScope, "")
if err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n), fmt.Sprintf("persona to identity: %v", err))
}
if len(tid.Issuer) == 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "passport", "standardClaims", "iss"), "persona requires an issuer")
}
if len(tid.Subject) == 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "passport", "standardClaims", "sub"), "persona requires a subject")
}
if pmatch, ok := personaEmail[tid.Subject]; ok {
return httputils.NewInfoStatus(codes.AlreadyExists, httputils.StatusPath(cfgTestPersonas, n, "passport", "standardClaims", "sub"), fmt.Sprintf("persona subject %q conflicts with test persona %q", tid.Subject, pmatch))
}
for i, a := range tp.Passport.Ga4GhAssertions {
// Test Persona conditions should meet the same criteria as policies that have no variables / arguments.
policy := &pb.Policy{
AnyOf: a.AnyOfConditions,
}
if path, err := validator.ValidatePolicy(policy, cfg.VisaTypes, cfg.TrustedSources, nil); err != nil {
path = strings.Replace(path, "anyOf/", "anyOfConditions/", 1)
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "passport", "ga4ghAssertions", strconv.Itoa(i), path), err.Error())
}
}
if path, err := check.CheckUI(tp.Ui, false); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, path), fmt.Sprintf("test persona UI settings: %v", err))
}
// Checking persona expectations is in checkExtraIntegrity() to give an
// opportunity for runTests() to catch problems and calculate a ConfigModification
// response.
}
if stat := checkOptionsIntegrity(cfg.Options, vopts); stat != nil {
return stat
}
if path, err := check.CheckUI(cfg.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgRoot, path), fmt.Sprintf("root config UI settings: %v", err))
}
return nil
}
func checkExtraIntegrity(cfg *pb.DamConfig, vopts ValidateCfgOpts) *status.Status {
for n, tp := range cfg.TestPersonas {
for i, access := range tp.Access {
aparts := strings.Split(access, "/")
if len(aparts) != 3 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "access", strconv.Itoa(i)), "invalid access entry format (expecting 'resourceName/viewName/roleName')")
}
rn := aparts[0]
vn := aparts[1]
rolename := aparts[2]
res, ok := cfg.Resources[rn]
if !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "access", strconv.Itoa(i), "resource"), fmt.Sprintf("access entry resource %q not found", rn))
}
view, ok := res.Views[vn]
if !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "access", strconv.Itoa(i), "view"), fmt.Sprintf("access entry view %q not found", vn))
}
roleView := makeView(vn, view, res, cfg, vopts.HidePolicyBasis, vopts.Services)
if roleView.Roles == nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "access", strconv.Itoa(i), "role"), fmt.Sprintf("access entry no roles defined for view %q", vn))
}
if _, ok := roleView.Roles[rolename]; !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, n, "access", strconv.Itoa(i), "role"), fmt.Sprintf("access entry role %q not found on view %q", rolename, vn))
}
}
}
return nil
}
func checkViewIntegrity(name string, view *pb.View, resName string, res *pb.Resource, cfg *pb.DamConfig, vopts ValidateCfgOpts) *status.Status {
if err := checkName(name); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name), err.Error())
}
if len(view.ServiceTemplate) == 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name, "serviceTemplate"), "service template is not defined")
}
st, ok := cfg.ServiceTemplates[view.ServiceTemplate]
if !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name, "serviceTemplate"), fmt.Sprintf("service template %q not found", view.ServiceTemplate))
}
if len(view.Labels) == 0 || view.Labels["version"] == "" {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name, "metadata", "version"), "version is empty")
}
if path, err := checkAccessRequirements(view.ServiceTemplate, st, resName, name, view, cfg, vopts); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, path), fmt.Sprintf("access requirements: %v", err))
}
if len(view.DefaultRole) == 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name, "defaultRole"), "default role is empty")
}
if _, ok := view.Roles[view.DefaultRole]; !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name, "defaultRole"), "default role is not defined within the view")
}
if len(view.ComputedInterfaces) > 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name, "interfaces"), "interfaces should be determined at runtime and cannot be stored as part of the config")
}
if path, err := check.CheckUI(view.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgResources, resName, "views", name, path), fmt.Sprintf("view UI settings: %v", err))
}
return nil
}
func checkServiceTemplate(name string, template *pb.ServiceTemplate, cfg *pb.DamConfig, vopts ValidateCfgOpts) *status.Status {
if err := checkName(name); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgServiceTemplates, name), err.Error())
}
if len(template.ServiceName) == 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgServiceTemplates, name, "serviceName"), "service is not specified")
}
service, ok := vopts.Services.ByServiceName[template.ServiceName]
if !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgServiceTemplates, name, "serviceName", template.ServiceName), "service is not a recognized by this DAM")
}
if path, err := service.CheckConfig(name, template, "", "", nil, cfg, vopts.Services); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, path, err.Error())
}
if path, err := checkServiceRoles(template.ServiceRoles, name, template.ServiceName, cfg, vopts); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, path, err.Error())
}
varNames := make(map[string]bool)
desc := vopts.Services.Descriptors[template.ServiceName]
for varName, v := range desc.ItemVariables {
varNames[varName] = true
if v.Type != "const" && v.Type != "split_pattern" {
return httputils.NewInfoStatus(codes.Internal, httputils.StatusPath("serviceDescriptors", template.ServiceName, "itemVariables", varName, "type"), fmt.Sprintf("variable type %q must be %q or %q", v.Type, "const", "split_pattern"))
}
}
for k, v := range template.Interfaces {
match := interfaceRE.FindAllString(v, -1)
for _, varMatch := range match {
// Remove the `${` prefix and `}` suffix.
varName := varMatch[2 : len(varMatch)-1]
if _, ok := varNames[varName]; !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgServiceTemplates, name, "interfaces", k), fmt.Sprintf("interface %q variable %q not defined for this service", k, varName))
}
}
}
if path, err := check.CheckUI(template.Ui, true); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgServiceTemplates, name, path), fmt.Sprintf("service template UI settings: %v", err))
}
return nil
}
func checkAccessRequirements(templateName string, template *pb.ServiceTemplate, resName, viewName string, view *pb.View, cfg *pb.DamConfig, vopts ValidateCfgOpts) (string, error) {
adapt, ok := vopts.Services.ByServiceName[template.ServiceName]
if !ok {
return httputils.StatusPath("services"), fmt.Errorf("service template %q service %q is not a recognized by this DAM", templateName, template.ServiceName)
}
if path, err := adapt.CheckConfig(templateName, template, resName, viewName, view, cfg, vopts.Services); err != nil {
return path, err
}
if path, err := checkAccessRoles(view.Roles, templateName, template.ServiceName, cfg, vopts); err != nil {
return httputils.StatusPath("views", viewName, "roles", path), fmt.Errorf("invalid view: %v", err)
}
desc, ok := vopts.Services.Descriptors[template.ServiceName]
if !ok {
return httputils.StatusPath("services", template.ServiceName), fmt.Errorf("internal error: service %q does not have a service descriptor", template.ServiceName)
}
if len(desc.ItemVariables) > 0 && len(view.Items) == 0 {
return httputils.StatusPath("views", viewName, "items"), fmt.Errorf("view %q does not provide any target items", viewName)
}
if len(desc.ItemVariables) > 0 && desc.Properties != nil && desc.Properties.SingleItem && len(view.Items) > 1 {
return httputils.StatusPath("views", viewName, "items"), fmt.Errorf("view %q provides more than one item when only one was expected for service %q", viewName, template.ServiceName)
}
for idx, item := range view.Items {
vars, path, err := adapter.GetItemVariables(vopts.Services, template.ServiceName, item)
if err != nil {
return httputils.StatusPath("views", viewName, "items", strconv.Itoa(idx), path), err
}
if len(vars) == 0 {
return httputils.StatusPath("views", viewName, "items", strconv.Itoa(idx), "vars"), fmt.Errorf("no variables defined")
}
}
return "", nil
}
func checkAccessRoles(roles map[string]*pb.ViewRole, templateName, serviceName string, cfg *pb.DamConfig, vopts ValidateCfgOpts) (string, error) {
if len(roles) == 0 {
return "", fmt.Errorf("a view must have at least one role with a selected policy")
}
desc := vopts.Services.Descriptors[serviceName]
for rname, role := range roles {
if err := checkName(rname); err != nil {
return httputils.StatusPath(rname), fmt.Errorf("role has invalid name %q: %v", rname, err)
}
if len(role.ComputedPolicyBasis) > 0 {
return httputils.StatusPath(rname, "roleCategories"), fmt.Errorf("role %q roleCategories should be determined at runtime and cannot be stored as part of the config", rname)
}
if len(role.ComputedPolicyBasis) > 0 {
return httputils.StatusPath(rname, "policyBasis"), fmt.Errorf("role %q policyBasis should be determined at runtime and cannot be stored as part of the config", rname)
}
if len(role.Policies) > 20 {
return httputils.StatusPath(rname, "policies"), fmt.Errorf("role exceeeds policy limit")
}
hasAllowlist := false
for i, p := range role.Policies {
if len(p.Name) == 0 {
return httputils.StatusPath(rname, "policies", strconv.Itoa(i), "name"), fmt.Errorf("access policy name is not defined")
}
if p.Name == allowlistPolicyName {
hasAllowlist = true
emails := strings.Split(p.Args["users"], ";")
if len(emails) > 20 {
return httputils.StatusPath(rname, "policies", strconv.Itoa(i), "args", "users"), fmt.Errorf("number of emails on allowlist policy exceeeds limit")
}
for j, email := range emails {
if _, err := mail.ParseAddress(email); err != nil {
return httputils.StatusPath(rname, "policies", strconv.Itoa(i), "args", "users"), fmt.Errorf("email entry %d (%q) is invalid", j, email)
}
}
}
policy, ok := cfg.Policies[p.Name]
if !ok {
return httputils.StatusPath(rname, "policies", strconv.Itoa(i), "name"), fmt.Errorf("policy %q is not defined", p.Name)
}
if path, err := validator.ValidatePolicy(policy, cfg.VisaTypes, cfg.TrustedSources, p.Args); err != nil {
return httputils.StatusPath(rname, "policies", strconv.Itoa(i), path), err
}
}
if len(role.Policies) == 0 && !desc.Properties.IsAggregate {
return httputils.StatusPath(rname, "policies"), fmt.Errorf("must provide at least one target policy")
}
if hasAllowlist && len(role.Policies) > 1 {
return httputils.StatusPath(rname, "policies"), fmt.Errorf("allowlist policies cannot be used in combination with any other policies")
}
}
return "", nil
}
func checkServiceRoles(roles map[string]*pb.ServiceRole, templateName, serviceName string, cfg *pb.DamConfig, vopts ValidateCfgOpts) (string, error) {
if len(roles) == 0 {
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles"), fmt.Errorf("no roles provided")
}
desc := vopts.Services.Descriptors[serviceName]
for rname, role := range roles {
if err := checkName(rname); err != nil {
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles", rname), fmt.Errorf("role has invalid name %q: %v", rname, err)
}
if len(role.DamRoleCategories) == 0 {
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles", rname, "damRoleCategories"), fmt.Errorf("role %q does not provide a DAM role category", rname)
}
for i, pt := range role.DamRoleCategories {
if _, ok := vopts.RoleCategories[pt]; !ok {
return httputils.StatusPath(
cfgServiceTemplates, templateName, "roles", rname, "damRoleCategories", strconv.Itoa(i)),
fmt.Errorf("role %q DAM role category %q is not defined (valid types are: %s)", rname, pt,
strings.Join(roleCategorySet(vopts.RoleCategories), ", "))
}
}
for vname, def := range desc.ServiceVariables {
arg, ok := role.ServiceArgs[vname]
if !ok {
if def.Optional {
continue
}
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles", rname, "serviceArgs", vname), fmt.Errorf("missing required service argument %q", vname)
}
re, err := regexp.Compile(def.Regexp)
if err != nil {
return httputils.StatusPath("services", templateName, "serviceArgs", vname), fmt.Errorf("variable format regexp %q is not a valid regular expression", def.Regexp)
}
for ival, val := range arg.Values {
if len(val) == 0 {
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles", rname, "serviceArgs", vname, "values", strconv.Itoa(ival)), fmt.Errorf("service argument value %d is empty", ival)
}
if !re.MatchString(val) {
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles", rname, "serviceArgs", vname, "values", strconv.Itoa(ival)), fmt.Errorf("service argument value %q is not valid", val)
}
}
}
for aname := range role.ServiceArgs {
if _, ok := desc.ServiceVariables[aname]; !ok {
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles", rname, "serviceArgs", aname), fmt.Errorf("service argument name %q is not a known input for service %q", aname, serviceName)
}
}
if path, err := check.CheckUI(role.Ui, true); err != nil {
return httputils.StatusPath(cfgServiceTemplates, templateName, "roles", rname, path), fmt.Errorf("role %q: %v", rname, err)
}
}
return "", nil
}
func checkOptionsIntegrity(opts *pb.ConfigOptions, vopts ValidateCfgOpts) *status.Status {
if opts == nil {
return nil
}
// Get the descriptors.
opts = makeConfigOptions(opts)
if err := check.CheckIntOption(opts.AwsManagedKeysPerIamUser, "awsManagedKeysPerIamUser", opts.ComputedDescriptors); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgOptions, "awsManagedKeysPerIamUser"), err.Error())
}
if err := check.CheckStringOption(opts.GcpManagedKeysMaxRequestedTtl, "gcpManagedKeysMaxRequestedTtl", opts.ComputedDescriptors); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgOptions, "gcpManagedKeysMaxRequestedTtl"), err.Error())
}
if err := check.CheckIntOption(opts.GcpManagedKeysPerAccount, "gcpManagedKeysPerAccount", opts.ComputedDescriptors); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgOptions, "gcpManagedKeysPerAccount"), err.Error())
}
if err := check.CheckStringOption(opts.GcpServiceAccountProject, "gcpServiceAccountProject", opts.ComputedDescriptors); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgOptions, "gcpServiceAccountProject"), err.Error())
}
if err := check.CheckStringOption(opts.GcpIamBillingProject, "gcpIamBillingProject", opts.ComputedDescriptors); err != nil {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgOptions, "gcpIamBillingProject"), err.Error())
}
return nil
}
func configCheckIntegrity(cfg *pb.DamConfig, mod *pb.ConfigModification, r *http.Request, vopts ValidateCfgOpts) *status.Status {
bad := codes.InvalidArgument
if err := check.ValidToWriteConfig(getRealm(r), cfg.Options.ReadOnlyMasterRealm); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if len(cfg.Version) == 0 {
return httputils.NewStatus(bad, "missing config version")
}
if cfg.Revision <= 0 {
return httputils.NewStatus(bad, "invalid config revision")
}
if err := configRevision(mod, cfg); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if stat := updateTests(cfg, mod, vopts); stat != nil {
return stat
}
if stat := checkBasicIntegrity(cfg, vopts); stat != nil {
return stat
}
if tests := runTests(r.Context(), cfg, nil, vopts); hasTestError(tests) {
stat := httputils.NewStatus(codes.FailedPrecondition, tests.Error)
return httputils.AddStatusDetails(stat, tests.Modification)
}
if stat := checkExtraIntegrity(cfg, vopts); stat != nil {
return stat
}
return nil
}
func checkTrustedIssuerClientCredentials(name, defaultBroker string, tpi *pb.TrustedIssuer, vopts ValidateCfgOpts) *status.Status {
if name != defaultBroker {
return nil
}
if len(tpi.AuthUrl) == 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedPassportIssuer, name, "authUrl"), "AuthUrl not provided")
}
if len(tpi.TokenUrl) == 0 {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTrustedPassportIssuer, name, "tokenUrl"), "TokenUrl not provided")
}
return nil
}
func checkTrustedIssuer(iss string, cfg *pb.DamConfig, vopts ValidateCfgOpts) *status.Status {
if len(iss) == 0 {
return httputils.NewStatus(codes.PermissionDenied, "unauthorized missing passport issuer")
}
foundIssuer := false
for _, ti := range cfg.TrustedIssuers {
if iss == ti.Issuer {
foundIssuer = true
break
}
}
if !foundIssuer {
return httputils.NewStatus(codes.PermissionDenied, fmt.Sprintf("unauthorized passport issuer %q", iss))
}
return nil
}
func rmTestResource(cfg *pb.DamConfig, name string) {
prefix := name + "/"
for _, p := range cfg.TestPersonas {
p.Access = strutil.FilterStringsByPrefix(p.Access, prefix)
}
}
func rmTestView(cfg *pb.DamConfig, resName, viewName string) {
prefix := resName + "/" + viewName + "/"
for _, p := range cfg.TestPersonas {
p.Access = strutil.FilterStringsByPrefix(p.Access, prefix)
}
}
func updateTests(cfg *pb.DamConfig, modification *pb.ConfigModification, vopts ValidateCfgOpts) *status.Status {
if modification == nil {
return nil
}
for name, td := range modification.TestPersonas {
p, ok := cfg.TestPersonas[name]
if !ok {
return httputils.NewInfoStatus(codes.InvalidArgument, httputils.StatusPath(cfgTestPersonas, name), fmt.Sprintf("test persona %q not found", name))
}
p.Access = td.Access
sort.Strings(p.Access)
}
return nil
}
func runTests(ctx context.Context, cfg *pb.DamConfig, resources []string, vopts ValidateCfgOpts) *pb.GetTestResultsResponse {
t := float64(time.Now().UnixNano()) / 1e9
personas := make(map[string]*cpb.TestPersona)
results := make([]*pb.GetTestResultsResponse_TestResult, 0)
passed := int32(0)
tc := int32(0)
if resources == nil {
resources = make([]string, 0, len(cfg.Resources))
for k := range cfg.Resources {
resources = append(resources, k)
}
}
modification := &pb.ConfigModification{
TestPersonas: make(map[string]*pb.ConfigModification_PersonaModification),
}
for pname, p := range cfg.TestPersonas {
tc++
personas[pname] = &cpb.TestPersona{
Passport: p.Passport,
Access: p.Access,
}
status, got, rejectedVisas, err := testPersona(ctx, pname, resources, cfg, vopts)
e := ""
if err == nil {
passed++
} else {
e = err.Error()
}
results = append(results, &pb.GetTestResultsResponse_TestResult{
Name: pname,
Result: status,
Access: got,
RejectedVisas: makeRejectedVisas(rejectedVisas),
Error: e,
})
calculateModification(pname, p.Access, got, modification, vopts)
}
e := ""
if passed < tc {
e = fmt.Errorf("%d of %d tests passed, %d failed", passed, tc, tc-passed).Error()
}
return &pb.GetTestResultsResponse{
Version: cfg.Version,
Revision: cfg.Revision,
Timestamp: t,
Personas: personas,
TestResults: results,
Executed: tc,
Passed: passed,
Modification: modification,
Error: e,
}
}
func makeRejectedVisas(rejected []*ga4gh.RejectedVisa) []*pb.GetTestResultsResponse_RejectedVisa {
if len(rejected) == 0 {
return nil
}
out := []*pb.GetTestResultsResponse_RejectedVisa{}
for _, reject := range rejected {
out = append(out, &pb.GetTestResultsResponse_RejectedVisa{
Reason: reject.Rejection.Reason,
Field: reject.Rejection.Field,
Description: reject.Rejection.Description,
VisaType: string(reject.Assertion.Type),
Source: string(reject.Assertion.Source),
Value: string(reject.Assertion.Value),
By: string(reject.Assertion.By),
})
}
return out
}
func hasTestError(tr *pb.GetTestResultsResponse) bool {
return len(tr.Error) > 0
}
func calculateModification(name string, want []string, got []string, modification *pb.ConfigModification, vopts ValidateCfgOpts) {
entry, ok := modification.TestPersonas[name]
if !ok {
entry = &pb.ConfigModification_PersonaModification{
Access: got,
AddAccess: []string{},
RemoveAccess: []string{},
}
modification.TestPersonas[name] = entry
}
deltaResourceModification(entry, want, got, vopts)
if len(entry.AddAccess) == 0 && len(entry.RemoveAccess) == 0 {
delete(modification.TestPersonas, name)
}
}
func deltaResourceModification(entry *pb.ConfigModification_PersonaModification, want []string, got []string, vopts ValidateCfgOpts) bool {
// Assumes view list entries are sorted on both |want| and |got|.
var add []string
var rm []string
w := 0
g := 0
wl := 0
if want != nil {
wl = len(want)
}
gl := 0
if got != nil {
gl = len(got)
}
for w < wl || g < gl {
if w >= wl {
add = append(add, got[g:]...)
break
}
if g >= gl {
rm = append(rm, want[w:]...)
break
}
if c := strings.Compare(want[w], got[g]); c == 0 {
w++
g++
} else if c < 0 {
rm = append(rm, want[w])
w++
} else {
add = append(add, got[g])
g++
}
}
if len(add) == 0 && len(rm) == 0 {
return false
}
if len(add) > 0 {
entry.AddAccess = append(entry.AddAccess, add...)
}
if len(rm) > 0 {
entry.RemoveAccess = append(entry.RemoveAccess, rm...)
}
return true
}