zitadel/cmd/admin/initialise/verify_grant.go
Silvan 4272ea6fe1
fix: init sub commands (#3218)
* fix(init): add sub commands

* fix(init): admin user in config,
test(init): verify functions

* refactor: config, remove second commands

* refactor: init steps

* chore: fix link in readme

* chore: numerate sql files

* Update cmd/admin/initialise/sql/README.md

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* Update cmd/admin/initialise/sql/README.md

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix(init): remove unused index

* user

* fix database username in defaults.yaml

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-02-16 12:30:49 +00:00

48 lines
1.1 KiB
Go

package initialise
import (
"database/sql"
_ "embed"
"fmt"
"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
)
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 {
return err
}
return initialise(config, verifyGrant(config.Database))
},
}
}
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)),
)
}
}