2020-04-23 01:11:32 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
)
|
|
|
|
|
2021-03-22 09:04:09 +00:00
|
|
|
// SecretNameToEnvName converts a secret name into the env name.
|
|
|
|
func SecretNameToEnvName(secretName string) (envName string) {
|
|
|
|
return "authelia." + secretName + ".file"
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSecretKey(value string) (isSecretKey bool) {
|
|
|
|
for _, secretKey := range SecretNames {
|
|
|
|
if value == secretKey || value == SecretNameToEnvName(secretKey) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
// ValidateSecrets checks that secrets are either specified by config file/env or by file references.
|
|
|
|
func ValidateSecrets(configuration *schema.Configuration, validator *schema.StructValidator, viper *viper.Viper) {
|
2021-03-22 09:04:09 +00:00
|
|
|
configuration.JWTSecret = getSecretValue(SecretNames["JWTSecret"], validator, viper)
|
|
|
|
configuration.Session.Secret = getSecretValue(SecretNames["SessionSecret"], validator, viper)
|
2020-04-23 01:11:32 +00:00
|
|
|
|
|
|
|
if configuration.DuoAPI != nil {
|
2021-03-22 09:04:09 +00:00
|
|
|
configuration.DuoAPI.SecretKey = getSecretValue(SecretNames["DUOSecretKey"], validator, viper)
|
2020-04-23 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Session.Redis != nil {
|
2021-03-22 09:04:09 +00:00
|
|
|
configuration.Session.Redis.Password = getSecretValue(SecretNames["RedisPassword"], validator, viper)
|
|
|
|
|
|
|
|
if configuration.Session.Redis.HighAvailability != nil {
|
|
|
|
configuration.Session.Redis.HighAvailability.SentinelPassword =
|
|
|
|
getSecretValue(SecretNames["RedisSentinelPassword"], validator, viper)
|
|
|
|
}
|
2020-04-23 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 01:44:37 +00:00
|
|
|
if configuration.AuthenticationBackend.LDAP != nil {
|
|
|
|
configuration.AuthenticationBackend.LDAP.Password = getSecretValue(SecretNames["LDAPPassword"], validator, viper)
|
2020-04-23 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Notifier != nil && configuration.Notifier.SMTP != nil {
|
2021-03-22 09:04:09 +00:00
|
|
|
configuration.Notifier.SMTP.Password = getSecretValue(SecretNames["SMTPPassword"], validator, viper)
|
2020-04-23 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Storage.MySQL != nil {
|
2021-03-22 09:04:09 +00:00
|
|
|
configuration.Storage.MySQL.Password = getSecretValue(SecretNames["MySQLPassword"], validator, viper)
|
2020-04-23 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Storage.PostgreSQL != nil {
|
2021-03-22 09:04:09 +00:00
|
|
|
configuration.Storage.PostgreSQL.Password = getSecretValue(SecretNames["PostgreSQLPassword"], validator, viper)
|
2020-04-23 01:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSecretValue(name string, validator *schema.StructValidator, viper *viper.Viper) string {
|
|
|
|
configValue := viper.GetString(name)
|
2021-03-22 09:04:09 +00:00
|
|
|
fileEnvValue := viper.GetString(SecretNameToEnvName(name))
|
2020-04-23 01:11:32 +00:00
|
|
|
|
|
|
|
// Error Checking.
|
2020-05-08 01:01:57 +00:00
|
|
|
if fileEnvValue != "" && configValue != "" {
|
2020-04-23 01:11:32 +00:00
|
|
|
validator.Push(fmt.Errorf("error loading secret (%s): it's already defined in the config file", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive Secret.
|
|
|
|
if fileEnvValue != "" {
|
|
|
|
content, err := ioutil.ReadFile(fileEnvValue)
|
|
|
|
if err != nil {
|
|
|
|
validator.Push(fmt.Errorf("error loading secret file (%s): %s", name, err))
|
|
|
|
} else {
|
2020-09-18 12:05:43 +00:00
|
|
|
return strings.ReplaceAll(string(content), "\n", "")
|
2020-04-23 01:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-23 01:11:32 +00:00
|
|
|
return configValue
|
|
|
|
}
|