-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
runtime-watcher/internal/cacertificatehandler/ca_certificate_handler_test.go
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cacertificatehandler_test | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/tls" | ||
"encoding/pem" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kyma-project/runtime-watcher/skr/internal/cacertificatehandler" | ||
"github.com/kyma-project/runtime-watcher/skr/internal/tlstest" | ||
) | ||
|
||
func TestGetCertificatePool(t *testing.T) { | ||
certPath := "ca.crt" | ||
err := createCaCertFile(certPath) | ||
assert.NoError(t, err) | ||
defer deleteCaCertFile(certPath) | ||
|
||
got, err := cacertificatehandler.GetCertificatePool(certPath) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, got) | ||
|
||
assert.Equal(t, 2, len(got.Subjects())) | ||
Check failure on line 28 in runtime-watcher/internal/cacertificatehandler/ca_certificate_handler_test.go GitHub Actions / lint-build-test
|
||
assert.Contains(t, string(got.Subjects()[0]), "oldCert") | ||
Check failure on line 29 in runtime-watcher/internal/cacertificatehandler/ca_certificate_handler_test.go GitHub Actions / lint-build-test
|
||
assert.Contains(t, string(got.Subjects()[1]), "newCert") | ||
Check failure on line 30 in runtime-watcher/internal/cacertificatehandler/ca_certificate_handler_test.go GitHub Actions / lint-build-test
|
||
} | ||
|
||
func createCaCertFile(certPath string) error { | ||
certFile, err := os.OpenFile(certPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return fmt.Errorf("failed to create cert file: %w", err) | ||
} | ||
firstCert, err := createCertificate("oldCert") | ||
if err != nil { | ||
return fmt.Errorf("failed to create certificate: %w", err) | ||
} | ||
|
||
secondCert, err := createCertificate("newCert") | ||
if err != nil { | ||
return fmt.Errorf("failed to create certificate: %w", err) | ||
} | ||
|
||
firstCertBytes := pem.EncodeToMemory(&pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: firstCert.Certificate[0], | ||
}) | ||
|
||
secondCertBytes := pem.EncodeToMemory(&pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: secondCert.Certificate[0], | ||
}) | ||
|
||
if _, err = certFile.Write(firstCertBytes); err != nil { | ||
return fmt.Errorf("failed to write cert file: %w", err) | ||
} | ||
if _, err = certFile.Write(secondCertBytes); err != nil { | ||
return fmt.Errorf("failed to write cert file: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func createCertificate(subjectName string) (*tls.Certificate, error) { | ||
key, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create key: %w", err) | ||
} | ||
certTemplate, err := tlstest.CreateCertTemplate(true) | ||
certTemplate.Subject.CommonName = subjectName | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create cert template: %w", err) | ||
} | ||
cert, err := tlstest.CreateCert(certTemplate, certTemplate, key, key) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create cert: %w", err) | ||
} | ||
|
||
return cert, nil | ||
} | ||
|
||
func deleteCaCertFile(certPath string) error { | ||
err := os.Remove(certPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete cert file: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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