diff --git a/main.go b/main.go index 1b3a816..5940fe3 100644 --- a/main.go +++ b/main.go @@ -554,7 +554,7 @@ func runRPC(wg *sync.WaitGroup, ctx context.Context) { var opts []_grpc.ServerOption if !dev { _log.Infow("adding auth interceptor") - ac := authv3.NewAuthContext(db) + ac := authv3.NewAuthContext(kc, ks, as) o := authv3.Option{} opts = append(opts, _grpc.UnaryInterceptor( ac.NewAuthUnaryInterceptor(o), diff --git a/pkg/auth/v3/auth.go b/pkg/auth/v3/auth.go index 849bfcb..01bddea 100644 --- a/pkg/auth/v3/auth.go +++ b/pkg/auth/v3/auth.go @@ -1,6 +1,8 @@ package authv3 import ( + "database/sql" + "fmt" "os" "github.com/RafayLabs/rcloud-base/pkg/enforcer" @@ -8,6 +10,9 @@ import ( "github.com/RafayLabs/rcloud-base/pkg/service" kclient "github.com/ory/kratos-client-go" "github.com/uptrace/bun" + + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/driver/pgdriver" "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -32,15 +37,26 @@ type authContext struct { as service.AuthzService } -// NewAuthContext setup authentication and authorization dependencies. -func NewAuthContext(db *bun.DB) authContext { +// SetupAuthContext sets up new authContext along with its +// dependencies. If the caller already has instances of authContext +// fields created then use NewAuthContext instead. +func SetupAuthContext() authContext { var ( kc *kclient.APIClient kratosScheme string kratosAddr string + db *bun.DB ) - // TODO: https://github.com/RafayLabs/prompt/pull/3#issuecomment-1073557206 - // Where exactly should we be getting these values from? + + // Initialize database + dbUser := getEnvWithDefault("DB_USER", "admindbuser") + dbPassword := getEnvWithDefault("DB_PASSWORD", "admindbpassword") + dbAddr := getEnvWithDefault("DB_ADDR", "localhost:5432") + dbName := getEnvWithDefault("DB_NAME", "admindb") + dsn := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", dbUser, dbPassword, dbAddr, dbName) + sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) + db = bun.NewDB(sqldb, pgdialect.New()) + if v, ok := os.LookupEnv("KRATOS_SCHEME"); ok { kratosScheme = v } else { @@ -71,3 +87,27 @@ func NewAuthContext(db *bun.DB) authContext { return authContext{kc: kc, as: as, ks: service.NewApiKeyService(db)} } + +func getEnvWithDefault(env, def string) string { + val := os.Getenv(env) + if val == "" { + return def + } + return val +} + +// NewAuthContext instantiate authContext. NewAuthContext creates +// authContext reusing dependency instances from calling function +// instead of creating new instances. To create authContext along with +// its dependencies, use SetupAuthContext. +func NewAuthContext( + kc *kclient.APIClient, + apiKeySvc service.ApiKeyService, + authzSvc service.AuthzService, +) authContext { + return authContext{ + kc: kc, + ks: apiKeySvc, + as: authzSvc, + } +} diff --git a/pkg/auth/v3/middleware.go b/pkg/auth/v3/middleware.go index 3eae9b8..c49d48c 100644 --- a/pkg/auth/v3/middleware.go +++ b/pkg/auth/v3/middleware.go @@ -18,10 +18,9 @@ type authMiddleware struct { opt Option } -func NewAuthMiddleware(opt Option, db *bun.DB) negroni.Handler { +func NewAuthMiddleware(opt Option) negroni.Handler { return &authMiddleware{ - db: db, - ac: NewAuthContext(db), + ac: SetupAuthContext(), opt: opt, } }