This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
certs.go
124 lines (108 loc) · 3.34 KB
/
certs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package agent
import (
"crypto/rand"
"crypto/rsa"
_ "crypto/sha1"
_ "crypto/sha256"
_ "crypto/sha512"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"io/ioutil"
"math/big"
"net"
"os"
"strings"
"time"
"github.com/docker/dockercloud-agent/utils"
)
func CreateCerts(keyFilePath, certFilePath, host string) {
if !isCertificateExist(keyFilePath, certFilePath) {
if host == "" {
os.RemoveAll(AgentPidFile)
Logger.Fatal("CertCommonName is empty. This may be caused by a failed node registration with Docker Cloud")
}
genCetificate(keyFilePath, certFilePath, host)
Logger.Println("New TLS certificates generated")
}
}
func isCertificateExist(keyFilePath, certFilePath string) (isExist bool) {
if utils.FileExist(keyFilePath) && utils.FileExist(certFilePath) {
return true
}
return false
}
func genCetificate(keyFilePath, certFilePath, host string) {
validFor := 10 * 365 * 24 * time.Hour
isCA := true
rsaBits := 2048
priv, err := rsa.GenerateKey(rand.Reader, rsaBits)
if err != nil {
SendError(err, "Fatal: Failed to generate private key", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatalf("Failed to generate private key: %s", err)
}
notBefore := time.Now()
notAfter := notBefore.Add(validFor)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
SendError(err, "Fatal: Failed to generate serial number", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatalf("Failed to generate serial number: %s", err)
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Docker Cloud Self-Signed Host"},
CommonName: host,
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
hosts := strings.Split(host, ",")
for _, h := range hosts {
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}
if isCA {
template.IsCA = true
template.KeyUsage |= x509.KeyUsageCertSign
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
SendError(err, "Fatal: Failed to create certificate", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatalf("Failed to create certificate: %s", err)
}
certOut, err := os.Create(certFilePath)
if err != nil {
SendError(err, "Fatal: Failed to open cert.pem for writing", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatalf("Failed to open cert.pem for writing: %s", err)
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close()
keyOut, err := os.OpenFile(keyFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
SendError(err, "Fatal: Failed to open key.pem for writing", nil)
os.RemoveAll(AgentPidFile)
Logger.Fatalf("Failed to open key.pem for writing:", err)
}
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
keyOut.Close()
}
func GetCertificate(certFilePath string) (*string, error) {
content, err := ioutil.ReadFile(certFilePath)
if err != nil {
return nil, err
}
cert := string(content[:])
return &cert, nil
}