Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: tokenserver/appengine/impl/serviceaccounts/config.go

Issue 2993023002: tokenserver: Boilerplate for loading and serving service_accounts.cfg. (Closed)
Patch Set: add test Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The LUCI Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package serviceaccounts
16
17 import (
18 "fmt"
19
20 "golang.org/x/net/context"
21
22 "github.com/luci/luci-go/tokenserver/api/admin/v1"
23 "github.com/luci/luci-go/tokenserver/appengine/impl/utils/policy"
24 )
25
26 // serviceAccountsCfg is name of the config file with the policy.
27 //
28 // Also used as a name for the imported configs in the datastore, so change it
29 // very carefully.
30 const serviceAccountsCfg = "service_accounts.cfg"
31
32 // Rules is queryable representation of service_accounts.cfg rules.
33 type Rules struct {
34 revision string // config revision this policy is import ed from
35 rules map[string]*parsedRule // service account email -> rule for it
36 }
37
38 // parsedRule is queriable in-memory representation of ServiceAccountRule.
39 type parsedRule struct {
40 // TODO(vadimsh): Implement.
41 }
42
43 // RulesCache is a stateful object with parsed service_accounts.cfg rules.
44 //
45 // It uses policy.Policy internally to manage datastore-cached copy of imported
46 // service accounts configs.
47 //
48 // Use NewRulesCache() to create a new instance. Each instance owns its own
49 // in-memory cache, but uses same shared datastore cache.
50 //
51 // There's also a process global instance of RulesCache (GlobalRulesCache var)
52 // which is used by the main process. Unit tests don't use it though to avoid
53 // relying on shared state.
54 type RulesCache struct {
55 policy policy.Policy // holds cached *Rules
56 }
57
58 // GlobalRulesCache is the process-wide rules cache.
59 var GlobalRulesCache = NewRulesCache()
60
61 // NewRulesCache properly initializes RulesCache instance.
62 func NewRulesCache() *RulesCache {
63 return &RulesCache{
64 policy: policy.Policy{
65 Name: serviceAccountsCfg, // used as part of datasto re keys
66 Fetch: fetchConfigs, // see below
67 Validate: validateConfigs, // see config_validation.g o
68 Prepare: prepareRules, // see below
69 },
70 }
71 }
72
73 // ImportConfigs refetches service_accounts.cfg and updates the datastore copy.
74 //
75 // Called from cron.
76 func (rc *RulesCache) ImportConfigs(c context.Context) (rev string, err error) {
77 return rc.policy.ImportConfigs(c)
78 }
79
80 // Rules returns in-memory copy of service accounts rules, ready for querying.
81 func (rc *RulesCache) Rules(c context.Context) (*Rules, error) {
82 q, err := rc.policy.Queryable(c)
83 if err != nil {
84 return nil, err
85 }
86 return q.(*Rules), nil
87 }
88
89 // fetchConfigs loads proto messages with rules from the config.
90 func fetchConfigs(c context.Context, f policy.ConfigFetcher) (policy.ConfigBundl e, error) {
91 cfg := &admin.ServiceAccountsPermissions{}
92 if err := f.FetchTextProto(c, serviceAccountsCfg, cfg); err != nil {
93 return nil, err
94 }
95 return policy.ConfigBundle{serviceAccountsCfg: cfg}, nil
96 }
97
98 // prepareRules converts validated configs into *Rules.
99 //
100 // Returns them as policy.Queryable object to satisfy policy.Policy API.
101 func prepareRules(cfg policy.ConfigBundle, revision string) (policy.Queryable, e rror) {
smut 2017/08/04 22:43:42 Is a pointer to a Rules struct a type of policy.Qu
Vadim Sh. 2017/08/04 23:25:29 policy.Queryable is defined here: https://github.c
smut 2017/08/04 23:40:24 Got it, thanks. I did't expect that interfaces wou
102 parsed, ok := cfg[serviceAccountsCfg].(*admin.ServiceAccountsPermissions )
103 if !ok {
104 return nil, fmt.Errorf("wrong type of %s - %T", serviceAccountsC fg, cfg[serviceAccountsCfg])
105 }
106 // TODO(vadimsh): Convert parsed.Rules into map[string]*parsedRule.
107 _ = parsed
108 return &Rules{
109 revision: revision,
110 }, nil
111 }
112
113 // ConfigRevision is part of policy.Queryable interface.
114 func (r *Rules) ConfigRevision() string {
115 return r.revision
116 }
117
118 // TODO(vadimsh): Implement rest of Rules.
OLDNEW
« no previous file with comments | « tokenserver/appengine/backend/main.go ('k') | tokenserver/appengine/impl/serviceaccounts/config_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698