-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfig.go
More file actions
152 lines (141 loc) · 5.44 KB
/
config.go
File metadata and controls
152 lines (141 loc) · 5.44 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package bootstrap
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/Azure/aks-secure-tls-bootstrap/client/internal/cloud"
"go.uber.org/zap"
)
const (
defaultTLSMinVersion = "1.3"
defaultValidateKubeconfigTimeout = 15 * time.Second
defaultGetAccessTokenTimeout = time.Minute
defaultGetInstanceDataTimeout = 15 * time.Second
defaultGetNonceTimeout = 15 * time.Second
defaultGetAttestedDataTimeout = 15 * time.Second
defaultGetCredentialTimeout = 6 * time.Minute
)
type Config struct {
CloudProviderConfig *cloud.ProviderConfig
CloudProviderConfigPath string `json:"cloudProviderConfigPath"`
APIServerFQDN string `json:"apiServerFqdn"`
UserAssignedIdentityID string `json:"userAssignedIdentityId"`
NextProto string `json:"nextProto"`
AADResource string `json:"aadResource"`
ClusterCAFilePath string `json:"clusterCaFilePath"`
KubeconfigPath string `json:"kubeconfigPath"`
CertDir string `json:"certDir"`
TLSMinVersion string `json:"tlsMinVersion"`
EnsureAuthorizedClient bool `json:"ensureAuthorizedClient"`
ValidateKubeconfigTimeout time.Duration `json:"validateKubeconfigTimeout"`
GetAccessTokenTimeout time.Duration `json:"getAccessTokenTimeout"`
GetInstanceDataTimeout time.Duration `json:"getInstanceDataTimeout"`
GetNonceTimeout time.Duration `json:"getNonceTimeout"`
GetAttestedDataTimeout time.Duration `json:"getAttestedDataTimeout"`
GetCredentialTimeout time.Duration `json:"getCredentialTimeout"`
// Deadline is now deprecated and will not be respected.
// Use per-RPC timeouts instead.
Deadline time.Duration `json:"deadline"`
}
func (c *Config) LoadFromFile(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("reading config file: %w", err)
}
if err := json.Unmarshal(data, c); err != nil {
return fmt.Errorf("unmarshalling config file content: %w", err)
}
return nil
}
func (c *Config) DefaultAndValidate() error {
c.applyDefaults()
return c.validate()
}
func (c *Config) ToZapFields() []zap.Field {
return []zap.Field{
zap.String("cloudProviderConfigPath", c.CloudProviderConfigPath),
zap.String("apiServerFqdn", c.APIServerFQDN),
zap.String("userAssignedIdentityId", c.UserAssignedIdentityID),
zap.String("nextProto", c.NextProto),
zap.String("aadResource", c.AADResource),
zap.String("clusterCaFilePath", c.ClusterCAFilePath),
zap.String("kubeconfigPath", c.KubeconfigPath),
zap.String("certDir", c.CertDir),
zap.String("tlsMinVersion", c.TLSMinVersion),
zap.Bool("ensureAuthorizedClient", c.EnsureAuthorizedClient),
zap.Int64("validateKubeconfigTimeoutMilliseconds", c.ValidateKubeconfigTimeout.Milliseconds()),
zap.Int64("getAccessTokenTimeoutMilliseconds", c.GetAccessTokenTimeout.Milliseconds()),
zap.Int64("getInstanceDataTimeoutMilliseconds", c.GetInstanceDataTimeout.Milliseconds()),
zap.Int64("getNonceTimeoutMilliseconds", c.GetNonceTimeout.Milliseconds()),
zap.Int64("getAttestedDataTimeoutMilliseconds", c.GetAttestedDataTimeout.Milliseconds()),
zap.Int64("getCredentialTimeoutMilliseconds", c.GetCredentialTimeout.Milliseconds()),
zap.Int64("deadlineMilliseconds", c.Deadline.Milliseconds()),
}
}
func (c *Config) applyDefaults() {
if c.TLSMinVersion == "" {
c.TLSMinVersion = defaultTLSMinVersion
}
if c.ValidateKubeconfigTimeout == 0 {
c.ValidateKubeconfigTimeout = defaultValidateKubeconfigTimeout
}
if c.GetAccessTokenTimeout == 0 {
c.GetAccessTokenTimeout = defaultGetAccessTokenTimeout
}
if c.GetInstanceDataTimeout == 0 {
c.GetInstanceDataTimeout = defaultGetInstanceDataTimeout
}
if c.GetNonceTimeout == 0 {
c.GetNonceTimeout = defaultGetNonceTimeout
}
if c.GetAttestedDataTimeout == 0 {
c.GetAttestedDataTimeout = defaultGetAttestedDataTimeout
}
if c.GetCredentialTimeout == 0 {
c.GetCredentialTimeout = defaultGetCredentialTimeout
}
}
func (c *Config) validate() error {
if c.APIServerFQDN == "" {
return fmt.Errorf("apiserver FQDN must be specified")
}
if c.NextProto == "" {
return fmt.Errorf("next proto header value must be specified")
}
if c.AADResource == "" {
return fmt.Errorf("AAD resource must be specified")
}
if c.KubeconfigPath == "" {
return fmt.Errorf("kubeconfig path must be specified")
}
if c.CertDir == "" {
return fmt.Errorf("cert dir must be specified")
}
if c.TLSMinVersion != "1.2" && c.TLSMinVersion != "1.3" {
return fmt.Errorf(`when specified, TLS min version can either be "1.2" or "1.3"`)
}
if c.CloudProviderConfigPath == "" {
return fmt.Errorf("cloud provider config path must be specified")
}
data, err := os.ReadFile(c.CloudProviderConfigPath)
if err != nil {
return fmt.Errorf("cannot read cloud provider config data: %w", err)
}
c.CloudProviderConfig = new(cloud.ProviderConfig)
if err = json.Unmarshal(data, c.CloudProviderConfig); err != nil {
return fmt.Errorf("cannot unmarshal cloud provider config data: %w", err)
}
if c.ClusterCAFilePath == "" {
return fmt.Errorf("cluster CA file path must be specified")
}
if _, err := os.Stat(c.ClusterCAFilePath); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("specified cluster CA file does not exist: %s", c.ClusterCAFilePath)
}
return fmt.Errorf("unable to verify existence of cluster CA file: %w", err)
}
return nil
}