-
Notifications
You must be signed in to change notification settings - Fork 14
/
configuration_factory.go
156 lines (132 loc) · 4.24 KB
/
configuration_factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package config
import (
"errors"
"fmt"
"github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/common"
"io/ioutil"
"os"
"strings"
jwtAuthenticator "github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/jwt"
k8sAuthenticator "github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/k8s"
"github.com/cyberark/conjur-authn-k8s-client/pkg/log"
)
const authnURLVarName string = "CONJUR_AUTHN_URL"
// AuthnSettings represents a group of authenticator client configuration settings.
type AuthnSettings map[string]string
// NewConfigFromEnv returns a config ConfigFromEnv using the standard file reader for reading certs
func NewConfigFromEnv() (Configuration, error) {
return ConfigFromEnv(ioutil.ReadFile)
}
// ConfigFromEnv returns a new authenticator configuration object
func ConfigFromEnv(readFileFunc common.ReadFileFunc) (Configuration, error) {
return NewConfigFromCustomEnv(readFileFunc, os.Getenv)
}
func NewConfigFromCustomEnv(readFileFunc common.ReadFileFunc, customEnv func(key string) string) (Configuration, error) {
log.Debug(log.CAKC068)
configureDebugIfNeeded(customEnv)
authnUrl := customEnv(authnURLVarName)
conf, err := getConfiguration(authnUrl)
if err != nil {
return nil, err
}
envSettings := GatherSettings(conf, customEnv)
errLogs := envSettings.validate(conf, readFileFunc)
if len(errLogs) > 0 {
logErrors(errLogs)
return nil, errors.New(log.CAKC061)
}
log.Debug(log.CAKC074)
conf.LoadConfig(envSettings)
return conf, nil
}
// GatherSettings retrieves authenticator client configuration settings from a slice
// of arbitrary `func(key string) string` functions. Values received from 'Getter' functions
// are prioritized in the order that the functions are provided.
func GatherSettings(conf Configuration, getters ...func(key string) string) AuthnSettings {
log.Debug(log.CAKC071)
defaultVariables := conf.GetDefaultValues()
getDefault := func(key string) string {
return defaultVariables[key]
}
getters = append(getters, getDefault)
settings := make(AuthnSettings)
getEnv := getConfigVariable(getters...)
for _, key := range conf.GetEnvVariables() {
value := getEnv(key)
settings[key] = value
}
log.Debug(log.CAKC072)
return settings
}
func getConfiguration(url string) (Configuration, error) {
switch {
case strings.Contains(url, k8sAuthenticator.AuthnType):
log.Info(log.CAKC070, k8sAuthenticator.AuthnType)
return &k8sAuthenticator.Config{}, nil
case strings.Contains(url, jwtAuthenticator.AuthnType):
log.Info(log.CAKC070, jwtAuthenticator.AuthnType)
return &jwtAuthenticator.Config{}, nil
default:
return nil, fmt.Errorf(log.CAKC063, url)
}
}
// Validate confirms that the given AuthnSettings yield a valid authenticator
// client configuration. Returns a list of Error logs.
func (settings AuthnSettings) validate(conf Configuration, readFileFunc common.ReadFileFunc) []error {
log.Debug(log.CAKC073)
errorLogs := []error{}
// ensure required values exist
for _, key := range conf.GetRequiredVariables() {
if settings[key] == "" {
errorLogs = append(errorLogs, fmt.Errorf(log.CAKC062, key))
}
}
// ensure provided values are of the correct type
for _, key := range conf.GetEnvVariables() {
err := common.ValidateSetting(key, settings[key])
if err != nil {
errorLogs = append(errorLogs, err)
}
}
// ensure that the certificate settings are valid
cert, err := common.ReadSSLCert(settings, readFileFunc)
if err != nil {
errorLogs = append(errorLogs, err)
} else {
if settings["CONJUR_SSL_CERTIFICATE"] == "" {
settings["CONJUR_SSL_CERTIFICATE"] = string(cert)
}
}
return errorLogs
}
func logErrors(errLogs []error) {
for _, err := range errLogs {
log.Error(err.Error())
}
}
func getConfigVariable(getters ...func(key string) string) func(string) string {
return func(key string) string {
var val string
for _, getter := range getters {
val = getter(key)
if len(val) > 0 {
return val
}
}
return ""
}
}
func configureDebugIfNeeded(getConfigFunc func(key string) string) {
validVal := "true"
debugValue := getConfigFunc("DEBUG")
switch debugValue {
case validVal:
log.EnableDebugMode()
case "":
// Log level not configured
break
default:
// Log level is configured but it's invalid
log.Warn(log.CAKC034, debugValue, validVal)
}
}