| 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 |
var claims ioClaims |
| 44 |
if err := reserveInputs(&claims, "path", *pf.path); err != nil { |
| 45 |
return err |
| 46 |
} |
| 47 |
if err := reserveOutputs(&claims, "out-qr", *pf.outQRPath); err != nil { |
| 48 |
return err |
| 49 |
} |
| 50 |
|
| 51 |
rawCert, err := readInput("path", *pf.path, &claims) |
| 52 |
if err != nil { |
| 53 |
return fmt.Errorf("unable to read cert; %s", err) |
| 54 |
} |
| 55 |
|
| 56 |
// When the QR is going to stdout, suppress the human-readable text/json |
| 57 |
// output so the binary stream is not contaminated. |
| 58 |
qrToStdout := isStdio(*pf.outQRPath) |
| 59 |
|
| 60 |
var c cert.Certificate |
| 61 |
var qrBytes []byte |
| 62 |
part := 0 |
| 63 |
|
| 64 |
var jsonCerts []cert.Certificate |
| 65 |
|
| 66 |
for { |
| 67 |
c, rawCert, err = cert.UnmarshalCertificateFromPEM(rawCert) |
| 68 |
if err != nil { |
| 69 |
return fmt.Errorf("error while unmarshaling cert: %s", err) |
| 70 |
} |
| 71 |
|
| 72 |
if !qrToStdout { |
| 73 |
if *pf.json { |
| 74 |
jsonCerts = append(jsonCerts, c) |
| 75 |
} else { |
| 76 |
_, _ = out.Write([]byte(c.String())) |
| 77 |
_, _ = out.Write([]byte("\n")) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if *pf.outQRPath != "" { |
| 82 |
b, err := c.MarshalPEM() |
| 83 |
if err != nil { |
| 84 |
return fmt.Errorf("error while marshalling cert to PEM: %s", err) |
| 85 |
} |
| 86 |
qrBytes = append(qrBytes, b...) |
| 87 |
} |
| 88 |
|
| 89 |
if rawCert == nil || len(rawCert) == 0 || strings.TrimSpace(string(rawCert)) == "" { |
| 90 |
break |
| 91 |
} |
| 92 |
|
| 93 |
part++ |
| 94 |
} |
| 95 |
|
| 96 |
if *pf.json && !qrToStdout { |
| 97 |
b, _ := json.Marshal(jsonCerts) |
| 98 |
_, _ = out.Write(b) |
| 99 |
_, _ = out.Write([]byte("\n")) |
| 100 |
} |
| 101 |
|
| 102 |
if *pf.outQRPath != "" { |
| 103 |
b, err := qrcode.Encode(string(qrBytes), qrcode.Medium, -5) |
| 104 |
if err != nil { |
| 105 |
return fmt.Errorf("error while generating qr code: %s", err) |
| 106 |
} |
| 107 |
|
| 108 |
err = writeOutput(*pf.outQRPath, b, 0600, out) |
| 109 |
if err != nil { |
| 110 |
return fmt.Errorf("error while writing out-qr: %s", err) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
func printSummary() string { |
| 118 |
return "print <flags>: prints details about a certificate" |
| 119 |
} |
| 120 |
|
| 121 |
func printHelp(out io.Writer) { |
| 122 |
pf := newPrintFlags() |
| 123 |
out.Write([]byte("Usage of " + os.Args[0] + " " + printSummary() + "\n")) |
| 124 |
out.Write([]byte(stdioHelpText)) |
| 125 |
pf.set.SetOutput(out) |
| 126 |
pf.set.PrintDefaults() |
| 127 |
} |