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

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

Issue 2997433002: tokenserver: Validate and parse service_accounts.cfg rules. (Closed)
Patch Set: set order 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
1 // Copyright 2017 The LUCI Authors. 1 // Copyright 2017 The LUCI Authors.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 package serviceaccounts 15 package serviceaccounts
16 16
17 import ( 17 import (
18 "fmt" 18 "fmt"
19 19
20 "golang.org/x/net/context" 20 "golang.org/x/net/context"
21 21
22 "github.com/luci/luci-go/common/config/validation"
23 "github.com/luci/luci-go/common/data/stringset"
24
22 "github.com/luci/luci-go/tokenserver/api/admin/v1" 25 "github.com/luci/luci-go/tokenserver/api/admin/v1"
26 "github.com/luci/luci-go/tokenserver/appengine/impl/utils/identityset"
23 "github.com/luci/luci-go/tokenserver/appengine/impl/utils/policy" 27 "github.com/luci/luci-go/tokenserver/appengine/impl/utils/policy"
24 ) 28 )
25 29
26 // serviceAccountsCfg is name of the config file with the policy. 30 // serviceAccountsCfg is name of the config file with the policy.
27 // 31 //
28 // Also used as a name for the imported configs in the datastore, so change it 32 // Also used as a name for the imported configs in the datastore, so change it
29 // very carefully. 33 // very carefully.
30 const serviceAccountsCfg = "service_accounts.cfg" 34 const serviceAccountsCfg = "service_accounts.cfg"
31 35
36 // defaultMaxGrantValidityDuration is value for max_grant_validity_duration if
37 // it isn't specified in the config.
38 const defaultMaxGrantValidityDuration = 24 * 3600
39
32 // Rules is queryable representation of service_accounts.cfg rules. 40 // Rules is queryable representation of service_accounts.cfg rules.
33 type Rules struct { 41 type Rules struct {
34 » revision string // config revision this policy is import ed from 42 » revision string // config revision this policy is imported fro m
35 » rules map[string]*parsedRule // service account email -> rule for it 43 » rules map[string]*Rule // service account email -> rule for it
36 } 44 }
37 45
38 // parsedRule is queriable in-memory representation of ServiceAccountRule. 46 // Rule is queriable in-memory representation of ServiceAccountRule.
39 type parsedRule struct { 47 //
40 » // TODO(vadimsh): Implement. 48 // It should be treated like read-only object. It is shared by many concurrent
49 // requests.
50 type Rule struct {
51 » Rule *admin.ServiceAccountRule // original proto with the rule
52 » AllowedScopes stringset.Set // parsed 'allowed_scope'
53 » EndUsers *identityset.Set // parsed 'end_user'
54 » Proxies *identityset.Set // parsed 'proxy'
41 } 55 }
42 56
43 // RulesCache is a stateful object with parsed service_accounts.cfg rules. 57 // RulesCache is a stateful object with parsed service_accounts.cfg rules.
44 // 58 //
45 // It uses policy.Policy internally to manage datastore-cached copy of imported 59 // It uses policy.Policy internally to manage datastore-cached copy of imported
46 // service accounts configs. 60 // service accounts configs.
47 // 61 //
48 // Use NewRulesCache() to create a new instance. Each instance owns its own 62 // Use NewRulesCache() to create a new instance. Each instance owns its own
49 // in-memory cache, but uses same shared datastore cache. 63 // in-memory cache, but uses same shared datastore cache.
50 // 64 //
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 } 110 }
97 111
98 // prepareRules converts validated configs into *Rules. 112 // prepareRules converts validated configs into *Rules.
99 // 113 //
100 // Returns them as policy.Queryable object to satisfy policy.Policy API. 114 // Returns them as policy.Queryable object to satisfy policy.Policy API.
101 func prepareRules(cfg policy.ConfigBundle, revision string) (policy.Queryable, e rror) { 115 func prepareRules(cfg policy.ConfigBundle, revision string) (policy.Queryable, e rror) {
102 parsed, ok := cfg[serviceAccountsCfg].(*admin.ServiceAccountsPermissions ) 116 parsed, ok := cfg[serviceAccountsCfg].(*admin.ServiceAccountsPermissions )
103 if !ok { 117 if !ok {
104 return nil, fmt.Errorf("wrong type of %s - %T", serviceAccountsC fg, cfg[serviceAccountsCfg]) 118 return nil, fmt.Errorf("wrong type of %s - %T", serviceAccountsC fg, cfg[serviceAccountsCfg])
105 } 119 }
106 » // TODO(vadimsh): Convert parsed.Rules into map[string]*parsedRule. 120
107 » _ = parsed 121 » // Note: per policy.Policy API the config here was already validated whe n it
122 » // was imported, but we double check core assumptions anyway. This check may
123 » // fail if new code (with some new validation rules) uses old configs st ored
124 » // in the datastore (which were validated by old code). In practice this most
125 » // certainly never happens.
126 » rules := map[string]*Rule{}
127 » for _, ruleProto := range parsed.Rules {
128 » » r, err := makeRule(ruleProto)
129 » » if err != nil {
130 » » » return nil, err
131 » » }
132 » » for _, account := range ruleProto.ServiceAccount {
133 » » » if rules[account] != nil {
134 » » » » return nil, fmt.Errorf("two rules for service ac count %q", account)
135 » » » }
136 » » » rules[account] = r
137 » » }
138 » }
139
108 return &Rules{ 140 return &Rules{
109 revision: revision, 141 revision: revision,
142 rules: rules,
110 }, nil 143 }, nil
111 } 144 }
112 145
113 // ConfigRevision is part of policy.Queryable interface. 146 // ConfigRevision is part of policy.Queryable interface.
114 func (r *Rules) ConfigRevision() string { 147 func (r *Rules) ConfigRevision() string {
115 return r.revision 148 return r.revision
116 } 149 }
117 150
118 // TODO(vadimsh): Implement rest of Rules. 151 // Rule returns a rule governing the access to the given service account.
152 //
153 // Returns nil if such service account is not specified in the config.
154 func (r *Rules) Rule(serviceAccount string) *Rule {
155 » return r.rules[serviceAccount]
156 }
157
158 // makeRule converts ServiceAccountRule into queriable Rule.
159 //
160 // Mutates 'ruleProto' in-place filling in defaults.
161 func makeRule(ruleProto *admin.ServiceAccountRule) (*Rule, error) {
162 » v := validation.Context{}
163 » validateRule(ruleProto.Name, ruleProto, &v)
164 » if err := v.Finalize(); err != nil {
165 » » return nil, err
166 » }
167
168 » allowedScopes := stringset.New(len(ruleProto.AllowedScope))
169 » for _, scope := range ruleProto.AllowedScope {
170 » » allowedScopes.Add(scope)
171 » }
172
173 » endUsers, err := identityset.FromStrings(ruleProto.EndUser, nil)
174 » if err != nil {
175 » » return nil, fmt.Errorf("bad 'end_user' set - %s", err)
176 » }
177
178 » proxies, err := identityset.FromStrings(ruleProto.Proxy, nil)
179 » if err != nil {
180 » » return nil, fmt.Errorf("bad 'proxy' set - %s", err)
181 » }
182
183 » if ruleProto.MaxGrantValidityDuration == 0 {
184 » » ruleProto.MaxGrantValidityDuration = defaultMaxGrantValidityDura tion
185 » }
186
187 » return &Rule{
188 » » Rule: ruleProto,
189 » » AllowedScopes: allowedScopes,
190 » » EndUsers: endUsers,
191 » » Proxies: proxies,
192 » }, nil
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698