Skip to content

Commit

Permalink
feat: change public key value to string (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso authored Dec 7, 2023
1 parent 960fd7d commit 4f97d12
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 26 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY pkg/ pkg/
COPY internal/ internal/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
Expand Down
13 changes: 2 additions & 11 deletions api/v1alpha1/componentversion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
package v1alpha1

import (
"bytes"
"encoding/base64"
"fmt"
"io"
"time"

"github.com/fluxcd/pkg/apis/meta"
Expand Down Expand Up @@ -93,22 +91,15 @@ type PublicKey struct {

// Value defines a PEM/base64 encoded public key value.
// +optional
Value []byte `json:"value,omitempty"`
Value string `json:"value,omitempty"`
}

func (p *PublicKey) DecodePublicValue() ([]byte, error) {
if len(p.Value) == 0 {
return nil, fmt.Errorf("key value not provided")
}

decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewBuffer(p.Value))

content, err := io.ReadAll(decoder)
if err != nil {
return nil, fmt.Errorf("failed to decode public key pem: %w", err)
}

return content, nil
return base64.StdEncoding.DecodeString(p.Value)
}

// Version specifies version information that can be used to resolve a Component Version.
Expand Down
5 changes: 0 additions & 5 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ spec:
value:
description: Value defines a PEM/base64 encoded public key
value.
format: byte
type: string
type: object
required:
Expand Down
2 changes: 1 addition & 1 deletion pkg/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (c *Client) VerifyComponent(
err error
)

if signature.PublicKey.Value != nil {
if signature.PublicKey.Value != "" {
cert, err = signature.PublicKey.DecodePublicValue()
} else {
if signature.PublicKey.SecretRef == nil {
Expand Down
12 changes: 4 additions & 8 deletions pkg/ocm/ocm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,7 @@ func TestClient_VerifyComponentWithValueKey(t *testing.T) {
}
require.NoError(t, octx.AddComponent(c))
//var buffer []byte
buf := bytes.Buffer{}
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
_, err = encoder.Write(publicKey1)
require.NoError(t, encoder.Close())
require.NoError(t, err)
pubKey := base64.StdEncoding.EncodeToString(publicKey1)
cv := &v1alpha1.ComponentVersion{
ObjectMeta: metav1.ObjectMeta{
Name: "test-name",
Expand All @@ -635,7 +631,7 @@ func TestClient_VerifyComponentWithValueKey(t *testing.T) {
{
Name: Signature,
PublicKey: v1alpha1.PublicKey{
Value: buf.Bytes(),
Value: pubKey,
},
},
},
Expand Down Expand Up @@ -688,15 +684,15 @@ func TestClient_VerifyComponentWithValueKeyFailsIfValueIsEmpty(t *testing.T) {
{
Name: Signature,
PublicKey: v1alpha1.PublicKey{
Value: []byte{},
Value: "",
},
},
},
},
}

_, err = ocmClient.VerifyComponent(context.Background(), octx, cv, "v0.0.1")
assert.EqualError(t, err, "failed to get public key for verification: key value not provided")
assert.EqualError(t, err, "kubernetes secret reference not provided")
}

func TestClient_VerifyComponentDifferentPublicKey(t *testing.T) {
Expand Down

0 comments on commit 4f97d12

Please sign in to comment.