mirror of
https://github.com/paralus/paralus.git
synced 2026-05-06 16:36:46 +00:00
Update Auth constructor function and middleware
Auth constructor should handle creation of Kratos, database, ApiKeyService itself for benefit to other dependent modules/repos. The Prompt module does not need db instance hence it should not pass db parameter when initializing Auth context and middleware. Signed-off-by: Akshay Gaikwad <akshay.gaikwad@rafay.co>
This commit is contained in:
2
main.go
2
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()
|
||||
o := authv3.Option{}
|
||||
opts = append(opts, _grpc.UnaryInterceptor(
|
||||
ac.NewAuthUnaryInterceptor(o),
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
@@ -33,14 +38,23 @@ type authContext struct {
|
||||
}
|
||||
|
||||
// NewAuthContext setup authentication and authorization dependencies.
|
||||
func NewAuthContext(db *bun.DB) authContext {
|
||||
func NewAuthContext() 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 +85,11 @@ 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
|
||||
}
|
||||
|
||||
@@ -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: NewAuthContext(),
|
||||
opt: opt,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user