Partial implementation of CA database.
This commit is contained in:
84
main.go
84
main.go
@@ -9,6 +9,9 @@ import (
|
||||
|
||||
var Version = "dev"
|
||||
|
||||
// Global CA state variable
|
||||
var GlobalCAState *CAState
|
||||
|
||||
func main() {
|
||||
var configPath string
|
||||
var overwrite bool
|
||||
@@ -30,7 +33,7 @@ func main() {
|
||||
},
|
||||
}
|
||||
|
||||
var initcaCmd = &cobra.Command{
|
||||
var initCmd = &cobra.Command{
|
||||
Use: "initca",
|
||||
Short: "Generate a new CA certificate and key",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
@@ -38,85 +41,14 @@ func main() {
|
||||
},
|
||||
}
|
||||
|
||||
initcaCmd.Flags().StringVar(&configPath, "config", "ca_config.hcl", "Path to CA configuration file")
|
||||
initcaCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Allow overwriting existing files")
|
||||
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 fromFile != "" {
|
||||
certDefs, defaults, err := LoadCertificatesFile(fromFile)
|
||||
if err != nil {
|
||||
fmt.Printf("Error loading certificates file: %v\n", err)
|
||||
return
|
||||
}
|
||||
successes := 0
|
||||
errors := 0
|
||||
for i, def := range certDefs {
|
||||
if defaults != nil {
|
||||
if def.Type == "" {
|
||||
def.Type = defaults.Type
|
||||
}
|
||||
if def.Validity == "" {
|
||||
def.Validity = defaults.Validity
|
||||
}
|
||||
if len(def.SAN) == 0 && len(defaults.SAN) > 0 {
|
||||
def.SAN = defaults.SAN
|
||||
}
|
||||
}
|
||||
finalDef := renderCertificateDefTemplates(def, defaults)
|
||||
|
||||
fmt.Printf("[%d/%d] Issuing %s... ", i+1, len(certDefs), finalDef.Name)
|
||||
|
||||
if dryRun {
|
||||
fmt.Printf("(dry run)\n")
|
||||
}
|
||||
|
||||
if verbose {
|
||||
fmt.Printf("\n Name: %s\n", finalDef.Name)
|
||||
fmt.Printf(" Subject: %s\n", finalDef.Subject)
|
||||
fmt.Printf(" Type: %s\n", finalDef.Type)
|
||||
fmt.Printf(" Validity: %s\n", finalDef.Validity)
|
||||
fmt.Printf(" SAN: %v\n\n", finalDef.SAN)
|
||||
}
|
||||
|
||||
basename := finalDef.Name + "." + finalDef.Type
|
||||
|
||||
if dryRun {
|
||||
successes++
|
||||
continue
|
||||
}
|
||||
|
||||
err := IssueCertificateWithBasename(configPath, basename, finalDef.Subject, finalDef.Type, finalDef.Validity, finalDef.SAN, overwrite, dryRun)
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
errors++
|
||||
} else {
|
||||
if !verbose {
|
||||
fmt.Printf("done\n")
|
||||
}
|
||||
successes++
|
||||
}
|
||||
}
|
||||
fmt.Printf("Batch complete: %d succeeded, %d failed.\n", successes, errors)
|
||||
return
|
||||
}
|
||||
// Simple mode
|
||||
subjectName := subject
|
||||
if subjectName == "" {
|
||||
subjectName = name
|
||||
}
|
||||
finalDef := renderCertificateDefTemplates(CertificateDef{Name: name, Subject: subject, Type: certType, Validity: validity, SAN: san}, nil)
|
||||
if verbose {
|
||||
fmt.Printf("\nCertificate:\n")
|
||||
fmt.Printf(" Name: %s\n", finalDef.Name)
|
||||
fmt.Printf(" Subject: %s\n", finalDef.Subject)
|
||||
fmt.Printf(" Type: %s\n", finalDef.Type)
|
||||
fmt.Printf(" Validity: %s\n", finalDef.Validity)
|
||||
fmt.Printf(" SAN: %v\n", finalDef.SAN)
|
||||
}
|
||||
IssueCertificate(configPath, finalDef.Name, finalDef.Subject, finalDef.Type, finalDef.Validity, finalDef.SAN, overwrite)
|
||||
IssueCertificate(configPath, subject, certType, validity, san, name, fromFile, overwrite, dryRun, verbose)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -146,7 +78,7 @@ func main() {
|
||||
fmt.Printf("lab-ca version: %s\n", Version)
|
||||
},
|
||||
}
|
||||
rootCmd.AddCommand(initcaCmd)
|
||||
rootCmd.AddCommand(initCmd)
|
||||
rootCmd.AddCommand(issueCmd)
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
|
||||
|
Reference in New Issue
Block a user