Last active
October 21, 2020 11:27
-
-
Save velotiotech/2e0cfd15043513d253cad7c9126d2026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
cryptorand "crypto/rand" | |
"crypto/rsa" | |
"crypto/x509" | |
"crypto/x509/pkix" | |
"encoding/pem" | |
"fmt" | |
log "github.com/sirupsen/logrus" | |
"math/big" | |
"os" | |
"time" | |
) | |
func main() { | |
var caPEM, serverCertPEM, serverPrivKeyPEM *bytes.Buffer | |
// CA config | |
ca := &x509.Certificate{ | |
SerialNumber: big.NewInt(2020), | |
Subject: pkix.Name{ | |
Organization: []string{"velotio.com"}, | |
}, | |
NotBefore: time.Now(), | |
NotAfter: time.Now().AddDate(1, 0, 0), | |
IsCA: true, | |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, | |
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | |
BasicConstraintsValid: true, | |
} | |
// CA private key | |
caPrivKey, err := rsa.GenerateKey(cryptorand.Reader, 4096) | |
if err != nil { | |
fmt.Println(err) | |
} | |
// Self signed CA certificate | |
caBytes, err := x509.CreateCertificate(cryptorand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey) | |
if err != nil { | |
fmt.Println(err) | |
} | |
// PEM encode CA cert | |
caPEM = new(bytes.Buffer) | |
_ = pem.Encode(caPEM, &pem.Block{ | |
Type: "CERTIFICATE", | |
Bytes: caBytes, | |
}) | |
dnsNames := []string{"webhook-service", | |
"webhook-service.default", "webhook-service.default.svc"} | |
commonName := "webhook-service.default.svc" | |
// server cert config | |
cert := &x509.Certificate{ | |
DNSNames: dnsNames, | |
SerialNumber: big.NewInt(1658), | |
Subject: pkix.Name{ | |
CommonName: commonName, | |
Organization: []string{"velotio.com"}, | |
}, | |
NotBefore: time.Now(), | |
NotAfter: time.Now().AddDate(1, 0, 0), | |
SubjectKeyId: []byte{1, 2, 3, 4, 6}, | |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, | |
KeyUsage: x509.KeyUsageDigitalSignature, | |
} | |
// server private key | |
serverPrivKey, err := rsa.GenerateKey(cryptorand.Reader, 4096) | |
if err != nil { | |
fmt.Println(err) | |
} | |
// sign the server cert | |
serverCertBytes, err := x509.CreateCertificate(cryptorand.Reader, cert, ca, &serverPrivKey.PublicKey, caPrivKey) | |
if err != nil { | |
fmt.Println(err) | |
} | |
// PEM encode the server cert and key | |
serverCertPEM = new(bytes.Buffer) | |
_ = pem.Encode(serverCertPEM, &pem.Block{ | |
Type: "CERTIFICATE", | |
Bytes: serverCertBytes, | |
}) | |
serverPrivKeyPEM = new(bytes.Buffer) | |
_ = pem.Encode(serverPrivKeyPEM, &pem.Block{ | |
Type: "RSA PRIVATE KEY", | |
Bytes: x509.MarshalPKCS1PrivateKey(serverPrivKey), | |
}) | |
err = os.MkdirAll("/etc/webhook/certs/", 0666) | |
if err != nil { | |
log.Panic(err) | |
} | |
err = WriteFile("/etc/webhook/certs/tls.crt", serverCertPEM) | |
if err != nil { | |
log.Panic(err) | |
} | |
err = WriteFile("/etc/webhook/certs/tls.key", serverPrivKeyPEM) | |
if err != nil { | |
log.Panic(err) | |
} | |
} | |
// WriteFile writes data in the file at the given path | |
func WriteFile(filepath string, sCert *bytes.Buffer) error { | |
f, err := os.Create(filepath) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
_, err = f.Write(sCert.Bytes()) | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment