-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathacl.go
350 lines (302 loc) · 11.4 KB
/
acl.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
package client
import (
"time"
"github.com/armon/go-metrics"
"github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/nomad/structs"
)
const (
// policyCacheSize is the number of ACL policies to keep cached. Policies have a fetching cost
// so we keep the hot policies cached to reduce the ACL token resolution time.
policyCacheSize = 64
// aclCacheSize is the number of ACL objects to keep cached. ACLs have a parsing and
// construction cost, so we keep the hot objects cached to reduce the ACL token resolution time.
aclCacheSize = 64
// tokenCacheSize is the number of bearer tokens, ACL and workload identity,
// to keep cached. Tokens have a fetching cost, so we keep the hot tokens
// cached to reduce the lookups.
tokenCacheSize = 128
// roleCacheSize is the number of ACL roles to keep cached. Looking up
// roles requires an RPC call, so we keep the hot roles cached to reduce
// the number of lookups.
roleCacheSize = 64
)
// clientACLResolver holds the state required for client resolution
// of ACLs
type clientACLResolver struct {
// aclCache is used to maintain the parsed ACL objects
aclCache *structs.ACLCache[*acl.ACL]
// policyCache is used to maintain the fetched policy objects
policyCache *structs.ACLCache[*structs.ACLPolicy]
// tokenCache is used to maintain the fetched token objects
tokenCache *structs.ACLCache[*structs.AuthenticatedIdentity]
// roleCache is used to maintain a cache of the fetched ACL roles. Each
// entry is keyed by the role ID.
roleCache *structs.ACLCache[*structs.ACLRole]
}
// init is used to setup the client resolver state
func (c *clientACLResolver) init() {
c.aclCache = structs.NewACLCache[*acl.ACL](aclCacheSize)
c.policyCache = structs.NewACLCache[*structs.ACLPolicy](policyCacheSize)
c.tokenCache = structs.NewACLCache[*structs.AuthenticatedIdentity](tokenCacheSize)
c.roleCache = structs.NewACLCache[*structs.ACLRole](roleCacheSize)
}
// ResolveToken is used to translate an ACL Token Secret ID or workload
// identity into an ACL object, nil if ACLs are disabled, or an error.
func (c *Client) ResolveToken(bearerToken string) (*acl.ACL, error) {
a, _, err := c.resolveTokenAndACL(bearerToken)
return a, err
}
func (c *Client) resolveTokenAndACL(bearerToken string) (*acl.ACL, *structs.AuthenticatedIdentity, error) {
// Fast-path if ACLs are disabled
if !c.GetConfig().ACLEnabled {
return nil, nil, nil
}
defer metrics.MeasureSince([]string{"client", "acl", "resolve_token"}, time.Now())
// Resolve the token value
ident, err := c.resolveTokenValue(bearerToken)
if err != nil {
return nil, nil, err
}
// Only allow ACLs and workload identities to call client RPCs
if ident.ACLToken == nil && ident.Claims == nil {
return nil, nil, structs.ErrTokenNotFound
}
// Give the token expiry some slight leeway in case the client and server
// clocks are skewed.
if ident.IsExpired(time.Now().Add(2 * time.Second)) {
return nil, nil, structs.ErrTokenExpired
}
var policies []*structs.ACLPolicy
// Resolve token policies
if token := ident.ACLToken; token != nil {
// Check if this is a management token
if ident.ACLToken.Type == structs.ACLManagementToken {
return acl.ManagementACL, ident, nil
}
// Resolve the policy links within the token ACL roles.
policyNames, err := c.resolveTokenACLRoles(bearerToken, token.Roles)
if err != nil {
return nil, nil, err
}
// Generate a slice of all policy names included within the token, taken
// from both the ACL roles and the direct assignments.
policyNames = append(policyNames, token.Policies...)
// Resolve ACL token policies
if policies, err = c.resolvePolicies(token.SecretID, policyNames); err != nil {
return nil, nil, err
}
} else {
// Resolve policies for workload identities
policyArgs := structs.GenericRequest{
QueryOptions: structs.QueryOptions{
AuthToken: bearerToken,
Region: c.Region(),
},
}
policyReply := structs.ACLPolicySetResponse{}
if err := c.RPC("ACL.GetClaimPolicies", &policyArgs, &policyReply); err != nil {
return nil, nil, err
}
policies = make([]*structs.ACLPolicy, 0, len(policyReply.Policies))
for _, p := range policyReply.Policies {
policies = append(policies, p)
}
}
// Resolve the ACL object
aclObj, err := structs.CompileACLObject(c.aclCache, policies)
if err != nil {
return nil, nil, err
}
return aclObj, ident, nil
}
// resolveTokenValue is used to translate a bearer token, either an ACL token's
// secret or a workload identity, into an ACL token with caching We use a local
// cache up to the TTL limit, and then resolve via a server. If we cannot
// reach a server, but have a cached value we extend the TTL to gracefully handle outages.
func (c *Client) resolveTokenValue(bearerToken string) (*structs.AuthenticatedIdentity, error) {
// Hot-path the anonymous token
if bearerToken == "" {
return &structs.AuthenticatedIdentity{ACLToken: structs.AnonymousACLToken}, nil
}
// Lookup the token entry in the cache
entry, ok := c.tokenCache.Get(bearerToken)
if ok {
if entry.Age() <= c.GetConfig().ACLTokenTTL {
return entry.Get(), nil
}
}
// Lookup the token
req := structs.GenericRequest{
QueryOptions: structs.QueryOptions{
AuthToken: bearerToken,
Region: c.Region(),
AllowStale: true,
},
}
var resp structs.ACLWhoAmIResponse
if err := c.RPC("ACL.WhoAmI", &req, &resp); err != nil {
// If we encounter an error but have a cached value, mask the error and extend the cache
if ok {
c.logger.Warn("failed to resolve token, using expired cached value", "error", err)
return entry.Get(), nil
}
return nil, err
}
// Cache the response (positive or negative)
c.tokenCache.Add(bearerToken, resp.Identity)
return resp.Identity, nil
}
// resolvePolicies is used to translate a set of named ACL policies into the objects.
// We cache the policies locally, and fault them from a server as necessary. Policies
// are cached for a TTL, and then refreshed. If a server cannot be reached, the cache TTL
// will be ignored to gracefully handle outages.
func (c *Client) resolvePolicies(secretID string, policies []string) ([]*structs.ACLPolicy, error) {
var out []*structs.ACLPolicy
var expired []*structs.ACLPolicy
var missing []string
// Scan the cache for each policy
for _, policyName := range policies {
// Lookup the policy in the cache
entry, ok := c.policyCache.Get(policyName)
if !ok {
missing = append(missing, policyName)
continue
}
// Check if the cached value is valid or expired
if entry.Age() <= c.GetConfig().ACLPolicyTTL {
out = append(out, entry.Get())
} else {
expired = append(expired, entry.Get())
}
}
// Hot-path if we have no missing or expired policies
if len(missing)+len(expired) == 0 {
return out, nil
}
// Lookup the missing and expired policies
fetch := missing
for _, p := range expired {
fetch = append(fetch, p.Name)
}
req := structs.ACLPolicySetRequest{
Names: fetch,
QueryOptions: structs.QueryOptions{
Region: c.Region(),
AuthToken: secretID,
AllowStale: true,
},
}
var resp structs.ACLPolicySetResponse
if err := c.RPC("ACL.GetPolicies", &req, &resp); err != nil {
// If we encounter an error but have cached policies, mask the error and extend the cache
if len(missing) == 0 {
c.logger.Warn("failed to resolve policies, using expired cached value", "error", err)
out = append(out, expired...)
return out, nil
}
return nil, err
}
// Handle each output
for _, policy := range resp.Policies {
c.policyCache.Add(policy.Name, policy)
out = append(out, policy)
}
// Return the valid policies
return out, nil
}
// resolveTokenACLRoles is used to unpack an ACL roles and their policy
// assignments into a list of ACL policy names. This can then be used to
// compile an ACL object.
//
// When roles need to be looked up from state via server RPC, we may use the
// expired cache version. This can only occur if we can fully resolve the role
// via the cache.
func (c *Client) resolveTokenACLRoles(secretID string, roleLinks []*structs.ACLTokenRoleLink) ([]string, error) {
var (
// policyNames tracks the resolved ACL policies which are linked to the
// role. This is the output object and represents the authorisation
// this role provides token bearers.
policyNames []string
// missingRoleIDs are the roles linked which are not found within our
// cache. These must be looked up from the server via and RPC, so we
// can correctly identify the policy links.
missingRoleIDs []string
// expiredRoleIDs are the roles linked which have been found within our
// cache, but are expired. These must be looked up from the server via
// and RPC, so we can correctly identify the policy links.
expiredRoleIDs []string
)
for _, roleLink := range roleLinks {
// Look within the cache to see if the role is already present. If we
// do not find it, add the ID to our tracking, so we look this up via
// RPC.
entry, ok := c.roleCache.Get(roleLink.ID)
if !ok {
missingRoleIDs = append(missingRoleIDs, roleLink.ID)
continue
}
// If the cached value is expired, add the ID to our tracking, so we
// look this up via RPC. Otherwise, iterate the policy links and add
// each policy name to our return object tracking.
if entry.Age() <= c.GetConfig().ACLRoleTTL {
for _, policyLink := range entry.Get().Policies {
policyNames = append(policyNames, policyLink.Name)
}
} else {
expiredRoleIDs = append(expiredRoleIDs, entry.Get().ID)
}
}
// Hot-path: we were able to resolve all ACL roles via the cache and
// generate a list of linked policy names. Therefore, we can avoid making
// any RPC calls.
if len(missingRoleIDs)+len(expiredRoleIDs) == 0 {
return policyNames, nil
}
// Created a combined list of role IDs that we need to lookup from server
// state.
roleIDsToFetch := missingRoleIDs
roleIDsToFetch = append(roleIDsToFetch, expiredRoleIDs...)
// Generate an RPC request to detail all the ACL roles that we did not find
// or were expired within the cache.
roleByIDReq := structs.ACLRolesByIDRequest{
ACLRoleIDs: roleIDsToFetch,
QueryOptions: structs.QueryOptions{
Region: c.Region(),
AuthToken: secretID,
AllowStale: true,
},
}
var roleByIDResp structs.ACLRolesByIDResponse
// Perform the RPC call to detail the required ACL roles. If the RPC call
// fails, and we are only updating expired cache entries, use the expired
// entries. This allows use to handle intermittent failures.
err := c.RPC(structs.ACLGetRolesByIDRPCMethod, &roleByIDReq, &roleByIDResp)
if err != nil {
if len(missingRoleIDs) == 0 {
c.logger.Warn("failed to resolve ACL roles, using expired cached value", "error", err)
for _, aclRole := range roleByIDResp.ACLRoles {
for _, rolePolicyLink := range aclRole.Policies {
policyNames = append(policyNames, rolePolicyLink.Name)
}
}
return policyNames, nil
}
return nil, err
}
// Generate a timestamp for the cache entry. We do not need to use a
// timestamp per ACL role response integration.
now := time.Now()
for _, aclRole := range roleByIDResp.ACLRoles {
// Add an entry to the cache using the generated timestamp for future
// expiry calculations. Any existing, expired entry will be
// overwritten.
c.roleCache.AddAtTime(aclRole.ID, aclRole, now)
// Iterate the role policy links, extracting the name and adding this
// to our return response tracking.
for _, rolePolicyLink := range aclRole.Policies {
policyNames = append(policyNames, rolePolicyLink.Name)
}
}
return policyNames, nil
}