-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewCACommand.go
66 lines (59 loc) · 2.01 KB
/
ViewCACommand.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
package certs
import (
"fmt"
"github.com/hiveot/hub/lib/certs"
"log/slog"
"path"
"time"
)
import "github.com/urfave/cli/v2"
// ViewCACommand shows info on the Hub self-signed CA
// This does not require any services to run.
//
// hubcli vca [--certs=CertFolder] view
func ViewCACommand(certsFolder *string) *cli.Command {
return &cli.Command{
Name: "vca",
Category: "certs",
Usage: "View CA and server certificate info",
Action: func(cCtx *cli.Context) error {
if cCtx.NArg() > 0 {
return fmt.Errorf("unexpected argument(s) '%s'", cCtx.Args().First())
}
err := HandleViewCACert(*certsFolder)
return err
},
}
}
// HandleViewCACert shows CA certificate information
func HandleViewCACert(certsFolder string) error {
caCertPath := path.Join(certsFolder, certs.DefaultCaCertFile)
caCert, err := certs.LoadX509CertFromPEM(caCertPath)
if err != nil {
slog.Error("Unable to load the CA certificate", "err", err)
return err
}
fmt.Println("CA certificate path: ", caCertPath)
fmt.Println(" IsCA : ", caCert.IsCA)
fmt.Println(" Version : ", caCert.Version)
fmt.Println(" Valid until: ", caCert.NotAfter.Format(time.RFC1123Z))
fmt.Println(" Subject : ", caCert.Subject.String())
fmt.Println(" Issuer : ", caCert.Issuer.String())
fmt.Println(" DNS names : ", caCert.DNSNames)
fmt.Println()
hubCertPath := path.Join(certsFolder, "hubCert.pem")
serverCert, err := certs.LoadX509CertFromPEM(hubCertPath)
if err != nil {
slog.Error("Unable to load the server certificate", "err", err)
return err
}
fmt.Println("Server certificate path: ", hubCertPath)
fmt.Println(" IsCA : ", serverCert.IsCA)
fmt.Println(" Version : ", serverCert.Version)
fmt.Println(" Valid until : ", serverCert.NotAfter.Format(time.RFC1123Z))
fmt.Println(" Subject : ", serverCert.Subject.String())
fmt.Println(" Issuer : ", serverCert.Issuer.String())
fmt.Println(" DNS names : ", serverCert.DNSNames)
fmt.Println(" IP addresses: ", serverCert.IPAddresses)
return nil
}