Files
knox/internal/cmd/tls.go
T
david f80424d707 Merge commit '52c4d1f' into feat/remote-node
# Conflicts:
#	internal/db/db.go
2026-11-03 23:00:20 -08:00

61 lines
2.0 KiB
Go

package cmd
import (
"fmt"
"path/filepath"
"strings"
"time"
"github.com/david/knox/internal/httpauth"
"github.com/spf13/cobra"
)
// NewTLSCmd exposes certificate management for nodes that must serve TLS
// without a public DNS name or a CA-issued certificate.
func NewTLSCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "tls",
Short: "Manage TLS material for a remote knox node",
}
var (
hosts []string
out string
days int
)
initCmd := &cobra.Command{
Use: "init",
Short: "Generate a self-signed certificate usable without a public name",
Long: `Generate a self-signed certificate and key for this node.
The certificate is its own CA, so peers (and MCP clients) trust it by pointing
KNOX_TLS_CA at the certificate file. No public DNS name or external certificate
authority is involved: pass the addresses clients actually dial, e.g.
knox tls init --host 172.30.170.155 --host knox.dhg.lol --out /data/tls`,
RunE: func(c *cobra.Command, args []string) error {
certPath := filepath.Join(out, "cert.pem")
keyPath := filepath.Join(out, "key.pem")
if err := httpauth.GenerateSelfSigned(certPath, keyPath, hosts, time.Duration(days)*24*time.Hour); err != nil {
return err
}
fmt.Printf("wrote %s\nwrote %s\n\n", certPath, keyPath)
fmt.Println("serve with:")
fmt.Printf(" %s=%s\n %s=%s\n %s=<shared-secret>\n",
httpauth.CertEnv, certPath, httpauth.KeyEnv, keyPath, httpauth.TokenEnv)
fmt.Println("\npeers trust it with:")
fmt.Printf(" %s=%s\n %s=<same-secret>\n", httpauth.CAEnv, certPath, httpauth.TokenEnv)
if len(hosts) > 0 {
fmt.Printf("\ncertificate covers: %s\n", strings.Join(hosts, ", "))
}
return nil
},
}
initCmd.Flags().StringSliceVar(&hosts, "host", nil, "DNS names and/or IP addresses clients dial (repeatable)")
initCmd.Flags().StringVar(&out, "out", "./tls", "Directory to write cert.pem and key.pem into")
initCmd.Flags().IntVar(&days, "days", 825, "Validity in days")
cmd.AddCommand(initCmd)
return cmd
}