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!: return CertificateVerficationResponse{} in VerifyCert() #74

Merged
merged 1 commit into from
Feb 16, 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
17 changes: 6 additions & 11 deletions pkg/capabilities/crypto/crypto.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package crypto

import (
"encoding/json"
"fmt"

cap "github.com/kubewarden/policy-sdk-go/pkg/capabilities"

"encoding/json"
)

type CryptoHost struct {
Expand Down Expand Up @@ -38,7 +37,7 @@ func (e CertificateEncoding) MarshalJSON() ([]byte, error) {
// (intermediates first, root last). If empty, certificate is assumed trusted.
// - not_after: string in RFC 3339 time format, to check expiration against.
// If None, certificate is assumed never expired.
func VerifyCert(h *cap.Host, cert Certificate, certChain []Certificate, notAfter string) (bool, error) {
func VerifyCert(h *cap.Host, cert Certificate, certChain []Certificate, notAfter string) (*CertificateVerificationResponse, error) {
requestObj := CertificateVerificationRequest{
Cert: cert,
CertChain: certChain,
Expand All @@ -47,23 +46,19 @@ func VerifyCert(h *cap.Host, cert Certificate, certChain []Certificate, notAfter

payload, err := json.Marshal(requestObj)
if err != nil {
return false, fmt.Errorf("cannot serialize request object: %w", err)
return &CertificateVerificationResponse{}, fmt.Errorf("cannot serialize request object: %w", err)
}

// perform callback
responsePayload, err := h.Client.HostCall("kubewarden", "crypto", "v1/is_certificate_trusted", payload)
if err != nil {
return false, err
return &CertificateVerificationResponse{}, err
}

responseObj := CertificateVerificationResponse{}
if err := json.Unmarshal(responsePayload, &responseObj); err != nil {
return false, fmt.Errorf("cannot unmarshall response object: %w", err)
return &CertificateVerificationResponse{}, fmt.Errorf("cannot unmarshall response object: %w", err)
}

if responseObj.Trusted {
return true, nil
} else {
return false, fmt.Errorf(responseObj.Reason)
}
return &responseObj, nil
}
2 changes: 1 addition & 1 deletion pkg/capabilities/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestV1IsCertificateTrusted(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !res {
if !res.Trusted {
t.Fatalf("expected trusted image, got untrusted")
}
}
Loading