zitadel/cmd/admin/initialise/verify_grant.go

48 lines
1.1 KiB
Go
Raw Normal View History

2022-02-11 13:07:32 +00:00
package initialise
import (
"database/sql"
_ "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"
)
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 {
config := Config{}
if err := viper.Unmarshal(&config); err != nil {
2022-02-11 13:07:32 +00:00
return err
}
return initialise(config, verifyGrant(config.Database))
2022-02-11 13:07:32 +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
}
}