2020-07-23 11:47:31 +02:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
|
|
|
)
|
|
|
|
|
2020-10-24 08:17:01 +02:00
|
|
|
const multipleValuesForSecretSource = "multiple values specified for secret source: specify either value, fromEnv of fromFile"
|
|
|
|
|
2020-07-23 11:47:31 +02:00
|
|
|
func validateSecretSource(source options.SecretSource) string {
|
|
|
|
switch {
|
|
|
|
case len(source.Value) > 0 && source.FromEnv == "" && source.FromFile == "":
|
2020-11-19 21:58:50 +02:00
|
|
|
return ""
|
2020-07-23 11:47:31 +02:00
|
|
|
case len(source.Value) == 0 && source.FromEnv != "" && source.FromFile == "":
|
|
|
|
return validateSecretSourceEnv(source.FromEnv)
|
|
|
|
case len(source.Value) == 0 && source.FromEnv == "" && source.FromFile != "":
|
|
|
|
return validateSecretSourceFile(source.FromFile)
|
|
|
|
default:
|
2020-10-24 08:17:01 +02:00
|
|
|
return multipleValuesForSecretSource
|
2020-07-23 11:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSecretSourceEnv(key string) string {
|
|
|
|
if value := os.Getenv(key); value == "" {
|
|
|
|
return fmt.Sprintf("error loading secret from environent: no value for for key %q", key)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateSecretSourceFile(path string) string {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
return fmt.Sprintf("error loadig secret from file: %v", err)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|