Skip to content

Commit 9b24551

Browse files
committed
inital commit
0 parents  commit 9b24551

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
docker-compose.env
2+
cmd/mediatord/certify
3+
builds/Darwin/certify

Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
BUILD_FLAGS := "-s -w"
2+
SYSTEM = `uname -s`
3+
default: clean build
4+
5+
linux-binary:
6+
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags $(BUILD_FLAGS) -o builds/Linux/certify cmd/certify/main.go
7+
8+
osx-binary:
9+
@CGO_ENABLED=0 GOOS=darwin go build -a -installsuffix cgo -ldflags $(BUILD_FLAGS) -o builds/Darwin/certify cmd/certify/main.go
10+
11+
build:
12+
@go build -a -ldflags $(BUILD_FLAGS) -o builds/$(SYSTEM)/certify cmd/certify/main.go
13+
14+
install: build mv-bin
15+
16+
mv-bin:
17+
@cp builds/${SYSTEM}/certify ${GOPATH}/bin/
18+
19+
clean:
20+
rm -f cmd/certify/certify
21+
rm -f builds/Darwin/certify
22+
rm -f builds/Linux/certify
23+
24+
restore:
25+
godep restore
26+
27+
depsave:
28+
rm -f Godeps/Godeps.json
29+
godep save
30+
31+
test:
32+
go test -cover ./...
33+
34+
container: clean linux-binary
35+
docker build -t quay.io/devx/certify:latest .
36+
37+
release: clean restore test osx-binary linux-binary

builds/Darwin/.deleteme

Whitespace-only changes.

builds/Linux/.deleteme

Whitespace-only changes.

