authorizer/server/env/env.go

330 lines
10 KiB
Go
Raw Normal View History

2021-12-20 12:03:11 +00:00
package env
2021-07-28 06:23:37 +00:00
import (
2022-02-26 04:36:26 +00:00
"errors"
2021-07-28 06:23:37 +00:00
"os"
"strings"
2022-05-13 01:58:31 +00:00
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
2021-07-28 06:23:37 +00:00
"github.com/authorizerdev/authorizer/server/constants"
2022-02-26 04:14:55 +00:00
"github.com/authorizerdev/authorizer/server/crypto"
2022-05-29 11:52:46 +00:00
"github.com/authorizerdev/authorizer/server/memorystore"
"github.com/authorizerdev/authorizer/server/utils"
2021-07-28 06:23:37 +00:00
)
2022-01-17 06:02:13 +00:00
// InitEnv to initialize EnvData and through error if required env are not present
2022-02-26 04:36:26 +00:00
func InitAllEnv() error {
2022-02-26 04:14:55 +00:00
envData, err := GetEnvData()
if err != nil {
2022-05-13 01:58:31 +00:00
log.Info("No env data found in db, using local clone of env data")
2022-02-26 04:14:55 +00:00
// get clone of current store
2022-05-29 11:52:46 +00:00
envData, err = memorystore.Provider.GetEnvStore()
if err != nil {
log.Debug("Error while getting env data from memorystore: ", err)
return err
}
2022-02-26 04:14:55 +00:00
}
2022-01-17 06:02:13 +00:00
2022-05-29 11:52:46 +00:00
clientID := envData[constants.EnvKeyClientID].(string)
// unique client id for each instance
if clientID == "" {
clientID = uuid.New().String()
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyClientID] = clientID
}
2022-05-29 11:52:46 +00:00
clientSecret := envData[constants.EnvKeyClientSecret]
2022-02-28 07:44:16 +00:00
// unique client id for each instance
2022-03-07 06:53:45 +00:00
if clientSecret == "" {
2022-02-28 07:44:16 +00:00
clientSecret = uuid.New().String()
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyClientSecret] = clientSecret
2022-02-28 07:44:16 +00:00
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyEnv] == "" {
envData[constants.EnvKeyEnv] = os.Getenv(constants.EnvKeyEnv)
if envData[constants.EnvKeyEnv] == "" {
envData[constants.EnvKeyEnv] = "production"
2022-01-17 06:02:13 +00:00
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyEnv] == "production" {
envData[constants.EnvKeyIsProd] = true
2022-01-17 06:02:13 +00:00
} else {
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyIsProd] = false
2022-01-17 06:02:13 +00:00
}
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyAppURL] == "" {
envData[constants.EnvKeyAppURL] = os.Getenv(constants.EnvKeyAppURL)
2022-01-17 06:02:13 +00:00
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyAuthorizerURL] == "" {
envData[constants.EnvKeyAuthorizerURL] = os.Getenv(constants.EnvKeyAuthorizerURL)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyPort] == "" {
envData[constants.EnvKeyPort] = os.Getenv(constants.EnvKeyPort)
if envData[constants.EnvKeyPort] == "" {
envData[constants.EnvKeyPort] = "8080"
2022-01-17 06:02:13 +00:00
}
2021-07-28 06:23:37 +00:00
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyAccessTokenExpiryTime] == "" {
envData[constants.EnvKeyAccessTokenExpiryTime] = os.Getenv(constants.EnvKeyAccessTokenExpiryTime)
if envData[constants.EnvKeyAccessTokenExpiryTime] == "" {
envData[constants.EnvKeyAccessTokenExpiryTime] = "30m"
2022-03-25 14:59:00 +00:00
}
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyAdminSecret] == "" {
envData[constants.EnvKeyAdminSecret] = os.Getenv(constants.EnvKeyAdminSecret)
2022-01-17 06:02:13 +00:00
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeySmtpHost] == "" {
envData[constants.EnvKeySmtpHost] = os.Getenv(constants.EnvKeySmtpHost)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeySmtpPort] == "" {
envData[constants.EnvKeySmtpPort] = os.Getenv(constants.EnvKeySmtpPort)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeySmtpUsername] == "" {
envData[constants.EnvKeySmtpUsername] = os.Getenv(constants.EnvKeySmtpUsername)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeySmtpPassword] == "" {
envData[constants.EnvKeySmtpPassword] = os.Getenv(constants.EnvKeySmtpPassword)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeySenderEmail] == "" {
envData[constants.EnvKeySenderEmail] = os.Getenv(constants.EnvKeySenderEmail)
}
2022-05-29 11:52:46 +00:00
algo := envData[constants.EnvKeyJwtType].(string)
if algo == "" {
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyJwtType] = os.Getenv(constants.EnvKeyJwtType)
if envData[constants.EnvKeyJwtType] == "" {
envData[constants.EnvKeyJwtType] = "RS256"
algo = envData[constants.EnvKeyJwtType].(string)
2022-02-26 04:14:55 +00:00
} else {
2022-05-29 11:52:46 +00:00
algo = envData[constants.EnvKeyJwtType].(string)
2022-02-26 04:14:55 +00:00
if !crypto.IsHMACA(algo) && !crypto.IsRSA(algo) && !crypto.IsECDSA(algo) {
2022-05-25 07:00:22 +00:00
log.Debug("Invalid JWT Algorithm")
2022-02-26 04:36:26 +00:00
return errors.New("invalid JWT_TYPE")
2022-02-26 04:14:55 +00:00
}
}
}
2022-02-26 15:06:22 +00:00
if crypto.IsHMACA(algo) {
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyJwtSecret] == "" {
envData[constants.EnvKeyJwtSecret] = os.Getenv(constants.EnvKeyJwtSecret)
if envData[constants.EnvKeyJwtSecret] == "" {
envData[constants.EnvKeyJwtSecret], _, err = crypto.NewHMACKey(algo, clientID)
2022-02-26 15:06:22 +00:00
if err != nil {
return err
}
}
2021-12-31 08:22:10 +00:00
}
}
2022-02-26 04:14:55 +00:00
if crypto.IsRSA(algo) || crypto.IsECDSA(algo) {
2022-02-26 15:06:22 +00:00
privateKey, publicKey := "", ""
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyJwtPrivateKey] == "" {
2022-02-26 04:14:55 +00:00
privateKey = os.Getenv(constants.EnvKeyJwtPrivateKey)
}
2022-02-12 10:24:23 +00:00
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyJwtPublicKey] == "" {
2022-02-26 04:14:55 +00:00
publicKey = os.Getenv(constants.EnvKeyJwtPublicKey)
}
2022-02-12 10:24:23 +00:00
2022-02-26 04:14:55 +00:00
// 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) {
2022-02-26 15:06:22 +00:00
_, privateKey, publicKey, _, err = crypto.NewRSAKey(algo, clientID)
2022-02-26 04:14:55 +00:00
if err != nil {
2022-02-26 04:36:26 +00:00
return err
2022-02-26 04:14:55 +00:00
}
} else if crypto.IsECDSA(algo) {
2022-02-26 15:06:22 +00:00
_, privateKey, publicKey, _, err = crypto.NewECDSAKey(algo, clientID)
2022-02-26 04:14:55 +00:00
if err != nil {
2022-02-26 04:36:26 +00:00
return err
2022-02-26 04:14:55 +00:00
}
}
} else {
// parse keys to make sure they are valid
if crypto.IsRSA(algo) {
_, err = crypto.ParseRsaPrivateKeyFromPemStr(privateKey)
if err != nil {
2022-02-26 04:36:26 +00:00
return err
2022-02-26 04:14:55 +00:00
}
2022-02-26 15:06:22 +00:00
_, err := crypto.ParseRsaPublicKeyFromPemStr(publicKey)
if err != nil {
return err
}
2022-02-26 04:14:55 +00:00
} else if crypto.IsECDSA(algo) {
_, err = crypto.ParseEcdsaPrivateKeyFromPemStr(privateKey)
if err != nil {
2022-02-26 04:36:26 +00:00
return err
2022-02-26 04:14:55 +00:00
}
2022-02-26 15:06:22 +00:00
_, err := crypto.ParseEcdsaPublicKeyFromPemStr(publicKey)
2022-02-26 04:14:55 +00:00
if err != nil {
2022-02-26 04:36:26 +00:00
return err
2022-02-26 04:14:55 +00:00
}
}
2021-12-31 08:22:10 +00:00
}
2022-02-26 04:36:26 +00:00
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyJwtPrivateKey] = privateKey
envData[constants.EnvKeyJwtPublicKey] = publicKey
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyJwtRoleClaim] == "" {
envData[constants.EnvKeyJwtRoleClaim] = os.Getenv(constants.EnvKeyJwtRoleClaim)
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyJwtRoleClaim] == "" {
envData[constants.EnvKeyJwtRoleClaim] = "role"
}
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyCustomAccessTokenScript] == "" {
envData[constants.EnvKeyCustomAccessTokenScript] = os.Getenv(constants.EnvKeyCustomAccessTokenScript)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyRedisURL] == "" {
envData[constants.EnvKeyRedisURL] = os.Getenv(constants.EnvKeyRedisURL)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyGoogleClientID] == "" {
envData[constants.EnvKeyGoogleClientID] = os.Getenv(constants.EnvKeyGoogleClientID)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyGoogleClientSecret] == "" {
envData[constants.EnvKeyGoogleClientSecret] = os.Getenv(constants.EnvKeyGoogleClientSecret)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyGithubClientID] == "" {
envData[constants.EnvKeyGithubClientID] = os.Getenv(constants.EnvKeyGithubClientID)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyGithubClientSecret] == "" {
envData[constants.EnvKeyGithubClientSecret] = os.Getenv(constants.EnvKeyGithubClientSecret)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyFacebookClientID] == "" {
envData[constants.EnvKeyFacebookClientID] = os.Getenv(constants.EnvKeyFacebookClientID)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyFacebookClientSecret] == "" {
envData[constants.EnvKeyFacebookClientSecret] = os.Getenv(constants.EnvKeyFacebookClientSecret)
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyResetPasswordURL] == "" {
envData[constants.EnvKeyResetPasswordURL] = strings.TrimPrefix(os.Getenv(constants.EnvKeyResetPasswordURL), "/")
}
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyDisableBasicAuthentication] = os.Getenv(constants.EnvKeyDisableBasicAuthentication) == "true"
envData[constants.EnvKeyDisableEmailVerification] = os.Getenv(constants.EnvKeyDisableEmailVerification) == "true"
envData[constants.EnvKeyDisableMagicLinkLogin] = os.Getenv(constants.EnvKeyDisableMagicLinkLogin) == "true"
envData[constants.EnvKeyDisableLoginPage] = os.Getenv(constants.EnvKeyDisableLoginPage) == "true"
envData[constants.EnvKeyDisableSignUp] = os.Getenv(constants.EnvKeyDisableSignUp) == "true"
2022-01-17 06:02:13 +00:00
// no need to add nil check as its already done above
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeySmtpHost] == "" || envData[constants.EnvKeySmtpUsername] == "" || envData[constants.EnvKeySmtpPassword] == "" || envData[constants.EnvKeySenderEmail] == "" && envData[constants.EnvKeySmtpPort] == "" {
envData[constants.EnvKeyDisableEmailVerification] = true
envData[constants.EnvKeyDisableMagicLinkLogin] = true
}
2022-05-29 11:52:46 +00:00
if envData[constants.EnvKeyDisableEmailVerification].(bool) {
envData[constants.EnvKeyDisableMagicLinkLogin] = true
2021-07-28 06:23:37 +00:00
}
2022-02-12 10:24:23 +00:00
allowedOriginsSplit := strings.Split(os.Getenv(constants.EnvKeyAllowedOrigins), ",")
allowedOrigins := []string{}
hasWildCard := false
for _, val := range allowedOriginsSplit {
trimVal := strings.TrimSpace(val)
if trimVal != "" {
if trimVal != "*" {
host, port := utils.GetHostParts(trimVal)
allowedOrigins = append(allowedOrigins, host+":"+port)
} else {
hasWildCard = true
allowedOrigins = append(allowedOrigins, trimVal)
break
}
}
}
if len(allowedOrigins) > 1 && hasWildCard {
allowedOrigins = []string{"*"}
}
if len(allowedOrigins) == 0 {
allowedOrigins = []string{"*"}
}
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyAllowedOrigins] = allowedOrigins
2021-11-14 22:42:28 +00:00
2022-02-12 10:24:23 +00:00
rolesEnv := strings.TrimSpace(os.Getenv(constants.EnvKeyRoles))
2021-12-24 13:12:32 +00:00
rolesSplit := strings.Split(rolesEnv, ",")
roles := []string{}
2021-12-24 13:12:32 +00:00
if len(rolesEnv) == 0 {
roles = []string{"user"}
}
2022-02-12 10:24:23 +00:00
defaultRolesEnv := strings.TrimSpace(os.Getenv(constants.EnvKeyDefaultRoles))
2021-12-24 13:12:32 +00:00
defaultRoleSplit := strings.Split(defaultRolesEnv, ",")
defaultRoles := []string{}
2021-12-24 13:12:32 +00:00
if len(defaultRolesEnv) == 0 {
defaultRoles = []string{"user"}
}
2022-02-12 10:24:23 +00:00
protectedRolesEnv := strings.TrimSpace(os.Getenv(constants.EnvKeyProtectedRoles))
2021-12-24 13:12:32 +00:00
protectedRolesSplit := strings.Split(protectedRolesEnv, ",")
protectedRoles := []string{}
2021-12-24 13:12:32 +00:00
if len(protectedRolesEnv) > 0 {
for _, val := range protectedRolesSplit {
trimVal := strings.TrimSpace(val)
protectedRoles = append(protectedRoles, trimVal)
}
}
for _, val := range rolesSplit {
trimVal := strings.TrimSpace(val)
if trimVal != "" {
roles = append(roles, trimVal)
2022-01-31 06:05:24 +00:00
if utils.StringSliceContains(defaultRoleSplit, trimVal) {
defaultRoles = append(defaultRoles, trimVal)
}
}
}
2021-12-31 08:22:10 +00:00
if len(roles) > 0 && len(defaultRoles) == 0 && len(defaultRolesEnv) > 0 {
2022-05-25 07:00:22 +00:00
log.Debug("Default roles not found in roles list. It can be one from ROLES only")
2022-02-26 04:36:26 +00:00
return errors.New(`invalid DEFAULT_ROLE environment variable. It can be one from give ROLES environment variable value`)
}
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyRoles] = roles
envData[constants.EnvKeyDefaultRoles] = defaultRoles
envData[constants.EnvKeyProtectedRoles] = protectedRoles
2022-02-12 10:24:23 +00:00
if os.Getenv(constants.EnvKeyOrganizationName) != "" {
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyOrganizationName] = os.Getenv(constants.EnvKeyOrganizationName)
}
2022-02-12 10:24:23 +00:00
if os.Getenv(constants.EnvKeyOrganizationLogo) != "" {
2022-05-29 11:52:46 +00:00
envData[constants.EnvKeyOrganizationLogo] = os.Getenv(constants.EnvKeyOrganizationLogo)
}
2022-01-17 06:02:13 +00:00
2022-05-29 11:52:46 +00:00
memorystore.Provider.UpdateEnvStore(envData)
2022-02-26 04:36:26 +00:00
return nil
2021-07-28 06:23:37 +00:00
}