-
Notifications
You must be signed in to change notification settings - Fork 494
/
config.go
330 lines (288 loc) · 8.83 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Copyright (c) 2017, The Easegress Authors
* All rights reserved.
*
* 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 general provides the general utilities for the client.
package general
import (
"encoding/base64"
"errors"
"fmt"
"os"
"path"
"github.com/megaease/easegress/v2/pkg/util/codectool"
)
type (
// GlobalFlags is the global flags for the whole client.
GlobalFlags struct {
Server string
ForceTLS bool
InsecureSkipVerify bool
OutputFormat string
// following are some general flags. Can be used by all commands. But not all commands use them.
Verbose bool
}
// APIErr is the standard return of error.
APIErr struct {
Code int `json:"code"`
Message string `json:"message"`
}
)
// DefaultFormat is the default output format.
func (g *GlobalFlags) DefaultFormat() bool {
return g.OutputFormat == DefaultFormat
}
// CmdGlobalFlags is the singleton of GlobalFlags.
var CmdGlobalFlags GlobalFlags
/*
clusters:
- cluster:
server: localhost:2381
certificate-authority: "/tmp/certs/ca.crt"
certificate-authority-data: "xxxx"
name: cluster-default
contexts:
- context:
cluster: cluster-default
user: user-default
name: context-default
current-context: context-default
kind: Config
users:
- name: user-default
user:
username: admin
password: admin
client-certificate-data: "xxx"
client-key-data: "xxx"
client-certificate: "/tmp/certs/client.crt"
client-key: "/tmp/certs/client.key"
*/
// Config is the configuration of egctl.
type Config struct {
Kind string `json:"kind"`
Clusters []NamedCluster `json:"clusters"`
AuthInfos []NamedAuthInfo `json:"users"`
Contexts []NamedContext `json:"contexts"`
CurrentContext string `json:"current-context"`
}
// Cluster is the cluster configuration.
type Cluster struct {
Server string `json:"server"`
CertificateAuthority string `json:"certificate-authority,omitempty"`
CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
}
// AuthInfo is the user configuration.
type AuthInfo struct {
ClientCertificate string `json:"client-certificate,omitempty"`
ClientCertificateData []byte `json:"client-certificate-data,omitempty"`
ClientKey string `json:"client-key,omitempty"`
ClientKeyData []byte `json:"client-key-data,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
// Context is the context configuration.
type Context struct {
Cluster string `json:"cluster"`
AuthInfo string `json:"user"`
}
// NamedCluster is the cluster with name.
type NamedCluster struct {
Name string `json:"name"`
Cluster Cluster `json:"cluster"`
}
// NamedContext is the context with name.
type NamedContext struct {
Name string `json:"name"`
Context Context `json:"context"`
}
// NamedAuthInfo is the user with name.
type NamedAuthInfo struct {
Name string `json:"name"`
AuthInfo AuthInfo `json:"user"`
}
// CurrentConfig is config contains current used cluster and user.
type CurrentConfig struct {
CurrentContext string `json:"current-context"`
Context NamedContext `json:"context"`
Cluster NamedCluster `json:"cluster"`
AuthInfo NamedAuthInfo `json:"user"`
}
// GetServer returns the current used server.
func (c *CurrentConfig) GetServer() string {
return c.Cluster.Cluster.Server
}
// GetUsername returns the current used username.
func (c *CurrentConfig) GetUsername() string {
return c.AuthInfo.AuthInfo.Username
}
// GetPassword returns the current used password.
func (c *CurrentConfig) GetPassword() string {
return c.AuthInfo.AuthInfo.Password
}
// GetClientCertificateData returns the current used client certificate data.
func (c *CurrentConfig) GetClientCertificateData() []byte {
return c.AuthInfo.AuthInfo.ClientCertificateData
}
// GetClientCertificate returns the current used client certificate file name.
func (c *CurrentConfig) GetClientCertificate() string {
return c.AuthInfo.AuthInfo.ClientCertificate
}
// GetClientKey returns the current used client key file name.
func (c *CurrentConfig) GetClientKey() string {
return c.AuthInfo.AuthInfo.ClientKey
}
// GetClientKeyData returns the current used client key data.
func (c *CurrentConfig) GetClientKeyData() []byte {
return c.AuthInfo.AuthInfo.ClientKeyData
}
// GetCertificateAuthority returns the current used certificate authority file name.
func (c *CurrentConfig) GetCertificateAuthority() string {
return c.Cluster.Cluster.CertificateAuthority
}
// GetCertificateAuthorityData returns the current used certificate authority data.
func (c *CurrentConfig) GetCertificateAuthorityData() []byte {
return c.Cluster.Cluster.CertificateAuthorityData
}
// UseHTTPS returns whether the current used server is HTTPS.
func (c *CurrentConfig) UseHTTPS() bool {
return len(c.GetCertificateAuthorityData()) > 0 || len(c.GetCertificateAuthority()) > 0 || len(c.GetClientCertificateData()) > 0 || len(c.GetClientCertificate()) > 0
}
func getConfigPath() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return path.Join(homeDir, ".egctlrc"), nil
}
// WriteConfig writes the config to file.
func WriteConfig(config *Config) error {
data, err := codectool.MarshalJSON(config)
if err != nil {
return err
}
data, err = codectool.JSONToYAML(data)
if err != nil {
return err
}
configPath, err := getConfigPath()
if err != nil {
return err
}
info, err := os.Stat(configPath)
if err != nil {
return err
}
err = os.WriteFile(configPath, data, info.Mode())
if err != nil {
return err
}
return nil
}
var globalConfig *Config
// GetConfig returns the config.
func GetConfig() (*Config, error) {
if globalConfig != nil {
return globalConfig, nil
}
configPath, err := getConfigPath()
if err != nil {
return nil, err
}
_, err = os.Stat(configPath)
if errors.Is(err, os.ErrNotExist) {
// not exist means no config
return nil, nil
} else if err != nil {
return nil, err
}
data, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
config := &Config{}
err = codectool.Unmarshal(data, config)
if err != nil {
return nil, fmt.Errorf("unmarshal config file %s failed: %v", configPath, err)
}
globalConfig = config
return config, nil
}
// GetRedactedConfig returns the config with sensitive data redacted.
func GetRedactedConfig(c *Config) *Config {
config := *c
redacted, _ := base64.StdEncoding.DecodeString(string("REDACTED"))
config.Clusters = Map(config.Clusters, func(c NamedCluster) NamedCluster {
if len(c.Cluster.CertificateAuthorityData) > 0 {
c.Cluster.CertificateAuthorityData = redacted
}
return c
})
config.AuthInfos = Map(config.AuthInfos, func(u NamedAuthInfo) NamedAuthInfo {
if len(u.AuthInfo.ClientCertificateData) > 0 {
u.AuthInfo.ClientCertificateData = redacted
}
if len(u.AuthInfo.ClientKeyData) > 0 {
u.AuthInfo.ClientKeyData = redacted
}
if len(u.AuthInfo.Password) > 0 {
u.AuthInfo.Password = "REDACTED"
}
return u
})
return &config
}
var globalCurrentConfig *CurrentConfig
// GetCurrentConfig returns the current config.
func GetCurrentConfig() (*CurrentConfig, error) {
if globalCurrentConfig != nil {
return globalCurrentConfig, nil
}
config, err := GetConfig()
if err != nil {
return nil, err
}
if config == nil {
return nil, nil
}
if config.CurrentContext == "" {
return nil, fmt.Errorf("current context not found in .egctlrc file")
}
currentCtx, ok := Find(config.Contexts, func(c NamedContext) bool {
return c.Name == config.CurrentContext
})
if !ok {
return nil, fmt.Errorf("current context %s not found in .egctlrc file", config.CurrentContext)
}
cluster, ok := Find(config.Clusters, func(c NamedCluster) bool {
return c.Name == currentCtx.Context.Cluster
})
if !ok {
return nil, fmt.Errorf("cluster %s not found in .egctlrc file", currentCtx.Context.Cluster)
}
user, ok := Find(config.AuthInfos, func(u NamedAuthInfo) bool {
return u.Name == currentCtx.Context.AuthInfo
})
if !ok {
return nil, fmt.Errorf("user %s not found in .egctlrc file", currentCtx.Context.AuthInfo)
}
globalCurrentConfig = &CurrentConfig{
CurrentContext: config.CurrentContext,
Context: *currentCtx,
Cluster: *cluster,
AuthInfo: *user,
}
return globalCurrentConfig, nil
}