-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
69 lines (55 loc) · 2.01 KB
/
config.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
package config
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/dinumathai/auth-webhook-sample/log"
"github.com/dinumathai/auth-webhook-sample/types"
"github.com/ghodss/yaml"
)
var AppConfig = types.ConfigMap{}
// Load will load configuration from k8s config map.
// If it did not find any then it will fall back to config map provided with binary
func Load() (*types.ConfigMap, error) {
var config types.ConfigMap
data, err := ReadConfigData("Main auth data", "CONFIG_FILE", "auth_config.yaml")
if err != nil {
return &config, err
}
if yamlErr := yaml.Unmarshal(data, &config); yamlErr != nil {
log.Fatalf("Error deserializing yaml data starting %s: %s", string(data[0:20]), yamlErr.Error())
return nil, yamlErr
}
signingKey := os.Getenv("AUTH_SIGING_KEY")
if len(signingKey) != 0 {
config.AuthConfig.AuthSigningKey = signingKey
}
if len(config.AuthConfig.AuthSigningKey) == 0 {
log.Fatal("Unable to get SigningKey from environment variable(AUTH_SIGING_KEY) or application configuration")
}
AppConfig = config
return &config, nil
}
// ReadConfigData will read the data from a k8s config map.
// If it did not find any then it will fall back to a file provided with binary.
// Name: descriptive string for log messages
// Envvar: the name of the environment var where the relative path to the config file is stored
func ReadConfigData(name string, envvar string, fallback string) ([]byte, error) {
var data []byte
fileP := os.Getenv(envvar) //Get File path from Env Var
if fileP == "" { // None derived from env var & mapped k8s config-map so fall back
wd, _ := os.Getwd()
fileP = filepath.Join(wd, "config", fallback)
log.Infof("No k8s %s given in %s, falling back to: %s", name, envvar, fileP)
} else {
fileP = filepath.FromSlash(fileP)
log.Infof("Loading %s from k8s config Map: %s", name, fileP)
name += " via k8s"
}
data, err := ioutil.ReadFile(fileP)
if err != nil {
log.Fatalf("Error reading %s from %s: %s", name, fileP, err.Error())
return data, err
}
return data, nil
}