174 lines
4.6 KiB
Go
174 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
|
|
"git.nevets.tech/Keys/CertManager/commands"
|
|
"git.nevets.tech/Keys/CertManager/internal"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var configFile string
|
|
|
|
var fqdnRegex = regexp.MustCompile(`^(?i:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$`)
|
|
|
|
//TODO create logic for gh vs gt repos
|
|
|
|
func main() {
|
|
rootCmd := &cobra.Command{
|
|
Use: "certman",
|
|
Short: "CertMan",
|
|
Long: "Certificate Manager",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return cmd.Help()
|
|
},
|
|
}
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "/etc/certman/certman.conf", "Configuration file")
|
|
|
|
rootCmd.AddCommand(basicCmd("version", "Show version", commands.VersionCmd))
|
|
rootCmd.AddCommand(basicCmd("gen-key", "Generates encryption key", commands.NewKeyCmd))
|
|
rootCmd.AddCommand(basicCmd("dev", "Dev Function", commands.DevCmd))
|
|
|
|
var domainCertDir string
|
|
newDomainCmd := &cobra.Command{
|
|
Use: "new-domain",
|
|
Short: "Create config and directories for new domain",
|
|
Args: cobra.ExactArgs(1),
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
dirOverridden := cmd.Flags().Changed("dir")
|
|
return commands.NewDomainCmd(args[0], domainCertDir, dirOverridden)
|
|
},
|
|
}
|
|
newDomainCmd.Flags().StringVar(&domainCertDir, "dir", "/var/local/certman/certificates/", "Alternate directory for certificates")
|
|
rootCmd.AddCommand(newDomainCmd)
|
|
|
|
var (
|
|
modeFlag string
|
|
thinInstallFlag bool
|
|
)
|
|
installCmd := &cobra.Command{
|
|
Use: "install",
|
|
Short: "Create certman files and directories",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
switch modeFlag {
|
|
case "server", "client":
|
|
return commands.InstallCmd(thinInstallFlag, modeFlag)
|
|
default:
|
|
return fmt.Errorf("invalid --mode %q (must be server or client)", modeFlag)
|
|
}
|
|
},
|
|
}
|
|
installCmd.Flags().StringVar(&modeFlag, "mode", "client", "CertManager mode [server, client]")
|
|
installCmd.Flags().BoolVarP(&thinInstallFlag, "thin", "t", false, "Thin install (skip creating dirs)")
|
|
rootCmd.AddCommand(installCmd)
|
|
|
|
certCmd := &cobra.Command{
|
|
Use: "cert",
|
|
Short: "Certificate management",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return cmd.Help()
|
|
},
|
|
}
|
|
|
|
var noPush bool
|
|
renewCertCmd := &cobra.Command{
|
|
Use: "renew",
|
|
Short: "Renews a domains certificate",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return commands.RenewCertCmd(args[0], noPush, internal.Server)
|
|
},
|
|
}
|
|
renewCertCmd.Flags().BoolVar(&noPush, "no-push", false, "Don't push certs to repo, renew locally only [server mode only]")
|
|
certCmd.AddCommand(renewCertCmd)
|
|
|
|
updateCertLinkCmd := &cobra.Command{
|
|
Use: "update-link",
|
|
Short: "Update linked certificates",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return commands.UpdateLinksCmd(args[0])
|
|
},
|
|
}
|
|
certCmd.AddCommand(updateCertLinkCmd)
|
|
|
|
rootCmd.AddCommand(certCmd)
|
|
|
|
daemonCmd := &cobra.Command{
|
|
Use: "daemon",
|
|
Short: "Daemon management",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return cmd.Help()
|
|
},
|
|
}
|
|
|
|
daemonCmd.AddCommand(&cobra.Command{
|
|
Use: "start",
|
|
Short: "Start the daemon",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return commands.RunDaemonCmd()
|
|
},
|
|
})
|
|
|
|
daemonCmd.AddCommand(&cobra.Command{
|
|
Use: "stop",
|
|
Short: "Stop the daemon",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return commands.StopDaemonCmd()
|
|
},
|
|
})
|
|
|
|
daemonCmd.AddCommand(&cobra.Command{
|
|
Use: "reload",
|
|
Short: "Reload daemon configs",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return commands.ReloadDaemonCmd()
|
|
},
|
|
})
|
|
|
|
daemonCmd.AddCommand(&cobra.Command{
|
|
Use: "tick",
|
|
Short: "Manually triggers daemon tick",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return commands.TickDaemonCmd()
|
|
},
|
|
})
|
|
|
|
daemonCmd.AddCommand(&cobra.Command{
|
|
Use: "status",
|
|
Short: "Show daemon status",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return commands.DaemonStatusCmd()
|
|
},
|
|
})
|
|
|
|
rootCmd.AddCommand(daemonCmd)
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func basicCmd(use, short string, commandFunc func(cmd *cobra.Command, args []string)) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: use,
|
|
Short: short,
|
|
Run: commandFunc,
|
|
}
|
|
}
|
|
|
|
func IsValidFQDN(domain string) bool {
|
|
return len(domain) <= 253 && fqdnRegex.MatchString(domain)
|
|
}
|