|
| 1 | +package cacertificatehandler_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/x509" |
| 6 | + "crypto/x509/pkix" |
| 7 | + "encoding/pem" |
| 8 | + "fmt" |
| 9 | + "math/big" |
| 10 | + "net" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/kyma-project/runtime-watcher/skr/internal/cacertificatehandler" |
| 18 | + "github.com/kyma-project/runtime-watcher/skr/internal/tlstest" |
| 19 | +) |
| 20 | + |
| 21 | +func TestGetCertificatePool1(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + tests := []struct { |
| 24 | + name string |
| 25 | + certificateCount int |
| 26 | + certPath string |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "certificate pool with one certificate", |
| 30 | + certificateCount: 1, |
| 31 | + certPath: "ca-1.cert", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "certificate pool with two certificates", |
| 35 | + certificateCount: 2, |
| 36 | + certPath: "ca-2.cert", |
| 37 | + }, |
| 38 | + } |
| 39 | + for _, tt := range tests { |
| 40 | + testCase := tt |
| 41 | + t.Run(testCase.name, func(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + file, err := os.CreateTemp("", testCase.certPath) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + err = writeCertificatesToFile(file, testCase.certificateCount) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + got, err := cacertificatehandler.GetCertificatePool(file.Name()) |
| 50 | + require.NoError(t, err) |
| 51 | + require.False(t, got.Equal(x509.NewCertPool())) |
| 52 | + |
| 53 | + certificates, err := getCertificates(file.Name()) |
| 54 | + require.NoError(t, err) |
| 55 | + err = os.Remove(file.Name()) |
| 56 | + require.NoError(t, err) |
| 57 | + expectedCertPool := x509.NewCertPool() |
| 58 | + for _, certificate := range certificates { |
| 59 | + expectedCertPool.AddCert(certificate) |
| 60 | + } |
| 61 | + require.True(t, got.Equal(expectedCertPool)) |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func getCertificates(certPath string) ([]*x509.Certificate, error) { |
| 67 | + caCertBytes, err := os.ReadFile(certPath) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("could not load CA certificate :%w", err) |
| 70 | + } |
| 71 | + var certs []*x509.Certificate |
| 72 | + remainingCert := caCertBytes |
| 73 | + for len(remainingCert) > 0 { |
| 74 | + var publicPemBlock *pem.Block |
| 75 | + publicPemBlock, remainingCert = pem.Decode(remainingCert) |
| 76 | + rootPubCrt, errParse := x509.ParseCertificate(publicPemBlock.Bytes) |
| 77 | + if errParse != nil { |
| 78 | + msg := "failed to parse public key" |
| 79 | + return nil, fmt.Errorf("%s :%w", msg, errParse) |
| 80 | + } |
| 81 | + certs = append(certs, rootPubCrt) |
| 82 | + } |
| 83 | + |
| 84 | + return certs, nil |
| 85 | +} |
| 86 | + |
| 87 | +func createCertificate() *x509.Certificate { |
| 88 | + sn, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) |
| 89 | + cert := &x509.Certificate{ |
| 90 | + SerialNumber: sn, |
| 91 | + Subject: pkix.Name{ |
| 92 | + CommonName: "127.0.0.1", |
| 93 | + }, |
| 94 | + IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}, |
| 95 | + NotBefore: time.Now(), |
| 96 | + NotAfter: time.Now().Add(time.Hour), |
| 97 | + IsCA: true, |
| 98 | + KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 99 | + BasicConstraintsValid: true, |
| 100 | + } |
| 101 | + |
| 102 | + return cert |
| 103 | +} |
| 104 | + |
| 105 | +func writeCertificatesToFile(certFile *os.File, certificateCount int) error { |
| 106 | + var certs []byte |
| 107 | + |
| 108 | + for i := 0; i < certificateCount; i++ { |
| 109 | + rootKey, err := tlstest.GenerateRootKey() |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to generate root key: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + certificate := createCertificate() |
| 115 | + cert, err := tlstest.CreateCert(certificate, certificate, rootKey, rootKey) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("failed to create certificate: %w", err) |
| 118 | + } |
| 119 | + certBytes := pem.EncodeToMemory(&pem.Block{ |
| 120 | + Type: "CERTIFICATE", |
| 121 | + Bytes: cert.Certificate[0], |
| 122 | + }) |
| 123 | + certs = append(certs, certBytes...) |
| 124 | + } |
| 125 | + |
| 126 | + if _, err := certFile.Write(certs); err != nil { |
| 127 | + certFile.Close() |
| 128 | + return fmt.Errorf("failed to write certificates to file: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + return certFile.Close() |
| 132 | +} |
0 commit comments