Skip to content

Commit

Permalink
Adding remaining oneshot functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brimstone committed Dec 2, 2015
1 parent 0dda72d commit 8691d9e
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
101 changes: 98 additions & 3 deletions sbuca.go
Expand Up @@ -284,6 +284,18 @@ func main() {
Name: "key",
Usage: "Path to private key file",
},
cli.StringFlag{
Name: "crt",
Usage: "Path to public cert file",
},
cli.StringFlag{
Name: "ca",
Usage: "Path to public ca file",
},
cli.StringFlag{
Name: "token",
Usage: "Authorization Token",
},
},
Usage: "Generate key, request, and submit to a server, all in one shot",
Action: func(c *cli.Context) {
Expand All @@ -292,13 +304,42 @@ func main() {
fmt.Fprintln(os.Stderr, "Path to private key required")
return
}
crtpath := c.String("crt")
if crtpath == "" {
fmt.Fprintln(os.Stderr, "Path to public certificate required")
return
}
capath := c.String("ca")
if capath == "" {
fmt.Fprintln(os.Stderr, "Path to central authority required")
return
}
host := c.String("host")
if host == "" {
fmt.Fprintln(os.Stderr, "Location of host required")
return
}
keyfile, err := os.Create(keypath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer keyfile.Close()

crtfile, err := os.Create(crtpath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer crtfile.Close()

cafile, err := os.Create(capath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer cafile.Close()

// genkey
key, err := pkix.NewKey()
if err != nil {
Expand All @@ -318,9 +359,63 @@ func main() {
}
fmt.Println("Key written to", keypath)

//TODO gencsr
//TODO submitcsr
//TODO getcacrt
// gencsr
csr, err := pkix.NewCertificateRequest(key)
if err != nil {
fmt.Fprintln(os.Stderr, "[ERROR] Failed to generate CSR: "+err.Error())
return
}
csrpem, err := csr.ToPEM()
// submitcsr
data := make(url.Values)
data.Add("csr", string(csrpem))

//resp, err := http.PostForm("http://"+host+"/certificates", data)
client := &http.Client{}
req, _ := http.NewRequest("POST", "http://"+host+"/certificates", strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if c.String("token") != "" {
req.Header.Set("X-API-KEY", c.String("token"))
}
resp, err := client.Do(req)
if err != nil {
fmt.Fprintln(os.Stderr, "[ERROR] Failed to request: "+err.Error())
return
}
decoder := json.NewDecoder(resp.Body)
respData := make(map[string]map[string]interface{})
if err := decoder.Decode(&respData); err != nil {
panic(err)
}

n, err = io.WriteString(crtfile, string(respData["certificate"]["crt"].(string)))
if err != nil {
fmt.Println(n, err)
}
fmt.Println("Certificate written to", crtpath)

// getcacrt
resp, err = http.Get("http://" + host + "/ca/certificate")
if err != nil {
fmt.Fprintln(os.Stderr, "[ERROR] Failed to request CA cert: "+err.Error())
os.Exit(1)
}

decoder = json.NewDecoder(resp.Body)
if resp.StatusCode != 200 {
fmt.Fprintln(os.Stderr, "[ERROR] Failed to request CA cert: "+resp.Status)
os.Exit(resp.StatusCode)
}
respData = make(map[string]map[string]interface{})
if err := decoder.Decode(&respData); err != nil {
panic(err)
}

n, err = io.WriteString(cafile, string(respData["ca"]["crt"].(string)))
if err != nil {
fmt.Println(n, err)
}
fmt.Println("Certificate Authority written to", capath)
},
},
}
Expand Down
12 changes: 11 additions & 1 deletion sbuca_test.go
Expand Up @@ -39,6 +39,16 @@ func Test_main_genkey(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

os.Args = []string{"sbuca", "oneshot", "--key", "server.key"}
os.Args = []string{"sbuca",
"oneshot",
"--key",
"server.key",
"--crt",
"server.crt",
"--ca",
"ca.crt",
"--host",
"localhost:8600",
}
main()
}

0 comments on commit 8691d9e

Please sign in to comment.