add test for create_certs args

add test for create_certs args
This commit is contained in:
Brian Martin 2019-06-10 13:49:39 -04:00
parent d3db3e35e3
commit 990a93e583
2 changed files with 65 additions and 23 deletions

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"bufio" "bufio"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
@ -10,47 +9,50 @@ import (
"crypto/x509" "crypto/x509"
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/pem" "encoding/pem"
"flag"
"fmt"
"log" "log"
"math/big" "math/big"
"net" "net"
"os" "os"
"time"
"flag"
"path/filepath" "path/filepath"
"time"
) )
var InBumperSan string
var OutCertDirectory string
var inBumperSan string func setFlags() {
var outCertDirectory string
func main() {
exePath, _ := os.Executable() exePath, _ := os.Executable()
currentDir, _ := os.Getwd() currentDir, _ := os.Getwd()
//certPath will be current working directory by default //certPath will be current working directory by defaultß
certPath, err := filepath.Abs(currentDir) certPath, err := filepath.Abs(currentDir)
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
} }
//sanPath will be exe path /Bumper_SAN.txt by default //sanPath will be exe path /Bumper_SAN.txt by default
sanPath, err := filepath.Abs(filepath.Join(exePath,"..","/Bumper_SAN.txt")) sanPath, err := filepath.Abs(filepath.Join(exePath, "..", "/Bumper_SAN.txt"))
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
} }
var inBumperSan string flag.StringVar(&InBumperSan, "inSAN", sanPath, "Input file containing a list of Subject Alternate Names (line separated)")
var outCertDirectory string flag.StringVar(&OutCertDirectory, "out", certPath, "Directory to output certificates to")
flag.StringVar(&inBumperSan, "inSAN", sanPath, "Input file containing a list of Subject Alternate Names (line separated)")
flag.StringVar(&outCertDirectory, "out", certPath, "Directory to output certificates to")
flag.Parse() flag.Parse()
}
func main() {
setFlags()
fmt.Printf("-------- Create_Certs --------\n") fmt.Printf("-------- Create_Certs --------\n")
//get absolute path //get absolute path
outCertDirectory, _ = filepath.Abs(outCertDirectory) outCertDirectory, _ := filepath.Abs(OutCertDirectory)
dexists, isdfile := pathExistsType(outCertDirectory) dexists, isdfile := pathExistsType(outCertDirectory)
if !dexists { if !dexists {
log.Fatalf("Certs directory doesn't exist: %v", outCertDirectory) log.Fatalf("Certs directory doesn't exist: %v", outCertDirectory)
@ -60,7 +62,7 @@ func main() {
} }
//get absolute path //get absolute path
inBumperSan, _ = filepath.Abs(inBumperSan) inBumperSan, _ := filepath.Abs(InBumperSan)
bexists, isbfile := pathExistsType(inBumperSan) bexists, isbfile := pathExistsType(inBumperSan)
if !bexists { if !bexists {
log.Printf("Bumper SAN doesn't exist, certificate won't contain Subject Alternate Names: %v\n", inBumperSan) log.Printf("Bumper SAN doesn't exist, certificate won't contain Subject Alternate Names: %v\n", inBumperSan)
@ -108,31 +110,30 @@ func make_CA(outCertDirectory string) {
log.Fatalf("Create ca failed: %v", err) log.Fatalf("Create ca failed: %v", err)
} }
// Public key // Public key
certOut, err := os.Create(filepath.Join(outCertDirectory,"ca.crt")) certOut, err := os.Create(filepath.Join(outCertDirectory, "ca.crt"))
if err != nil { if err != nil {
log.Fatalf("Create ca.crt failed: %v", err) log.Fatalf("Create ca.crt failed: %v", err)
} }
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: ca_b}) pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: ca_b})
certOut.Close() certOut.Close()
log.Printf("ca.crt created at %v\n", filepath.Join(outCertDirectory,"ca.crt")) log.Printf("ca.crt created at %v\n", filepath.Join(outCertDirectory, "ca.crt"))
// Private key // Private key
keyOut, err := os.OpenFile(filepath.Join(outCertDirectory,"ca.key"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) keyOut, err := os.OpenFile(filepath.Join(outCertDirectory, "ca.key"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil { if err != nil {
log.Fatalf("Create ca.key failed: %v", err) log.Fatalf("Create ca.key failed: %v", err)
} }
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}) pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
keyOut.Close() keyOut.Close()
log.Printf("ca.key created at %v\n",filepath.Join(outCertDirectory,"ca.key") ) log.Printf("ca.key created at %v\n", filepath.Join(outCertDirectory, "ca.key"))
} }
func signCert(outCertDirectory string, inBumperSan string) { func signCert(outCertDirectory string, inBumperSan string) {
fmt.Printf("-------- Creating Server Cert --------\n") fmt.Printf("-------- Creating Server Cert --------\n")
// Load CA // Load CA
catls, err := tls.LoadX509KeyPair(filepath.Join(outCertDirectory,"ca.crt"), filepath.Join(outCertDirectory,"ca.key")) catls, err := tls.LoadX509KeyPair(filepath.Join(outCertDirectory, "ca.crt"), filepath.Join(outCertDirectory, "ca.key"))
if err != nil { if err != nil {
log.Fatalf("Error loading ca cert: %v", err) log.Fatalf("Error loading ca cert: %v", err)
} }
@ -170,7 +171,7 @@ func signCert(outCertDirectory string, inBumperSan string) {
dnsNames := []string{hostname, "localhost"} dnsNames := []string{hostname, "localhost"}
//Read SANs from file //Read SANs from file
//get absolute path //get absolute path
bexists, isbfile := pathExistsType(inBumperSan) bexists, isbfile := pathExistsType(inBumperSan)
if !bexists { if !bexists {
log.Print("Bumper SAN doesn't exist, certificate won't contain Subject Alternate Names") log.Print("Bumper SAN doesn't exist, certificate won't contain Subject Alternate Names")
@ -207,7 +208,7 @@ func signCert(outCertDirectory string, inBumperSan string) {
} }
// Sign the certificate // Sign the certificate
cert_b, err := x509.CreateCertificate(rand.Reader, &template, ca, pubKey, catls.PrivateKey) cert_b, err := x509.CreateCertificate(rand.Reader, &template, ca, pubKey, catls.PrivateKey)
// Public key // Public key
certOut, err := os.Create(filepath.Join(outCertDirectory, "bumper.crt")) certOut, err := os.Create(filepath.Join(outCertDirectory, "bumper.crt"))

View file

@ -0,0 +1,41 @@
package main
import (
"flag"
"os"
"testing"
)
func TestArgs(t *testing.T) {
orArgs := os.Args
flag.CommandLine = flag.NewFlagSet(orArgs[0], flag.ContinueOnError)
os.Args = []string{"cmd", "-inSAN", "123"}
setFlags()
if InBumperSan != "123" {
t.Error("InBumperSan not set by arg")
}
flag.CommandLine = flag.NewFlagSet(orArgs[0], flag.ContinueOnError)
os.Args = []string{"cmd", "-out", "456"}
setFlags()
if OutCertDirectory != "456" {
t.Error("Out path not set by arg")
}
flag.CommandLine = flag.NewFlagSet(orArgs[0], flag.ContinueOnError)
os.Args = []string{"cmd", "-inSAN", "san1", "-out", "out2"}
setFlags()
if InBumperSan != "san1" {
t.Error("InBumperSan not set by arg")
}
if OutCertDirectory != "out2" {
t.Error("Out path not set by arg")
}
os.Args = orArgs
}