2019-11-19 10:00:20 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-06-30 16:53:30 -06:00
|
|
|
|
2021-02-11 17:53:25 -07:00
|
|
|
"github.com/skip2/go-qrcode"
|
2020-06-30 16:53:30 -06:00
|
|
|
"github.com/slackhq/nebula/cert"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type printFlags struct {
|
2021-02-11 17:53:25 -07:00
|
|
|
set *flag.FlagSet
|
|
|
|
json *bool
|
|
|
|
outQRPath *string
|
|
|
|
path *string
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPrintFlags() *printFlags {
|
|
|
|
pf := printFlags{set: flag.NewFlagSet("print", flag.ContinueOnError)}
|
|
|
|
pf.set.Usage = func() {}
|
|
|
|
pf.json = pf.set.Bool("json", false, "Optional: outputs certificates in json format")
|
2021-02-11 17:53:25 -07:00
|
|
|
pf.outQRPath = pf.set.String("out-qr", "", "Optional: output a qr code image (png) of the certificate")
|
2019-11-19 10:00:20 -07:00
|
|
|
pf.path = pf.set.String("path", "", "Required: path to the certificate")
|
|
|
|
|
|
|
|
return &pf
|
|
|
|
}
|
|
|
|
|
|
|
|
func printCert(args []string, out io.Writer, errOut io.Writer) error {
|
|
|
|
pf := newPrintFlags()
|
|
|
|
err := pf.set.Parse(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mustFlagString("path", pf.path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-31 08:35:13 -06:00
|
|
|
rawCert, err := os.ReadFile(*pf.path)
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read cert; %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c *cert.NebulaCertificate
|
2021-02-11 17:53:25 -07:00
|
|
|
var qrBytes []byte
|
|
|
|
part := 0
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
for {
|
|
|
|
c, rawCert, err = cert.UnmarshalNebulaCertificateFromPEM(rawCert)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while unmarshaling cert: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *pf.json {
|
|
|
|
b, _ := json.Marshal(c)
|
|
|
|
out.Write(b)
|
|
|
|
out.Write([]byte("\n"))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
out.Write([]byte(c.String()))
|
|
|
|
out.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
|
2021-02-11 17:53:25 -07:00
|
|
|
if *pf.outQRPath != "" {
|
|
|
|
b, err := c.MarshalToPEM()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while marshalling cert to PEM: %s", err)
|
|
|
|
}
|
|
|
|
qrBytes = append(qrBytes, b...)
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
if rawCert == nil || len(rawCert) == 0 || strings.TrimSpace(string(rawCert)) == "" {
|
|
|
|
break
|
|
|
|
}
|
2021-02-11 17:53:25 -07:00
|
|
|
|
|
|
|
part++
|
|
|
|
}
|
|
|
|
|
|
|
|
if *pf.outQRPath != "" {
|
|
|
|
b, err := qrcode.Encode(string(qrBytes), qrcode.Medium, -5)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while generating qr code: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-10-31 08:35:13 -06:00
|
|
|
err = os.WriteFile(*pf.outQRPath, b, 0600)
|
2021-02-11 17:53:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while writing out-qr: %s", err)
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printSummary() string {
|
|
|
|
return "print <flags>: prints details about a certificate"
|
|
|
|
}
|
|
|
|
|
|
|
|
func printHelp(out io.Writer) {
|
|
|
|
pf := newPrintFlags()
|
|
|
|
out.Write([]byte("Usage of " + os.Args[0] + " " + printSummary() + "\n"))
|
|
|
|
pf.set.SetOutput(out)
|
|
|
|
pf.set.PrintDefaults()
|
|
|
|
}
|