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/caos/zitadel/internal/database"
|
|
|
|
"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
|
|
|
|
`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-02-16 12:30:49 +00:00
|
|
|
config := Config{}
|
|
|
|
if err := viper.Unmarshal(&config); err != nil {
|
2022-02-11 13:07:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-16 12:30:49 +00:00
|
|
|
return initialise(config, verifyGrant(config.Database))
|
2022-02-11 13:07:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 12:30:49 +00:00
|
|
|
func verifyGrant(config database.Config) func(*sql.DB) error {
|
|
|
|
return func(db *sql.DB) error {
|
|
|
|
logging.WithFields("user", config.Username).Info("verify grant")
|
|
|
|
return verify(db,
|
|
|
|
exists(fmt.Sprintf(searchGrant, config.Database), config.Username),
|
|
|
|
exec(fmt.Sprintf(grantStmt, config.Database, config.Username)),
|
|
|
|
)
|
2022-02-11 13:07:32 +00:00
|
|
|
}
|
|
|
|
}
|