2020-04-23 08:47:27 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-03 16:55:21 +07:00
|
|
|
"strings"
|
2020-04-23 08:47:27 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2020-04-23 08:47:27 +07:00
|
|
|
)
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
// ValidateKeys determines if all provided keys are valid.
|
|
|
|
func ValidateKeys(keys []string, prefix string, validator *schema.StructValidator) {
|
2020-04-23 08:47:27 +07:00
|
|
|
var errStrings []string
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-23 08:47:27 +07:00
|
|
|
for _, key := range keys {
|
2021-08-03 16:55:21 +07:00
|
|
|
expectedKey := reKeyReplacer.ReplaceAllString(key, "[]")
|
2020-04-23 08:47:27 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
if utils.IsStringInSlice(expectedKey, ValidKeys) {
|
2021-03-22 16:04:09 +07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
if newKey, ok := replacedKeys[expectedKey]; ok {
|
2021-04-16 08:44:37 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtReplacedConfigurationKey, key, newKey))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
if err, ok := specificErrorKeys[expectedKey]; ok {
|
2020-04-23 08:47:27 +07:00
|
|
|
if !utils.IsStringInSlice(err, errStrings) {
|
|
|
|
errStrings = append(errStrings, err)
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-03 16:55:21 +07:00
|
|
|
if strings.HasPrefix(key, prefix) {
|
|
|
|
validator.PushWarning(fmt.Errorf("configuration environment variable not expected: %s", key))
|
|
|
|
} else {
|
|
|
|
validator.Push(fmt.Errorf("configuration key not expected: %s", key))
|
|
|
|
}
|
2020-04-23 08:47:27 +07:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-23 08:47:27 +07:00
|
|
|
for _, err := range errStrings {
|
|
|
|
validator.Push(errors.New(err))
|
|
|
|
}
|
|
|
|
}
|