|
| 1 | +/* |
| 2 | +Copyright 2021 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package cmd |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "os" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/spf13/cobra" |
| 22 | + |
| 23 | + "github.com/dapr/cli/pkg/kubernetes" |
| 24 | + "github.com/dapr/cli/pkg/print" |
| 25 | + "github.com/dapr/cli/utils" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + privateKey string |
| 30 | + caRootCertificateFile string |
| 31 | + issuerPrivateKeyFile string |
| 32 | + issuerPublicCertificateFile string |
| 33 | + validUntil uint |
| 34 | + restartDaprServices bool |
| 35 | +) |
| 36 | + |
| 37 | +func RenewCertificateCmd() *cobra.Command { |
| 38 | + command := &cobra.Command{ |
| 39 | + Use: "renew-certificate", |
| 40 | + Short: "Rotates Dapr root certificate of your kubernetes cluster", |
| 41 | + |
| 42 | + Example: ` |
| 43 | +# Generates new root and issuer certificates for kubernetes cluster |
| 44 | +dapr mtls renew-certificate -k --valid-until <no of days> --restart |
| 45 | +
|
| 46 | +# Uses existing private root.key to generate new root and issuer certificates for kubernetes cluster |
| 47 | +dapr mtls renew-certificate -k --private-key myprivatekey.key --valid-until <no of days> |
| 48 | +
|
| 49 | +# Rotates certificate of your kubernetes cluster with provided ca.cert, issuer.crt and issuer.key file path |
| 50 | +dapr mtls renew-certificate -k --ca-root-certificate <ca.crt> --issuer-private-key <issuer.key> --issuer-public-certificate <issuer.crt> --restart |
| 51 | +
|
| 52 | +# See more at: https://docs.dapr.io/getting-started/ |
| 53 | +`, |
| 54 | + |
| 55 | + Run: func(cmd *cobra.Command, args []string) { |
| 56 | + if kubernetesMode { |
| 57 | + print.PendingStatusEvent(os.Stdout, "Starting certificate rotation") |
| 58 | + if caRootCertificateFile != "" && issuerPrivateKeyFile != "" && issuerPublicCertificateFile != "" { |
| 59 | + print.InfoStatusEvent(os.Stdout, "Using provided certificates") |
| 60 | + err := kubernetes.RenewCertificate(kubernetes.RenewCertificateParams{ |
| 61 | + RootCertificateFilePath: caRootCertificateFile, |
| 62 | + IssuerCertificateFilePath: issuerPublicCertificateFile, |
| 63 | + IssuerPrivateKeyFilePath: issuerPrivateKeyFile, |
| 64 | + Timeout: timeout, |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + logErrorAndExit(err) |
| 68 | + } |
| 69 | + } else if privateKey != "" { |
| 70 | + print.InfoStatusEvent(os.Stdout, "Using password file to generate root certificate") |
| 71 | + err := kubernetes.RenewCertificate(kubernetes.RenewCertificateParams{ |
| 72 | + RootPrivateKeyFilePath: privateKey, |
| 73 | + ValidUntil: time.Hour * time.Duration(validUntil*24), |
| 74 | + Timeout: timeout, |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + logErrorAndExit(err) |
| 78 | + } |
| 79 | + } else { |
| 80 | + print.InfoStatusEvent(os.Stdout, "generating fresh certificates") |
| 81 | + err := kubernetes.RenewCertificate(kubernetes.RenewCertificateParams{ |
| 82 | + ValidUntil: time.Hour * time.Duration(validUntil*24), |
| 83 | + Timeout: timeout, |
| 84 | + }) |
| 85 | + if err != nil { |
| 86 | + logErrorAndExit(err) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + }, |
| 91 | + PostRun: func(cmd *cobra.Command, args []string) { |
| 92 | + expiry, err := kubernetes.Expiry() |
| 93 | + if err != nil { |
| 94 | + logErrorAndExit(err) |
| 95 | + } |
| 96 | + print.SuccessStatusEvent(os.Stdout, |
| 97 | + fmt.Sprintf("Certificate rotation is successful! Your new certicate is valid through %s", expiry.Format(time.RFC1123))) |
| 98 | + |
| 99 | + if restartDaprServices { |
| 100 | + restartControlPlaneService() |
| 101 | + if err != nil { |
| 102 | + print.FailureStatusEvent(os.Stdout, err.Error()) |
| 103 | + os.Exit(1) |
| 104 | + } |
| 105 | + } |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + command.Flags().BoolVarP(&kubernetesMode, "kubernetes", "k", false, "Renews root and issuer certificates of Dapr in a Kubernetes cluster") |
| 110 | + command.Flags().StringVarP(&privateKey, "private-key", "", "", "The root.key file which is used to generate root certificate") |
| 111 | + command.Flags().StringVarP(&caRootCertificateFile, "ca-root-certificate", "", "", "The root certificate file") |
| 112 | + command.Flags().StringVarP(&issuerPrivateKeyFile, "issuer-private-key", "", "", "The issuer certificate private key") |
| 113 | + command.Flags().StringVarP(&issuerPublicCertificateFile, "issuer-public-certificate", "", "", "The issuer certificate") |
| 114 | + command.Flags().UintVarP(&validUntil, "valid-until", "", 365, "Max days before certificate expires") |
| 115 | + command.Flags().BoolVarP(&restartDaprServices, "restart", "", false, "Restart Dapr control plane services") |
| 116 | + command.Flags().UintVarP(&timeout, "timeout", "", 300, "The timeout for the certificate renewal") |
| 117 | + command.MarkFlagRequired("kubernetes") |
| 118 | + return command |
| 119 | +} |
| 120 | + |
| 121 | +func logErrorAndExit(err error) { |
| 122 | + err = fmt.Errorf("certificate rotation failed %w", err) |
| 123 | + print.FailureStatusEvent(os.Stderr, err.Error()) |
| 124 | + os.Exit(1) |
| 125 | +} |
| 126 | + |
| 127 | +func restartControlPlaneService() error { |
| 128 | + controlPlaneServices := []string{"deploy/dapr-sentry", "deploy/dapr-operator", "statefulsets/dapr-placement-server"} |
| 129 | + namespace, err := getDaprNamespace() |
| 130 | + if err != nil { |
| 131 | + print.FailureStatusEvent(os.Stdout, "Failed to fetch Dapr namespace") |
| 132 | + } |
| 133 | + for _, name := range controlPlaneServices { |
| 134 | + print.InfoStatusEvent(os.Stdout, fmt.Sprintf("Restarting %s..", name)) |
| 135 | + _, err := utils.RunCmdAndWait("kubectl", "rollout", "restart", name, "-n", namespace) |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf("error in restarting deployment %s. Error is %w", name, err) |
| 138 | + } |
| 139 | + _, err = utils.RunCmdAndWait("kubectl", "rollout", "status", name, "-n", namespace) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("error in checking status for deployment %s. Error is %w", name, err) |
| 142 | + } |
| 143 | + } |
| 144 | + print.SuccessStatusEvent(os.Stdout, "All control plane services have restarted successfully!") |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func getDaprNamespace() (string, error) { |
| 149 | + status, err := kubernetes.GetDaprResourcesStatus() |
| 150 | + if err != nil { |
| 151 | + return "", err |
| 152 | + } |
| 153 | + return status[0].Namespace, nil |
| 154 | +} |
0 commit comments