|
6 | 6 | package catpb
|
7 | 7 |
|
8 | 8 | import (
|
9 |
| - "fmt" |
10 | 9 | "sort"
|
11 | 10 |
|
12 | 11 | "github.com/cockroachdb/cockroach/pkg/keys"
|
13 | 12 | "github.com/cockroachdb/cockroach/pkg/security/username"
|
| 13 | + "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" |
14 | 15 | "github.com/cockroachdb/cockroach/pkg/sql/privilege"
|
15 | 16 | "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
|
16 | 17 | "github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
|
17 | 18 | "github.com/cockroachdb/errors"
|
| 19 | + "github.com/cockroachdb/redact" |
18 | 20 | )
|
19 | 21 |
|
20 | 22 | // PrivilegeDescVersion is a custom type for PrivilegeDescriptor versions.
|
@@ -364,21 +366,27 @@ func (p PrivilegeDescriptor) ValidateSuperuserPrivileges(
|
364 | 366 | // We expect an "admin" role. Check that it has desired superuser permissions.
|
365 | 367 | username.AdminRoleName(),
|
366 | 368 | } {
|
| 369 | + // In case we hit an error, we include the user name in the redacted message. |
| 370 | + // It's safe to include this since it's hardcoded system user. |
| 371 | + redactSafeUser := redact.SafeString(user.Normalized()) |
| 372 | + |
367 | 373 | superPriv, ok := p.FindUser(user)
|
368 | 374 | if !ok {
|
369 |
| - return fmt.Errorf( |
| 375 | + return pgerror.Newf( |
| 376 | + pgcode.InsufficientPrivilege, |
370 | 377 | "user %s does not have privileges over %s",
|
371 |
| - user, |
| 378 | + redactSafeUser, |
372 | 379 | privilegeObject(parentID, objectType, objectName),
|
373 | 380 | )
|
374 | 381 | }
|
375 | 382 |
|
376 | 383 | // The super users must match the allowed privilege set exactly.
|
377 | 384 | if superPriv.Privileges != allowedSuperuserPrivileges.ToBitField() {
|
378 |
| - return fmt.Errorf( |
379 |
| - "user %s must have exactly %s privileges on %s", |
380 |
| - user, |
381 |
| - allowedSuperuserPrivileges.SortedDisplayNames(), |
| 385 | + return pgerror.Newf( |
| 386 | + pgcode.InsufficientPrivilege, |
| 387 | + "user %s must have exactly [%v] privileges on %s", |
| 388 | + redactSafeUser, |
| 389 | + allowedSuperuserPrivileges, |
382 | 390 | privilegeObject(parentID, objectType, objectName),
|
383 | 391 | )
|
384 | 392 | }
|
@@ -574,10 +582,10 @@ func (p *PrivilegeDescriptor) SetVersion(version PrivilegeDescVersion) {
|
574 | 582 | // privilegeObject is a helper function for privilege errors.
|
575 | 583 | func privilegeObject(
|
576 | 584 | parentID catid.DescID, objectType privilege.ObjectType, objectName string,
|
577 |
| -) string { |
| 585 | +) redact.RedactableString { |
578 | 586 | if parentID == keys.SystemDatabaseID ||
|
579 | 587 | (parentID == catid.InvalidDescID && objectName == catconstants.SystemDatabaseName) {
|
580 |
| - return fmt.Sprintf("system %s %q", objectType, objectName) |
| 588 | + return redact.Sprintf("system %s %q", objectType, objectName) |
581 | 589 | }
|
582 |
| - return fmt.Sprintf("%s %q", objectType, objectName) |
| 590 | + return redact.Sprintf("%s %q", objectType, objectName) |
583 | 591 | }
|
0 commit comments