-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
135 lines (103 loc) · 3.18 KB
/
configuration.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"gopkg.in/yaml.v3"
)
type ConfigurationFile struct {
Address string
LoopSeconds uint `yaml:"loopSeconds"`
Unseal ConfigurationFileUnseal
}
type ConfigurationFileUnseal struct {
Key string
Env string
File string
}
var (
flagShowHelp bool
flagVerboseOutput bool
flagConfigurationFilename string
flagAddress string
flagLoopSeconds uint
flagUnsealKey string
flagUnsealEnv string
flagUnsealFile string
)
func (cf *ConfigurationFile) getUnsealKey() (string, error) {
if cf.Unseal.Key != "" {
log.Println("Using passed unseal key")
return cf.Unseal.Key, nil
}
if cf.Unseal.Env != "" {
value, present := os.LookupEnv(cf.Unseal.Env)
if !present {
return "", fmt.Errorf("%s %s", "Unseal environment var is not set:", cf.Unseal.Env)
}
log.Printf("Using environment var %s for unseal key\n", cf.Unseal.Env)
return value, nil
}
if cf.Unseal.File != "" {
unsealKeyBody, err := os.ReadFile(cf.Unseal.File)
if err != nil {
return "", fmt.Errorf("%s %s", "Error reading unseal key file:", err)
}
log.Printf("Using file %s for unseal key\n", cf.Unseal.File)
return strings.TrimSpace(string(unsealKeyBody)), nil
}
return "", fmt.Errorf("%s", "No unseal key provided")
}
func initConfiguration() {
flag.BoolVar(&flagShowHelp, "help", false, "Show help")
flag.BoolVar(&flagVerboseOutput, "verbose", false, "Show verbose output")
flag.StringVar(&flagConfigurationFilename, "config", "conf/configuration.yaml", "Path to the configuration file to load")
flag.StringVar(&flagAddress, "address", "", "The address of the Vault server")
flag.UintVar(&flagLoopSeconds, "loop-seconds", 0, "The time to wait between loop iterations for unseal attempts")
flag.StringVar(&flagUnsealKey, "unseal-key", "", "The unseal key")
flag.StringVar(&flagUnsealEnv, "unseal-env", "", "The environmental variable which contains the unseal key")
flag.StringVar(&flagUnsealFile, "unseal-file", "", "The path to the file which contains the unseal key")
}
func processConfiguration() (*ConfigurationFile, error) {
flag.Parse()
if flagShowHelp {
flag.Usage()
os.Exit(0)
}
configurationFileBody, err := os.ReadFile(flagConfigurationFilename)
if err != nil {
return nil, fmt.Errorf("%s %s", "Error reading configuration file:", err)
}
var cf ConfigurationFile
err = yaml.Unmarshal(configurationFileBody, &cf)
if err != nil {
return nil, fmt.Errorf("%s %s", "Error decoding configuration YAML:", err)
}
if flagAddress != "" {
cf.Address = flagAddress
}
if flagLoopSeconds != 0 {
cf.LoopSeconds = flagLoopSeconds
}
// Blanking out of unseal vars below is so that *any* flags passed in take
// precedence over the configuration file, and flag checks are in the order
// of least- to most-important flags
if flagUnsealFile != "" {
cf.Unseal.File = flagUnsealFile
cf.Unseal.Env = ""
cf.Unseal.Key = ""
}
if flagUnsealEnv != "" {
cf.Unseal.File = ""
cf.Unseal.Env = flagUnsealEnv
cf.Unseal.Key = ""
}
if flagUnsealKey != "" {
cf.Unseal.File = ""
cf.Unseal.Env = ""
cf.Unseal.Key = flagUnsealKey
}
return &cf, nil
}