go.temporal.io/server/tests/testutils/certificate.go
126 LOC · 71 covered · 55 uncovered · 13 ranges · 19 concepts · 1 introducers · 9 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
package testutils
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
mathrand "math/rand"
"net"
"strings"
"time"
)
// GenerateSelfSignedX509CA generates a TLS serverCert that is self-signed
func generateSelfSignedX509CA(commonName string, extUsage []x509.ExtKeyUsage, keyLengthBits int) (*tls.Certificate, error) {
tls.go ×20
now := time.Now().UTC()
template := &x509.Certificate{
SerialNumber: big.NewInt(now.Unix()),
Subject: pkix.Name{
CommonName: commonName,
Country: []string{"USA"},
Organization: []string{"TemporalTechnologiesTesting"},
},
NotBefore: now.Add(-time.Minute),
NotAfter: now.AddDate(0, 0, 1), // 1 day expiry
BasicConstraintsValid: true,
IsCA: true,
ExtKeyUsage: extUsage,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageKeyEncipherment |
x509.KeyUsageDigitalSignature,
}
if ip := net.ParseIP(commonName); ip != nil {
if ip.IsLoopback() {
template.IPAddresses = []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)}
template.DNSNames = []string{"localhost"}
} else {
template.IPAddresses = []net.IP{ip}
}
template.DNSNames = []string{commonName}
}
template.IPAddresses = []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)}
}
if err != nil {
return &tls.Certificate{}, err
}
cert, err := x509.CreateCertificate(rand.Reader, template, template, privateKey.Public(), privateKey)
tls.go ×20
if err != nil {
return &tls.Certificate{}, err
}
tlsCert.Certificate = append(tlsCert.Certificate, cert)
tlsCert.PrivateKey = privateKey
return &tlsCert, nil
}
// GenerateServerX509UsingCA generates a TLS serverCert that is self-signed
func generateServerX509UsingCAAndSerialNumber(commonName string, serialNumber int64, ca *tls.Certificate) (*tls.Certificate, *rsa.PrivateKey, error) {
tls.go ×20
now := time.Now().UTC()
i := serialNumber
if i == 0 {
i = mathrand.Int63n(100000000000000000)
}
SerialNumber: big.NewInt(i),
Subject: pkix.Name{
CommonName: commonName,
Country: []string{"USA"},
Organization: []string{"TemporalTechnologiesTesting"},
},
NotBefore: now.Add(-time.Minute),
NotAfter: now.AddDate(0, 0, 1), // 1 day expiry
BasicConstraintsValid: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
if ip := net.ParseIP(commonName); ip != nil {
if ip.IsLoopback() {
template.IPAddresses = []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)}
template.DNSNames = []string{"localhost"}
} else {
template.IPAddresses = []net.IP{ip}
}
} else {
template.DNSNames = []string{commonName}
}
template.IPAddresses = []net.IP{net.IPv6loopback, net.IPv4(127, 0, 0, 1)}
}
if err != nil {
return &tls.Certificate{}, nil, err
}
if err != nil {
return nil, nil, err
}
cert, err := x509.CreateCertificate(rand.Reader, template, caCert, privateKey.Public(), ca.PrivateKey)
tls.go ×20
if err != nil {
return &tls.Certificate{}, nil, err
}
tlsCert.Certificate = append(tlsCert.Certificate, cert)
tlsCert.PrivateKey = privateKey
return &tlsCert, privateKey, err
}