go.temporal.io/server/tests/testutils/tls.go
186 LOC · 66 covered · 120 uncovered · 22 ranges · 19 concepts · 2 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 (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"os"
)
type CertChain struct {
CertPubFile string
CertKeyFile string
CaPubFile string
}
return dir + "/cert_pub.pem"
}
return dir + "/cert_priv.pem"
}
return dir + "/ca_pub.pem"
}
fileBytes, err := os.ReadFile(file)
if err != nil {
panic(err)
}
}
chain, _, err := GenerateTestChainWithSN(tempDir, commonName, 0)
return chain, err
}
func GenerateTestChainWithSN(tempDir string, commonName string, serialNumber int64,
caPubFile := CAFilePath(tempDir)
certPubFile := CertFilePath(tempDir)
certPrivFile := KeyFilePath(tempDir)
caCert, err := GenerateSelfSignedCA(caPubFile)
if err != nil {
return CertChain{}, nil, err
}
if _, err = GenerateServerCert(caCert, commonName, serialNumber, certPubFile, certPrivFile); err != nil {
tls.go ×20
return CertChain{}, nil, err
}
return CertChain{CaPubFile: caPubFile, CertPubFile: certPubFile, CertKeyFile: certPrivFile}, caCert, err
tls.go ×20
}
func GenerateTestCerts(tempDir string, commonName string, num int) ([]*tls.Certificate, *x509.CertPool, *x509.CertPool, error) {
caCert, err := GenerateSelfSignedCA(CAFilePath(tempDir))
if err != nil {
return nil, nil, nil, err
}
caPool, err := GenerateSelfSignedCAPool(caCert)
if err != nil {
return nil, nil, nil, err
}
chains := make([]*tls.Certificate, num)
for i := range num {
certPubFile := tempDir + fmt.Sprintf("/cert_pub_%d.pem", i)
certPrivFile := tempDir + fmt.Sprintf("/cert_priv_%d.pem", i)
cert, err := GenerateServerCert(caCert, commonName, int64(i+100), certPubFile, certPrivFile)
if err != nil {
return nil, nil, nil, err
}
chains[i] = cert
}
wrongCACert, err := GenerateSelfSignedCA(CAFilePath(tempDir))
if err != nil {
return nil, nil, nil, err
}
wrongCAPool, err := GenerateSelfSignedCAPool(wrongCACert)
return chains, caPool, wrongCAPool, err
}
func GenerateSelfSignedCAPool(caCert *tls.Certificate) (*x509.CertPool, error) {
caPEM := &pem.Block{
Type: "CERTIFICATE",
Bytes: caCert.Certificate[0],
}
caPool := x509.NewCertPool()
caPem, err := pemEncodeToBytes(caPEM)
if err != nil {
return nil, err
}
caPool.AppendCertsFromPEM(caPem)
return caPool, nil
}
caCert, err := generateSelfSignedX509CA("undefined", nil, 1024)
if err != nil {
return nil, err
}
Type: "CERTIFICATE",
Bytes: caCert.Certificate[0],
}); err != nil {
return nil, err
}
}
func GenerateServerCert(
caCert *tls.Certificate,
commonName string,
serialNumber int64,
certPubFile string,
certPrivFile string,
serverCert, privKey, err := generateServerX509UsingCAAndSerialNumber(commonName, serialNumber, caCert)
if err != nil {
return nil, err
}
Type: "CERTIFICATE",
Bytes: serverCert.Certificate[0],
}
if err := pemEncodeToFile(certPubFile, certPEM); err != nil {
return nil, err
}
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privKey),
}
err = pemEncodeToFile(certPrivFile, keyPEM)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return &cert, err
}
bytes, err := pemEncodeToBytes(block)
if err != nil {
return err
}
}
pemBuffer := new(bytes.Buffer)
err := pem.Encode(pemBuffer, block)
if err != nil {
return nil, err
}
}