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

refactor: dedicated target data struct #415

Merged
merged 1 commit into from
Sep 10, 2024
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
4 changes: 2 additions & 2 deletions pkg/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result

if target.Kind == configMapTarget {
syncFunc = func(targetLog logr.Logger, target targetResource, shouldExist bool) (bool, error) {
return b.syncConfigMapTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle, shouldExist)
return b.syncConfigMapTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle.targetData, shouldExist)
}
}
if target.Kind == secretTarget {
syncFunc = func(targetLog logr.Logger, target targetResource, shouldExist bool) (bool, error) {
return b.syncSecretTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle, shouldExist)
return b.syncSecretTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle.targetData, shouldExist)
}
}

Expand Down
31 changes: 2 additions & 29 deletions pkg/bundle/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
"github.com/cert-manager/trust-manager/pkg/bundle/internal/truststore"
"github.com/cert-manager/trust-manager/pkg/util"
)

Expand All @@ -37,8 +36,7 @@ type notFoundError struct{ error }
// certificate data from concatenating all the sources together, binary data for any additional formats and
// any metadata from the sources which needs to be exposed on the Bundle resource's status field.
type bundleData struct {
data string
binaryData map[string][]byte
targetData

defaultCAPackageStringID string
}
Expand Down Expand Up @@ -93,7 +91,7 @@ func (b *bundle) buildSourceBundle(ctx context.Context, sources []trustapi.Bundl
return bundleData{}, fmt.Errorf("couldn't find any valid certificates in bundle")
}

if err := resolvedBundle.populateData(certPool, formats); err != nil {
if err := resolvedBundle.populate(certPool, formats); err != nil {
return bundleData{}, err
}

Expand Down Expand Up @@ -193,28 +191,3 @@ func (b *bundle) secretBundle(ctx context.Context, ref *trustapi.SourceObjectKey
}
return results.String(), nil
}

func (b *bundleData) populateData(pool *util.CertPool, formats *trustapi.AdditionalFormats) error {
b.data = pool.PEM()

if formats != nil {
b.binaryData = make(map[string][]byte)

if formats.JKS != nil {
encoded, err := truststore.NewJKSEncoder(*formats.JKS.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode JKS: %w", err)
}
b.binaryData[formats.JKS.Key] = encoded
}

if formats.PKCS12 != nil {
encoded, err := truststore.NewPKCS12Encoder(*formats.PKCS12.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode PKCS12: %w", err)
}
b.binaryData[formats.PKCS12.Key] = encoded
}
}
return nil
}
36 changes: 34 additions & 2 deletions pkg/bundle/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (

trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
"github.com/cert-manager/trust-manager/pkg/bundle/internal/ssa_client"
"github.com/cert-manager/trust-manager/pkg/bundle/internal/truststore"
"github.com/cert-manager/trust-manager/pkg/util"
)

// syncConfigMapTarget syncs the given data to the target ConfigMap in the given namespace.
Expand All @@ -48,7 +50,7 @@ func (b *bundle) syncConfigMapTarget(
bundle *trustapi.Bundle,
name string,
namespace string,
resolvedBundle bundleData,
resolvedBundle targetData,
shouldExist bool,
) (bool, error) {
configMap := &metav1.PartialObjectMetadata{
Expand Down Expand Up @@ -154,7 +156,7 @@ func (b *bundle) syncSecretTarget(
bundle *trustapi.Bundle,
name string,
namespace string,
resolvedBundle bundleData,
resolvedBundle targetData,
shouldExist bool,
) (bool, error) {
secret := &metav1.PartialObjectMetadata{
Expand Down Expand Up @@ -394,3 +396,33 @@ func (b *bundle) patchSecretResource(ctx context.Context, applyConfig *coreapply

return nil
}

type targetData struct {
data string
binaryData map[string][]byte
}

func (b *targetData) populate(pool *util.CertPool, formats *trustapi.AdditionalFormats) error {
b.data = pool.PEM()

if formats != nil {
b.binaryData = make(map[string][]byte)

if formats.JKS != nil {
encoded, err := truststore.NewJKSEncoder(*formats.JKS.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode JKS: %w", err)
}
b.binaryData[formats.JKS.Key] = encoded
}

if formats.PKCS12 != nil {
encoded, err := truststore.NewPKCS12Encoder(*formats.PKCS12.Password).Encode(pool)
if err != nil {
return fmt.Errorf("failed to encode PKCS12: %w", err)
}
b.binaryData[formats.PKCS12.Key] = encoded
}
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/bundle/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func Test_syncConfigMapTarget(t *testing.T) {
AdditionalFormats: &trustapi.AdditionalFormats{},
},
}
resolvedBundle := bundleData{data: data, binaryData: make(map[string][]byte)}
resolvedBundle := targetData{data: data, binaryData: make(map[string][]byte)}
if test.withJKS {
spec.Target.AdditionalFormats.JKS = &trustapi.JKS{
KeySelector: trustapi.KeySelector{
Expand Down Expand Up @@ -1237,7 +1237,7 @@ func Test_syncSecretTarget(t *testing.T) {
AdditionalFormats: &trustapi.AdditionalFormats{},
},
}
resolvedBundle := bundleData{data: data, binaryData: make(map[string][]byte)}
resolvedBundle := targetData{data: data, binaryData: make(map[string][]byte)}
if test.withJKS {
spec.Target.AdditionalFormats.JKS = &trustapi.JKS{
KeySelector: trustapi.KeySelector{
Expand Down