Skip to content

Commit 4694df5

Browse files
committed
Fix #6.
Decided not to check for validity and overwrite invalid PKI. If a user is specifying their own PKI, they would most likely get upset if we replace them with our own self signed PKI.
1 parent cd6fc77 commit 4694df5

File tree

2 files changed

+106
-24
lines changed

2 files changed

+106
-24
lines changed

ssl.go

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,72 @@ import (
88
)
99

1010
// initSSL creates a SSL certificate and key using system's openssl.
11-
// TODO: don't blow away PKI if it already exists and is valid.
1211
func initSSL(certPath, keyPath string) ([]byte, error) {
1312
if out, err := initRndFile(); err != nil {
1413
return out, err
1514
}
1615

17-
fqdn, err := getFQDN()
16+
if out, err := initSSLKey(keyPath); err != nil {
17+
return out, err
18+
}
1819

19-
if err != nil {
20-
return []byte{}, err
20+
return initSSLCert(certPath, keyPath)
21+
}
22+
23+
func initSSLKey(keyPath string) (out []byte, err error) {
24+
if fileExists(keyPath) {
25+
return
2126
}
22-
if err := mkdirP(certPath); err != nil {
23-
return []byte{}, err
27+
28+
if err = mkdirP(keyPath); err != nil {
29+
return
30+
}
31+
32+
return runCommand(fmt.Sprintf("openssl genrsa -out %s 4096", keyPath))
33+
}
34+
35+
func initSSLCert(certPath, keyPath string) (out []byte, err error) {
36+
if fileExists(certPath) {
37+
return
38+
}
39+
40+
var fqdn string
41+
fqdn, err = getFQDN()
42+
43+
if err != nil {
44+
return
2445
}
25-
if err := mkdirP(keyPath); err != nil {
26-
return []byte{}, err
46+
47+
if err = mkdirP(certPath); err != nil {
48+
return
2749
}
2850

29-
command := "openssl"
3051
args := []string{
3152
"req",
3253
"-new",
33-
"-newkey",
34-
"rsa:4096",
3554
"-days",
3655
"3650",
3756
"-nodes",
3857
"-x509",
58+
"-key",
59+
keyPath,
3960
"-subj",
4061
fmt.Sprintf("/C=US/ST=Somewhere/L=Unknown/O=Idk/CN=%s", fqdn),
41-
"-keyout",
42-
keyPath,
4362
"-out",
4463
certPath,
4564
}
4665

47-
return runCommand(command + " " + strings.Join(args, " "))
66+
return runCommand("openssl " + strings.Join(args, " "))
4867
}
4968

50-
func initRndFile() ([]byte, error) {
51-
return runCommand(`openssl rand -out "$HOME/.rnd" -hex 256`)
69+
func initRndFile() (out []byte, err error) {
70+
rndPath := fmt.Sprintf("%s/.rnd", os.Getenv("HOME"))
71+
if fileExists(rndPath) {
72+
return
73+
}
74+
75+
command := fmt.Sprintf("openssl rand -out %s -hex 256", rndPath)
76+
return runCommand(command)
5277
}
5378

5479
func getFQDN() (fqdn string, err error) {
@@ -75,3 +100,13 @@ func mkdirP(p string) error {
75100

76101
return os.MkdirAll(dir, 0700)
77102
}
103+
104+
func fileExists(filename string) bool {
105+
info, err := os.Stat(filename)
106+
if os.IsNotExist(err) {
107+
return false
108+
109+
}
110+
return !info.IsDir()
111+
112+
}

ssl_test.go

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"os"
67
"os/exec"
78
"testing"
@@ -11,15 +12,10 @@ import (
1112
"github.com/pkg/errors"
1213
)
1314

14-
func fileExists(filename string) bool {
15-
info, err := os.Stat(filename)
16-
if os.IsNotExist(err) {
17-
return false
18-
19-
}
20-
return !info.IsDir()
21-
15+
func createFile(path, content string) error {
16+
return ioutil.WriteFile(path, []byte(content), 0644)
2217
}
18+
2319
func TestInitSSL(t *testing.T) {
2420
id, _ := uuid.NewUUID()
2521

@@ -54,3 +50,54 @@ func TestInitSSL(t *testing.T) {
5450

5551
os.RemoveAll("/tmp/bashrpc")
5652
}
53+
54+
func TestExistingSSLKey(t *testing.T) {
55+
keyPath := "/tmp/test-ssl.key"
56+
Given("an SSL key that already exists")
57+
if err := createFile(keyPath, "I am a key"); err != nil {
58+
t.Error(err)
59+
}
60+
61+
When("initializing SSL key")
62+
if _, err := initSSLKey(keyPath); err != nil {
63+
t.Error(err)
64+
}
65+
66+
Then("it should NOT be overwritten")
67+
key, err := ioutil.ReadFile(keyPath)
68+
if err != nil {
69+
t.Error(err)
70+
}
71+
72+
Assert(string(key), "I am a key", t)
73+
74+
os.Remove(keyPath)
75+
}
76+
77+
func TestExistingSSLCert(t *testing.T) {
78+
keyPath := "/tmp/test-ssl.key"
79+
certPath := "/tmp/test-ssl.cert"
80+
Given("an SSL cert that already exists")
81+
if err := createFile(keyPath, "I am a key"); err != nil {
82+
t.Error(err)
83+
}
84+
if err := createFile(certPath, "I am a cert"); err != nil {
85+
t.Error(err)
86+
}
87+
88+
When("initializing SSL cert")
89+
if _, err := initSSLCert(certPath, keyPath); err != nil {
90+
t.Error(err)
91+
}
92+
93+
Then("it should NOT be overwritten")
94+
cert, err := ioutil.ReadFile(certPath)
95+
if err != nil {
96+
t.Error(err)
97+
}
98+
99+
Assert(string(cert), "I am a cert", t)
100+
101+
os.Remove(keyPath)
102+
os.Remove(certPath)
103+
}

0 commit comments

Comments
 (0)