-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
100 lines (86 loc) · 2.41 KB
/
main.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"flag"
"log"
"math/big"
"os"
"path/filepath"
"strings"
"time"
)
var (
host = flag.String("host", "coilv2-webhook-service.kube-system.svc", "TLS hostname")
validFor = flag.Duration("duration", 36500*24*time.Hour, "Duration that certificate is valid for")
outDir = flag.String("outdir", ".", "Directory where the certificate files are created")
)
func main() {
flag.Parse()
priv, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
log.Fatal(err)
}
keyUsage := x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment
notBefore := time.Now()
notAfter := notBefore.Add(*validFor)
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: "coilv2-webhook-service",
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: keyUsage,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: dnsAliases(*host),
}
certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, priv.Public(), priv)
if err != nil {
log.Fatalf("failed to create certificate: %v", err)
}
_, err = os.Stat(*outDir)
switch {
case err == nil:
case os.IsNotExist(err):
err = os.MkdirAll(*outDir, 0755)
if err != nil {
log.Fatalf("failed to create output directory: %v", err)
}
default:
log.Fatalf("stat %s failed: %v", *outDir, err)
}
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
log.Fatalf("failed to marshal private key: %v", err)
}
outputPEM(filepath.Join(*outDir, "cert.pem"), "CERTIFICATE", certBytes)
outputPEM(filepath.Join(*outDir, "key.pem"), "PRIVATE KEY", privBytes)
}
func dnsAliases(host string) []string {
parts := strings.Split(host, ".")
aliases := make([]string, len(parts))
for i := 0; i < len(parts); i++ {
aliases[i] = strings.Join(parts[0:len(parts)-i], ".")
}
return aliases
}
func outputPEM(fname string, pemType string, data []byte) {
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("failed to open %s: %v", fname, err)
}
defer f.Close()
err = pem.Encode(f, &pem.Block{Type: pemType, Bytes: data})
if err != nil {
log.Fatalf("failed to encode: %v", err)
}
err = f.Sync()
if err != nil {
log.Fatalf("failed to fsync: %v", err)
}
}