-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
auth_tenant.go
376 lines (324 loc) · 11.9 KB
/
auth_tenant.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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package rpc
import (
"context"
"strconv"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/logtags"
"google.golang.org/grpc"
)
// tenantAuthorizer authorizes RPCs sent by tenants to a node's tenant RPC
// server, that is, it ensures that the request only accesses resources
// available to the tenant.
type tenantAuthorizer struct {
// tenantID is the tenant ID for the current node.
// Equals SystemTenantID when running a KV node.
tenantID roachpb.TenantID
}
func tenantFromCommonName(commonName string) (roachpb.TenantID, error) {
tenID, err := strconv.ParseUint(commonName, 10, 64)
if err != nil {
return roachpb.TenantID{}, authErrorf("could not parse tenant ID from Common Name (CN): %s", err)
}
if tenID < roachpb.MinTenantID.ToUint64() || tenID > roachpb.MaxTenantID.ToUint64() {
return roachpb.TenantID{}, authErrorf("invalid tenant ID %d in Common Name (CN)", tenID)
}
return roachpb.MakeTenantID(tenID), nil
}
// authorize enforces a security boundary around endpoints that tenants
// request from the host KV node or other tenant SQL pod.
func (a tenantAuthorizer) authorize(
tenID roachpb.TenantID, fullMethod string, req interface{},
) error {
switch fullMethod {
case "/cockroach.roachpb.Internal/Batch":
return a.authBatch(tenID, req.(*roachpb.BatchRequest))
case "/cockroach.roachpb.Internal/RangeLookup":
return a.authRangeLookup(tenID, req.(*roachpb.RangeLookupRequest))
case "/cockroach.roachpb.Internal/RangeFeed":
return a.authRangeFeed(tenID, req.(*roachpb.RangeFeedRequest))
case "/cockroach.roachpb.Internal/GossipSubscription":
return a.authGossipSubscription(tenID, req.(*roachpb.GossipSubscriptionRequest))
case "/cockroach.roachpb.Internal/TokenBucket":
return a.authTokenBucket(tenID, req.(*roachpb.TokenBucketRequest))
case "/cockroach.roachpb.Internal/TenantSettings":
return a.authTenantSettings(tenID, req.(*roachpb.TenantSettingsRequest))
case "/cockroach.rpc.Heartbeat/Ping":
return nil // no restriction to usage of this endpoint by tenants
case "/cockroach.server.serverpb.Status/Regions":
return nil // no restriction to usage of this endpoint by tenants
case "/cockroach.server.serverpb.Status/Statements":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/CombinedStatementStats":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/ResetSQLStats":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/ListContentionEvents":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/ListLocalContentionEvents":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/ListSessions":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/ListLocalSessions":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/IndexUsageStatistics":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/CancelSession":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/CancelLocalSession":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/CancelQuery":
return a.authTenant(tenID)
case "/cockroach.server.serverpb.Status/CancelLocalQuery":
return a.authTenant(tenID)
case "/cockroach.roachpb.Internal/GetSpanConfigs":
return a.authGetSpanConfigs(tenID, req.(*roachpb.GetSpanConfigsRequest))
case "/cockroach.roachpb.Internal/UpdateSpanConfigs":
return a.authUpdateSpanConfigs(tenID, req.(*roachpb.UpdateSpanConfigsRequest))
default:
return authErrorf("unknown method %q", fullMethod)
}
}
// authBatch authorizes the provided tenant to invoke the Batch RPC with the
// provided args.
func (a tenantAuthorizer) authBatch(tenID roachpb.TenantID, args *roachpb.BatchRequest) error {
// Consult reqMethodAllowlist to determine whether each request in the batch
// is permitted. If not, reject the entire batch.
for _, ru := range args.Requests {
if !reqAllowed(ru.GetInner()) {
return authErrorf("request [%s] not permitted", args.Summary())
}
}
// All keys in the request must reside within the tenant's keyspace.
rSpan, err := keys.Range(args.Requests)
if err != nil {
return authError(err.Error())
}
tenSpan := tenantPrefix(tenID)
if !tenSpan.ContainsKeyRange(rSpan.Key, rSpan.EndKey) {
return authErrorf("requested key span %s not fully contained in tenant keyspace %s", rSpan, tenSpan)
}
return nil
}
var reqMethodAllowlist = [...]bool{
roachpb.Get: true,
roachpb.Put: true,
roachpb.ConditionalPut: true,
roachpb.Increment: true,
roachpb.Delete: true,
roachpb.DeleteRange: true,
roachpb.ClearRange: true,
roachpb.RevertRange: true,
roachpb.Scan: true,
roachpb.ReverseScan: true,
roachpb.EndTxn: true,
roachpb.AdminSplit: true,
roachpb.HeartbeatTxn: true,
roachpb.QueryTxn: true,
roachpb.QueryIntent: true,
roachpb.InitPut: true,
roachpb.Export: true,
roachpb.AdminScatter: true,
roachpb.AddSSTable: true,
roachpb.Refresh: true,
roachpb.RefreshRange: true,
}
func reqAllowed(r roachpb.Request) bool {
m := int(r.Method())
return m < len(reqMethodAllowlist) && reqMethodAllowlist[m]
}
// authRangeLookup authorizes the provided tenant to invoke the RangeLookup RPC
// with the provided args.
func (a tenantAuthorizer) authRangeLookup(
tenID roachpb.TenantID, args *roachpb.RangeLookupRequest,
) error {
tenSpan := tenantPrefix(tenID)
if !tenSpan.ContainsKey(args.Key) {
return authErrorf("requested key %s not fully contained in tenant keyspace %s", args.Key, tenSpan)
}
return nil
}
// authRangeFeed authorizes the provided tenant to invoke the RangeFeed RPC with
// the provided args.
func (a tenantAuthorizer) authRangeFeed(
tenID roachpb.TenantID, args *roachpb.RangeFeedRequest,
) error {
rSpan, err := keys.SpanAddr(args.Span)
if err != nil {
return authError(err.Error())
}
tenSpan := tenantPrefix(tenID)
if !tenSpan.ContainsKeyRange(rSpan.Key, rSpan.EndKey) {
return authErrorf("requested key span %s not fully contained in tenant keyspace %s", rSpan, tenSpan)
}
return nil
}
// authGossipSubscription authorizes the provided tenant to invoke the
// GossipSubscription RPC with the provided args.
func (a tenantAuthorizer) authGossipSubscription(
tenID roachpb.TenantID, args *roachpb.GossipSubscriptionRequest,
) error {
for _, pat := range args.Patterns {
allowed := false
for _, allow := range gossipSubscriptionPatternAllowlist {
if pat == allow {
allowed = true
break
}
}
if !allowed {
return authErrorf("requested pattern %q not permitted", pat)
}
}
return nil
}
// authTenant checks if the given tenantID matches the one the
// authorizer was initialized with. This authorizer is used for
// endpoints that should remain within a single tenant's pods.
func (a tenantAuthorizer) authTenant(id roachpb.TenantID) error {
if a.tenantID != id {
return authErrorf("request from tenant %s not permitted on tenant %s", id, a.tenantID)
}
return nil
}
// gossipSubscriptionPatternAllowlist contains keys outside of a tenant's
// keyspace that GossipSubscription RPC invocations are allowed to touch.
// WIP: can't import gossip directly.
var gossipSubscriptionPatternAllowlist = []string{
"cluster-id",
"node:.*",
"system-db",
}
// authTokenBucket authorizes the provided tenant to invoke the
// TokenBucket RPC with the provided args.
func (a tenantAuthorizer) authTokenBucket(
tenID roachpb.TenantID, args *roachpb.TokenBucketRequest,
) error {
if args.TenantID == 0 {
return authErrorf("token bucket request with unspecified tenant not permitted")
}
if argTenant := roachpb.MakeTenantID(args.TenantID); argTenant != tenID {
return authErrorf("token bucket request for tenant %s not permitted", argTenant)
}
return nil
}
// authTenantSettings authorizes the provided tenant to invoke the
// TenantSettings RPC with the provided args.
func (a tenantAuthorizer) authTenantSettings(
tenID roachpb.TenantID, args *roachpb.TenantSettingsRequest,
) error {
if !args.TenantID.IsSet() {
return authErrorf("tenant settings request with unspecified tenant not permitted")
}
if args.TenantID != tenID {
return authErrorf("tenant settings request for tenant %s not permitted", args.TenantID)
}
return nil
}
// authGetSpanConfigs authorizes the provided tenant to invoke the
// GetSpanConfigs RPC with the provided args.
func (a tenantAuthorizer) authGetSpanConfigs(
tenID roachpb.TenantID, args *roachpb.GetSpanConfigsRequest,
) error {
for _, target := range args.Targets {
if err := validateSpanConfigTarget(tenID, target); err != nil {
return err
}
}
return nil
}
// authUpdateSpanConfigs authorizes the provided tenant to invoke the
// UpdateSpanConfigs RPC with the provided args.
func (a tenantAuthorizer) authUpdateSpanConfigs(
tenID roachpb.TenantID, args *roachpb.UpdateSpanConfigsRequest,
) error {
for _, entry := range args.ToUpsert {
if err := validateSpanConfigTarget(tenID, entry.Target); err != nil {
return err
}
}
for _, target := range args.ToDelete {
if err := validateSpanConfigTarget(tenID, target); err != nil {
return err
}
}
return nil
}
// validateSpanConfigTarget validates that the tenant is authorized to interact
// with the supplied span config target. In particular, span targets must be
// wholly contained within the tenant keyspace and system span config targets
// must be well-formed.
func validateSpanConfigTarget(
tenID roachpb.TenantID, spanConfigTarget roachpb.SpanConfigTarget,
) error {
validateSystemTarget := func(target roachpb.SystemSpanConfigTarget) error {
if tenID == roachpb.SystemTenantID {
// Nothing to validate, the system tenant is allowed to set system span
// configurations over secondary tenants.
return nil
}
if target.SourceTenantID != tenID || target.SourceTenantID != target.TargetTenantID {
return authErrorf(
"secondary tenants cannot set system span configurations that target other tenants",
)
}
return nil
}
validateSpan := func(sp roachpb.Span) error {
tenSpan := tenantPrefix(tenID)
rSpan, err := keys.SpanAddr(sp)
if err != nil {
return authError(err.Error())
}
if !tenSpan.ContainsKeyRange(rSpan.Key, rSpan.EndKey) {
return authErrorf("requested key span %s not fully contained in tenant keyspace %s", rSpan, tenSpan)
}
return nil
}
switch spanConfigTarget.Union.(type) {
case *roachpb.SpanConfigTarget_Span:
return validateSpan(*spanConfigTarget.GetSpan())
case *roachpb.SpanConfigTarget_SystemSpanConfigTarget:
return validateSystemTarget(*spanConfigTarget.GetSystemSpanConfigTarget())
default:
panic("unknown span config target type")
}
}
func contextWithTenant(ctx context.Context, tenID roachpb.TenantID) context.Context {
ctx = roachpb.NewContextForTenant(ctx, tenID)
ctx = logtags.AddTag(ctx, "tenant", tenID.String())
return ctx
}
func tenantPrefix(tenID roachpb.TenantID) roachpb.RSpan {
// TODO(nvanbenschoten): consider caching this span.
prefix := roachpb.RKey(keys.MakeTenantPrefix(tenID))
return roachpb.RSpan{
Key: prefix,
EndKey: prefix.PrefixEnd(),
}
}
// wrappedServerStream is a thin wrapper around grpc.ServerStream that allows
// modifying its context and overriding its RecvMsg method. It can be used to
// intercept each message and inject custom validation logic.
type wrappedServerStream struct {
grpc.ServerStream
ctx context.Context
recv func(interface{}) error
}
// Context overrides the nested grpc.ServerStream.Context().
func (ss *wrappedServerStream) Context() context.Context {
return ss.ctx
}
// RecvMsg overrides the nested grpc.ServerStream.RecvMsg().
func (ss *wrappedServerStream) RecvMsg(m interface{}) error {
return ss.recv(m)
}