update script email_pass_to_oidc
This commit is contained in:
parent
3ccb7519ef
commit
e767c4f5ed
2
Makefile
2
Makefile
@ -1,2 +1,4 @@
|
|||||||
run_usersUpdateLoginMethod:
|
run_usersUpdateLoginMethod:
|
||||||
go run cmd/main.go -c usersUpdateLoginMethod
|
go run cmd/main.go -c usersUpdateLoginMethod
|
||||||
|
help:
|
||||||
|
go run cmd/main.go -c help
|
||||||
@ -25,7 +25,8 @@ func main() {
|
|||||||
config.MustLoad(confPath)
|
config.MustLoad(confPath)
|
||||||
|
|
||||||
if commandName == "" {
|
if commandName == "" {
|
||||||
l.Log.Fatal().Msg("flag '-c' required")
|
l.Log.Error().Msg("flag '-c' required")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.MustInit()
|
logger.MustInit()
|
||||||
@ -36,12 +37,13 @@ func main() {
|
|||||||
l.Log.Warn().Err(e.ErrCommandNotFound).Str("command", commandName).Send()
|
l.Log.Warn().Err(e.ErrCommandNotFound).Str("command", commandName).Send()
|
||||||
l.Log.Info().Msg("selected command - 'help'")
|
l.Log.Info().Msg("selected command - 'help'")
|
||||||
if err := command.Scripts["help"].Exec(); err != nil {
|
if err := command.Scripts["help"].Exec(); err != nil {
|
||||||
l.Log.Fatal().Err(err).Str("command", commandName).Msg("failed to use help command")
|
l.Log.Error().Err(err).Str("command", commandName).Msg("failed to use help command")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Log.Info().Msgf("selected command - %s", commandName)
|
l.Log.Info().Str("selected command", commandName).Send()
|
||||||
if err := cmd.Exec(); err != nil {
|
if err := cmd.Exec(); err != nil {
|
||||||
l.Log.Error().Err(err).Str("command", commandName).Msg("failed to execude command")
|
l.Log.Error().Err(err).Str("command", commandName).Msg("failed to execude command")
|
||||||
return
|
return
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
c "scripts/internal/config"
|
c "scripts/internal/config"
|
||||||
l "scripts/pkg/logger"
|
l "scripts/pkg/logger"
|
||||||
"scripts/pkg/postgres"
|
"scripts/pkg/postgres"
|
||||||
@ -13,7 +14,10 @@ import (
|
|||||||
const Name = "usersUpdateLoginMethod"
|
const Name = "usersUpdateLoginMethod"
|
||||||
|
|
||||||
func UsersUpdateLoginMethod() error {
|
func UsersUpdateLoginMethod() error {
|
||||||
postgres.InitDB()
|
if err := postgres.InitDB(); err != nil {
|
||||||
|
l.Log.Error().Err(err).Msg("failed to init postgres")
|
||||||
|
return err
|
||||||
|
}
|
||||||
defer postgres.DB.Close()
|
defer postgres.DB.Close()
|
||||||
|
|
||||||
tokenData, err := keycloakAccessToken()
|
tokenData, err := keycloakAccessToken()
|
||||||
@ -48,12 +52,19 @@ func UsersUpdateLoginMethod() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func keycloakAccessToken() (*KeycloakTokenResponse, error) {
|
func keycloakAccessToken() (*KeycloakTokenResponse, error) {
|
||||||
url := fmt.Sprintf("%s/realms/%s/protocol/openid-connect/token", c.Conf.KeycloakSettings.Address, "master")
|
const path = "/realms/master/protocol/openid-connect/token"
|
||||||
|
|
||||||
|
endpoint, err := url.Parse(c.Conf.KeycloakSettings.Address)
|
||||||
|
if err != nil {
|
||||||
|
l.Log.Error().Err(err).Msg("failed to create url")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
endpoint.Path = path
|
||||||
|
|
||||||
var data bytes.Buffer
|
var data bytes.Buffer
|
||||||
fmt.Fprintf(&data, "grant_type=password&client_id=admin-cli&username=%s&password=%s", c.Conf.KeycloakSettings.AdminName, c.Conf.KeycloakSettings.AdminPass)
|
fmt.Fprintf(&data, "grant_type=password&client_id=admin-cli&username=%s&password=%s", c.Conf.KeycloakSettings.AdminName, c.Conf.KeycloakSettings.AdminPass)
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, &data)
|
req, err := http.NewRequest("POST", endpoint.String(), &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Error().Err(err).Msg("failed to create http request")
|
l.Log.Error().Err(err).Msg("failed to create http request")
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -100,9 +111,16 @@ func allLoopUsers() (map[string]LoopUser, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func allKeycloakUsers(accessToken string) ([]KeycloakUser, error) {
|
func allKeycloakUsers(accessToken string) ([]KeycloakUser, error) {
|
||||||
url := fmt.Sprintf("%s/admin/realms/%s/users", c.Conf.KeycloakSettings.Address, c.Conf.KeycloakSettings.RealmName)
|
var path = fmt.Sprintf("/admin/realms/%s/users", c.Conf.KeycloakSettings.RealmName)
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
endpoint, err := url.Parse(c.Conf.KeycloakSettings.Address)
|
||||||
|
if err != nil {
|
||||||
|
l.Log.Error().Err(err).Msg("failed to create url")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
endpoint.Path = path
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", endpoint.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Error().Err(err).Msg("failed to create http request")
|
l.Log.Error().Err(err).Msg("failed to create http request")
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package postgres
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
c "scripts/internal/config"
|
c "scripts/internal/config"
|
||||||
l "scripts/pkg/logger"
|
l "scripts/pkg/logger"
|
||||||
"time"
|
"time"
|
||||||
@ -33,7 +34,7 @@ func WithContext(ctx context.Context) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitDB(opts ...Option) {
|
func InitDB(opts ...Option) error {
|
||||||
p := &Postgres{
|
p := &Postgres{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
}
|
}
|
||||||
@ -44,7 +45,8 @@ func InitDB(opts ...Option) {
|
|||||||
|
|
||||||
db, err := sqlx.ConnectContext(p.ctx, c.Conf.SqlSettings.DriverName, c.Conf.SqlSettings.DataSource)
|
db, err := sqlx.ConnectContext(p.ctx, c.Conf.SqlSettings.DriverName, c.Conf.SqlSettings.DataSource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Log.Fatal().Err(err).Msg("failed to connect DB")
|
l.Log.Error().Err(err).Msg("failed to connect DB")
|
||||||
|
return fmt.Errorf("failed to connect DB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
db.SetMaxIdleConns(c.Conf.SqlSettings.MaxIdleConns)
|
db.SetMaxIdleConns(c.Conf.SqlSettings.MaxIdleConns)
|
||||||
@ -53,16 +55,19 @@ func InitDB(opts ...Option) {
|
|||||||
db.SetConnMaxIdleTime(time.Duration(c.Conf.SqlSettings.ConnMaxIdleTimeMilliseconds) * time.Millisecond)
|
db.SetConnMaxIdleTime(time.Duration(c.Conf.SqlSettings.ConnMaxIdleTimeMilliseconds) * time.Millisecond)
|
||||||
|
|
||||||
if err := db.Ping(); err != nil {
|
if err := db.Ping(); err != nil {
|
||||||
l.Log.Fatal().Err(err).Msg("failed to ping DB")
|
l.Log.Error().Err(err).Msg("failed to ping DB")
|
||||||
|
return fmt.Errorf("failed to ping DB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.conn = db
|
p.conn = db
|
||||||
DB = p
|
DB = p
|
||||||
l.Log.Info().Msg("connect to postgres successfully ")
|
l.Log.Info().Msg("connect to postgres successfully ")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Postgres) Close() {
|
func (p *Postgres) Close() {
|
||||||
if err := p.Conn().Close(); err != nil {
|
if err := p.Conn().Close(); err != nil {
|
||||||
l.Log.Error().Err(err).Msg("failed to close connect DB")
|
l.Log.Error().Err(err).Msg("failed to close connect DB")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user