github.com/slackhq/nebula@v1.9.0/cmd/nebula-cert/print.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "flag" 6 "fmt" 7 "io" 8 "os" 9 "strings" 10 11 "github.com/skip2/go-qrcode" 12 "github.com/slackhq/nebula/cert" 13 ) 14 15 type printFlags struct { 16 set *flag.FlagSet 17 json *bool 18 outQRPath *string 19 path *string 20 } 21 22 func newPrintFlags() *printFlags { 23 pf := printFlags{set: flag.NewFlagSet("print", flag.ContinueOnError)} 24 pf.set.Usage = func() {} 25 pf.json = pf.set.Bool("json", false, "Optional: outputs certificates in json format") 26 pf.outQRPath = pf.set.String("out-qr", "", "Optional: output a qr code image (png) of the certificate") 27 pf.path = pf.set.String("path", "", "Required: path to the certificate") 28 29 return &pf 30 } 31 32 func printCert(args []string, out io.Writer, errOut io.Writer) error { 33 pf := newPrintFlags() 34 err := pf.set.Parse(args) 35 if err != nil { 36 return err 37 } 38 39 if err := mustFlagString("path", pf.path); err != nil { 40 return err 41 } 42 43 rawCert, err := os.ReadFile(*pf.path) 44 if err != nil { 45 return fmt.Errorf("unable to read cert; %s", err) 46 } 47 48 var c *cert.NebulaCertificate 49 var qrBytes []byte 50 part := 0 51 52 for { 53 c, rawCert, err = cert.UnmarshalNebulaCertificateFromPEM(rawCert) 54 if err != nil { 55 return fmt.Errorf("error while unmarshaling cert: %s", err) 56 } 57 58 if *pf.json { 59 b, _ := json.Marshal(c) 60 out.Write(b) 61 out.Write([]byte("\n")) 62 63 } else { 64 out.Write([]byte(c.String())) 65 out.Write([]byte("\n")) 66 } 67 68 if *pf.outQRPath != "" { 69 b, err := c.MarshalToPEM() 70 if err != nil { 71 return fmt.Errorf("error while marshalling cert to PEM: %s", err) 72 } 73 qrBytes = append(qrBytes, b...) 74 } 75 76 if rawCert == nil || len(rawCert) == 0 || strings.TrimSpace(string(rawCert)) == "" { 77 break 78 } 79 80 part++ 81 } 82 83 if *pf.outQRPath != "" { 84 b, err := qrcode.Encode(string(qrBytes), qrcode.Medium, -5) 85 if err != nil { 86 return fmt.Errorf("error while generating qr code: %s", err) 87 } 88 89 err = os.WriteFile(*pf.outQRPath, b, 0600) 90 if err != nil { 91 return fmt.Errorf("error while writing out-qr: %s", err) 92 } 93 } 94 95 return nil 96 } 97 98 func printSummary() string { 99 return "print <flags>: prints details about a certificate" 100 } 101 102 func printHelp(out io.Writer) { 103 pf := newPrintFlags() 104 out.Write([]byte("Usage of " + os.Args[0] + " " + printSummary() + "\n")) 105 pf.set.SetOutput(out) 106 pf.set.PrintDefaults() 107 }