fix: segregate env setup
This commit is contained in:
205
server/env/env.go
vendored
205
server/env/env.go
vendored
@@ -1,22 +1,80 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
"github.com/authorizerdev/authorizer/server/crypto"
|
||||
"github.com/authorizerdev/authorizer/server/envstore"
|
||||
"github.com/authorizerdev/authorizer/server/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// InitRequiredEnv to initialize EnvData and through error if required env are not present
|
||||
func InitRequiredEnv() {
|
||||
envPath := os.Getenv(constants.EnvKeyEnvPath)
|
||||
|
||||
if envPath == "" {
|
||||
envPath = `.env`
|
||||
}
|
||||
|
||||
if envstore.ARG_ENV_FILE != nil && *envstore.ARG_ENV_FILE != "" {
|
||||
envPath = *envstore.ARG_ENV_FILE
|
||||
}
|
||||
|
||||
err := godotenv.Load(envPath)
|
||||
if err != nil {
|
||||
log.Printf("using OS env instead of %s file", envPath)
|
||||
}
|
||||
|
||||
dbURL := os.Getenv(constants.EnvKeyDatabaseURL)
|
||||
dbType := os.Getenv(constants.EnvKeyDatabaseType)
|
||||
dbName := os.Getenv(constants.EnvKeyDatabaseName)
|
||||
|
||||
if dbType == "" {
|
||||
if envstore.ARG_DB_TYPE != nil && *envstore.ARG_DB_TYPE != "" {
|
||||
dbType = *envstore.ARG_DB_TYPE
|
||||
}
|
||||
|
||||
if dbType == "" {
|
||||
panic("DATABASE_TYPE is required")
|
||||
}
|
||||
}
|
||||
|
||||
if dbURL == "" {
|
||||
if envstore.ARG_DB_URL != nil && *envstore.ARG_DB_URL != "" {
|
||||
dbURL = *envstore.ARG_DB_URL
|
||||
}
|
||||
|
||||
if dbURL == "" {
|
||||
panic("DATABASE_URL is required")
|
||||
}
|
||||
}
|
||||
|
||||
if dbName == "" {
|
||||
if dbName == "" {
|
||||
dbName = "authorizer"
|
||||
}
|
||||
}
|
||||
|
||||
envstore.EnvInMemoryStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyEnvPath, envPath)
|
||||
envstore.EnvInMemoryStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyDatabaseURL, dbURL)
|
||||
envstore.EnvInMemoryStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyDatabaseType, dbType)
|
||||
envstore.EnvInMemoryStoreObj.UpdateEnvVariable(constants.StringStoreIdentifier, constants.EnvKeyDatabaseName, dbName)
|
||||
}
|
||||
|
||||
// InitEnv to initialize EnvData and through error if required env are not present
|
||||
func InitEnv() {
|
||||
// get clone of current store
|
||||
envData := envstore.EnvInMemoryStoreObj.GetEnvStoreClone()
|
||||
func InitAllEnv() {
|
||||
envData, err := GetEnvData()
|
||||
if err != nil {
|
||||
log.Println("No env data found in db, using local clone of env data")
|
||||
// get clone of current store
|
||||
envData = envstore.EnvInMemoryStoreObj.GetEnvStoreClone()
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyEnv] == "" {
|
||||
envData.StringEnv[constants.EnvKeyEnv] = os.Getenv(constants.EnvKeyEnv)
|
||||
@@ -36,19 +94,6 @@ func InitEnv() {
|
||||
envData.StringEnv[constants.EnvKeyAppURL] = os.Getenv(constants.EnvKeyAppURL)
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyEnvPath] == "" {
|
||||
envData.StringEnv[constants.EnvKeyEnvPath] = `.env`
|
||||
}
|
||||
|
||||
if envstore.ARG_ENV_FILE != nil && *envstore.ARG_ENV_FILE != "" {
|
||||
envData.StringEnv[constants.EnvKeyEnvPath] = *envstore.ARG_ENV_FILE
|
||||
}
|
||||
|
||||
err := godotenv.Load(envData.StringEnv[constants.EnvKeyEnvPath])
|
||||
if err != nil {
|
||||
log.Printf("using OS env instead of %s file", envData.StringEnv[constants.EnvKeyEnvPath])
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyPort] == "" {
|
||||
envData.StringEnv[constants.EnvKeyPort] = os.Getenv(constants.EnvKeyPort)
|
||||
if envData.StringEnv[constants.EnvKeyPort] == "" {
|
||||
@@ -60,37 +105,6 @@ func InitEnv() {
|
||||
envData.StringEnv[constants.EnvKeyAdminSecret] = os.Getenv(constants.EnvKeyAdminSecret)
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyDatabaseType] == "" {
|
||||
envData.StringEnv[constants.EnvKeyDatabaseType] = os.Getenv(constants.EnvKeyDatabaseType)
|
||||
|
||||
if envstore.ARG_DB_TYPE != nil && *envstore.ARG_DB_TYPE != "" {
|
||||
envData.StringEnv[constants.EnvKeyDatabaseType] = *envstore.ARG_DB_TYPE
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyDatabaseType] == "" {
|
||||
panic("DATABASE_TYPE is required")
|
||||
}
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyDatabaseURL] == "" {
|
||||
envData.StringEnv[constants.EnvKeyDatabaseURL] = os.Getenv(constants.EnvKeyDatabaseURL)
|
||||
|
||||
if envstore.ARG_DB_URL != nil && *envstore.ARG_DB_URL != "" {
|
||||
envData.StringEnv[constants.EnvKeyDatabaseURL] = *envstore.ARG_DB_URL
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyDatabaseURL] == "" {
|
||||
panic("DATABASE_URL is required")
|
||||
}
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyDatabaseName] == "" {
|
||||
envData.StringEnv[constants.EnvKeyDatabaseName] = os.Getenv(constants.EnvKeyDatabaseName)
|
||||
if envData.StringEnv[constants.EnvKeyDatabaseName] == "" {
|
||||
envData.StringEnv[constants.EnvKeyDatabaseName] = "authorizer"
|
||||
}
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeySmtpHost] == "" {
|
||||
envData.StringEnv[constants.EnvKeySmtpHost] = os.Getenv(constants.EnvKeySmtpHost)
|
||||
}
|
||||
@@ -111,32 +125,83 @@ func InitEnv() {
|
||||
envData.StringEnv[constants.EnvKeySenderEmail] = os.Getenv(constants.EnvKeySenderEmail)
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyJwtSecret] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtSecret] = os.Getenv(constants.EnvKeyJwtSecret)
|
||||
if envData.StringEnv[constants.EnvKeyJwtSecret] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtSecret] = uuid.New().String()
|
||||
}
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyCustomAccessTokenScript] == "" {
|
||||
envData.StringEnv[constants.EnvKeyCustomAccessTokenScript] = os.Getenv(constants.EnvKeyCustomAccessTokenScript)
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyJwtPrivateKey] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtPrivateKey] = os.Getenv(constants.EnvKeyJwtPrivateKey)
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyJwtPublicKey] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtPublicKey] = os.Getenv(constants.EnvKeyJwtPublicKey)
|
||||
}
|
||||
|
||||
algo := ""
|
||||
if envData.StringEnv[constants.EnvKeyJwtType] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtType] = os.Getenv(constants.EnvKeyJwtType)
|
||||
if envData.StringEnv[constants.EnvKeyJwtType] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtType] = "HS256"
|
||||
envData.StringEnv[constants.EnvKeyJwtType] = "RS256"
|
||||
algo = envData.StringEnv[constants.EnvKeyJwtType]
|
||||
} else {
|
||||
algo = envData.StringEnv[constants.EnvKeyJwtType]
|
||||
if !crypto.IsHMACA(algo) && !crypto.IsRSA(algo) && !crypto.IsECDSA(algo) {
|
||||
panic("JWT_TYPE is invalid")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyJwtSecret] == "" && crypto.IsHMACA(algo) {
|
||||
envData.StringEnv[constants.EnvKeyJwtSecret] = os.Getenv(constants.EnvKeyJwtSecret)
|
||||
if envData.StringEnv[constants.EnvKeyJwtSecret] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtSecret] = crypto.NewHMACKey()
|
||||
}
|
||||
}
|
||||
|
||||
if crypto.IsRSA(algo) || crypto.IsECDSA(algo) {
|
||||
privateKey, publicKey := "", ""
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyJwtPrivateKey] == "" {
|
||||
privateKey = os.Getenv(constants.EnvKeyJwtPrivateKey)
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyJwtPublicKey] == "" {
|
||||
publicKey = os.Getenv(constants.EnvKeyJwtPublicKey)
|
||||
}
|
||||
|
||||
// if algo is RSA / ECDSA, then we need to have both private and public key
|
||||
// if either of them is not present generate new keys
|
||||
if privateKey == "" || publicKey == "" {
|
||||
if crypto.IsRSA(algo) {
|
||||
_, privateKey, publicKey, err = crypto.NewRSAKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else if crypto.IsECDSA(algo) {
|
||||
_, privateKey, publicKey, err = crypto.NewECDSAKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// parse keys to make sure they are valid
|
||||
if crypto.IsRSA(algo) {
|
||||
_, err = crypto.ParseRsaPrivateKeyFromPemStr(privateKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = crypto.ParseRsaPublicKeyFromPemStr(publicKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else if crypto.IsECDSA(algo) {
|
||||
_, err = crypto.ParseEcdsaPrivateKeyFromPemStr(privateKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = crypto.ParseEcdsaPublicKeyFromPemStr(publicKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
fmt.Println("=> keys parsed successfully")
|
||||
}
|
||||
fmt.Println(privateKey)
|
||||
fmt.Println(publicKey)
|
||||
envData.StringEnv[constants.EnvKeyJwtPrivateKey] = privateKey
|
||||
envData.StringEnv[constants.EnvKeyJwtPublicKey] = publicKey
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyJwtRoleClaim] == "" {
|
||||
envData.StringEnv[constants.EnvKeyJwtRoleClaim] = os.Getenv(constants.EnvKeyJwtRoleClaim)
|
||||
|
||||
@@ -145,6 +210,10 @@ func InitEnv() {
|
||||
}
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyCustomAccessTokenScript] == "" {
|
||||
envData.StringEnv[constants.EnvKeyCustomAccessTokenScript] = os.Getenv(constants.EnvKeyCustomAccessTokenScript)
|
||||
}
|
||||
|
||||
if envData.StringEnv[constants.EnvKeyRedisURL] == "" {
|
||||
envData.StringEnv[constants.EnvKeyRedisURL] = os.Getenv(constants.EnvKeyRedisURL)
|
||||
}
|
||||
|
Reference in New Issue
Block a user