Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for truststore deterministic #443

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/bundle/internal/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (r *Reconciler) SyncConfigMap(
return false, errors.New("target not defined")
}

// Generated JKS is not deterministic - best we can do here is update if the pem cert has
// changed (hence not checking if JKS matches)
// Generated PKCS #12 is not deterministic - best we can do here is update if the pem cert has
// changed (hence not checking if PKCS #12 matches)
dataHash := fmt.Sprintf("%x", sha256.Sum256([]byte(resolvedBundle.Data)))
configMapData := map[string]string{
bundleTarget.ConfigMap.Key: resolvedBundle.Data,
Expand Down Expand Up @@ -185,8 +185,8 @@ func (r *Reconciler) SyncSecret(
return false, errors.New("target not defined")
}

// Generated JKS is not deterministic - best we can do here is update if the pem cert has
// changed (hence not checking if JKS matches)
// Generated PKCS #12 is not deterministic - best we can do here is update if the pem cert has
// changed (hence not checking if PKCS #12 matches)
dataHash := fmt.Sprintf("%x", sha256.Sum256([]byte(resolvedBundle.Data)))
secretData := map[string][]byte{
bundleTarget.Secret.Key: []byte(resolvedBundle.Data),
Expand Down
52 changes: 52 additions & 0 deletions pkg/bundle/internal/truststore/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,64 @@ import (
"testing"

"github.com/pavlo-v-chernykh/keystore-go/v4"
"github.com/stretchr/testify/assert"

"github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
"github.com/cert-manager/trust-manager/pkg/util"
"github.com/cert-manager/trust-manager/test/dummy"
)

func Test_Encoder_Deterministic(t *testing.T) {
tests := map[string]struct {
encoder Encoder
expNonDeterministic bool
}{
"JKS default password": {
encoder: NewJKSEncoder(v1alpha1.DefaultJKSPassword),
},
"JKS custom password": {
encoder: NewJKSEncoder("my-password"),
},
"PKCS#12 default password": {
encoder: NewPKCS12Encoder(v1alpha1.DefaultPKCS12Password),
},
"PKCS#12 custom password": {
encoder: NewPKCS12Encoder("my-password"),
// FIXME: We should try to make all encoders deterministic
expNonDeterministic: true,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()

bundle := dummy.JoinCerts(dummy.TestCertificate1, dummy.TestCertificate2, dummy.TestCertificate3)

certPool := util.NewCertPool()
if err := certPool.AddCertsFromPEM([]byte(bundle)); err != nil {
t.Fatalf("didn't expect an error but got: %s", err)
}

store, err := test.encoder.Encode(certPool)
if err != nil {
t.Fatalf("didn't expect an error but got: %s", err)
}

store2, err := test.encoder.Encode(certPool)
if err != nil {
t.Fatalf("didn't expect an error but got: %s", err)
}

if test.expNonDeterministic {
assert.NotEqual(t, store, store2, "expected encoder to be non-deterministic")
} else {
assert.Equal(t, store, store2, "expected encoder to be deterministic")
}
})
}
}

func Test_encodeJKSAliases(t *testing.T) {
// IMPORTANT: We use TestCertificate1 and TestCertificate2 here because they're defined
// to be self-signed and to also use the same Subject, while being different certs.
Expand Down