Skip to content

Commit

Permalink
[SQL]:Support show users from tenant_name (#655)
Browse files Browse the repository at this point in the history
* fix:Support show users from tenant_name

* delete public function   annotation

* add: integrationTest

* add: integrationTest
  • Loading branch information
gongna-au authored Mar 22, 2023
1 parent 98f5d01 commit 76c4e80
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/appleboy/gin-jwt/v2 v2.9.1
github.com/arana-db/parser v0.2.11
github.com/arana-db/parser v0.2.12
github.com/blang/semver v3.5.1+incompatible
github.com/bwmarrin/snowflake v0.3.0
github.com/cespare/xxhash/v2 v2.1.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ github.com/appleboy/gin-jwt/v2 v2.9.1 h1:l29et8iLW6omcHltsOP6LLk4s3v4g2FbFs0koxG
github.com/appleboy/gin-jwt/v2 v2.9.1/go.mod h1:jwcPZJ92uoC9nOUTOKWoN/f6JZOgMSKlFSHw5/FrRUk=
github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4=
github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw=
github.com/arana-db/parser v0.2.11 h1:w1J9hf+5XAFm0+lbggqJRWMaVQ9iOn2Fg6XD+l0wSQ0=
github.com/arana-db/parser v0.2.11/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/arana-db/parser v0.2.12 h1:bzjm6qom5aetfcWi7X66z01MWMLapKrkORf4QD/1gBg=
github.com/arana-db/parser v0.2.12/go.mod h1:/XA29bplweWSEAjgoM557ZCzhBilSawUlHcZFjOeDAc=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (executor *RedirectExecutor) doExecutorComQuery(ctx *proto.Context, act ast
switch stmt.Tp {
case ast.ShowDatabases, ast.ShowVariables, ast.ShowTopology, ast.ShowStatus, ast.ShowTableStatus,
ast.ShowWarnings, ast.ShowCharset, ast.ShowMasterStatus, ast.ShowProcessList, ast.ShowReplicas,
ast.ShowReplicaStatus:
ast.ShowReplicaStatus, ast.ShowUsers:
return true
default:
return false
Expand Down
3 changes: 3 additions & 0 deletions pkg/mysql/thead/thead.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var (
Database = Thead{
Col{Name: "Database", FieldType: consts.FieldTypeVarString},
}
Users = Thead{
Col{Name: "user_name", FieldType: consts.FieldTypeVarString},
}
)

type Col struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/runtime/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ func (cc *convCtx) convShowStmt(node *ast.ShowStmt) Statement {
return &ShowTopology{baseShow: toBaseShow()}
case ast.ShowOpenTables:
return &ShowOpenTables{baseShow: toBaseShow()}
case ast.ShowUsers:
return &ShowUsers{Tenant: node.Tenant}
case ast.ShowTables:
ret := &ShowTables{baseShow: toBaseShow()}
if like, ok := toLike(node); ok {
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func TestParse_ShowStatement(t *testing.T) {
{"show table status in foo", (*ShowTableStatus)(nil), "SHOW TABLE STATUS FROM `foo`"},
{"show table status in foo like '%bar%'", (*ShowTableStatus)(nil), "SHOW TABLE STATUS FROM `foo` LIKE '%bar%'"},
{"show table status from foo where name='bar'", (*ShowTableStatus)(nil), "SHOW TABLE STATUS FROM `foo` WHERE `name` = 'bar'"},
{"show users from arana", (*ShowUsers)(nil), "SHOW USERS FROM `arana`"},
} {
t.Run(it.input, func(t *testing.T) {
_, stmt, err := Parse(it.input)
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/ast/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
SQLTypeShowCreate // SHOW CREATE
SQLTypeShowVariables // SHOW VARIABLES
SQLTypeShowTopology // SHOW TOPOLOGY
SQLTypeShowUsers // SHOW USERS
SQLTypeDescribe // DESCRIBE
SQLTypeUnion // UNION
SQLTypeDropTrigger // DROP TRIGGER
Expand Down
17 changes: 17 additions & 0 deletions pkg/runtime/ast/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,20 @@ func (s *ShowReplicaStatus) Restore(flag RestoreFlag, sb *strings.Builder, args
sb.WriteString("SHOW REPLICA STATUS")
return s.baseShow.Restore(flag, sb, args)
}

type ShowUsers struct {
Tenant string
}

func (s *ShowUsers) Mode() SQLType {
return SQLTypeShowUsers
}

func (s *ShowUsers) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
sb.WriteString("SHOW USERS FROM ")

if len(s.Tenant) > 0 {
WriteID(sb, s.Tenant)
}
return nil
}
39 changes: 39 additions & 0 deletions pkg/runtime/optimize/dal/show_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dal

import (
"context"
)

import (
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/runtime/ast"
"github.com/arana-db/arana/pkg/runtime/optimize"
"github.com/arana-db/arana/pkg/runtime/plan/dal"
)

func init() {
optimize.Register(ast.SQLTypeShowUsers, optimizeShowUsers)
}

func optimizeShowUsers(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
stmt := o.Stmt.(*ast.ShowUsers)
ret := dal.NewShowUsersPlan(stmt)
return ret, nil
}
69 changes: 69 additions & 0 deletions pkg/runtime/plan/dal/show_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dal

import (
"context"
)

import (
"github.com/arana-db/arana/pkg/dataset"
"github.com/arana-db/arana/pkg/mysql/rows"
"github.com/arana-db/arana/pkg/mysql/thead"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/resultx"
"github.com/arana-db/arana/pkg/runtime/ast"
rcontext "github.com/arana-db/arana/pkg/runtime/context"
"github.com/arana-db/arana/pkg/runtime/plan"
"github.com/arana-db/arana/pkg/security"
)

var _ proto.Plan = (*ShowUsers)(nil)

type ShowUsers struct {
Stmt *ast.ShowUsers
}

func NewShowUsersPlan(stmt *ast.ShowUsers) *ShowUsers {
return &ShowUsers{
Stmt: stmt,
}
}

func (su *ShowUsers) Type() proto.PlanType {
return proto.PlanTypeQuery
}

func (su *ShowUsers) ExecIn(ctx context.Context, conn proto.VConn) (proto.Result, error) {
ctx, span := plan.Tracer.Start(ctx, "ShowUsers.ExecIn")
defer span.End()
tenant := su.Stmt.Tenant
if len(tenant) == 0 {
tenant = rcontext.Tenant(ctx)
}

columns := thead.Users.ToFields()
ds := &dataset.VirtualDataset{
Columns: columns,
}

users, _ := security.DefaultTenantManager().GetUsers(tenant)
for _, user := range users {
ds.Rows = append(ds.Rows, rows.NewTextVirtualRow(columns, []proto.Value{proto.NewValueString(user.Username)}))
}
return resultx.New(resultx.WithDataset(ds)), nil
}
17 changes: 17 additions & 0 deletions pkg/security/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type TenantManager interface {
GetTenants() []string
// GetUser returns user by tenant and username.
GetUser(tenant string, username string) (*config.User, bool)
// GetUsers returns users by tenant
GetUsers(tenant string) ([]*config.User, bool)
// GetClusters returns cluster names.
GetClusters(tenant string) []string
// GetTenantsOfCluster returns all tenants of cluster.
Expand Down Expand Up @@ -83,6 +85,21 @@ func (st *simpleTenantManager) GetUser(tenant string, username string) (*config.
return user, ok
}

func (st *simpleTenantManager) GetUsers(tenant string) ([]*config.User, bool) {
st.RLock()
defer st.RUnlock()
var users = []*config.User{}
exist, ok := st.tenants[tenant]
if !ok {
return nil, false
} else {
for _, v := range exist.users {
users = append(users, v)
}
}
return users, ok
}

func (st *simpleTenantManager) GetClusters(tenant string) []string {
st.RLock()
defer st.RUnlock()
Expand Down
10 changes: 10 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,16 @@ func (s *IntegrationSuite) TestShowCreate() {
assert.Equal(t, "student", table)
}

func (s *IntegrationSuite) TestShowUsers() {
var (
db = s.DB()
t = s.T()
)

_, err := db.Query("show users from arana")
assert.NoErrorf(t, err, "show users error: %v", err)
}

func (s *IntegrationSuite) TestDropTrigger() {
var (
db = s.DB()
Expand Down

0 comments on commit 76c4e80

Please sign in to comment.