Skip to content

Commit

Permalink
backupccl: add an owner column behind the WITH PRIVILEGES option
Browse files Browse the repository at this point in the history
Previously, when users perform RESTORE, they are ignorant of the original owner.

This PR gives ownership data as a column behind privileges.

Resolves: #57906.

Release note(sql change): Update SHOW BACKUP ... WITH PRIVILEGES to display ownership information of objects in the backup.
  • Loading branch information
Elliebababa committed Feb 3, 2021
1 parent cba0f7a commit 8aa486f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pkg/ccl/backupccl/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func backupShowerHeaders(showSchemas bool, opts map[string]string) colinfo.Resul
}
if _, shouldShowPrivleges := opts[backupOptWithPrivileges]; shouldShowPrivleges {
baseHeaders = append(baseHeaders, colinfo.ResultColumn{Name: "privileges", Typ: types.String})
baseHeaders = append(baseHeaders, colinfo.ResultColumn{Name: "owner", Typ: types.String})
}
return baseHeaders
}
Expand Down Expand Up @@ -369,6 +370,8 @@ func backupShowerDefault(
}
if _, shouldShowPrivileges := opts[backupOptWithPrivileges]; shouldShowPrivileges {
row = append(row, tree.NewDString(showPrivileges(descriptor)))
owner := desc.GetPrivileges().Owner().SQLIdentifier()
row = append(row, tree.NewDString(owner))
}
rows = append(rows, row)
}
Expand Down
36 changes: 29 additions & 7 deletions pkg/ccl/backupccl/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,20 +350,42 @@ GRANT UPDATE ON top_secret TO agent_bond;

want := [][]string{
{`mi5`, `database`, `GRANT ALL ON mi5 TO admin; GRANT CREATE, DELETE, DROP, GRANT, INSERT, ` +
`SELECT, ZONECONFIG ON mi5 TO agents; GRANT ALL ON mi5 TO root; `},
`SELECT, ZONECONFIG ON mi5 TO agents; GRANT ALL ON mi5 TO root; `, `root`},
{`locator`, `schema`, `GRANT ALL ON locator TO admin; GRANT CREATE, GRANT ON locator TO agent_bond; GRANT ALL ON locator TO m; ` +
`GRANT ALL ON locator TO root; `},
{`continent`, `type`, `GRANT ALL ON continent TO admin; GRANT GRANT ON continent TO agent_bond; GRANT ALL ON continent TO m; GRANT ALL ON continent TO root; `},
{`_continent`, `type`, `GRANT ALL ON _continent TO admin; GRANT ALL ON _continent TO root; `},
`GRANT ALL ON locator TO root; `, `root`},
{`continent`, `type`, `GRANT ALL ON continent TO admin; GRANT GRANT ON continent TO agent_bond; GRANT ALL ON continent TO m; GRANT ALL ON continent TO root; `, `root`},
{`_continent`, `type`, `GRANT ALL ON _continent TO admin; GRANT ALL ON _continent TO root; `, `root`},
{`agent_locations`, `table`, `GRANT ALL ON agent_locations TO admin; ` +
`GRANT SELECT ON agent_locations TO agent_bond; GRANT UPDATE ON agent_locations TO agents; ` +
`GRANT ALL ON agent_locations TO m; GRANT ALL ON agent_locations TO root; `},
`GRANT ALL ON agent_locations TO m; GRANT ALL ON agent_locations TO root; `, `root`},
{`top_secret`, `table`, `GRANT ALL ON top_secret TO admin; ` +
`GRANT SELECT, UPDATE ON top_secret TO agent_bond; GRANT INSERT ON top_secret TO agents; ` +
`GRANT ALL ON top_secret TO m; GRANT ALL ON top_secret TO root; `},
`GRANT ALL ON top_secret TO m; GRANT ALL ON top_secret TO root; `, `root`},
}

showQuery := fmt.Sprintf(`SELECT object_name, object_type, privileges FROM [SHOW BACKUP '%s' WITH privileges]`, showPrivs)
showQuery := fmt.Sprintf(`SELECT object_name, object_type, privileges, owner FROM [SHOW BACKUP '%s' WITH privileges]`, showPrivs)
sqlDBRestore.CheckQueryResults(t, showQuery, want)

// Change the owner and expect the changes to be reflected in a new backup
showOwner := LocalFoo + "/show_owner"
sqlDB.Exec(t, `
ALTER DATABASE mi5 OWNER TO agent_thomas;
ALTER SCHEMA locator OWNER TO agent_thomas;
ALTER TYPE locator.continent OWNER TO agent_bond;
ALTER TABLE locator.agent_locations OWNER TO agent_bond;
`)
sqlDB.Exec(t, `BACKUP DATABASE mi5 TO $1;`, showOwner)

want = [][]string{
{`agent_thomas`},
{`agent_thomas`},
{`agent_bond`},
{`agent_bond`},
{`agent_bond`},
{`root`},
}

showQuery = fmt.Sprintf(`SELECT owner FROM [SHOW BACKUP '%s' WITH privileges]`, showOwner)
sqlDBRestore.CheckQueryResults(t, showQuery, want)
}
}
Expand Down

0 comments on commit 8aa486f

Please sign in to comment.