| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package delegation | 5 package delegation |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 9 | 9 |
| 10 "github.com/golang/protobuf/proto" | 10 "github.com/golang/protobuf/proto" |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 "google.golang.org/grpc" | 12 "google.golang.org/grpc" |
| 13 "google.golang.org/grpc/codes" | 13 "google.golang.org/grpc/codes" |
| 14 | 14 |
| 15 ds "github.com/luci/gae/service/datastore" | 15 ds "github.com/luci/gae/service/datastore" |
| 16 "github.com/luci/gae/service/info" | |
| 17 | 16 |
| 18 "github.com/luci/luci-go/common/config" | |
| 19 "github.com/luci/luci-go/common/logging" | 17 "github.com/luci/luci-go/common/logging" |
| 20 "github.com/luci/luci-go/common/proto/google" | 18 "github.com/luci/luci-go/common/proto/google" |
| 19 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 20 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 21 | 21 |
| 22 "github.com/luci/luci-go/tokenserver/api/admin/v1" | 22 "github.com/luci/luci-go/tokenserver/api/admin/v1" |
| 23 ) | 23 ) |
| 24 | 24 |
| 25 const delegationCfg = "delegation.cfg" | 25 const delegationCfg = "delegation.cfg" |
| 26 | 26 |
| 27 // ImportDelegationConfigsRPC implements Admin.ImportDelegationConfigs method. | 27 // ImportDelegationConfigsRPC implements Admin.ImportDelegationConfigs method. |
| 28 type ImportDelegationConfigsRPC struct { | 28 type ImportDelegationConfigsRPC struct { |
| 29 } | 29 } |
| 30 | 30 |
| 31 // ImportDelegationConfigs fetches configs from from luci-config right now. | 31 // ImportDelegationConfigs fetches configs from from luci-config right now. |
| 32 func (r *ImportDelegationConfigsRPC) ImportDelegationConfigs(c context.Context,
_ *google.Empty) (*admin.ImportedConfigs, error) { | 32 func (r *ImportDelegationConfigsRPC) ImportDelegationConfigs(c context.Context,
_ *google.Empty) (*admin.ImportedConfigs, error) { |
| 33 » cfg, err := fetchConfigFile(c, delegationCfg) | 33 » msg, meta, err := fetchConfigDelegationPermissions(c, delegationCfg) |
| 34 if err != nil { | 34 if err != nil { |
| 35 return nil, grpc.Errorf(codes.Internal, "can't read config file
- %s", err) | 35 return nil, grpc.Errorf(codes.Internal, "can't read config file
- %s", err) |
| 36 } | 36 } |
| 37 » logging.Infof(c, "Importing %q at rev %s", delegationCfg, cfg.Revision) | 37 » logging.Infof(c, "Importing %q at rev %s", delegationCfg, meta.Revision) |
| 38 | 38 |
| 39 // This is returned on successful import. | 39 // This is returned on successful import. |
| 40 successResp := &admin.ImportedConfigs{ | 40 successResp := &admin.ImportedConfigs{ |
| 41 ImportedConfigs: []*admin.ImportedConfigs_ConfigFile{ | 41 ImportedConfigs: []*admin.ImportedConfigs_ConfigFile{ |
| 42 { | 42 { |
| 43 Name: delegationCfg, | 43 Name: delegationCfg, |
| 44 » » » » Revision: cfg.Revision, | 44 » » » » Revision: meta.Revision, |
| 45 }, | 45 }, |
| 46 }, | 46 }, |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Already have this revision in the datastore? | 49 // Already have this revision in the datastore? |
| 50 existing, err := FetchDelegationConfig(c) | 50 existing, err := FetchDelegationConfig(c) |
| 51 if err != nil { | 51 if err != nil { |
| 52 return nil, grpc.Errorf(codes.Internal, "can't read existing con
fig - %s", err) | 52 return nil, grpc.Errorf(codes.Internal, "can't read existing con
fig - %s", err) |
| 53 } | 53 } |
| 54 » if existing.Revision == cfg.Revision { | 54 » if existing.Revision == meta.Revision { |
| 55 » » logging.Infof(c, "Up-to-date at rev %s", cfg.Revision) | 55 » » logging.Infof(c, "Up-to-date at rev %s", meta.Revision) |
| 56 return successResp, nil | 56 return successResp, nil |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Validate the new config before storing. | 59 // Validate the new config before storing. |
| 60 msg := &admin.DelegationPermissions{} | |
| 61 if err = proto.UnmarshalText(cfg.Content, msg); err != nil { | |
| 62 return nil, grpc.Errorf(codes.InvalidArgument, "can't parse conf
ig file - %s", err) | |
| 63 } | |
| 64 if merr := ValidateConfig(msg); len(merr) != 0 { | 60 if merr := ValidateConfig(msg); len(merr) != 0 { |
| 65 » » logging.Errorf(c, "The config at rev %s is invalid: %s", cfg.Rev
ision, merr) | 61 » » logging.Errorf(c, "The config at rev %s is invalid: %s", meta.Re
vision, merr) |
| 66 for _, err := range merr { | 62 for _, err := range merr { |
| 67 logging.Errorf(c, "%s", err) | 63 logging.Errorf(c, "%s", err) |
| 68 } | 64 } |
| 69 return nil, grpc.Errorf(codes.InvalidArgument, "validation error
- %s", merr) | 65 return nil, grpc.Errorf(codes.InvalidArgument, "validation error
- %s", merr) |
| 70 } | 66 } |
| 71 | 67 |
| 72 // Success! | 68 // Success! |
| 73 blob, err := proto.Marshal(msg) | 69 blob, err := proto.Marshal(msg) |
| 74 if err != nil { | 70 if err != nil { |
| 75 return nil, grpc.Errorf(codes.Internal, "can't serialize proto -
%s", err) | 71 return nil, grpc.Errorf(codes.Internal, "can't serialize proto -
%s", err) |
| 76 } | 72 } |
| 77 imported := DelegationConfig{ | 73 imported := DelegationConfig{ |
| 78 » » Revision: cfg.Revision, | 74 » » Revision: meta.Revision, |
| 79 Config: blob, | 75 Config: blob, |
| 80 ParsedConfig: msg, | 76 ParsedConfig: msg, |
| 81 } | 77 } |
| 82 if err := ds.Put(c, &imported); err != nil { | 78 if err := ds.Put(c, &imported); err != nil { |
| 83 return nil, grpc.Errorf(codes.Internal, "failed to store the con
fig - %s", err) | 79 return nil, grpc.Errorf(codes.Internal, "failed to store the con
fig - %s", err) |
| 84 } | 80 } |
| 85 | 81 |
| 86 logging.Infof(c, "Updated delegation config %s => %s", existing.Revision
, imported.Revision) | 82 logging.Infof(c, "Updated delegation config %s => %s", existing.Revision
, imported.Revision) |
| 87 return successResp, nil | 83 return successResp, nil |
| 88 } | 84 } |
| 89 | 85 |
| 90 // fetchConfigFile fetches a file from this services' config set. | 86 // fetchConfigDelegationPermissions fetches a file from this services' config se
t. |
| 91 func fetchConfigFile(c context.Context, path string) (*config.Config, error) { | 87 func fetchConfigDelegationPermissions(c context.Context, path string) (*admin.De
legationPermissions, *cfgclient.Meta, error) { |
| 92 » configSet := "services/" + info.AppID(c) | 88 » configSet := cfgclient.CurrentServiceConfigSet(c) |
| 93 logging.Infof(c, "Reading %q from config set %q", path, configSet) | 89 logging.Infof(c, "Reading %q from config set %q", path, configSet) |
| 94 » c, _ = context.WithTimeout(c, 30*time.Second) // URL fetch deadline | 90 » c, cancelFunc := context.WithTimeout(c, 30*time.Second) // URL fetch dea
dline |
| 95 » return config.GetConfig(c, configSet, path, false) | 91 » defer cancelFunc() |
| 92 |
| 93 » var ( |
| 94 » » meta cfgclient.Meta |
| 95 » » msg admin.DelegationPermissions |
| 96 » ) |
| 97 » if err := cfgclient.Get(c, cfgclient.AsService, configSet, path, textpro
to.Message(&msg), &meta); err != nil { |
| 98 » » return nil, nil, err |
| 99 » } |
| 100 » return &msg, &meta, nil |
| 96 } | 101 } |
| OLD | NEW |