forked from Venafi/vcert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
244 lines (218 loc) · 6.61 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright 2018 Venafi, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package vcert
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os/user"
"path/filepath"
"github.com/Venafi/vcert/v4/pkg/endpoint"
"gopkg.in/ini.v1"
)
// Config is a basic structure for high level initiating connector to Trust Platform (TPP)/Venafi Cloud
type Config struct {
// ConnectorType specify what do you want to use. May be "Cloud", "TPP" or "Fake" for development.
ConnectorType endpoint.ConnectorType
// BaseUrl should be specified for Venafi Platform. Optional for Cloud implementations that do not use https://venafi.cloud/.
BaseUrl string
// Zone is name of a policy zone in Venafi Platform or Cloud. For TPP, if necessary, escape backslash symbols. For example, "test\\zone" or `test\zone`.
Zone string
// Credentials should contain either User and Password for TPP connections or an APIKey for Cloud.
Credentials *endpoint.Authentication
// ConnectionTrust may contain a trusted CA or certificate of server if you use self-signed certificate.
ConnectionTrust string // *x509.CertPool
LogVerbose bool
// http.Client to use durring construction
Client *http.Client
}
// LoadConfigFromFile is deprecated. In the future will be rewrited.
func LoadConfigFromFile(path, section string) (cfg Config, err error) {
if section == "" {
// nolint:staticcheck
section = ini.DEFAULT_SECTION
}
log.Printf("Loading configuration from %s section %s", path, section)
fname, err := expand(path)
if err != nil {
return cfg, fmt.Errorf("failed to load config: %s", err)
}
iniFile, err := ini.Load(fname)
if err != nil {
return cfg, fmt.Errorf("failed to load config: %s", err)
}
err = validateFile(iniFile)
if err != nil {
return cfg, fmt.Errorf("failed to load config: %s", err)
}
ok := func() bool {
for _, s := range iniFile.Sections() {
if s.Name() == section {
return true
}
}
return false
}()
if !ok {
return cfg, fmt.Errorf("section %s has not been found in %s", section, path)
}
var m dict = iniFile.Section(section).KeysHash()
var connectorType endpoint.ConnectorType
var baseUrl string
var auth = &endpoint.Authentication{}
if m.has("tpp_user") || m.has("access_token") {
connectorType = endpoint.ConnectorTypeTPP
if m["tpp_url"] != "" {
baseUrl = m["tpp_url"]
} else if m["url"] != "" {
baseUrl = m["url"]
}
auth.AccessToken = m["access_token"]
auth.User = m["tpp_user"]
auth.Password = m["tpp_password"]
if m.has("tpp_zone") {
cfg.Zone = m["tpp_zone"]
}
if m.has("cloud_zone") {
cfg.Zone = m["cloud_zone"]
}
} else if m.has("cloud_apikey") {
connectorType = endpoint.ConnectorTypeCloud
if m["cloud_url"] != "" {
baseUrl = m["cloud_url"]
} else if m["url"] != "" {
baseUrl = m["url"]
}
auth.APIKey = m["cloud_apikey"]
if m.has("cloud_zone") {
cfg.Zone = m["cloud_zone"]
}
} else if m.has("test_mode") && m["test_mode"] == "true" {
connectorType = endpoint.ConnectorTypeFake
} else {
return cfg, fmt.Errorf("failed to load config: connector type cannot be defined")
}
if m.has("trust_bundle") {
fname, err := expand(m["trust_bundle"])
if err != nil {
return cfg, fmt.Errorf("failed to load trust-bundle: %s", err)
}
data, err := ioutil.ReadFile(fname)
if err != nil {
return cfg, fmt.Errorf("failed to load trust-bundle: %s", err)
}
cfg.ConnectionTrust = string(data)
}
cfg.ConnectorType = connectorType
cfg.Credentials = auth
cfg.BaseUrl = baseUrl
return
}
func expand(path string) (string, error) {
if len(path) == 0 || path[0] != '~' {
return path, nil
}
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, path[1:]), nil
}
type dict map[string]string
func (d dict) has(key string) bool {
if _, ok := d[key]; ok {
return true
}
return false
}
type set map[string]bool
func (d set) has(key string) bool {
if _, ok := d[key]; ok {
return true
}
return false
}
func validateSection(s *ini.Section) error {
var TPPValidKeys set = map[string]bool{
"url": true,
"access_token": true,
"tpp_url": true,
"tpp_user": true,
"tpp_password": true,
"tpp_zone": true,
"trust_bundle": true,
}
var CloudValidKeys set = map[string]bool{
"url": true,
"trust_bundle": true,
"cloud_url": true,
"cloud_apikey": true,
"cloud_zone": true,
}
log.Printf("Validating configuration section %s", s.Name())
var m dict = s.KeysHash()
if m.has("access_token") && m.has("cloud_apikey") {
return fmt.Errorf("configuration issue in section %s: could not set both TPP token and cloud api key", s.Name())
}
if m.has("tpp_user") || m.has("access_token") || m.has("tpp_password") {
// looks like TPP config section
for k := range m {
if !TPPValidKeys.has(k) {
return fmt.Errorf("illegal key '%s' in TPP section %s", k, s.Name())
}
}
if m.has("tpp_user") && m.has("access_token") {
return fmt.Errorf("configuration issue in section %s: could not have both TPP user and access token", s.Name())
}
if !m.has("tpp_user") && !m.has("access_token") {
return fmt.Errorf("configuration issue in section %s: missing TPP user", s.Name())
}
if !m.has("tpp_password") && !m.has("access_token") {
return fmt.Errorf("configuration issue in section %s: missing TPP password", s.Name())
}
} else if m.has("cloud_apikey") {
// looks like Cloud config section
for k := range m {
if !CloudValidKeys.has(k) {
return fmt.Errorf("illegal key '%s' in Cloud section %s", k, s.Name())
}
}
} else if m.has("test_mode") {
// it's ok
} else if m.has("url") {
return fmt.Errorf("could not determine connection endpoint with only url information in section %s", s.Name())
} else {
return fmt.Errorf("section %s looks empty", s.Name())
}
return nil
}
func validateFile(f *ini.File) error {
for _, section := range f.Sections() {
if len(section.Keys()) == 0 {
if len(f.Sections()) > 1 {
// empty section is not valid. skipping it if there are more sections in the file
log.Printf("Warning: empty section %s", section.Name())
continue
}
}
err := validateSection(section)
if err != nil {
return err
}
}
return nil
}