2022-02-11 13:07:32 +00:00
|
|
|
package initialise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-02-16 12:30:49 +00:00
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2022-02-11 13:07:32 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-02-11 13:07:32 +00:00
|
|
|
)
|
|
|
|
|
2022-02-16 12:30:49 +00:00
|
|
|
var (
|
|
|
|
searchGrant = "SELECT * FROM [SHOW GRANTS ON DATABASE %s] where grantee = $1 AND privilege_type = 'ALL'"
|
|
|
|
//go:embed sql/03_grant_user.sql
|
|
|
|
grantStmt string
|
|
|
|
)
|
|
|
|
|
2022-02-11 13:07:32 +00:00
|
|
|
func newGrant() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "grant",
|
|
|
|
Short: "set ALL grant to user",
|
|
|
|
Long: `Sets ALL grant to the database user.
|
|
|
|
|
|
|
|
Prereqesits:
|
|
|
|
- cockroachdb
|
|
|
|
`,
|
2022-03-28 08:05:09 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
config := MustNewConfig(viper.New())
|
|
|
|
|
2022-07-28 14:25:42 +00:00
|
|
|
err := initialise(config.Database, VerifyGrant(config.Database.Database(), config.Database.Username()))
|
2022-03-28 08:05:09 +00:00
|
|
|
logging.OnError(err).Fatal("unable to set grant")
|
2022-02-11 13:07:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 06:19:02 +00:00
|
|
|
func VerifyGrant(database, username string) func(*sql.DB) error {
|
2022-02-16 12:30:49 +00:00
|
|
|
return func(db *sql.DB) error {
|
2022-04-13 05:42:48 +00:00
|
|
|
logging.WithFields("user", username, "database", database).Info("verify grant")
|
2022-02-16 12:30:49 +00:00
|
|
|
return verify(db,
|
2022-03-15 06:19:02 +00:00
|
|
|
exists(fmt.Sprintf(searchGrant, database), username),
|
|
|
|
exec(fmt.Sprintf(grantStmt, database, username)),
|
2022-02-16 12:30:49 +00:00
|
|
|
)
|
2022-02-11 13:07:32 +00:00
|
|
|
}
|
|
|
|
}
|