vitess.io/vitess@v0.16.2/go/vt/mysqlctl/permissions.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mysqlctl
    18  
    19  import (
    20  	"context"
    21  
    22  	"vitess.io/vitess/go/vt/mysqlctl/tmutils"
    23  	tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
    24  )
    25  
    26  // GetPermissions lists the permissions on the mysqld.
    27  // The rows are sorted in primary key order to help with comparing
    28  // permissions between tablets.
    29  func GetPermissions(mysqld MysqlDaemon) (*tabletmanagerdatapb.Permissions, error) {
    30  	ctx := context.TODO()
    31  	permissions := &tabletmanagerdatapb.Permissions{}
    32  
    33  	// get Users
    34  	qr, err := mysqld.FetchSuperQuery(ctx, "SELECT * FROM mysql.user ORDER BY host, user")
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	for _, row := range qr.Rows {
    39  		permissions.UserPermissions = append(permissions.UserPermissions, tmutils.NewUserPermission(qr.Fields, row))
    40  	}
    41  
    42  	// get Dbs
    43  	qr, err = mysqld.FetchSuperQuery(ctx, "SELECT * FROM mysql.db ORDER BY host, db, user")
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	for _, row := range qr.Rows {
    48  		permissions.DbPermissions = append(permissions.DbPermissions, tmutils.NewDbPermission(qr.Fields, row))
    49  	}
    50  
    51  	return permissions, nil
    52  }