Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the service user password fetching from vault in run migrations… #496

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions capten/common-pkg/postgres/db-init/db_migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbinit

import (
"context"
"fmt"
"os"
"time"
Expand All @@ -12,6 +13,7 @@ import (
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/intelops/go-common/logging"
"github.com/kelseyhightower/envconfig"
"github.com/kube-tarian/kad/capten/common-pkg/credential"
"github.com/pkg/errors"
)

Expand All @@ -26,12 +28,13 @@ const (
var log = logging.NewLogger()

type DBConfig struct {
DBAddr string `envconfig:"PG_DB_HOST" required:"true"`
DBPort string `envconfig:"PG_DB_PORT" default:"5432"`
DBName string `envconfig:"PG_DB_NAME" required:"true"`
Username string `envconfig:"PG_DB_SERVICE_USERNAME" required:"true"`
Password string `envconfig:"PG_DB_SERVICE_USERPASSWORD" required:"false"`
SourceURI string `envconfig:"PG_SOURCE_URI" default:"file:///postgres/migrations"`
DBAddr string `envconfig:"PG_DB_HOST" required:"true"`
DBPort string `envconfig:"PG_DB_PORT" default:"5432"`
DBName string `envconfig:"PG_DB_NAME" required:"true"`
EntityName string `envconfig:"PG_DB_ENTITY_NAME" default:"postgres"`
Username string `envconfig:"PG_DB_SERVICE_USERNAME" required:"true"`
Password string `envconfig:"PG_DB_SERVICE_USERPASSWORD" required:"false"`
SourceURI string `envconfig:"PG_SOURCE_URI" default:"file:///postgres/migrations"`
}

func RunMigrations(mode Mode) error {
Expand All @@ -40,6 +43,14 @@ func RunMigrations(mode Mode) error {
return err
}

if len(conf.Password) == 0 {
serviceCredential, err := credential.GetServiceUserCredential(context.Background(),
conf.EntityName, conf.Username)
if err != nil {
return errors.WithMessage(err, "DB user credential fetching failed")
}
conf.Password = serviceCredential.Password
}
return RunMigrationsWithConfig(conf, mode)
}

Expand Down
16 changes: 14 additions & 2 deletions capten/common-pkg/postgres/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
package postgresdb

import (
"context"
"errors"
"fmt"

"github.com/intelops/go-common/logging"
"github.com/kelseyhightower/envconfig"
"github.com/kube-tarian/kad/capten/common-pkg/credential"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"

"gorm.io/driver/postgres"
Expand All @@ -28,9 +30,10 @@ const (

type Config struct {
Username string `envconfig:"PG_DB_SERVICE_USERNAME" required:"true"`
Password string `envconfig:"PG_DB_SERVICE_USERPASSWORD" required:"true"`
Password string `envconfig:"PG_DB_SERVICE_USERPASSWORD" required:"false"`
DBHost string `envconfig:"PG_DB_HOST" required:"true"`
DBPort string `envconfig:"PG_DB_PORT" required:"true"`
EntityName string `envconfig:"PG_DB_ENTITY_NAME" default:"postgres"`
DatabaseName string `envconfig:"PG_DB_NAME" required:"true"`
IsTLSEnabled bool `envconfig:"PG_DB_TLS_ENABLED" default:"false"`
}
Expand All @@ -46,6 +49,15 @@ func NewDBFromENV(logger logging.Logger) (*gorm.DB, error) {
return nil, err
}

if len(conf.Password) == 0 {
serviceCredential, err := credential.GetServiceUserCredential(context.Background(),
conf.EntityName, conf.Username)
if err != nil {
return nil, err
}
conf.Password = serviceCredential.Password
}

return NewDB(&conf, logger)
}

Expand All @@ -68,7 +80,7 @@ func NewDBClient(logger logging.Logger) (store *DBClient, err error) {
logger.Debug("Getting db connection for ...")
session, err := NewDBFromENV(logger)
if err != nil {
return nil, fmt.Errorf("error while creating mariadb client session, %v", err)
return nil, fmt.Errorf("error while creating postgres client session, %v", err)
}
store = &DBClient{
session: session,
Expand Down
Loading