@@ -3,60 +3,52 @@ package commands
33import (
44 "bonvoy/consul"
55 "bonvoy/envoy"
6- "flag"
76 "fmt"
7+ "github.com/spf13/cobra"
88 "strings"
99)
1010
11- type ExpiredCertificatesCommand struct {
12- fs * flag.FlagSet
13- consul consul.Client
14- name string
11+ type ExpiredCertificatesController struct {
12+ ServiceName string
13+ Consul consul.Client
1514}
1615
17- func BuildExpiredCertificatesCommand () * ExpiredCertificatesCommand {
18- gc := & ExpiredCertificatesCommand {
19- fs : flag .NewFlagSet ("certs-expired" , flag .ContinueOnError ),
20- consul : consul .NewClient (),
21- }
22- gc .fs .Arg (0 )
23- return gc
24- }
25-
26- func (g * ExpiredCertificatesCommand ) Name () string {
27- return g .fs .Name ()
28- }
29-
30- func (g * ExpiredCertificatesCommand ) Init (args []string ) error {
31- return g .fs .Parse (args )
16+ func BuildExpiredCertificatesController (rootCmd * cobra.Command ) {
17+ rootCmd .AddCommand (& cobra.Command {
18+ Use : "certificates expired" ,
19+ Short : "Show all expired certificates" ,
20+ Long : `Display all expired sidecar certificates as compared to the local Consul agent` ,
21+ Args : cobra .MinimumNArgs (2 ),
22+ RunE : func (cmd * cobra.Command , args []string ) error {
23+ controller := ExpiredCertificatesController {
24+ ServiceName : args [1 ],
25+ Consul : consul .NewClient (),
26+ }
27+ return controller .Run ()
28+ },
29+ })
3230}
3331
34- func (g * ExpiredCertificatesCommand ) Run () error {
35- var name = g .name
36- if name == "" {
37- name = g .fs .Arg (0 )
38- }
39-
40- e , err := envoy .NewFromServiceName (name )
32+ func (c * ExpiredCertificatesController ) Run () error {
33+ e , err := envoy .NewFromServiceName (c .ServiceName )
4134 if err != nil {
4235 return err
4336 }
4437 data := e .Certificates ().Get ()
4538
4639 for _ , certs := range data .Certificates {
47- for _ , c := range certs .CertificateChain {
48- a := strings .Split (c .SubjectAltNames [0 ].Uri , "/" )
40+ for _ , cert := range certs .CertificateChain {
41+ a := strings .Split (cert .SubjectAltNames [0 ].Uri , "/" )
4942 svc := a [len (a )- 1 ]
50- leaf := g . consul .Agent ().GetConnectLeafCaCertificate (svc )
43+ leaf := c . Consul .Agent ().GetConnectLeafCaCertificate (svc )
5144
52- if c .ExpirationTime != leaf .ValidBefore {
45+ if cert .ExpirationTime != leaf .ValidBefore {
5346 fmt .Println (svc )
5447 fmt .Println (" Envoy Process ID:" , e .Pid )
55- fmt .Printf (" Envoy Certificate Expiry: %s (%s days)\n " , c .ExpirationTime , c .DaysUntilExpiration )
48+ fmt .Printf (" Envoy Certificate Expiry: %s (%s days)\n " , cert .ExpirationTime , cert .DaysUntilExpiration )
5649 fmt .Println (" Consul Agent Certificate Expiry: " , leaf .ValidBefore )
5750 }
5851 }
5952 }
60-
6153 return nil
62- }
54+ }
0 commit comments