This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
main.go
473 lines (401 loc) · 17.4 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2017 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 (
"context"
"fmt"
"os"
"strings"
"time"
"istio.io/istio/pkg/spiffe"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"istio.io/istio/pkg/collateral"
"istio.io/istio/pkg/ctrlz"
kubelib "istio.io/istio/pkg/kube"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/probe"
"istio.io/istio/pkg/version"
"istio.io/istio/security/pkg/caclient"
"istio.io/istio/security/pkg/cmd"
"istio.io/istio/security/pkg/k8s/controller"
"istio.io/istio/security/pkg/pki/ca"
probecontroller "istio.io/istio/security/pkg/probe"
"istio.io/istio/security/pkg/registry"
"istio.io/istio/security/pkg/registry/kube"
caserver "istio.io/istio/security/pkg/server/ca"
"istio.io/istio/security/pkg/server/monitoring"
)
type cliOptions struct { // nolint: maligned
// Comma separated string containing all listened namespaces
listenedNamespaces string
istioCaStorageNamespace string
kubeConfigFile string
readSigningCertOnly bool
certChainFile string
signingCertFile string
signingKeyFile string
rootCertFile string
selfSignedCA bool
selfSignedCACertTTL time.Duration
workloadCertTTL time.Duration
maxWorkloadCertTTL time.Duration
// The length of certificate rotation grace period, configured as the ratio of the certificate TTL.
workloadCertGracePeriodRatio float32
// The minimum grace period for workload cert rotation.
workloadCertMinGracePeriod time.Duration
// TODO(incfly): delete this field once we deprecate flag --grpc-hostname.
grpcHostname string
// Comma separated string containing all possible host name that clients may use to connect to.
grpcHosts string
grpcPort int
serverOnly bool
// Whether the CA signs certificates for other CAs.
signCACerts bool
cAClientConfig caclient.Config
// Monitoring port number
monitoringPort int
// Enable profiling in monitoring
enableProfiling bool
// The path to the file which indicates the liveness of the server by its existence.
// This will be used for k8s liveness probe. If empty, it does nothing.
LivenessProbeOptions *probe.Options
probeCheckInterval time.Duration
loggingOptions *log.Options
ctrlzOptions *ctrlz.Options
// Whether to append DNS names to the certificate
appendDNSNames bool
// Custom domain options, for control plane and special service accounts
// comma separated list of SERVICE_ACCOUNT.NAMESPACE:DOMAIN
customDNSNames string
// domain to use in SPIFFE identity URLs
trustDomain string
// Enable dual-use certs - SPIFFE in SAN and in CommonName
dualUse bool
}
var (
opts = cliOptions{
loggingOptions: log.DefaultOptions(),
ctrlzOptions: ctrlz.DefaultOptions(),
LivenessProbeOptions: &probe.Options{},
}
rootCmd = &cobra.Command{
Use: "istio_ca",
Short: "Istio Certificate Authority (CA).",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
runCA()
},
}
// ServiceAccount/DNS pair for generating DNS names in certificates.
// TODO: move it to a configmap later when we have more services to support.
webhookServiceAccounts = []string{
"istio-sidecar-injector-service-account",
"istio-galley-service-account",
}
webhookServiceNames = []string{
"istio-sidecar-injector",
"istio-galley",
}
)
func fatalf(template string, args ...interface{}) {
if len(args) > 0 {
log.Errorf(template, args...)
} else {
log.Errorf(template)
}
os.Exit(-1)
}
func init() {
flags := rootCmd.Flags()
// General configuration.
flags.StringVar(&opts.listenedNamespaces, "listened-namespace", "", "deprecated")
flags.MarkDeprecated("listened-namespace", "please use --listened-namespaces instead")
flags.StringVar(&opts.listenedNamespaces, "listened-namespaces", "",
"Select the namespaces for the Citadel to listen to, separated by comma. If unspecified, Citadel tries to use the ${"+
cmd.ListenedNamespaceKey+"} environment variable. If neither is set, Citadel listens to all namespaces.")
flags.StringVar(&opts.istioCaStorageNamespace, "citadel-storage-namespace", "istio-system", "Namespace where "+
"the Citadel pod is running. Will not be used if explicit file or other storage mechanism is specified.")
flags.StringVar(&opts.kubeConfigFile, "kube-config", "",
"Specifies path to kubeconfig file. This must be specified when not running inside a Kubernetes pod.")
flags.BoolVar(&opts.readSigningCertOnly, "read-signing-cert-only", false, "When set, Citadel only reads the self-signed signing "+
"key and cert from Kubernetes secret without generating one (if not exist). This flag avoids racing condition between "+
"multiple Citadels generating self-signed key and cert. Please make sure one and only one Citadel instance has this flag set "+
"to false.")
// Configuration if Citadel accepts key/cert configured through arguments.
flags.StringVar(&opts.certChainFile, "cert-chain", "", "Path to the certificate chain file")
flags.StringVar(&opts.signingCertFile, "signing-cert", "", "Path to the CA signing certificate file")
flags.StringVar(&opts.signingKeyFile, "signing-key", "", "Path to the CA signing key file")
flags.StringVar(&opts.rootCertFile, "root-cert", "", "Path to the root certificate file")
// Configuration if Citadel acts as a self signed CA.
flags.BoolVar(&opts.selfSignedCA, "self-signed-ca", false,
"Indicates whether to use auto-generated self-signed CA certificate. "+
"When set to true, the '--signing-cert' and '--signing-key' options are ignored.")
flags.DurationVar(&opts.selfSignedCACertTTL, "self-signed-ca-cert-ttl", cmd.DefaultSelfSignedCACertTTL,
"The TTL of self-signed CA root certificate")
flags.StringVar(&opts.trustDomain, "trust-domain", "",
"The domain serves to identify the system with spiffe ")
// Upstream CA configuration if Citadel interacts with upstream CA.
flags.StringVar(&opts.cAClientConfig.CAAddress, "upstream-ca-address", "", "The IP:port address of the upstream "+
"CA. When set, the CA will rely on the upstream Citadel to provision its own certificate.")
flags.StringVar(&opts.cAClientConfig.Org, "org", "", "Organization for the cert")
flags.DurationVar(&opts.cAClientConfig.RequestedCertTTL, "requested-ca-cert-ttl", cmd.DefaultRequestedCACertTTL,
"The requested TTL for the workload")
flags.IntVar(&opts.cAClientConfig.RSAKeySize, "key-size", 2048, "Size of generated private key")
// Certificate signing configuration.
flags.DurationVar(&opts.workloadCertTTL, "workload-cert-ttl", cmd.DefaultWorkloadCertTTL,
"The TTL of issued workload certificates")
flags.DurationVar(&opts.maxWorkloadCertTTL, "max-workload-cert-ttl", cmd.DefaultMaxWorkloadCertTTL,
"The max TTL of issued workload certificates")
flags.Float32Var(&opts.workloadCertGracePeriodRatio, "workload-cert-grace-period-ratio",
cmd.DefaultWorkloadCertGracePeriodRatio, "The workload certificate rotation grace period, as a ratio of the "+
"workload certificate TTL.")
flags.DurationVar(&opts.workloadCertMinGracePeriod, "workload-cert-min-grace-period",
cmd.DefaultWorkloadMinCertGracePeriod, "The minimum workload certificate rotation grace period.")
// gRPC server for signing CSRs.
flags.StringVar(&opts.grpcHosts, "grpc-host-identities", "istio-ca,istio-citadel",
"The list of hostnames for istio ca server, separated by comma.")
flags.IntVar(&opts.grpcPort, "grpc-port", 8060, "The port number for Citadel GRPC server. "+
"If unspecified, Citadel will not serve GRPC requests.")
flags.BoolVar(&opts.serverOnly, "server-only", false, "When set, Citadel only serves as a server without writing "+
"the Kubernetes secrets.")
flags.StringVar(&opts.grpcHostname, "grpc-hostname", "istio-ca", "deprecated")
flags.MarkDeprecated("grpc-hostname", "please use --grpc-host-identities instead")
flags.BoolVar(&opts.signCACerts, "sign-ca-certs", false, "Whether Citadel signs certificates for other CAs")
// Monitoring configuration
flags.IntVar(&opts.monitoringPort, "monitoring-port", 15014, "The port number for monitoring Citadel. "+
"If unspecified, Citadel will disable monitoring.")
flags.BoolVar(&opts.enableProfiling, "enable-profiling", false, "Enabling profiling when monitoring Citadel.")
// Liveness Probe configuration
flags.StringVar(&opts.LivenessProbeOptions.Path, "liveness-probe-path", "",
"Path to the file for the liveness probe.")
flags.DurationVar(&opts.LivenessProbeOptions.UpdateInterval, "liveness-probe-interval", 0,
"Interval of updating file for the liveness probe.")
flags.DurationVar(&opts.probeCheckInterval, "probe-check-interval", cmd.DefaultProbeCheckInterval,
"Interval of checking the liveness of the CA.")
flags.BoolVar(&opts.appendDNSNames, "append-dns-names", true,
"Append DNS names to the certificates for webhook services.")
flags.StringVar(&opts.customDNSNames, "custom-dns-names", "",
"The list of account.namespace:customdns names, separated by comma.")
// Dual-use certificate signing
flags.BoolVar(&opts.dualUse, "experimental-dual-use",
false, "Enable dual-use mode. Generates certificates with a CommonName identical to the SAN.")
rootCmd.AddCommand(version.CobraCommand())
rootCmd.AddCommand(collateral.CobraCommand(rootCmd, &doc.GenManHeader{
Title: "Citadel",
Section: "istio_ca CLI",
Manual: "Citadel",
}))
rootCmd.AddCommand(cmd.NewProbeCmd())
opts.loggingOptions.AttachCobraFlags(rootCmd)
opts.ctrlzOptions.AttachCobraFlags(rootCmd)
cmd.InitializeFlags(rootCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Errora(err)
os.Exit(-1)
}
}
// fqdn returns the k8s cluster dns name for the Citadel service.
func fqdn() string {
return fmt.Sprintf("istio-citadel.%v.svc.cluster.local", opts.istioCaStorageNamespace)
}
func runCA() {
if err := log.Configure(opts.loggingOptions); err != nil {
fatalf("Failed to configure logging (%v)", err)
}
_, _ = ctrlz.Run(opts.ctrlzOptions, nil)
if value, exists := os.LookupEnv(cmd.ListenedNamespaceKey); exists {
// When -namespace is not set, try to read the namespace from environment variable.
if opts.listenedNamespaces == "" {
opts.listenedNamespaces = value
}
// Use environment variable for istioCaStorageNamespace if it exists
opts.istioCaStorageNamespace = value
}
listenedNamespaces := strings.Split(opts.listenedNamespaces, ",")
verifyCommandLineOptions()
var webhooks map[string]*controller.DNSNameEntry
if opts.appendDNSNames {
webhooks = controller.ConstructCustomDNSNames(webhookServiceAccounts,
webhookServiceNames, opts.istioCaStorageNamespace, opts.customDNSNames)
}
cs, err := kubelib.CreateClientset(opts.kubeConfigFile, "")
if err != nil {
fatalf("Could not create k8s clientset: %v", err)
}
ca := createCA(cs.CoreV1())
stopCh := make(chan struct{})
if !opts.serverOnly {
log.Infof("Creating Kubernetes controller to write issued keys and certs into secret ...")
// For workloads in K8s, we apply the configured workload cert TTL.
sc, err := controller.NewSecretController(ca,
opts.workloadCertTTL,
opts.workloadCertGracePeriodRatio, opts.workloadCertMinGracePeriod, opts.dualUse,
cs.CoreV1(), opts.signCACerts, listenedNamespaces, webhooks)
if err != nil {
fatalf("Failed to create secret controller: %v", err)
}
sc.Run(stopCh)
} else {
log.Info("Citadel is running in server only mode, certificates will not be propagated via secret.")
if opts.grpcPort <= 0 {
fatalf("The gRPC port must be set in server-only model.")
}
}
if opts.grpcPort > 0 {
// start registry if gRPC server is to be started
reg := registry.GetIdentityRegistry()
// add certificate identity to the identity registry for the liveness probe check
if registryErr := reg.AddMapping(probecontroller.LivenessProbeClientIdentity,
probecontroller.LivenessProbeClientIdentity); registryErr != nil {
log.Errorf("Failed to add indentity mapping: %v", registryErr)
}
ch := make(chan struct{})
// monitor service objects with "alpha.istio.io/kubernetes-serviceaccounts" and
// "alpha.istio.io/canonical-serviceaccounts" annotations
serviceController := kube.NewServiceController(cs.CoreV1(), listenedNamespaces, reg)
serviceController.Run(ch)
// monitor service account objects for istio mesh expansion
serviceAccountController := kube.NewServiceAccountController(cs.CoreV1(), listenedNamespaces, reg)
serviceAccountController.Run(ch)
// The CA API uses cert with the max workload cert TTL.
hostnames := append(strings.Split(opts.grpcHosts, ","), fqdn())
caServer, startErr := caserver.New(ca, opts.maxWorkloadCertTTL, opts.signCACerts, hostnames, opts.grpcPort, spiffe.GetTrustDomain())
if startErr != nil {
fatalf("Failed to create istio ca server: %v", startErr)
}
if serverErr := caServer.Run(); serverErr != nil {
// stop the registry-related controllers
ch <- struct{}{}
log.Warnf("Failed to start GRPC server with error: %v", serverErr)
}
}
monitorErrCh := make(chan error)
// Start the monitoring server.
if opts.monitoringPort > 0 {
monitor, mErr := monitoring.NewMonitor(opts.monitoringPort, opts.enableProfiling)
if mErr != nil {
fatalf("Unable to setup monitoring: %v", mErr)
}
go monitor.Start(monitorErrCh)
log.Info("Citadel monitor has started.")
defer monitor.Close()
}
log.Info("Citadel has started")
rotatorErrCh := make(chan error)
// Start CA client if the upstream CA address is specified.
if len(opts.cAClientConfig.CAAddress) != 0 {
config := &opts.cAClientConfig
config.Env = "onprem"
config.Platform = "vm"
config.ForCA = true
config.CertFile = opts.signingCertFile
config.KeyFile = opts.signingKeyFile
config.CertChainFile = opts.certChainFile
config.RootCertFile = opts.rootCertFile
config.CSRGracePeriodPercentage = cmd.DefaultCSRGracePeriodPercentage
config.CSRMaxRetries = cmd.DefaultCSRMaxRetries
config.CSRInitialRetrialInterval = cmd.DefaultCSRInitialRetrialInterval
rotator, creationErr := caclient.NewKeyCertBundleRotator(config, ca.GetCAKeyCertBundle())
if creationErr != nil {
fatalf("Failed to create key cert bundle rotator: %v", creationErr)
}
go rotator.Start(rotatorErrCh)
log.Info("Key cert bundle rotator has started.")
defer rotator.Stop()
}
// Blocking until receives error.
for {
select {
case <-monitorErrCh:
fatalf("Monitoring server error: %v", err)
case <-rotatorErrCh:
fatalf("Key cert bundle rotator error: %v", err)
}
}
}
func createCA(client corev1.CoreV1Interface) *ca.IstioCA {
var caOpts *ca.IstioCAOptions
var err error
if opts.selfSignedCA {
log.Info("Use self-signed certificate as the CA certificate")
spiffe.SetTrustDomain(spiffe.DetermineTrustDomain(opts.trustDomain, true))
// Abort after 20 minutes.
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*20)
defer cancel()
var checkInterval time.Duration
if opts.readSigningCertOnly {
checkInterval = ca.ReadSigningCertCheckInterval
} else {
checkInterval = -1
}
caOpts, err = ca.NewSelfSignedIstioCAOptions(ctx, opts.selfSignedCACertTTL, opts.workloadCertTTL,
opts.maxWorkloadCertTTL, spiffe.GetTrustDomain(), opts.dualUse,
opts.istioCaStorageNamespace, checkInterval, client)
if err != nil {
fatalf("Failed to create a self-signed Citadel (error: %v)", err)
}
} else {
log.Info("Use certificate from argument as the CA certificate")
caOpts, err = ca.NewPluggedCertIstioCAOptions(opts.certChainFile, opts.signingCertFile, opts.signingKeyFile,
opts.rootCertFile, opts.workloadCertTTL, opts.maxWorkloadCertTTL, opts.istioCaStorageNamespace, client)
if err != nil {
fatalf("Failed to create an Citadel (error: %v)", err)
}
}
caOpts.LivenessProbeOptions = opts.LivenessProbeOptions
caOpts.ProbeCheckInterval = opts.probeCheckInterval
istioCA, err := ca.NewIstioCA(caOpts)
if err != nil {
log.Errorf("Failed to create an Citadel (error: %v)", err)
}
if opts.LivenessProbeOptions.IsValid() {
livenessProbeChecker, err := probecontroller.NewLivenessCheckController(
opts.probeCheckInterval, fmt.Sprintf("%v:%v", fqdn(), opts.grpcPort), istioCA,
opts.LivenessProbeOptions, probecontroller.GrpcProtocolProvider)
if err != nil {
log.Errorf("failed to create an liveness probe check controller (error: %v)", err)
} else {
livenessProbeChecker.Run()
}
}
return istioCA
}
func verifyCommandLineOptions() {
if opts.selfSignedCA {
return
}
if opts.signingCertFile == "" {
fatalf(
"No signing cert has been specified. Either specify a cert file via '-signing-cert' option " +
"or use '-self-signed-ca'")
}
if opts.signingKeyFile == "" {
fatalf(
"No signing key has been specified. Either specify a key file via '-signing-key' option " +
"or use '-self-signed-ca'")
}
if opts.rootCertFile == "" {
fatalf(
"No root cert has been specified. Either specify a root cert file via '-root-cert' option " +
"or use '-self-signed-ca'")
}
if opts.workloadCertGracePeriodRatio < 0 || opts.workloadCertGracePeriodRatio > 1 {
fatalf("Workload cert grace period ratio %f is invalid. It should be within [0, 1]",
opts.workloadCertGracePeriodRatio)
}
}