Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: anonymize schema names, owner names, and enum values #55473

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/generated/sql/bnf/grant_privileges.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ grant_stmt ::=


| 'GRANT' ( 'ALL' opt_privileges_clause | ( ( ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) ( ( ',' ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) )* ) ) 'ON' 'TYPE' target_types 'TO' ( ( user_name ) ( ( ',' user_name ) )* )
| 'GRANT' ( 'ALL' opt_privileges_clause | ( ( ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) ( ( ',' ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) )* ) ) 'ON' 'SCHEMA' schema_name_list 'TO' ( ( user_name ) ( ( ',' user_name ) )* )
| 'GRANT' ( 'ALL' opt_privileges_clause | ( ( ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) ( ( ',' ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) )* ) ) 'ON' 'SCHEMA' ( ( name ) ( ( ',' name ) )* ) 'TO' ( ( user_name ) ( ( ',' user_name ) )* )
2 changes: 1 addition & 1 deletion docs/generated/sql/bnf/revoke_privileges.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ revoke_stmt ::=


| 'REVOKE' ( 'ALL' opt_privileges_clause | ( ( ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) ( ( ',' ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) )* ) ) 'ON' 'TYPE' target_types 'FROM' ( ( user_name ) ( ( ',' user_name ) )* )
| 'REVOKE' ( 'ALL' opt_privileges_clause | ( ( ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) ( ( ',' ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) )* ) ) 'ON' 'SCHEMA' schema_name_list 'FROM' ( ( user_name ) ( ( ',' user_name ) )* )
| 'REVOKE' ( 'ALL' opt_privileges_clause | ( ( ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) ( ( ',' ( 'CREATE' | 'GRANT' | 'SELECT' | 'DROP' | 'INSERT' | 'DELETE' | 'UPDATE' ) ) )* ) ) 'ON' 'SCHEMA' ( ( name ) ( ( ',' name ) )* ) 'FROM' ( ( user_name ) ( ( ',' user_name ) )* )
17 changes: 7 additions & 10 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ grant_stmt ::=
| 'GRANT' privilege_list 'TO' name_list
| 'GRANT' privilege_list 'TO' name_list 'WITH' 'ADMIN' 'OPTION'
| 'GRANT' privileges 'ON' 'TYPE' target_types 'TO' name_list
| 'GRANT' privileges 'ON' 'SCHEMA' schema_name_list 'TO' name_list
| 'GRANT' privileges 'ON' 'SCHEMA' name_list 'TO' name_list

prepare_stmt ::=
'PREPARE' table_alias_name prep_type_clause 'AS' preparable_stmt
Expand All @@ -85,7 +85,7 @@ revoke_stmt ::=
| 'REVOKE' privilege_list 'FROM' name_list
| 'REVOKE' 'ADMIN' 'OPTION' 'FOR' privilege_list 'FROM' name_list
| 'REVOKE' privileges 'ON' 'TYPE' target_types 'FROM' name_list
| 'REVOKE' privileges 'ON' 'SCHEMA' schema_name_list 'FROM' name_list
| 'REVOKE' privileges 'ON' 'SCHEMA' name_list 'FROM' name_list

savepoint_stmt ::=
'SAVEPOINT' name
Expand Down Expand Up @@ -301,9 +301,6 @@ privilege_list ::=
target_types ::=
type_name_list

schema_name_list ::=
( schema_name ) ( ( ',' schema_name ) )*

prep_type_clause ::=
'(' type_list ')'
|
Expand Down Expand Up @@ -1160,9 +1157,6 @@ privilege ::=
type_name_list ::=
( type_name ) ( ( ',' type_name ) )*

schema_name ::=
name

type_list ::=
( typename ) ( ( ',' typename ) )*

Expand Down Expand Up @@ -1371,8 +1365,8 @@ drop_sequence_stmt ::=
| 'DROP' 'SEQUENCE' 'IF' 'EXISTS' table_name_list opt_drop_behavior

drop_schema_stmt ::=
'DROP' 'SCHEMA' schema_name_list opt_drop_behavior
| 'DROP' 'SCHEMA' 'IF' 'EXISTS' schema_name_list opt_drop_behavior
'DROP' 'SCHEMA' name_list opt_drop_behavior
| 'DROP' 'SCHEMA' 'IF' 'EXISTS' name_list opt_drop_behavior

