forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_credentials.go
More file actions
564 lines (485 loc) · 14.8 KB
/
user_credentials.go
File metadata and controls
564 lines (485 loc) · 14.8 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package database
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/keegancsmith/sqlf"
"github.com/sourcegraph/log"
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/encryption"
"github.com/sourcegraph/sourcegraph/internal/extsvc/auth"
"github.com/sourcegraph/sourcegraph/internal/timeutil"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// UserCredential represents a row in the `user_credentials` table.
type UserCredential struct {
ID int64
Domain string
UserID int32
ExternalServiceType string
ExternalServiceID string
CreatedAt time.Time
UpdatedAt time.Time
// TODO(batch-change-credential-encryption): On or after Sourcegraph 3.30,
// we should remove the credential and SSHMigrationApplied fields.
SSHMigrationApplied bool
Credential *EncryptableCredential
}
type EncryptableCredential = encryption.Encryptable
func NewEmptyCredential() *EncryptableCredential {
return NewUnencryptedCredential(nil)
}
func NewUnencryptedCredential(value []byte) *EncryptableCredential {
return encryption.NewUnencrypted(string(value))
}
func NewEncryptedCredential(cipher, keyID string, key encryption.Key) *EncryptableCredential {
return encryption.NewEncrypted(cipher, keyID, key)
}
// Authenticator decrypts and creates the authenticator associated with the user credential.
func (uc *UserCredential) Authenticator(ctx context.Context) (auth.Authenticator, error) {
decrypted, err := uc.Credential.Decrypt(ctx)
if err != nil {
return nil, err
}
a, err := UnmarshalAuthenticator(decrypted)
if err != nil {
return nil, errors.Wrap(err, "unmarshalling authenticator")
}
return a, nil
}
// SetAuthenticator encrypts and sets the authenticator within the user credential.
func (uc *UserCredential) SetAuthenticator(ctx context.Context, a auth.Authenticator) error {
if uc.Credential == nil {
uc.Credential = NewUnencryptedCredential(nil)
}
raw, err := MarshalAuthenticator(a)
if err != nil {
return errors.Wrap(err, "marshalling authenticator")
}
uc.Credential.Set(raw)
return nil
}
const (
// Valid domain values for user credentials.
UserCredentialDomainBatches = "batches"
)
// UserCredentialNotFoundErr is returned when a credential cannot be found from
// its ID or scope.
type UserCredentialNotFoundErr struct{ args []any }
func (err UserCredentialNotFoundErr) Error() string {
return fmt.Sprintf("user credential not found: %v", err.args)
}
func (UserCredentialNotFoundErr) NotFound() bool {
return true
}
type UserCredentialsStore interface {
basestore.ShareableStore
With(basestore.ShareableStore) UserCredentialsStore
Transact(context.Context) (UserCredentialsStore, error)
Create(ctx context.Context, scope UserCredentialScope, credential auth.Authenticator) (*UserCredential, error)
Update(context.Context, *UserCredential) error
Delete(ctx context.Context, id int64) error
GetByID(ctx context.Context, id int64) (*UserCredential, error)
GetByScope(context.Context, UserCredentialScope) (*UserCredential, error)
List(context.Context, UserCredentialsListOpts) ([]*UserCredential, int, error)
}
// userCredentialsStore provides access to the `user_credentials` table.
type userCredentialsStore struct {
logger log.Logger
*basestore.Store
key encryption.Key
}
// UserCredentialsWith instantiates and returns a new UserCredentialsStore using the other store handle.
func UserCredentialsWith(logger log.Logger, other basestore.ShareableStore, key encryption.Key) UserCredentialsStore {
return &userCredentialsStore{
logger: logger,
Store: basestore.NewWithHandle(other.Handle()),
key: key,
}
}
func (s *userCredentialsStore) With(other basestore.ShareableStore) UserCredentialsStore {
return &userCredentialsStore{
logger: s.logger,
Store: s.Store.With(other),
key: s.key,
}
}
func (s *userCredentialsStore) Transact(ctx context.Context) (UserCredentialsStore, error) {
txBase, err := s.Store.Transact(ctx)
return &userCredentialsStore{
logger: s.logger,
Store: txBase,
key: s.key,
}, err
}
// UserCredentialScope represents the unique scope for a credential. Only one
// credential may exist within a scope.
type UserCredentialScope struct {
Domain string
UserID int32
ExternalServiceType string
ExternalServiceID string
}
// Create creates a new user credential based on the given scope and
// authenticator. If the scope already has a credential, an error will be
// returned.
func (s *userCredentialsStore) Create(ctx context.Context, scope UserCredentialScope, credential auth.Authenticator) (*UserCredential, error) {
// SECURITY: check that the current user is authorised to create a user
// credential for the given user scope.
if err := userCredentialsAuthzScope(ctx, NewDBWith(s.logger, s), scope); err != nil {
return nil, err
}
encryptedCredential, keyID, err := EncryptAuthenticator(ctx, s.key, credential)
if err != nil {
return nil, err
}
q := sqlf.Sprintf(
userCredentialsCreateQueryFmtstr,
scope.Domain,
scope.UserID,
scope.ExternalServiceType,
scope.ExternalServiceID,
encryptedCredential, // N.B.: is already a []byte
keyID,
sqlf.Join(userCredentialsColumns, ", "),
)
cred := UserCredential{}
row := s.QueryRow(ctx, q)
if err := scanUserCredential(&cred, s.key, row); err != nil {
return nil, err
}
return &cred, nil
}
// Update updates a user credential in the database. If the credential cannot be found,
// an error is returned.
func (s *userCredentialsStore) Update(ctx context.Context, credential *UserCredential) error {
authz, err := userCredentialsAuthzQueryConds(ctx)
if err != nil {
return err
}
credential.UpdatedAt = timeutil.Now()
encryptedCredential, keyID, err := credential.Credential.Encrypt(ctx, s.key)
if err != nil {
return err
}
q := sqlf.Sprintf(
userCredentialsUpdateQueryFmtstr,
credential.Domain,
credential.UserID,
credential.ExternalServiceType,
credential.ExternalServiceID,
[]byte(encryptedCredential),
keyID,
credential.UpdatedAt,
credential.SSHMigrationApplied,
credential.ID,
authz,
sqlf.Join(userCredentialsColumns, ", "),
)
row := s.QueryRow(ctx, q)
if err := scanUserCredential(credential, s.key, row); err != nil {
return err
}
return nil
}
// Delete deletes the given user credential. Note that there is no concept of a
// soft delete with user credentials: once deleted, the relevant records are
// _gone_, so that we don't hold any sensitive data unexpectedly. 💀
func (s *userCredentialsStore) Delete(ctx context.Context, id int64) error {
authz, err := userCredentialsAuthzQueryConds(ctx)
if err != nil {
return err
}
q := sqlf.Sprintf("DELETE FROM user_credentials WHERE id = %s AND %s", id, authz)
res, err := s.ExecResult(ctx, q)
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return UserCredentialNotFoundErr{args: []any{id}}
}
return nil
}
// GetByID returns the user credential matching the given ID, or
// UserCredentialNotFoundErr if no such credential exists.
func (s *userCredentialsStore) GetByID(ctx context.Context, id int64) (*UserCredential, error) {
authz, err := userCredentialsAuthzQueryConds(ctx)
if err != nil {
return nil, err
}
q := sqlf.Sprintf(
"SELECT %s FROM user_credentials WHERE id = %s AND %s",
sqlf.Join(userCredentialsColumns, ", "),
id,
authz,
)
cred := UserCredential{}
row := s.QueryRow(ctx, q)
if err := scanUserCredential(&cred, s.key, row); err == sql.ErrNoRows {
return nil, UserCredentialNotFoundErr{args: []any{id}}
} else if err != nil {
return nil, err
}
return &cred, nil
}
// GetByScope returns the user credential matching the given scope, or
// UserCredentialNotFoundErr if no such credential exists.
func (s *userCredentialsStore) GetByScope(ctx context.Context, scope UserCredentialScope) (*UserCredential, error) {
authz, err := userCredentialsAuthzQueryConds(ctx)
if err != nil {
return nil, err
}
q := sqlf.Sprintf(
userCredentialsGetByScopeQueryFmtstr,
sqlf.Join(userCredentialsColumns, ", "),
scope.Domain,
scope.UserID,
scope.ExternalServiceType,
scope.ExternalServiceID,
authz,
)
cred := UserCredential{}
row := s.QueryRow(ctx, q)
if err := scanUserCredential(&cred, s.key, row); err == sql.ErrNoRows {
return nil, UserCredentialNotFoundErr{args: []any{scope}}
} else if err != nil {
return nil, err
}
return &cred, nil
}
// UserCredentialsListOpts provide the options when listing credentials. At
// least one field in Scope must be set.
type UserCredentialsListOpts struct {
*LimitOffset
Scope UserCredentialScope
ForUpdate bool
// TODO(batch-change-credential-encryption): this should be removed once the
// OOB SSH migration is removed.
SSHMigrationApplied *bool
}
// sql overrides LimitOffset.SQL() to give a LIMIT clause with one extra value
// so we can populate the next cursor.
func (opts *UserCredentialsListOpts) sql() *sqlf.Query {
if opts.LimitOffset == nil || opts.Limit == 0 {
return &sqlf.Query{}
}
return (&LimitOffset{Limit: opts.Limit + 1, Offset: opts.Offset}).SQL()
}
// List returns all user credentials matching the given options.
func (s *userCredentialsStore) List(ctx context.Context, opts UserCredentialsListOpts) ([]*UserCredential, int, error) {
authz, err := userCredentialsAuthzQueryConds(ctx)
if err != nil {
return nil, 0, err
}
preds := []*sqlf.Query{authz}
if opts.Scope.Domain != "" {
preds = append(preds, sqlf.Sprintf("domain = %s", opts.Scope.Domain))
}
if opts.Scope.UserID != 0 {
preds = append(preds, sqlf.Sprintf("user_id = %s", opts.Scope.UserID))
}
if opts.Scope.ExternalServiceType != "" {
preds = append(preds, sqlf.Sprintf("external_service_type = %s", opts.Scope.ExternalServiceType))
}
if opts.Scope.ExternalServiceID != "" {
preds = append(preds, sqlf.Sprintf("external_service_id = %s", opts.Scope.ExternalServiceID))
}
// TODO(batch-change-credential-encryption): remove the remaining predicates
// once the OOB SSH migration is removed.
if opts.SSHMigrationApplied != nil {
preds = append(preds, sqlf.Sprintf("ssh_migration_applied = %s", *opts.SSHMigrationApplied))
}
forUpdate := &sqlf.Query{}
if opts.ForUpdate {
forUpdate = sqlf.Sprintf("FOR UPDATE")
}
q := sqlf.Sprintf(
userCredentialsListQueryFmtstr,
sqlf.Join(userCredentialsColumns, ", "),
sqlf.Join(preds, "\n AND "),
opts.sql(),
forUpdate,
)
rows, err := s.Query(ctx, q)
if err != nil {
return nil, 0, err
}
defer rows.Close()
var creds []*UserCredential
for rows.Next() {
cred := UserCredential{}
if err := scanUserCredential(&cred, s.key, rows); err != nil {
return nil, 0, err
}
creds = append(creds, &cred)
}
// Check if there were more results than the limit: if so, then we need to
// set the return cursor and lop off the extra credential that we retrieved.
next := 0
if opts.LimitOffset != nil && opts.Limit != 0 && len(creds) == opts.Limit+1 {
next = opts.Offset + opts.Limit
creds = creds[:len(creds)-1]
}
return creds, next, nil
}
// 🐉 This marks the end of the public API. Beyond here are dragons.
// userCredentialsColumns are the columns that must be selected by
// user_credentials queries in order to use scanUserCredential().
var userCredentialsColumns = []*sqlf.Query{
sqlf.Sprintf("id"),
sqlf.Sprintf("domain"),
sqlf.Sprintf("user_id"),
sqlf.Sprintf("external_service_type"),
sqlf.Sprintf("external_service_id"),
sqlf.Sprintf("credential"),
sqlf.Sprintf("encryption_key_id"),
sqlf.Sprintf("created_at"),
sqlf.Sprintf("updated_at"),
sqlf.Sprintf("ssh_migration_applied"),
}
// The more unwieldy queries are below rather than inline in the above methods
// in a vain attempt to improve their readability.
const userCredentialsGetByScopeQueryFmtstr = `
-- source: internal/database/user_credentials.go:GetByScope
SELECT %s
FROM user_credentials
WHERE
domain = %s AND
user_id = %s AND
external_service_type = %s AND
external_service_id = %s AND
%s -- authz query conds
`
const userCredentialsListQueryFmtstr = `
-- source: internal/database/user_credentials.go:List
SELECT %s
FROM user_credentials
WHERE %s
ORDER BY created_at ASC, domain ASC, user_id ASC, external_service_id ASC
%s -- LIMIT clause
%s -- optional FOR UPDATE
`
const userCredentialsCreateQueryFmtstr = `
-- source: internal/database/user_credentials.go:Create
INSERT INTO
user_credentials (
domain,
user_id,
external_service_type,
external_service_id,
credential,
encryption_key_id,
created_at,
updated_at,
ssh_migration_applied
)
VALUES (
%s,
%s,
%s,
%s,
%s,
%s,
NOW(),
NOW(),
TRUE
)
RETURNING %s
`
const userCredentialsUpdateQueryFmtstr = `
-- source: internal/database/user_credentials.go:Update
UPDATE user_credentials
SET
domain = %s,
user_id = %s,
external_service_type = %s,
external_service_id = %s,
credential = %s,
encryption_key_id = %s,
updated_at = %s,
ssh_migration_applied = %s
WHERE
id = %s AND
%s -- authz query conds
RETURNING %s
`
// scanUserCredential scans a credential from the given scanner into the given
// credential.
//
// s is inspired by the BatchChange scanner type, but also matches sql.Row, which
// is generally used directly in this module.
func scanUserCredential(cred *UserCredential, key encryption.Key, s interface {
Scan(...any) error
}) error {
var (
credential []byte
keyID string
)
if err := s.Scan(
&cred.ID,
&cred.Domain,
&cred.UserID,
&cred.ExternalServiceType,
&cred.ExternalServiceID,
&credential,
&keyID,
&cred.CreatedAt,
&cred.UpdatedAt,
&cred.SSHMigrationApplied,
); err != nil {
return err
}
cred.Credential = NewEncryptedCredential(string(credential), keyID, key)
return nil
}
var errUserCredentialCreateAuthz = errors.New("current user cannot create a user credential in this scope")
func userCredentialsAuthzScope(ctx context.Context, db DB, scope UserCredentialScope) error {
a := actor.FromContext(ctx)
if a.IsInternal() {
return nil
}
user, err := db.Users().GetByCurrentAuthUser(ctx)
if err != nil {
return errors.Wrap(err, "getting auth user from context")
}
if user.SiteAdmin && !conf.Get().AuthzEnforceForSiteAdmins {
return nil
}
if user.ID != scope.UserID {
return errUserCredentialCreateAuthz
}
return nil
}
func userCredentialsAuthzQueryConds(ctx context.Context) (*sqlf.Query, error) {
a := actor.FromContext(ctx)
if a.IsInternal() {
return sqlf.Sprintf("(TRUE)"), nil
}
return sqlf.Sprintf(
userCredentialsAuthzQueryCondsFmtstr,
a.UID,
!conf.Get().AuthzEnforceForSiteAdmins,
a.UID,
), nil
}
const userCredentialsAuthzQueryCondsFmtstr = `
(
(
user_credentials.user_id = %s -- user credential user is the same as the actor
)
OR
(
%s -- negated authz.enforceForSiteAdmins site config setting
AND EXISTS (
SELECT 1
FROM users
WHERE site_admin = TRUE AND id = %s -- actor user ID
)
)
)
`