github.com/hernad/nomad@v1.6.112/command/tls_ca_info.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "os" 9 "strings" 10 11 "github.com/hernad/nomad/helper/tlsutil" 12 "github.com/posener/complete" 13 "github.com/ryanuber/columnize" 14 ) 15 16 type TLSCAInfoCommand struct { 17 Meta 18 } 19 20 func (c *TLSCAInfoCommand) Help() string { 21 helpText := ` 22 Usage: nomad tls ca info <CA file> 23 24 Show information about a certificate authority. 25 ` 26 return strings.TrimSpace(helpText) 27 } 28 29 func (c *TLSCAInfoCommand) AutocompleteFlags() complete.Flags { 30 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 31 complete.Flags{}) 32 } 33 34 func (c *TLSCAInfoCommand) AutocompleteArgs() complete.Predictor { 35 return complete.PredictOr( 36 complete.PredictFiles("*.pem"), 37 ) 38 } 39 40 func (c *TLSCAInfoCommand) Synopsis() string { 41 return "Show certificate authority information" 42 } 43 44 func (c *TLSCAInfoCommand) Name() string { return "tls cert info" } 45 46 func (c *TLSCAInfoCommand) Run(args []string) int { 47 48 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 49 flags.Usage = func() { c.Ui.Output(c.Help()) } 50 if err := flags.Parse(args); err != nil { 51 return 1 52 } 53 54 // Check that we got no arguments 55 args = flags.Args() 56 if l := len(args); l < 0 || l > 1 { 57 c.Ui.Error("This command takes up to one argument") 58 c.Ui.Error(commandErrorText(c)) 59 return 1 60 } 61 var certFile []byte 62 var err error 63 var file string 64 if len(args) == 0 { 65 c.Ui.Error(fmt.Sprintf("Error reading CA file: %v", err)) 66 return 1 67 } 68 if len(args) == 1 { 69 file = args[0] 70 certFile, err = os.ReadFile(file) 71 if err != nil { 72 c.Ui.Error(fmt.Sprintf("Error reading CA file: %v", err)) 73 return 1 74 } 75 } 76 77 certInfo, err := tlsutil.ParseCert(string(certFile)) 78 if err != nil { 79 c.Ui.Error(err.Error()) 80 return 1 81 } 82 // Format the certificate info 83 basic := []string{ 84 fmt.Sprintf("Serial Number|%s", certInfo.SerialNumber), 85 fmt.Sprintf("Issuer CN|%s", certInfo.Issuer.CommonName), 86 fmt.Sprintf("Common Name|%s", certInfo.Subject), 87 fmt.Sprintf("Expiry Date|%s", certInfo.NotAfter), 88 fmt.Sprintf("Permitted DNS Domains|%s", certInfo.PermittedDNSDomains), 89 } 90 91 // Print out the information 92 c.Ui.Output(columnize.SimpleFormat(basic)) 93 return 0 94 }