drop_type_stmt ::=
'DROP' 'TYPE' type_name_list opt_drop_behavior
Expand Down Expand Up @@ -1642,6 +1636,9 @@ alter_zone_partition_stmt ::=
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_index_name set_zone_config
| 'ALTER' 'PARTITION' partition_name 'OF' 'INDEX' table_name '@' '*' set_zone_config

schema_name ::=
name

opt_add_val_placement ::=
'BEFORE' 'SCONST'
| 'AFTER' 'SCONST'
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/alter_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (p *planner) AlterDatabaseOwner(
}
privs := dbDesc.GetPrivileges()

if err := p.checkCanAlterToNewOwner(ctx, dbDesc, n.Owner); err != nil {
newOwner := string(n.Owner)
if err := p.checkCanAlterToNewOwner(ctx, dbDesc, newOwner); err != nil {
return nil, err
}

Expand All @@ -43,14 +44,14 @@ func (p *planner) AlterDatabaseOwner(
}

// If the owner we want to set to is the current owner, do a no-op.
if n.Owner == privs.Owner {
if newOwner == privs.Owner {
return nil, nil
}
return &alterDatabaseOwnerNode{n: n, desc: dbDesc}, nil
}

func (n *alterDatabaseOwnerNode) startExec(params runParams) error {
n.desc.GetPrivileges().SetOwner(n.n.Owner)
n.desc.GetPrivileges().SetOwner(string(n.n.Owner))
return params.p.writeNonDropDatabaseChange(
params.ctx,
n.desc,
Expand Down
8 changes: 5 additions & 3 deletions pkg/sql/alter_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (p *planner) AlterSchema(ctx context.Context, n *tree.AlterSchema) (planNod
if err != nil {
return nil, err
}
found, schema, err := p.ResolveMutableSchemaDescriptor(ctx, db.ID, n.Schema, true /* required */)
found, schema, err := p.ResolveMutableSchemaDescriptor(ctx, db.ID, string(n.Schema), true /* required */)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -78,9 +78,11 @@ func (p *planner) AlterSchema(ctx context.Context, n *tree.AlterSchema) (planNod
func (n *alterSchemaNode) startExec(params runParams) error {
switch t := n.n.Cmd.(type) {
case *tree.AlterSchemaRename:
return params.p.renameSchema(params.ctx, n.db, n.desc, t.NewName, tree.AsStringWithFQNames(n.n, params.Ann()))
return params.p.renameSchema(
params.ctx, n.db, n.desc, string(t.NewName), tree.AsStringWithFQNames(n.n, params.Ann()))
case *tree.AlterSchemaOwner:
return params.p.alterSchemaOwner(params.ctx, n.db, n.desc, t.Owner, tree.AsStringWithFQNames(n.n, params.Ann()))
return params.p.alterSchemaOwner(
params.ctx, n.db, n.desc, string(t.Owner), tree.AsStringWithFQNames(n.n, params.Ann()))
default:
return errors.AssertionFailedf("unknown schema cmd %T", t)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ func (n *alterTableNode) startExec(params runParams) error {
}
descriptorChanged = true
case *tree.AlterTableOwner:
changed, err := params.p.alterTableOwner(params.p.EvalContext().Context, n, t.Owner)
changed, err := params.p.alterTableOwner(params.p.EvalContext().Context, n, string(t.Owner))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/alter_table_set_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (p *planner) AlterTableSetSchema(
}

return &alterTableSetSchemaNode{
newSchema: n.Schema,
newSchema: string(n.Schema),
tableDesc: tableDesc,
n: n,
}, nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/sql/alter_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func (n *alterTypeNode) startExec(params runParams) error {
case *tree.AlterTypeAddValue:
err = params.p.addEnumValue(params.ctx, n, t)
case *tree.AlterTypeRenameValue:
err = params.p.renameTypeValue(params.ctx, n, t.OldVal, t.NewVal)
err = params.p.renameTypeValue(params.ctx, n, string(t.OldVal), string(t.NewVal))
case *tree.AlterTypeRename:
err = params.p.renameType(params.ctx, n, t.NewName)
err = params.p.renameType(params.ctx, n, string(t.NewName))
case *tree.AlterTypeSetSchema:
err = params.p.setTypeSchema(params.ctx, n, t.Schema)
err = params.p.setTypeSchema(params.ctx, n, string(t.Schema))
case *tree.AlterTypeOwner:
err = params.p.alterTypeOwner(params.ctx, n, t.Owner)
err = params.p.alterTypeOwner(params.ctx, n, string(t.Owner))
default:
err = errors.AssertionFailedf("unknown alter type cmd %s", t)
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (p *planner) addEnumValue(
}
// See if the value already exists in the enum or not.
for _, member := range n.desc.EnumMembers {
if member.LogicalRepresentation == node.NewVal {
if member.LogicalRepresentation == string(node.NewVal) {
if node.IfNotExists {
p.BufferClientNotice(
ctx,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/catalog/typedesc/type_desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (desc *Mutable) AddEnumValue(node *tree.AlterTypeAddValue) error {
// If the value was requested to be added before or after an existing
// value, then find the index of where it should be inserted.
foundIndex := -1
existing := node.Placement.ExistingVal
existing := string(node.Placement.ExistingVal)
for i, member := range desc.EnumMembers {
if member.LogicalRepresentation == existing {
foundIndex = i
Expand All @@ -324,7 +324,7 @@ func (desc *Mutable) AddEnumValue(node *tree.AlterTypeAddValue) error {
// how to decode the physical representation.
newPhysicalRep := enum.GenByteStringBetween(getPhysicalRep(pos), getPhysicalRep(pos+1), enum.SpreadSpacing)
newMember := descpb.TypeDescriptor_EnumMember{
LogicalRepresentation: node.NewVal,
LogicalRepresentation: string(node.NewVal),
PhysicalRepresentation: newPhysicalRep,
Capability: descpb.TypeDescriptor_EnumMember_READ_ONLY,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,11 +1668,11 @@ CREATE TABLE crdb_internal.create_type_statements (
return forEachTypeDesc(ctx, p, db, func(db *dbdesc.Immutable, sc string, typeDesc *typedesc.Immutable) error {
switch typeDesc.Kind {
case descpb.TypeDescriptor_ENUM:
var enumLabels []string
var enumLabels tree.EnumValueList
enumLabelsDatum := tree.NewDArray(types.String)
for i := range typeDesc.EnumMembers {
rep := typeDesc.EnumMembers[i].LogicalRepresentation
enumLabels = append(enumLabels, rep)
enumLabels = append(enumLabels, tree.EnumValue(rep))
if err := enumLabelsDatum.Append(tree.NewDString(rep)); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/create_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *planner) createUserDefinedSchema(params runParams, n *tree.CreateSchema
return err
}

schemaName := n.Schema
schemaName := string(n.Schema)
if n.Schema == "" {
schemaName = n.AuthRole
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/create_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (p *planner) createEnum(params runParams, n *tree.CreateType) error {
sqltelemetry.IncrementEnumCounter(sqltelemetry.EnumCreate)

// Ensure there are no duplicates in the input enum values.
seenVals := make(map[string]struct{})
seenVals := make(map[tree.EnumValue]struct{})
for _, value := range n.EnumLabels {
_, ok := seenVals[value]
if ok {
Expand All @@ -265,7 +265,7 @@ func (p *planner) createEnum(params runParams, n *tree.CreateType) error {
physReps := enum.GenerateNEvenlySpacedBytes(len(n.EnumLabels))
for i := range n.EnumLabels {
members[i] = descpb.TypeDescriptor_EnumMember{
LogicalRepresentation: n.EnumLabels[i],
LogicalRepresentation: string(n.EnumLabels[i]),
PhysicalRepresentation: physReps[i],
Capability: descpb.TypeDescriptor_EnumMember_ALL,
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/delegate/show_grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ FROM "".information_schema.type_privileges`
fmt.Fprintf(&cond, `WHERE database_name IN (%s)`, strings.Join(params, ","))
}
} else if n.Targets != nil && len(n.Targets.Schemas) > 0 {
for _, schema := range n.Targets.Schemas {
schemaNames := n.Targets.Schemas.ToStrings()
for _, schema := range schemaNames {
name := cat.SchemaName{
SchemaName: tree.Name(schema),
ExplicitSchema: true,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/drop_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (p *planner) DropSchema(ctx context.Context, n *tree.DropSchema) (planNode,
d := newDropCascadeState()

// Collect all schemas to be deleted.
for _, scName := range n.Names {
for _, scName := range n.Names.ToStrings() {
found, sc, err := p.ResolveMutableSchemaDescriptor(ctx, db.ID, scName, false /* required */)
if err != nil {
return nil, err
Expand Down
Loading