108 lines
3.7 KiB
Go
108 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var Version = "dev"
|
|
|
|
func main() {
|
|
var configPath string
|
|
var overwrite bool
|
|
var subject string
|
|
var certType string
|
|
var validity string
|
|
var san []string
|
|
var name string
|
|
var fromFile string
|
|
var dryRun bool
|
|
var verbose bool
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "lab-ca",
|
|
Short: "Certificate Authority Utility",
|
|
Long: "lab-ca - Certificate Authority Utility",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
printMainHelp()
|
|
},
|
|
}
|
|
|
|
var initCmd = &cobra.Command{
|
|
Use: "initca",
|
|
Short: "Generate a new CA certificate and key",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if err := LoadCA(configPath); err != nil {
|
|
fmt.Printf("Error loading CA config: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
InitCA(configPath, overwrite)
|
|
},
|
|
}
|
|
|
|
initCmd.Flags().StringVar(&configPath, "config", "ca_config.hcl", "Path to CA configuration file")
|
|
initCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Allow overwriting existing files")
|
|
|
|
var issueCmd = &cobra.Command{
|
|
Use: "issue",
|
|
Short: "Issue a new certificate (client, server, server-only, code-signing, email)",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if err := LoadCA(configPath); err != nil {
|
|
fmt.Printf("Error loading CA config: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
IssueCertificate(configPath, subject, certType, validity, san, name, fromFile, overwrite, dryRun, verbose)
|
|
},
|
|
}
|
|
|
|
issueCmd.Flags().StringVar(&configPath, "config", "ca_config.hcl", "Path to CA configuration file")
|
|
issueCmd.Flags().StringVar(&subject, "subject", "", "Subject Common Name for the certificate (optional, defaults to --name)")
|
|
issueCmd.Flags().StringVar(&certType, "type", "server", "Certificate type: client, server, server-only, code-signing, email")
|
|
issueCmd.Flags().StringArrayVar(&san, "san", nil,
|
|
"Subject Alternative Name (SAN). Use multiple times for multiple values.\n"+
|
|
"Format: dns:example.com, ip:1.2.3.4, email:user@example.com")
|
|
issueCmd.Flags().StringVar(&validity, "validity", "1y", "Certificate validity (e.g. 2y, 6m, 30d). Overrides config file for this certificate.")
|
|
issueCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Allow overwriting existing files")
|
|
issueCmd.Flags().StringVar(&fromFile, "from-file", "", "Path to HCL file with multiple certificate definitions (batch mode)")
|
|
issueCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Validate and show what would be created, but do not write files (batch mode)")
|
|
issueCmd.Flags().BoolVar(&verbose, "verbose", false, "Print detailed information about each processed certificate")
|
|
// Only require --name in simple mode
|
|
issueCmd.Flags().StringVar(&name, "name", "", "Name for the certificate and key files (used as subject if --subject is omitted)")
|
|
issueCmd.PreRun = func(cmd *cobra.Command, args []string) {
|
|
if fromFile == "" {
|
|
cmd.MarkFlagRequired("name")
|
|
}
|
|
}
|
|
|
|
var versionCmd = &cobra.Command{
|
|
Use: "version",
|
|
Short: "Show version information",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
fmt.Printf("lab-ca version: %s\n", Version)
|
|
},
|
|
}
|
|
rootCmd.AddCommand(initCmd)
|
|
rootCmd.AddCommand(issueCmd)
|
|
rootCmd.AddCommand(versionCmd)
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func printMainHelp() {
|
|
fmt.Println("lab-ca - Certificate Authority Utility")
|
|
fmt.Println()
|
|
fmt.Println("Usage:")
|
|
fmt.Println(" lab-ca <command> [options]")
|
|
fmt.Println()
|
|
fmt.Println("Available commands:")
|
|
fmt.Println(" initca Generate a new CA certificate and key")
|
|
fmt.Println(" issue Issue a new client/server certificate")
|
|
fmt.Println(" version Show version information")
|
|
fmt.Println()
|
|
fmt.Println("Use 'lab-ca <command> --help' for more information about a command.")
|
|
}
|