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/caos/logging"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
err := initialise(config, VerifyGrant(config.Database.Database, config.Database.User.Username))
|
|
|
|
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-03-15 06:19:02 +00:00
|
|
|
logging.WithFields("user", username).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
|
|
|
}
|
|
|
|
}
|