-
Notifications
You must be signed in to change notification settings - Fork 55
/
cert_auth_header.go
50 lines (41 loc) · 1.17 KB
/
cert_auth_header.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
package helpers
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"time"
. "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file
)
func CreateCertificatePEM() []byte {
certPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
Expect(err).NotTo(HaveOccurred())
cert := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"cf-on-k8s"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 180),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
certBytes, err := x509.CreateCertificate(rand.Reader, cert, cert, &certPrivKey.PublicKey, certPrivKey)
Expect(err).NotTo(HaveOccurred())
out := new(bytes.Buffer)
err = pem.Encode(out, &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
Expect(err).NotTo(HaveOccurred())
err = pem.Encode(out, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
})
Expect(err).NotTo(HaveOccurred())
return out.Bytes()
}