-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (230 loc) · 10.1 KB
/
main.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
// Copyright 2018 Istio Authors
//
// 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 main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"istio.io/istio/pkg/cmd"
"istio.io/istio/pkg/log"
"istio.io/istio/security/pkg/nodeagent/cache"
"istio.io/istio/security/pkg/nodeagent/sds"
"istio.io/istio/security/pkg/nodeagent/secretfetcher"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
const (
// name of authentication provider.
caProvider = "CA_PROVIDER"
// CA endpoint.
caAddress = "CA_ADDR"
// names of authentication provider's plugins.
pluginNames = "Plugins"
// The trust domain corresponds to the trust root of a system.
// Refer to https://github.com/spiffe/spiffe/blob/master/standards/SPIFFE-ID.md#21-trust-domain
trustDomain = "Trust_Domain"
// The workload SDS mode allows node agent to provision credentials to workload proxy by sending
// CSR to CA.
enableWorkloadSDS = "ENABLE_WORKLOAD_SDS"
// The ingress gateway SDS mode allows node agent to provision credentials to ingress gateway
// proxy by watching kubernetes secrets.
enableIngressGatewaySDS = "ENABLE_INGRESS_GATEWAY_SDS"
// The environmental variable name for Vault CA address.
vaultAddress = "VAULT_ADDR"
// The environmental variable name for Vault auth path.
vaultAuthPath = "VAULT_AUTH_PATH"
// The environmental variable name for Vault role.
vaultRole = "VAULT_ROLE"
// The environmental variable name for Vault sign CSR path.
vaultSignCsrPath = "VAULT_SIGN_CSR_PATH"
// The environmental variable name for Vault TLS root certificate.
vaultTLSRootCert = "VAULT_TLS_ROOT_CERT"
// The environmental variable name for the flag which is used to indicate the token passed
// from envoy is always valid(ex, normal 8ks JWT).
alwaysValidTokenFlag = "VALID_TOKEN"
// The environmental variable name for secret TTL, node agent decides whether a secret
// is expired if time.now - secret.createtime >= secretTTL.
// example value format like "90m"
secretTTL = "SECRET_TTL"
// The environmental variable name for grace duration that secret is re-generated
// before it's expired time.
// example value format like "10m"
SecretRefreshGraceDuration = "SECRET_GRACE_DURATION"
// The environmental variable name for key rotation job running interval.
// example value format like "20m"
SecretRotationJobRunInterval = "SECRET_JOB_RUN_INTERVAL"
)
var (
workloadSdsCacheOptions cache.Options
gatewaySdsCacheOptions cache.Options
serverOptions sds.Options
gatewaySecretChan chan struct{}
loggingOptions = log.DefaultOptions()
// rootCmd defines the command for node agent.
rootCmd = &cobra.Command{
Use: "nodeagent",
Short: "Node agent",
RunE: func(c *cobra.Command, args []string) error {
if err := log.Configure(loggingOptions); err != nil {
return err
}
gatewaySdsCacheOptions = workloadSdsCacheOptions
if serverOptions.EnableIngressGatewaySDS && serverOptions.EnableWorkloadSDS &&
serverOptions.IngressGatewayUDSPath == serverOptions.WorkloadUDSPath {
log.Error("UDS paths for ingress gateway and workload are the same")
os.Exit(1)
}
if serverOptions.CAProviderName == "" && serverOptions.EnableWorkloadSDS {
log.Error("CA Provider is missing")
os.Exit(1)
}
if serverOptions.CAEndpoint == "" && serverOptions.EnableWorkloadSDS {
log.Error("CA Endpoint is missing")
os.Exit(1)
}
stop := make(chan struct{})
workloadSecretCache, gatewaySecretCache := newSecretCache(serverOptions)
if workloadSecretCache != nil {
defer workloadSecretCache.Close()
}
if gatewaySecretCache != nil {
defer gatewaySecretCache.Close()
}
server, err := sds.NewServer(serverOptions, workloadSecretCache, gatewaySecretCache)
defer server.Stop()
if err != nil {
log.Errorf("failed to create sds service: %v", err)
return fmt.Errorf("failed to create sds service")
}
cmd.WaitSignal(stop)
return nil
},
}
)
func newSecretCache(serverOptions sds.Options) (workloadSecretCache, gatewaySecretCache *cache.SecretCache) {
if serverOptions.EnableWorkloadSDS {
wSecretFetcher, err := secretfetcher.NewSecretFetcher(false, serverOptions.CAEndpoint,
serverOptions.CAProviderName, true, []byte(serverOptions.VaultTLSRootCert),
serverOptions.VaultAddress, serverOptions.VaultRole, serverOptions.VaultAuthPath,
serverOptions.VaultSignCsrPath)
if err != nil {
log.Errorf("failed to create secretFetcher for workload proxy: %v", err)
os.Exit(1)
}
workloadSdsCacheOptions.TrustDomain = serverOptions.TrustDomain
workloadSdsCacheOptions.Plugins = sds.NewPlugins(serverOptions.PluginNames)
workloadSecretCache = cache.NewSecretCache(wSecretFetcher, sds.NotifyProxy, workloadSdsCacheOptions)
} else {
workloadSecretCache = nil
}
if serverOptions.EnableIngressGatewaySDS {
gSecretFetcher, err := secretfetcher.NewSecretFetcher(true, "", "", false, nil, "", "", "", "")
if err != nil {
log.Errorf("failed to create secretFetcher for gateway proxy: %v", err)
os.Exit(1)
}
gatewaySecretChan = make(chan struct{})
gSecretFetcher.Run(gatewaySecretChan)
gatewaySecretCache = cache.NewSecretCache(gSecretFetcher, sds.NotifyProxy, gatewaySdsCacheOptions)
} else {
gatewaySecretCache = nil
}
return workloadSecretCache, gatewaySecretCache
}
func init() {
pluginNames := os.Getenv(pluginNames)
pns := []string{}
if pluginNames != "" {
pns = strings.Split(pluginNames, ",")
}
enableWorkloadSdsEnv := true
val := os.Getenv(enableWorkloadSDS)
if env, err := strconv.ParseBool(val); err == nil {
enableWorkloadSdsEnv = env
}
enableIngressGatewaySdsEnv := false
val = os.Getenv(enableIngressGatewaySDS)
if env, err := strconv.ParseBool(val); err == nil {
enableIngressGatewaySdsEnv = env
}
alwaysValidTokenFlagEnv := false
val = os.Getenv(alwaysValidTokenFlag)
if env, err := strconv.ParseBool(val); err == nil {
alwaysValidTokenFlagEnv = env
}
rootCmd.PersistentFlags().BoolVar(&serverOptions.EnableWorkloadSDS, "enableWorkloadSDS",
enableWorkloadSdsEnv,
"If true, node agent works as SDS server and provisions key/certificate to workload proxies.")
rootCmd.PersistentFlags().StringVar(&serverOptions.WorkloadUDSPath, "workloadUDSPath",
"/var/run/sds/uds_path", "Unix domain socket through which SDS server communicates with workload proxies")
rootCmd.PersistentFlags().BoolVar(&serverOptions.EnableIngressGatewaySDS, "enableIngressGatewaySDS",
enableIngressGatewaySdsEnv,
"If true, node agent works as SDS server and watches kubernetes secrets for ingress gateway.")
rootCmd.PersistentFlags().StringVar(&serverOptions.IngressGatewayUDSPath, "gatewayUdsPath",
"/var/run/ingress_gateway/sds", "Unix domain socket through which SDS server communicates with ingress gateway proxies.")
rootCmd.PersistentFlags().StringVar(&serverOptions.CAProviderName, "caProvider", os.Getenv(caProvider), "CA provider")
rootCmd.PersistentFlags().StringVar(&serverOptions.CAEndpoint, "caEndpoint", os.Getenv(caAddress), "CA endpoint")
rootCmd.PersistentFlags().StringVar(&serverOptions.TrustDomain, "trustDomain",
os.Getenv(trustDomain), "The trust domain this node agent run in")
rootCmd.PersistentFlags().StringArrayVar(&serverOptions.PluginNames, "pluginNames",
pns, "authentication provider specific plugin names")
rootCmd.PersistentFlags().StringVar(&serverOptions.CertFile, "sdsCertFile", "", "SDS gRPC TLS server-side certificate")
rootCmd.PersistentFlags().StringVar(&serverOptions.KeyFile, "sdsKeyFile", "", "SDS gRPC TLS server-side key")
setWorkloadCacheTimeParams()
rootCmd.PersistentFlags().BoolVar(&workloadSdsCacheOptions.AlwaysValidTokenFlag, "alwaysValidTokenFlag",
alwaysValidTokenFlagEnv,
"If true, node agent assume token passed from envoy is always valid.")
rootCmd.PersistentFlags().StringVar(&serverOptions.VaultAddress, "vaultAddress", os.Getenv(vaultAddress),
"Vault address")
rootCmd.PersistentFlags().StringVar(&serverOptions.VaultRole, "vaultRole", os.Getenv(vaultRole),
"Vault role")
rootCmd.PersistentFlags().StringVar(&serverOptions.VaultAuthPath, "vaultAuthPath", os.Getenv(vaultAuthPath),
"Vault auth path")
rootCmd.PersistentFlags().StringVar(&serverOptions.VaultSignCsrPath, "vaultSignCsrPath", os.Getenv(vaultSignCsrPath),
"Vault sign CSR path")
rootCmd.PersistentFlags().StringVar(&serverOptions.VaultTLSRootCert, "vaultTLSRootCert", os.Getenv(vaultTLSRootCert),
"Vault TLS root certificate")
// Attach the Istio logging options to the command.
loggingOptions.AttachCobraFlags(rootCmd)
}
func setWorkloadCacheTimeParams() {
secretTTLDuration := 24 * time.Hour
if env, err := time.ParseDuration(os.Getenv(secretTTL)); err == nil {
secretTTLDuration = env
}
secretRefreshGraceDuration := time.Hour
if env, err := time.ParseDuration(os.Getenv(SecretRefreshGraceDuration)); err == nil {
secretRefreshGraceDuration = env
}
secretRotationJobRunInterval := 10 * time.Minute
if env, err := time.ParseDuration(os.Getenv(SecretRotationJobRunInterval)); err == nil {
secretRotationJobRunInterval = env
}
rootCmd.PersistentFlags().DurationVar(&workloadSdsCacheOptions.SecretTTL, "secretTtl",
secretTTLDuration, "Secret's TTL")
rootCmd.PersistentFlags().DurationVar(&workloadSdsCacheOptions.SecretRefreshGraceDuration, "secretRefreshGraceDuration",
secretRefreshGraceDuration, "Secret's Refresh Grace Duration")
rootCmd.PersistentFlags().DurationVar(&workloadSdsCacheOptions.RotationInterval, "secretRotationInterval",
secretRotationJobRunInterval, "Secret rotation job running interval")
rootCmd.PersistentFlags().DurationVar(&workloadSdsCacheOptions.EvictionDuration, "secretEvictionDuration",
24*time.Hour, "Secret eviction time duration")
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Errora(err)
os.Exit(1)
}
}