isle/go/cmd/entrypoint/nebula.go

81 lines
1.6 KiB
Go

package main
import (
"fmt"
"isle/jsonutil"
"os"
)
var subCmdNebulaShow = subCmd{
name: "show",
descr: "Writes nebula network information to stdout in JSON format",
do: func(subCmdCtx subCmdCtx) error {
flags := subCmdCtx.flagSet(false)
if err := flags.Parse(subCmdCtx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
hostBootstrap, err := loadHostBootstrap()
if err != nil {
return fmt.Errorf("loading host bootstrap: %w", err)
}
caCert := hostBootstrap.CAPublicCredentials.Cert.Unwrap()
caCertPEM, err := caCert.MarshalToPEM()
if err != nil {
return fmt.Errorf("marshaling CA cert to PEM: %w", err)
}
if len(caCert.Details.Subnets) != 1 {
return fmt.Errorf(
"malformed ca.crt, contains unexpected subnets %#v",
caCert.Details.Subnets,
)
}
subnet := caCert.Details.Subnets[0]
type outLighthouse struct {
PublicAddr string
IP string
}
out := struct {
CACert string
SubnetCIDR string
Lighthouses []outLighthouse
}{
CACert: string(caCertPEM),
SubnetCIDR: subnet.String(),
}
for _, h := range hostBootstrap.Hosts {
if h.Nebula.PublicAddr == "" {
continue
}
out.Lighthouses = append(out.Lighthouses, outLighthouse{
PublicAddr: h.Nebula.PublicAddr,
IP: h.IP().String(),
})
}
if err := jsonutil.WriteIndented(os.Stdout, out); err != nil {
return fmt.Errorf("encoding to stdout: %w", err)
}
return nil
},
}
var subCmdNebula = subCmd{
name: "nebula",
descr: "Sub-commands related to the nebula VPN",
do: func(subCmdCtx subCmdCtx) error {
return subCmdCtx.doSubCmd(
subCmdNebulaShow,
)
},
}