This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
config.go
192 lines (157 loc) · 4.38 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package conjurapi
import (
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/infamousjoeg/authn-jwt-gitlab/internal/conjurapi/logging"
)
var supportedAuthnTypes = []string{"authn", "ldap"}
type Config struct {
Account string `yaml:"account,omitempty"`
ApplianceURL string `yaml:"appliance_url,omitempty"`
NetRCPath string `yaml:"netrc_path,omitempty"`
SSLCert string `yaml:"-"`
SSLCertPath string `yaml:"cert_file,omitempty"`
AuthnType string `yaml:"authn_type,omitempty"`
ServiceID string `yaml:"service_id,omitempty"`
}
func (c *Config) IsHttps() bool {
return c.SSLCertPath != "" || c.SSLCert != ""
}
func (c *Config) Validate() error {
errors := []string{}
if c.ApplianceURL == "" {
errors = append(errors, "Must specify an ApplianceURL")
}
if c.Account == "" {
errors = append(errors, "Must specify an Account")
}
if c.AuthnType != "" && !contains(supportedAuthnTypes, c.AuthnType) {
errors = append(errors, fmt.Sprintf("AuthnType must be one of %v", supportedAuthnTypes))
}
if c.AuthnType == "ldap" && c.ServiceID == "" {
errors = append(errors, "Must specify a ServiceID when using LDAP")
}
if len(errors) == 0 {
return nil
} else if logging.ApiLog.Level == logrus.DebugLevel {
errors = append(errors, fmt.Sprintf("config: %+v", c))
}
return fmt.Errorf("%s", strings.Join(errors, " -- "))
}
func (c *Config) ReadSSLCert() ([]byte, error) {
if c.SSLCert != "" {
return []byte(c.SSLCert), nil
}
return ioutil.ReadFile(c.SSLCertPath)
}
func (c *Config) BaseURL() string {
prefix := ""
if !strings.HasPrefix(c.ApplianceURL, "http") {
if c.IsHttps() {
prefix = "https://"
} else {
prefix = "http://"
}
}
return prefix + c.ApplianceURL
}
func mergeValue(a, b string) string {
if len(b) != 0 {
return b
}
return a
}
func (c *Config) merge(o *Config) {
c.ApplianceURL = mergeValue(c.ApplianceURL, o.ApplianceURL)
c.Account = mergeValue(c.Account, o.Account)
c.SSLCert = mergeValue(c.SSLCert, o.SSLCert)
c.SSLCertPath = mergeValue(c.SSLCertPath, o.SSLCertPath)
c.NetRCPath = mergeValue(c.NetRCPath, o.NetRCPath)
c.AuthnType = mergeValue(c.AuthnType, o.AuthnType)
c.ServiceID = mergeValue(c.ServiceID, o.ServiceID)
}
func (c *Config) mergeYAML(filename string) error {
buf, err := ioutil.ReadFile(filename)
if err != nil {
logging.ApiLog.Debugf("Failed reading %s, %v\n", filename, err)
// It is not an error if this file does not exist
return nil
}
aux := struct {
ConjurVersion string `yaml:"version"`
Config `yaml:",inline"`
}{}
if err := yaml.Unmarshal(buf, &aux); err != nil {
logging.ApiLog.Errorf("Parsing error %s: %s\n", filename, err)
return err
}
logging.ApiLog.Debugf("Config from %s: %+v\n", filename, aux.Config)
c.merge(&aux.Config)
return nil
}
func (c *Config) mergeEnv() {
env := Config{
ApplianceURL: os.Getenv("CONJUR_APPLIANCE_URL"),
SSLCert: os.Getenv("CONJUR_SSL_CERTIFICATE"),
SSLCertPath: os.Getenv("CONJUR_CERT_FILE"),
Account: os.Getenv("CONJUR_ACCOUNT"),
NetRCPath: os.Getenv("CONJUR_NETRC_PATH"),
AuthnType: os.Getenv("CONJUR_AUTHN_TYPE"),
ServiceID: os.Getenv("CONJUR_SERVICE_ID"),
}
logging.ApiLog.Debugf("Config from environment: %+v\n", env)
c.merge(&env)
}
func (c *Config) Conjurrc() []byte {
data, _ := yaml.Marshal(&c)
return data
}
func LoadConfig() (Config, error) {
config := Config{}
home, err := os.UserHomeDir()
if err != nil {
logging.ApiLog.Warningf("Could not detect homedir.")
}
// Default to using ~/.netrc, subsequent configuration can
// override it if the home dir is set.
if home != "" {
config = Config{NetRCPath: path.Join(home, ".netrc")}
}
err = config.mergeYAML(path.Join(getSystemPath(), "conjur.conf"))
if err != nil {
return config, err
}
conjurrc := os.Getenv("CONJURRC")
if conjurrc == "" && home != "" {
conjurrc = path.Join(home, ".conjurrc")
}
if conjurrc != "" {
config.mergeYAML(conjurrc)
}
config.mergeEnv()
logging.ApiLog.Debugf("Final config: %+v\n", config)
return config, nil
}
func getSystemPath() string {
if runtime.GOOS == "windows" {
//No way to use SHGetKnownFolderPath()
//Hardcoding should be fine for now since CONJURRC is available
return "C:\\windows"
} else {
return "/etc"
}
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}