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

Incorrect grant parsing fix #88

Merged
merged 1 commit into from
Aug 22, 2023
Merged
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
18 changes: 17 additions & 1 deletion mysql/resource_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type MySQLGrant struct {
Grant bool
}

func (m MySQLGrant) String() string {
return fmt.Sprintf("{Database=%v,Table=%v,Privileges=%v,Roles=%v,Grant=%v}", m.Database, m.Table, m.Privileges, m.Roles, m.Grant)
}

func resourceGrant() *schema.Resource {
return &schema.Resource{
CreateContext: CreateGrant,
Expand Down Expand Up @@ -537,7 +541,9 @@ func showGrant(ctx context.Context, db *sql.DB, user, database, table string, gr
Grant: grantOption,
}
for _, grant := range allGrants {
if grant.Database == database && grant.Table == table && grant.Grant == grantOption {
// We must normalize database as it may contain something like PROCEDURE `asd` or the same without backticks.
// TODO: write tests or consider some other way to handle permissions to PROCEDURE/FUNCTION
if normalizeDatabase(grant.Database) == normalizeDatabase(database) && grant.Table == table && grant.Grant == grantOption {
grants.Privileges = append(grants.Privileges, grant.Privileges...)
grants.Roles = append(grants.Roles, grant.Roles...)
}
Expand Down Expand Up @@ -641,6 +647,16 @@ func normalizeUserHost(userHost string) string {
return withoutDblQuotes
}

func normalizeDatabase(database string) string {
reProcedure := regexp.MustCompile("(?i)^(function|procedure) `(.*)$")
if reProcedure.MatchString(database) {
// This is only a hack - user can specify function / procedure as database.
database = reProcedure.ReplaceAllString(database, "$1 ${2}")
}

return database
}

func removeUselessPerms(grants []string) []string {
ret := []string{}
for _, grant := range grants {
Expand Down