@@ -16,6 +16,7 @@ package cmd
1616import (
1717 "fmt"
1818 "os"
19+ "strings"
1920 "time"
2021
2122 "github.com/spf13/cobra"
@@ -47,17 +48,29 @@ dapr mtls renew-certificate -k --valid-until <no of days> --restart
4748dapr mtls renew-certificate -k --private-key myprivatekey.key --valid-until <no of days>
4849
4950# 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+ dapr mtls renew-certificate -k --ca-root-certificate <root.pem > --issuer-private-key <issuer.key> --issuer-public-certificate <issuer.pem > --restart
5152
5253# See more at: https://docs.dapr.io/getting-started/
5354` ,
5455
5556 Run : func (cmd * cobra.Command , args []string ) {
57+ var err error
58+ pkFlag := cmd .Flags ().Lookup ("private-key" ).Changed
59+ rootcertFlag := cmd .Flags ().Lookup ("ca-root-certificate" ).Changed
60+ issuerKeyFlag := cmd .Flags ().Lookup ("issuer-private-key" ).Changed
61+ issuerCertFlag := cmd .Flags ().Lookup ("issuer-public-certificate" ).Changed
62+
5663 if kubernetesMode {
5764 print .PendingStatusEvent (os .Stdout , "Starting certificate rotation" )
58- if caRootCertificateFile != "" && issuerPrivateKeyFile != "" && issuerPublicCertificateFile != "" {
65+ if rootcertFlag || issuerKeyFlag || issuerCertFlag {
66+ flagArgsEmpty := checkReqFlagArgsEmpty (caRootCertificateFile , issuerPrivateKeyFile , issuerPublicCertificateFile )
67+ if flagArgsEmpty {
68+ err = fmt .Errorf ("all required flags for this certificate rotation path, %q, %q and %q are not present" ,
69+ "ca-root-certificate" , "issuer-private-key" , "issuer-public-certificate" )
70+ logErrorAndExit (err )
71+ }
5972 print .InfoStatusEvent (os .Stdout , "Using provided certificates" )
60- err : = kubernetes .RenewCertificate (kubernetes.RenewCertificateParams {
73+ err = kubernetes .RenewCertificate (kubernetes.RenewCertificateParams {
6174 RootCertificateFilePath : caRootCertificateFile ,
6275 IssuerCertificateFilePath : issuerPublicCertificateFile ,
6376 IssuerPrivateKeyFilePath : issuerPrivateKeyFile ,
@@ -66,9 +79,14 @@ dapr mtls renew-certificate -k --ca-root-certificate <ca.crt> --issuer-private-k
6679 if err != nil {
6780 logErrorAndExit (err )
6881 }
69- } else if privateKey != "" {
82+ } else if pkFlag {
83+ flagArgsEmpty := checkReqFlagArgsEmpty (privateKey )
84+ if flagArgsEmpty {
85+ err = fmt .Errorf ("%q flag has incorrect value" , "privateKey" )
86+ logErrorAndExit (err )
87+ }
7088 print .InfoStatusEvent (os .Stdout , "Using password file to generate root certificate" )
71- err : = kubernetes .RenewCertificate (kubernetes.RenewCertificateParams {
89+ err = kubernetes .RenewCertificate (kubernetes.RenewCertificateParams {
7290 RootPrivateKeyFilePath : privateKey ,
7391 ValidUntil : time .Hour * time .Duration (validUntil * 24 ),
7492 Timeout : timeout ,
@@ -78,7 +96,7 @@ dapr mtls renew-certificate -k --ca-root-certificate <ca.crt> --issuer-private-k
7896 }
7997 } else {
8098 print .InfoStatusEvent (os .Stdout , "generating fresh certificates" )
81- err : = kubernetes .RenewCertificate (kubernetes.RenewCertificateParams {
99+ err = kubernetes .RenewCertificate (kubernetes.RenewCertificateParams {
82100 ValidUntil : time .Hour * time .Duration (validUntil * 24 ),
83101 Timeout : timeout ,
84102 })
@@ -118,8 +136,17 @@ dapr mtls renew-certificate -k --ca-root-certificate <ca.crt> --issuer-private-k
118136 return command
119137}
120138
139+ func checkReqFlagArgsEmpty (params ... string ) bool {
140+ for _ , val := range params {
141+ if len (strings .TrimSpace (val )) == 0 {
142+ return true
143+ }
144+ }
145+ return false
146+ }
147+
121148func logErrorAndExit (err error ) {
122- err = fmt .Errorf ("certificate rotation failed %w" , err )
149+ err = fmt .Errorf ("certificate rotation failed: %w" , err )
123150 print .FailureStatusEvent (os .Stderr , err .Error ())
124151 os .Exit (1 )
125152}
0 commit comments