Skip to content

Commit f911c7a

Browse files
[release-23.0] vtctldclient GetPermissions: hide authentication_string from response (#18771) (#18799)
Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com> Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com> Co-authored-by: Tim Vaillancourt <tim@timvaillancourt.com>
1 parent 122c674 commit f911c7a

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

go/cmd/vtctldclient/command/permissions.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"vitess.io/vitess/go/cmd/vtctldclient/cli"
2525
"vitess.io/vitess/go/vt/topo/topoproto"
2626

27+
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
2728
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
2829
)
2930

@@ -56,6 +57,26 @@ var (
5657
}
5758
)
5859

60+
// redactUserPermissions ensures sensitive info is redacted from a
61+
// *tabletmanagerdatapb.Permissions response.
62+
func redactUserPermissions(perms *tabletmanagerdatapb.Permissions) {
63+
if perms == nil {
64+
return
65+
}
66+
for _, up := range perms.UserPermissions {
67+
if up == nil {
68+
continue
69+
}
70+
if up.Privileges != nil {
71+
// Remove the "authentication_string" field, which is a
72+
// sensitive field from the mysql.users table. This is
73+
// redacted server-side in v23+ so this line can be
74+
// removed in the future.
75+
delete(up.Privileges, "authentication_string")
76+
}
77+
}
78+
}
79+
5980
func commandGetPermissions(cmd *cobra.Command, args []string) error {
6081
alias, err := topoproto.ParseTabletAlias(cmd.Flags().Arg(0))
6182
if err != nil {
@@ -70,13 +91,9 @@ func commandGetPermissions(cmd *cobra.Command, args []string) error {
7091
if err != nil {
7192
return err
7293
}
73-
// Obfuscate the checksum so as not to potentially display sensitive info.
94+
// Obfuscate the secrets so as not to potentially display sensitive info.
7495
if resp != nil && resp.Permissions != nil {
75-
for _, up := range resp.Permissions.UserPermissions {
76-
if up != nil {
77-
up.PasswordChecksum = 0
78-
}
79-
}
96+
redactUserPermissions(resp.Permissions)
8097
}
8198
cli.DefaultMarshalOptions.EmitUnpopulated = false
8299
p, err := cli.MarshalJSON(resp.Permissions)

go/cmd/vtctldclient/command/permissions_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"vitess.io/vitess/go/vt/topo/memorytopo"
3434

3535
querypb "vitess.io/vitess/go/vt/proto/query"
36+
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
3637
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
3738
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
3839
)
@@ -606,3 +607,27 @@ func TestPermissions(t *testing.T) {
606607
})
607608
require.ErrorContains(t, err, "has an extra user")
608609
}
610+
611+
func TestRedactUserPermissions(t *testing.T) {
612+
perms := &tabletmanagerdatapb.Permissions{
613+
UserPermissions: []*tabletmanagerdatapb.UserPermission{
614+
{
615+
Host: "%",
616+
User: "vt",
617+
Privileges: map[string]string{
618+
"authentication_string": "this should be removed from the response",
619+
},
620+
},
621+
},
622+
}
623+
redactUserPermissions(perms)
624+
require.EqualValues(t, &tabletmanagerdatapb.Permissions{
625+
UserPermissions: []*tabletmanagerdatapb.UserPermission{
626+
{
627+
Host: "%",
628+
User: "vt",
629+
Privileges: map[string]string{},
630+
},
631+
},
632+
}, perms)
633+
}

go/vt/mysqlctl/tmutils/permissions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func NewUserPermission(fields []*querypb.Field, values []sqltypes.Value) *tablet
7373
case "password_last_changed":
7474
// we skip this one, as the value may be
7575
// different on primary and replicas.
76+
case "authentication_string":
77+
// skip authentication_string as it
78+
// may contain a password hash.
7679
default:
7780
up.Privileges[field.Name] = values[i].ToString()
7881
}

go/vt/mysqlctl/tmutils/permissions_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ package tmutils
1919
import (
2020
"testing"
2121

22+
"github.com/stretchr/testify/require"
23+
2224
"vitess.io/vitess/go/sqltypes"
2325
querypb "vitess.io/vitess/go/vt/proto/query"
24-
2526
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
2627
)
2728

@@ -60,6 +61,29 @@ func testPermissionsDiff(t *testing.T, left, right *tabletmanagerdatapb.Permissi
6061
}
6162
}
6263

64+
func TestNewUserPermission(t *testing.T) {
65+
up := NewUserPermission(mapToSQLResults(map[string]string{
66+
"Host": "%",
67+
"User": "vt",
68+
"Password": "correct horse battery staple",
69+
"Select_priv": "Y",
70+
"Insert_priv": "N",
71+
// Test the next field is skipped (to avoid date drifts).
72+
"password_last_changed": "2016-11-08 02:56:23",
73+
// Test the next field is filtered.
74+
"authentication_string": "this should be filtered out",
75+
}))
76+
require.EqualValues(t, &tabletmanagerdatapb.UserPermission{
77+
Host: "%",
78+
User: "vt",
79+
PasswordChecksum: 17759204488013904955,
80+
Privileges: map[string]string{
81+
"Insert_priv": "N",
82+
"Select_priv": "Y",
83+
},
84+
}, up)
85+
}
86+
6387
func TestPermissionsDiff(t *testing.T) {
6488

6589
p1 := &tabletmanagerdatapb.Permissions{}

0 commit comments

Comments
 (0)