Commit 67b9cc08 authored by Heiko Reese's avatar Heiko Reese
Browse files

Handle outdir as text.Template

parent 688be215
......@@ -26,8 +26,6 @@ import (
"crypto/x509"
"encoding/pem"
"path/filepath"
"time"
"net"
"net/url"
......@@ -35,6 +33,8 @@ import (
"github.com/kennygrant/sanitize"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"text/template"
"github.com/davecgh/go-spew/spew"
)
type sanGenerator struct {
......@@ -58,10 +58,8 @@ var requestCmd *cobra.Command = &cobra.Command{
outputdir string
)
viper.BindPFlags(cmd.PersistentFlags())
//spew.Dump(viper.AllSettings())
//spew.Dump(cmd.Args)
//spew.Dump(cmdArguments)
//os.Exit(0)
// write config values into cmdArguments
viper.Unmarshal(&cmdArguments)
// check ca name
if !viper.IsSet("ca") {
......@@ -89,16 +87,30 @@ var requestCmd *cobra.Command = &cobra.Command{
log.Fatal("Unknown profile ", profile)
}
// set default outpath
// set default outpath and filename
cmdArguments.CommonNameSanitized = sanitize.Name(commonName)
basefilename = sanitize.Name(commonName)
timestamp := time.Now().Format("_2006-02-01T15_04_05")
outputdir = viper.GetString("outdir")
// create output path
if outputdir == "" {
outputdir = basefilename + timestamp
// generate output directory name
spew.Dump(viper.GetString("outdir"))
if !viper.IsSet("outdir") {
outputdir = basefilename + "_" + cmdArguments.TimeStamp
viper.Set("outdir", outputdir)
} else {
directoryTemplate, err := template.New("outdir").Parse(viper.GetString("outdir"))
if err != nil {
// last chance fallback…
outputdir = viper.GetString("outdir")
}
outdirwriter := strings.Builder{}
directoryTemplate.Execute(&outdirwriter, cmdArguments)
if err != nil {
// last chance fallback…
outputdir = viper.GetString("outdir")
}
outputdir = outdirwriter.String()
}
// create output path
err = os.MkdirAll(outputdir, 0700)
if err != nil {
log.Fatalf("Unable to create output directory %s: %s", outputdir, err)
......@@ -153,7 +165,7 @@ var requestCmd *cobra.Command = &cobra.Command{
if err != nil {
log.Fatalf("Unable to write private to »%s« as PKCS1/DER: %s", filename, err)
} else {
log.Printf("Wrote private to »%s« (format: PKCS1/DER)", filename)
log.Printf("Wrote private key to »%s« (format: PKCS1/DER)", filename)
}
// save key as pkcs1/pem
pemkey := pem.EncodeToMemory(&pem.Block{
......@@ -165,7 +177,7 @@ var requestCmd *cobra.Command = &cobra.Command{
if err != nil {
log.Fatalf("Unable to write private to »%s« as PKCS1/PEM: %s", filename, err)
} else {
log.Printf("Wrote private to »%s« (format: PKCS1/PEM)", filename)
log.Printf("Wrote private key to »%s« (format: PKCS1/PEM)", filename)
}
}
......@@ -327,7 +339,7 @@ func init() {
rootCmd.AddCommand(requestCmd)
// Here you will define your flags and configuration settings.
// flags and configuration settings
requestCmd.PersistentFlags().SortFlags = false
requestCmd.PersistentFlags().StringVar(&cmdArguments.Profile, "profile", "Web Server", "Certificate profile")
requestCmd.PersistentFlags().StringVar(&cmdArguments.CommonName, "cn", "", "Set »CommonName« (common name) part of distinguished name")
......@@ -349,7 +361,7 @@ func init() {
requestCmd.MarkFlagFilename("keyfile", "")
requestCmd.PersistentFlags().StringVarP(&cmdArguments.Keypass, "keypass", "p", "", "Password if secret key is encrypted")
requestCmd.PersistentFlags().IntVar(&cmdArguments.Keysize, "keysize", 4096, "Size of secret key in bits (only used if --keyfile is not set; minimum size 2048 bits)")
requestCmd.PersistentFlags().StringVarP(&cmdArguments.Outdir, "outdir", "o", "", "Directory for all output files")
requestCmd.PersistentFlags().StringVarP(&cmdArguments.Outdir, "outdir", "o", "{{-.CommonNameSanitized}}_{{-.TimeStamp}}", "Directory for all output files")
requestCmd.PersistentFlags().BoolVarP(&cmdArguments.DryRun, "dry-run", "n", false, "Only show request data, don't execute anything")
requestCmd.PersistentFlags().BoolVarP(&cmdArguments.SkipPDF, "skip-pdf", "s", false, "Don't fetch pdf after request")
}
......@@ -37,6 +37,8 @@ import (
"log"
"time"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
......@@ -59,32 +61,35 @@ var (
TraverseChildren: true,
}
// see also: Zertifizierungsrichtlinie der DFN-PKI-Sicherheitsniveau Global, Version:3.8, (OID): 1.3.6.1.4.1.22177.300.1.1.4.3.8
cmdArguments struct {
CAName string
RAId int
Profile string
CommonName string
Organisation string
OU []string
Locality string
State string
Country string
DNS []string
IP []string
URI []string
Email []string
RequesterName string
RequesterEmail string
RequesterOU string
Keyfile string
Keypass string
Keysize int
PIN string
Outdir string
SkipPDF bool
Publish bool
DryRun bool
cmdArguments = struct {
CAName string
RAId int
Profile string
CommonName string
Organisation string
OU []string
Locality string
State string
Country string
DNS []string
IP []string
URI []string
Email []string
RequesterName string
RequesterEmail string
RequesterOU string
Keyfile string
Keypass string
Keysize int
PIN string
Outdir string
SkipPDF bool
Publish bool
DryRun bool
TimeStamp string
CommonNameSanitized string
}{
TimeStamp: time.Now().Format("2006-01-02T15_04_05"),
}
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment