|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package testtls provides runtime-generated TLS materials for tests. |
| 5 | +package testtls // import "go.opentelemetry.io/contrib/otelconf/internal/testtls" |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/rand" |
| 9 | + "crypto/rsa" |
| 10 | + "crypto/x509" |
| 11 | + "crypto/x509/pkix" |
| 12 | + "encoding/pem" |
| 13 | + "math/big" |
| 14 | + "net" |
| 15 | + "os" |
| 16 | + "path/filepath" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +// Material contains generated CA, server, and client cert file paths. |
| 21 | +type Material struct { |
| 22 | + CACertPath string |
| 23 | + CAKeyPath string |
| 24 | + ServerCertPath string |
| 25 | + ServerKeyPath string |
| 26 | + ClientCertPath string |
| 27 | + ClientKeyPath string |
| 28 | +} |
| 29 | + |
| 30 | +// TB is the minimal testing surface needed by this helper. |
| 31 | +type TB interface { |
| 32 | + Helper() |
| 33 | + TempDir() string |
| 34 | + Fatalf(format string, args ...any) |
| 35 | +} |
| 36 | + |
| 37 | +// Write generates mTLS assets under t.TempDir so tests do not depend on expiring fixtures. |
| 38 | +func Write(t TB) Material { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + dir := t.TempDir() |
| 42 | + now := time.Now().UTC() |
| 43 | + |
| 44 | + caKey := mustRSAKey(t) |
| 45 | + caTemplate := &x509.Certificate{ |
| 46 | + SerialNumber: big.NewInt(1), |
| 47 | + Subject: pkix.Name{ |
| 48 | + Country: []string{"US"}, |
| 49 | + Province: []string{"California"}, |
| 50 | + Locality: []string{"San Francisco"}, |
| 51 | + Organization: []string{"OpenTelemetry Test CA"}, |
| 52 | + CommonName: "otelconf test ca", |
| 53 | + }, |
| 54 | + NotBefore: now.Add(-time.Hour), |
| 55 | + NotAfter: now.AddDate(10, 0, 0), |
| 56 | + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, |
| 57 | + BasicConstraintsValid: true, |
| 58 | + IsCA: true, |
| 59 | + } |
| 60 | + caDER := mustCertificate(t, caTemplate, caTemplate, &caKey.PublicKey, caKey) |
| 61 | + |
| 62 | + serverKey := mustRSAKey(t) |
| 63 | + serverTemplate := &x509.Certificate{ |
| 64 | + SerialNumber: big.NewInt(now.UnixNano()), |
| 65 | + Subject: pkix.Name{ |
| 66 | + Country: []string{"US"}, |
| 67 | + Province: []string{"California"}, |
| 68 | + Locality: []string{"San Francisco"}, |
| 69 | + Organization: []string{"OpenTelemetry Tests"}, |
| 70 | + CommonName: "localhost", |
| 71 | + }, |
| 72 | + DNSNames: []string{"localhost"}, |
| 73 | + IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)}, |
| 74 | + NotBefore: now.Add(-time.Hour), |
| 75 | + NotAfter: now.AddDate(2, 0, 0), |
| 76 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, |
| 77 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 78 | + BasicConstraintsValid: true, |
| 79 | + } |
| 80 | + serverDER := mustCertificate(t, serverTemplate, caTemplate, &serverKey.PublicKey, caKey) |
| 81 | + |
| 82 | + clientKey := mustRSAKey(t) |
| 83 | + clientTemplate := &x509.Certificate{ |
| 84 | + SerialNumber: big.NewInt(now.UnixNano() + 1), |
| 85 | + Subject: pkix.Name{ |
| 86 | + Country: []string{"US"}, |
| 87 | + Province: []string{"California"}, |
| 88 | + Locality: []string{"San Francisco"}, |
| 89 | + Organization: []string{"OpenTelemetry Tests"}, |
| 90 | + CommonName: "otelconf test client", |
| 91 | + }, |
| 92 | + NotBefore: now.Add(-time.Hour), |
| 93 | + NotAfter: now.AddDate(2, 0, 0), |
| 94 | + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, |
| 95 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, |
| 96 | + BasicConstraintsValid: true, |
| 97 | + } |
| 98 | + clientDER := mustCertificate(t, clientTemplate, caTemplate, &clientKey.PublicKey, caKey) |
| 99 | + |
| 100 | + m := Material{ |
| 101 | + CACertPath: filepath.Join(dir, "ca.crt"), |
| 102 | + CAKeyPath: filepath.Join(dir, "ca.key"), |
| 103 | + ServerCertPath: filepath.Join(dir, "server.crt"), |
| 104 | + ServerKeyPath: filepath.Join(dir, "server.key"), |
| 105 | + ClientCertPath: filepath.Join(dir, "client.crt"), |
| 106 | + ClientKeyPath: filepath.Join(dir, "client.key"), |
| 107 | + } |
| 108 | + writeCert(t, m.CACertPath, caDER) |
| 109 | + writeKey(t, m.CAKeyPath, caKey) |
| 110 | + writeCert(t, m.ServerCertPath, serverDER) |
| 111 | + writeKey(t, m.ServerKeyPath, serverKey) |
| 112 | + writeCert(t, m.ClientCertPath, clientDER) |
| 113 | + writeKey(t, m.ClientKeyPath, clientKey) |
| 114 | + return m |
| 115 | +} |
| 116 | + |
| 117 | +func mustRSAKey(t TB) *rsa.PrivateKey { |
| 118 | + t.Helper() |
| 119 | + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("generate rsa key: %v", err) |
| 122 | + } |
| 123 | + return key |
| 124 | +} |
| 125 | + |
| 126 | +func mustCertificate(t TB, template, parent *x509.Certificate, publicKey *rsa.PublicKey, signer *rsa.PrivateKey) []byte { |
| 127 | + t.Helper() |
| 128 | + der, err := x509.CreateCertificate(rand.Reader, template, parent, publicKey, signer) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("create certificate: %v", err) |
| 131 | + } |
| 132 | + return der |
| 133 | +} |
| 134 | + |
| 135 | +func writeCert(t TB, path string, der []byte) { |
| 136 | + t.Helper() |
| 137 | + block := &pem.Block{Type: "CERTIFICATE", Bytes: der} |
| 138 | + if err := os.WriteFile(path, pem.EncodeToMemory(block), 0o600); err != nil { |
| 139 | + t.Fatalf("write cert %s: %v", path, err) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func writeKey(t TB, path string, key *rsa.PrivateKey) { |
| 144 | + t.Helper() |
| 145 | + block := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)} |
| 146 | + if err := os.WriteFile(path, pem.EncodeToMemory(block), 0o600); err != nil { |
| 147 | + t.Fatalf("write key %s: %v", path, err) |
| 148 | + } |
| 149 | +} |
0 commit comments