fix: public and private key mixed up by Skarlso · Pull Request #98 · open-component-model/replication-controller · GitHub
Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.
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
3 changes: 3 additions & 0 deletions docs/release_notes/v0.12.1.md
22 changes: 20 additions & 2 deletions pkg/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewClient(client client.Client) *Client {
// SignDestinationComponent signs the component before transferring it and returns the public key for storing it on the
// subscription.
func (c *Client) SignDestinationComponent(_ context.Context, component ocm.ComponentVersionAccess) ([]byte, error) {
pub, priv, err := sign.GenerateSigningKeyPEMPair()
priv, pub, err := sign.GenerateSigningKeyPEMPair()
if err != nil {
return nil, fmt.Errorf("failed to generate signing key: %w", err)
}
Expand Down Expand Up @@ -151,7 +151,25 @@ func (c *Client) GetComponentVersion(

func (c *Client) VerifyComponent(ctx context.Context, obj *v1alpha1.ComponentSubscription, cv ocm.ComponentVersionAccess) (bool, error) {
for _, signature := range obj.Spec.Verify {
cert, err := c.getPublicKey(ctx, obj.Namespace, signature.PublicKey.SecretRef.Name, signature.Name)
var (
cert []byte
err error
)

if signature.PublicKey.Value != nil {
cert, err = signature.PublicKey.DecodePublicValue()
} else {
if signature.PublicKey.SecretRef == nil {
return false, fmt.Errorf("kubernetes secret reference not provided")
}

cert, err = c.getPublicKey(
ctx,
obj.Namespace,
signature.PublicKey.SecretRef.Name,
signature.Name,
)
}
if err != nil {
return false, fmt.Errorf("verify error: %w", err)
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/ocm/ocm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package ocm

import (
"bytes"
"context"
"encoding/base64"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -465,6 +467,54 @@ func TestClient_VerifyComponent(t *testing.T) {
assert.True(t, verified, "verified should have been true, but it did not")
}

func TestClient_SignComponent(t *testing.T) {
fakeKubeClient := env.FakeKubeClient()
ocmClient := NewClient(fakeKubeClient)
component := "github.com/skarlso/ocm-demo-index"

octx := ocmcontext.NewFakeOCMContext()

c := &ocmcontext.Component{
Name: component,
Version: "v0.0.1",
}
require.NoError(t, octx.AddComponent(c))

pub, err := ocmClient.SignDestinationComponent(context.Background(), c)
assert.NoError(t, err)

var buff []byte
buffer := bytes.NewBuffer(buff)
encoder := base64.NewEncoder(base64.StdEncoding, buffer)
_, err = encoder.Write(pub)
require.NoError(t, err)
require.NoError(t, encoder.Close())
cv := &v1alpha1.ComponentSubscription{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Namespace: "default",
},
Spec: v1alpha1.ComponentSubscriptionSpec{
Component: component,
Source: v1alpha1.OCMRepository{
URL: "localhost",
},
Verify: []ocmv1alpha1.Signature{
{
Name: v1alpha1.InternalSignatureName,
PublicKey: ocmv1alpha1.PublicKey{
Value: buffer.Bytes(),
},
},
},
},
}

verified, err := ocmClient.VerifyComponent(context.Background(), cv, c)
assert.NoError(t, err)
assert.True(t, verified, "verified should have been true, but it did not")
}

func TestClient_VerifyComponentDifferentPublicKey(t *testing.T) {
publicKey2, err := os.ReadFile(filepath.Join("testdata", "public2_key.pem"))
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/release.go