cmd/certify/main.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"crypto/tls"
6+
"encoding/json"
7+
"flag"
8+
"fmt"
9+
"io/ioutil"
10+
"log"
11+
"net/http"
12+
"os"
13+
"strings"
14+
)
15+
16+
var (
17+
url = flag.String("url", "https://localhost", "CFSSL URL")
18+
username = flag.String("user", "", "user name to use for basic auth")
19+
password = flag.String("password", "", "password to use for basic auth")
20+
verifySSL = flag.Bool("verifySSL", true, "Verify certificate chain")
21+
certType = flag.String("type", "client-server", "The certificatle type to request: server, client, client-server")
22+
certName = flag.String("name", "", "Only used for client certificates, for server and client-server we use the hostname as the identifier.")
23+
caCertURL = flag.String("ca_cert_url", "", "Specify an url where to downlaod the CA's certificate")
24+
dir = flag.String("dir", "/etc/certificates", "directory where to store the certificates")
25+
force = flag.Bool("force", false, "If certificates exist, overwrite them by requesting new certificates")
26+
)
27+
28+
func main() {
29+
30+
flag.Parse()
31+
32+
if ok := reqClientCert(); !ok {
33+
log.Fatal("Failed to get retrieve a new certificate from cfssl")
34+
}
35+
os.Exit(0)
36+
}
37+
38+
func reqClientCert() bool {
39+
log.Println("Requesting New certificates")
40+
jsonStr := ""
41+
42+
hostname, _ := os.Hostname()
43+
44+
switch *certType {
45+
case "client":
46+
if _, err := os.Stat(fmt.Sprintf("%s/%s.pem", *dir, *certName)); err == nil {
47+
if !*force {
48+
fmt.Println("File already exists exiting")
49+
os.Exit(0)
50+
}
51+
}
52+
jsonStr = fmt.Sprintf("{ \"profile\": \"%s\", \"request\": { \"CN\": \"%s\", \"hosts\": [\"\"], \"key\": { \"algo\": \"rsa\", \"size\": 2048 }, \"names\": [ { \"C\": \"US\", \"L\": \"San Antonio\", \"O\": \"test\", \"OU\": \"kumoru.org\", \"ST\": \"Texas\" } ] } } ", *certType, *certName)
53+
case "server", "client-server":
54+
if _, err := os.Stat(fmt.Sprintf("%s/%s.pem", *dir, hostname)); err == nil {
55+
if !*force {
56+
fmt.Println("File already exists exiting")
57+
os.Exit(0)
58+
}
59+
}
60+
jsonStr = fmt.Sprintf("{ \"profile\": \"%s\", \"request\": { \"CN\": \"%s\", \"hosts\": [\"%s\"], \"key\": { \"algo\": \"rsa\", \"size\": 2048 }, \"names\": [ { \"C\": \"US\", \"L\": \"San Antonio\", \"O\": \"test\", \"OU\": \"kumoru.org\", \"ST\": \"Texas\" } ] } } ", *certType, hostname, hostname)
61+
}
62+
63+
tr := &http.Transport{
64+
TLSClientConfig: &tls.Config{
65+
MaxVersion: tls.VersionTLS11,
66+
InsecureSkipVerify: *verifySSL,
67+
},
68+
}
69+
70+
user := ""
71+
pass := ""
72+
73+
if os.Getenv("CFSSL_USERNAME") != "" {
74+
user = os.Getenv("CFSSL_USERNAME")
75+
pass = os.Getenv("CFSSL_PASSWORD")
76+
}
77+
78+
if *username != "" {
79+
user = *username
80+
pass = *password
81+
}
82+
83+
if !strings.Contains("https", *url) {
84+
fmt.Println("Please provide an https url")
85+
os.Exit(1)
86+
}
87+
88+
req, _ := http.NewRequest("POST", "https://ca.kumoru.org/api/v1/cfssl/newcert",
89+
bytes.NewBuffer([]byte(jsonStr))) // TODO: Replace host with variable
90+
91+
if user != "" {
92+
req.SetBasicAuth(user, pass)
93+
}
94+
95+
client := &http.Client{Transport: tr}
96+
resp, err := client.Do(req)
97+
if err != nil {
98+
fmt.Printf("%s\n", err)
99+
os.Exit(1)
100+
}
101+
body, _ := ioutil.ReadAll(resp.Body)
102+
defer resp.Body.Close()
103+
104+
r := new(CfsslResponse)
105+
if err = json.Unmarshal(body, &r); err != nil {
106+
log.Fatal("Failed to generage CFSLL Certificate")
107+
}
108+
109+
fmt.Printf("CFSSL response: %+v\n", r)
110+
111+
if err := ioutil.WriteFile("/etc/certificates/client.pem", []byte(r.Result.Certificate), 0644); err != nil {
112+
return false
113+
}
114+
if err := ioutil.WriteFile("/etc/certificates/client-key.pem", []byte(r.Result.PrivateKey), 0644); err != nil {
115+
return false
116+
}
117+
118+
if getCAcert() != nil {
119+
return false
120+
}
121+
122+
return true
123+
}
124+
125+
func getCAcert() error {
126+
caURL := ""
127+
128+
if os.Getenv("CA_CERT_URL") != "" {
129+
caURL = os.Getenv("CA_CERT_URL")
130+
}
131+
132+
if *caCertURL != "" {
133+
caURL = *caCertURL
134+
135+
}
136+
137+
if caURL == "" {
138+
return nil
139+
}
140+
141+
resp, err := http.Get(os.Getenv("CA_CERT_URL")) // TODO: Replace host with variable
142+
if err != nil {
143+
panic(err.Error())
144+
}
145+
body, _ := ioutil.ReadAll(resp.Body)
146+
defer resp.Body.Close()
147+
148+
return ioutil.WriteFile("/etc/certificates/ca.pem", body, 0644)
149+
150+
}
151+
152+
// CfsslResponse struct
153+
type CfsslResponse struct {
154+
Success bool `json:"success"`
155+
Result struct {
156+
Certificate string `json:"certificate"`
157+
CertificateRequest string `json:"certificate_request"`
158+
PrivateKey string `json:"private_key"`
159+
Sums struct {
160+
Certificate struct {
161+
Md5 string `json:"md5"`
162+
Sha1 string `json:"sha-1"`
163+
} `json:"certificate"`
164+
CertificateRequest struct {
165+
Md5 string `json:"md5"`
166+
Sha1 string `json:"sha-1"`
167+
} `json:"certificate_request"`
168+
} `json:"sums"`
169+
} `json:"result"`
170+
Errors []interface{} `json:"errors"`
171+
Messages []interface{} `json:"messages"`
172+
}

test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
client one http://192.123.12.123:2379 client two http://192.123.12.123:2379, client 3 http://192.123.12.123:2379

0 commit comments

Comments
 (0)