-
Notifications
You must be signed in to change notification settings - Fork 9
/
configadmin.go
487 lines (443 loc) · 15.3 KB
/
configadmin.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
// 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 ic
import (
"fmt"
"net/http"
"strconv"
"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/check" /* copybara-comment: check */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/ga4gh" /* copybara-comment: ga4gh */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/handlerfactory" /* copybara-comment: handlerfactory */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/httputils" /* copybara-comment: httputils */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/storage" /* copybara-comment: storage */
cpb "github.com/GoogleCloudPlatform/healthcare-federated-access-services/proto/common/v1" /* copybara-comment: go_proto */
pb "github.com/GoogleCloudPlatform/healthcare-federated-access-services/proto/ic/v1" /* copybara-comment: go_proto */
)
// HTTP handler for ".../config"
func (s *Service) configFactory() *handlerfactory.Options {
return &handlerfactory.Options{
TypeName: "config",
PathPrefix: configPath,
HasNamedIdentifiers: false,
Service: func() handlerfactory.Service {
return &config{
s: s,
input: &pb.ConfigRequest{},
}
},
}
}
type config struct {
s *Service
input *pb.ConfigRequest
cfg *pb.IcConfig
id *ga4gh.Identity
}
func (c *config) Setup(r *http.Request, tx storage.Tx) (int, error) {
cfg, _, id, status, err := c.s.handlerSetup(tx, r, noScope, c.input)
c.cfg = cfg
c.id = id
return status, err
}
func (c *config) LookupItem(r *http.Request, name string, vars map[string]string) bool {
// Trival name as there is only one config.
return true
}
func (c *config) NormalizeInput(r *http.Request, name string, vars map[string]string) error {
if err := httputils.DecodeProtoReq(c.input, r); err != nil {
return err
}
if c.input.Item == nil {
c.input.Item = &pb.IcConfig{}
}
if c.input.Modification == nil {
c.input.Modification = &pb.ConfigModification{}
}
if c.input.Item.IdentityProviders == nil {
c.input.Item.IdentityProviders = make(map[string]*cpb.IdentityProvider)
}
if c.input.Item.Clients == nil {
c.input.Item.Clients = make(map[string]*cpb.Client)
}
if c.input.Item.Options == nil {
c.input.Item.Options = &pb.ConfigOptions{}
}
c.input.Item.Options = receiveConfigOptions(c.input.Item.Options)
return nil
}
func (c *config) Get(r *http.Request, name string) (proto.Message, error) {
return makeConfig(c.cfg), nil
}
func (c *config) Post(r *http.Request, name string) (proto.Message, error) {
return nil, fmt.Errorf("POST not allowed")
}
func (c *config) Put(r *http.Request, name string) (proto.Message, error) {
if getRealm(r) != storage.DefaultRealm && !check.ClientsEqual(c.input.Item.Clients, c.cfg.Clients) {
return nil, status.Errorf(codes.PermissionDenied, "modify clients is only allowed on master realm")
}
if c.cfg.Version != c.input.Item.Version {
// TODO: consider upgrading older config versions automatically.
return nil, fmt.Errorf("PUT of config version %q mismatched with existing config version %q", c.input.Item.Version, c.cfg.Version)
}
// Retain the revision number (it will be incremented upon saving).
c.input.Item.Revision = c.cfg.Revision
return nil, nil
}
func (c *config) Patch(r *http.Request, name string) (proto.Message, error) {
return nil, fmt.Errorf("PATCH not allowed")
}
func (c *config) Remove(r *http.Request, name string) (proto.Message, error) {
return nil, fmt.Errorf("DELETE not allowed")
}
func (c *config) CheckIntegrity(r *http.Request) *status.Status {
bad := codes.InvalidArgument
if err := check.ValidToWriteConfig(getRealm(r), c.cfg.Options.ReadOnlyMasterRealm); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if len(c.input.Item.Version) == 0 {
return httputils.NewStatus(bad, "missing config version")
}
if c.input.Item.Revision <= 0 {
return httputils.NewStatus(bad, "invalid config revision")
}
if err := configRevision(c.input.Modification, c.cfg); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if err := c.s.checkConfigIntegrity(c.input.Item); err != nil {
return httputils.NewStatus(bad, err.Error())
}
return nil
}
func (c *config) Save(r *http.Request, tx storage.Tx, name string, vars map[string]string, desc, typeName string) error {
if err := c.s.saveConfig(c.input.Item, desc, typeName, r, c.id, c.cfg, c.input.Item, c.input.Modification, tx); err != nil {
return err
}
secrets, err := c.s.loadSecrets(tx)
if err != nil {
return err
}
// Assumes that secrets don't change within this handler.
if c.s.useHydra && !check.ClientsEqual(c.input.Item.Clients, c.cfg.Clients) {
if _, err = c.s.syncToHydra(c.input.Item.Clients, secrets.ClientSecrets, 0, tx); err != nil {
return err
}
}
return nil
}
// HTTP handler for ".../config/identityProviders/{name}"
func (s *Service) configIdpFactory() *handlerfactory.Options {
return &handlerfactory.Options{
TypeName: "configIDP",
PathPrefix: configIdentityProvidersPath,
HasNamedIdentifiers: true,
Service: func() handlerfactory.Service {
return &configIDP{
s: s,
input: &pb.ConfigIdentityProviderRequest{},
}
},
}
}
type configIDP struct {
s *Service
r *http.Request
input *pb.ConfigIdentityProviderRequest
item *cpb.IdentityProvider
save *cpb.IdentityProvider
sec *pb.IcSecrets
saveSecret *pb.IcSecrets
cfg *pb.IcConfig
id *ga4gh.Identity
tx storage.Tx
}
func (c *configIDP) Setup(r *http.Request, tx storage.Tx) (int, error) {
cfg, sec, id, status, err := c.s.handlerSetup(tx, r, noScope, c.input)
c.cfg = cfg
c.sec = sec
c.id = id
c.tx = tx
if c.sec.IdProviderSecrets == nil {
c.sec.IdProviderSecrets = map[string]string{}
}
return status, err
}
func (c *configIDP) LookupItem(r *http.Request, name string, vars map[string]string) bool {
if item, ok := c.cfg.IdentityProviders[name]; ok {
c.item = item
return true
}
return false
}
func (c *configIDP) NormalizeInput(r *http.Request, name string, vars map[string]string) error {
if err := httputils.DecodeProtoReq(c.input, r); err != nil {
return err
}
if c.input.Item == nil {
c.input.Item = &cpb.IdentityProvider{}
}
if c.input.Item.Scopes == nil {
c.input.Item.Scopes = []string{}
}
if c.input.Item.Ui == nil {
c.input.Item.Ui = make(map[string]string)
}
return nil
}
func (c *configIDP) Get(r *http.Request, name string) (proto.Message, error) {
return c.item, nil
}
func (c *configIDP) Post(r *http.Request, name string) (proto.Message, error) {
c.save = c.input.Item
c.cfg.IdentityProviders[name] = c.save
if err := c.modifyClientSecret(name); err != nil {
return nil, err
}
return nil, nil
}
func (c *configIDP) Put(r *http.Request, name string) (proto.Message, error) {
c.save = c.input.Item
c.cfg.IdentityProviders[name] = c.save
if err := c.modifyClientSecret(name); err != nil {
return nil, err
}
return nil, nil
}
func (c *configIDP) Patch(r *http.Request, name string) (proto.Message, error) {
c.save = &cpb.IdentityProvider{}
proto.Merge(c.save, c.item)
proto.Merge(c.save, c.input.Item)
c.save.Scopes = c.input.Item.Scopes
c.save.Ui = c.input.Item.Ui
c.cfg.IdentityProviders[name] = c.save
if err := c.modifyClientSecret(name); err != nil {
return nil, err
}
return nil, nil
}
func (c *configIDP) Remove(r *http.Request, name string) (proto.Message, error) {
delete(c.cfg.IdentityProviders, name)
c.save = &cpb.IdentityProvider{}
if len(c.item.ClientId) > 0 {
c.saveSecret = c.sec
delete(c.saveSecret.IdProviderSecrets, c.item.ClientId)
}
return nil, nil
}
func (c *configIDP) CheckIntegrity(r *http.Request) *status.Status {
bad := codes.InvalidArgument
if err := check.ValidToWriteConfig(getRealm(r), c.cfg.Options.ReadOnlyMasterRealm); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if err := configRevision(c.input.Modification, c.cfg); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if err := c.s.checkConfigIntegrity(c.cfg); err != nil {
return httputils.NewStatus(bad, err.Error())
}
return nil
}
func (c *configIDP) Save(r *http.Request, tx storage.Tx, name string, vars map[string]string, desc, typeName string) error {
if c.input.Modification != nil && c.input.Modification.DryRun {
return nil
}
if c.save != nil {
if err := c.s.saveConfig(c.cfg, desc, typeName, r, c.id, c.item, c.save, c.input.Modification, c.tx); err != nil {
return err
}
}
if c.saveSecret != nil {
if err := c.s.saveSecrets(c.saveSecret, desc, typeName, r, c.id, tx); err != nil {
return err
}
}
return nil
}
// modifyClientSecret when request includes clientSecret and clientId is set.
func (c *configIDP) modifyClientSecret(name string) error {
if len(c.input.ClientSecret) > 0 {
if len(c.save.ClientId) == 0 {
return status.Errorf(codes.InvalidArgument, "update trusted issuer %q client_secret but missing client_id", name)
}
c.saveSecret = c.sec
c.saveSecret.IdProviderSecrets[c.input.Item.ClientId] = c.input.ClientSecret
}
return nil
}
// HTTP handler for ".../config/options"
func (s *Service) configOptionsFactory() *handlerfactory.Options {
return &handlerfactory.Options{
TypeName: "configOptions",
PathPrefix: configOptionsPath,
HasNamedIdentifiers: false,
Service: func() handlerfactory.Service {
return &configOptions{
s: s,
input: &pb.ConfigOptionsRequest{},
}
},
}
}
type configOptions struct {
s *Service
r *http.Request
input *pb.ConfigOptionsRequest
item *pb.ConfigOptions
save *pb.ConfigOptions
cfg *pb.IcConfig
id *ga4gh.Identity
tx storage.Tx
}
func (c *configOptions) Setup(r *http.Request, tx storage.Tx) (int, error) {
cfg, _, id, status, err := c.s.handlerSetup(tx, r, noScope, c.input)
c.cfg = cfg
c.id = id
c.tx = tx
return status, err
}
func (c *configOptions) LookupItem(r *http.Request, name string, vars map[string]string) bool {
c.item = c.cfg.Options
return true
}
func (c *configOptions) NormalizeInput(r *http.Request, name string, vars map[string]string) error {
if err := httputils.DecodeProtoReq(c.input, r); err != nil {
return err
}
if c.input.Item == nil {
c.input.Item = &pb.ConfigOptions{}
}
c.input.Item = receiveConfigOptions(c.input.Item)
return nil
}
func (c *configOptions) Get(r *http.Request, name string) (proto.Message, error) {
return makeConfigOptions(c.item), nil
}
func (c *configOptions) Post(r *http.Request, name string) (proto.Message, error) {
c.save = c.input.Item
c.cfg.Options = c.save
return nil, nil
}
func (c *configOptions) Put(r *http.Request, name string) (proto.Message, error) {
c.save = c.input.Item
c.cfg.Options = c.save
return nil, nil
}
func (c *configOptions) Patch(r *http.Request, name string) (proto.Message, error) {
c.save = &pb.ConfigOptions{}
proto.Merge(c.save, c.item)
proto.Merge(c.save, c.input.Item)
c.save.ReadOnlyMasterRealm = c.input.Item.ReadOnlyMasterRealm
c.cfg.Options = c.save
return nil, nil
}
func (c *configOptions) Remove(r *http.Request, name string) (proto.Message, error) {
return nil, fmt.Errorf("DELETE not allowed")
}
func (c *configOptions) CheckIntegrity(r *http.Request) *status.Status {
bad := codes.InvalidArgument
if err := check.ValidToWriteConfig(getRealm(r), c.cfg.Options.ReadOnlyMasterRealm); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if err := configRevision(c.input.Modification, c.cfg); err != nil {
return httputils.NewStatus(bad, err.Error())
}
if err := c.s.checkConfigIntegrity(c.cfg); err != nil {
return httputils.NewStatus(bad, err.Error())
}
return nil
}
func (c *configOptions) Save(r *http.Request, tx storage.Tx, name string, vars map[string]string, desc, typeName string) error {
if c.save == nil || (c.input.Modification != nil && c.input.Modification.DryRun) {
return nil
}
if err := c.s.saveConfig(c.cfg, desc, typeName, r, c.id, c.item, c.save, c.input.Modification, c.tx); err != nil {
return err
}
return nil
}
// HTTP handler for ".../config/clients/{name}"
// ....
// HTTP handler for ".../config/options"
// ConfigHistory implements the HistoryConfig RPC method.
func (s *Service) ConfigHistory(w http.ResponseWriter, r *http.Request) {
// TODO: consider requiring an "admin" scope (modify all admin handlerSetup calls).
_, _, _, sts, err := s.handlerSetup(nil, r, noScope, nil)
if err != nil {
httputils.WriteError(w, status.Errorf(httputils.RPCCode(sts), "%v", err))
return
}
h, sts, err := storage.GetHistory(s.store, storage.ConfigDatatype, getRealm(r), storage.DefaultUser, storage.DefaultID, r)
if err != nil {
httputils.WriteError(w, status.Errorf(httputils.RPCCode(sts), "%v", err))
}
httputils.WriteResp(w, h)
}
// ConfigHistoryRevision implements the HistoryRevisionConfig RPC method.
func (s *Service) ConfigHistoryRevision(w http.ResponseWriter, r *http.Request) {
name := getName(r)
rev, err := strconv.ParseInt(name, 10, 64)
if err != nil {
httputils.WriteError(w, status.Errorf(codes.InvalidArgument, "invalid history revision: %q (must be a positive integer)", name))
return
}
_, _, _, sts, err := s.handlerSetup(nil, r, noScope, nil)
if err != nil {
httputils.WriteError(w, status.Errorf(httputils.RPCCode(sts), "%v", err))
return
}
cfg := &pb.IcConfig{}
if sts, err := s.realmReadTx(storage.ConfigDatatype, getRealm(r), storage.DefaultUser, storage.DefaultID, rev, cfg, nil); err != nil {
httputils.WriteError(w, status.Errorf(httputils.RPCCode(sts), "%v", err))
return
}
httputils.WriteResp(w, cfg)
}
// ConfigReset implements the corresponding method in the IC API.
func (s *Service) ConfigReset(w http.ResponseWriter, r *http.Request) {
_, _, _, sts, err := s.handlerSetup(nil, r, noScope, nil)
if err != nil {
httputils.WriteError(w, status.Errorf(httputils.RPCCode(sts), "%v", err))
return
}
if _, err = s.store.Wipe(r.Context(), storage.AllRealms, 0, 0); err != nil {
httputils.WriteError(w, status.Errorf(codes.Internal, "%v", err))
return
}
if err = ImportConfig(s.store, s.serviceName, nil, true, true, true); err != nil {
httputils.WriteError(w, status.Errorf(codes.Internal, "%v", err))
return
}
// Reset clients in Hyrdra
if s.useHydra {
if getRealm(r) != storage.DefaultRealm {
return
}
conf, err := s.loadConfig(nil, storage.DefaultRealm)
if err != nil {
httputils.WriteError(w, status.Errorf(codes.Unavailable, "%v", err))
return
}
secrets, err := s.loadSecrets(nil)
if err != nil {
httputils.WriteError(w, status.Errorf(codes.Unavailable, "%v", err))
return
}
if _, err := s.syncToHydra(conf.Clients, secrets.ClientSecrets, 0, nil); err != nil {
httputils.WriteError(w, status.Errorf(codes.Unavailable, "%v", err))
return
}
}
}