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: update verifier #1002

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 35 additions & 1 deletion cmd/notation/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@
package main

import (
"context"
"crypto/x509"
"errors"
"fmt"
"io/fs"
"net/http"
"os"
"reflect"
"time"

"github.com/notaryproject/notation-core-go/revocation"
"github.com/notaryproject/notation-go"
"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation-go/plugin"
"github.com/notaryproject/notation-go/verifier"
"github.com/notaryproject/notation-go/verifier/trustpolicy"
"github.com/notaryproject/notation-go/verifier/truststore"
"github.com/notaryproject/notation/cmd/notation/internal/experimental"
"github.com/notaryproject/notation/internal/cmd"
"github.com/notaryproject/notation/internal/httputil"
"github.com/notaryproject/notation/internal/ioutil"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -111,7 +119,7 @@
ctx := opts.LoggingFlagOpts.InitializeLogger(command.Context())

// initialize
sigVerifier, err := verifier.NewFromConfig()
sigVerifier, err := getVerifier(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -219,3 +227,29 @@
ioutil.PrintMetadataMap(os.Stdout, metadata)
}
}

func getVerifier(ctx context.Context) (notation.Verifier, error) {
policyDocument, err := trustpolicy.LoadOCIDocument()
if err != nil {
return nil, err
}
x509TrustStore := truststore.NewX509TrustStore(dir.ConfigFS())
revocationCodeSigningValidator, err := revocation.NewWithOptions(revocation.Options{
OCSPHTTPClient: httputil.NewClient(ctx, &http.Client{Timeout: 2 * time.Second}),
CertChainPurpose: x509.ExtKeyUsageCodeSigning,
})
if err != nil {
return nil, err

Check warning on line 242 in cmd/notation/verify.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/verify.go#L242

Added line #L242 was not covered by tests
}
revocationTimestampingValidator, err := revocation.NewWithOptions(revocation.Options{
OCSPHTTPClient: httputil.NewClient(ctx, &http.Client{Timeout: 2 * time.Second}),
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
CertChainPurpose: x509.ExtKeyUsageTimeStamping,
})
if err != nil {
return nil, err

Check warning on line 249 in cmd/notation/verify.go

View check run for this annotation

Codecov / codecov/patch

cmd/notation/verify.go#L249

Added line #L249 was not covered by tests
}
return verifier.NewVerifierWithOptions(policyDocument, nil, x509TrustStore, plugin.NewCLIManager(dir.PluginFS()), verifier.VerifierOptions{
RevocationCodeSigningValidator: revocationCodeSigningValidator,
RevocationTimestampingValidator: revocationTimestampingValidator,
})
}
14 changes: 14 additions & 0 deletions cmd/notation/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
package main

import (
"context"
"reflect"
"testing"

"github.com/notaryproject/notation-go/dir"
)

func TestVerifyCommand_BasicArgs(t *testing.T) {
Expand Down Expand Up @@ -80,3 +83,14 @@ func TestVerifyCommand_MissingArgs(t *testing.T) {
t.Fatal("Parse Args expected error, but ok")
}
}

func TestGetVerifier(t *testing.T) {
t.Run("non-existing trust policy", func(t *testing.T) {
dir.UserConfigDir = "/"
expectedErrMsg := "trust policy is not present. To create a trust policy, see: https://notaryproject.dev/docs/quickstart/#create-a-trust-policy"
_, err := getVerifier(context.Background())
if err == nil || err.Error() != expectedErrMsg {
t.Fatalf("expected %s, but got %s", expectedErrMsg, err)
}
})
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ require (
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.22.0 // indirect
)

replace github.com/notaryproject/notation-go => github.com/Two-Hearts/notation-go v0.0.0-20240801055637-02ac12ec085f

replace github.com/notaryproject/notation-core-go => github.com/Two-Hearts/notation-core-go v0.0.0-20240801005628-17703be86fe1
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 h1:mFRzDkZVAjdal+s7s0MwaRv9igoPqLRdzOLzw/8Xvq8=
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
github.com/Two-Hearts/notation-core-go v0.0.0-20240801005628-17703be86fe1 h1:BSSLhg7pNcaxaWgPg47G6oIMIcgNOY00haRwi+pky3g=
github.com/Two-Hearts/notation-core-go v0.0.0-20240801005628-17703be86fe1/go.mod h1:t57m88CEK1g1EkZ96vGLOPPDB6mSh/YZXD/aR2mSnRg=
github.com/Two-Hearts/notation-go v0.0.0-20240801055637-02ac12ec085f h1:Ll7/z4iFo8bp1Y/+hfFCKhYpFHvlJUP7CtDrJyz3FVc=
github.com/Two-Hearts/notation-go v0.0.0-20240801055637-02ac12ec085f/go.mod h1:T9cJ0Mbp0s5FqTcfktZAA6ERT+XQuQJ51aMC7DppAmw=
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa h1:LHTHcTQiSGT7VVbI0o4wBRNQIgn917usHWOd6VAffYI=
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -35,10 +39,6 @@ github.com/jcmturner/gokrb5/v8 v8.4.4 h1:x1Sv4HaTpepFkXbt2IkL29DXRf8sOfZXo8eRKh6
github.com/jcmturner/gokrb5/v8 v8.4.4/go.mod h1:1btQEpgT6k+unzCwX1KdWMEwPPkkgBtP+F6aCACiMrs=
github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZY=
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
github.com/notaryproject/notation-core-go v1.1.0-beta.1 h1:Rpg8qhoEo4nIdiAWz597lbEcvEmjKwJLUtvJqXa8M4c=
github.com/notaryproject/notation-core-go v1.1.0-beta.1/go.mod h1:MdxSbL9F5h63EmtXWfYMWy7hEmGmOmsfN4B6KM2WyhY=
github.com/notaryproject/notation-go v1.2.0-beta.1 h1:xSDkmdZ5j7kLrVs2ae8MyowdN7kEzwlV0EIhHjKfF90=
github.com/notaryproject/notation-go v1.2.0-beta.1/go.mod h1:RypdKUKCUZ8QgJAqaaesgUkTRHQIB+puCPdH3/3dVGE=
github.com/notaryproject/notation-plugin-framework-go v1.0.0 h1:6Qzr7DGXoCgXEQN+1gTZWuJAZvxh3p8Lryjn5FaLzi4=
github.com/notaryproject/notation-plugin-framework-go v1.0.0/go.mod h1:RqWSrTOtEASCrGOEffq0n8pSg2KOgKYiWqFWczRSics=
github.com/notaryproject/tspclient-go v0.1.1-0.20240715235637-df25ef8d2172 h1:Q8UsmeFMzyFuMMq4dlbIRJUi7khEKXKUe2H2Hm3W92Y=
Expand Down
Loading