Skip to content

Commit

Permalink
feat(fulcioroots): singleton error pattern (#1965)
Browse files Browse the repository at this point in the history
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com>
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>

Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com>
  • Loading branch information
developer-guy and Dentrax authored Jun 7, 2022
1 parent 63fc4b5 commit 75b2bd1
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cmd/cosign/cli/fulcio/fulcio.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ func (f *Signer) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey,

var _ signature.Signer = &Signer{}

func GetRoots() *x509.CertPool {
func GetRoots() (*x509.CertPool, error) {
return fulcioroots.Get()
}

func GetIntermediates() *x509.CertPool {
func GetIntermediates() (*x509.CertPool, error) {
return fulcioroots.GetIntermediates()
}

Expand Down
29 changes: 14 additions & 15 deletions cmd/cosign/cli/fulcio/fulcioroots/fulcioroots.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
)

var (
rootsOnce sync.Once
roots *x509.CertPool
intermediates *x509.CertPool
rootsOnce sync.Once
roots *x509.CertPool
intermediates *x509.CertPool
singletonRootErr error
)

// This is the root in the fulcio project.
Expand Down Expand Up @@ -61,26 +62,24 @@ const (
altRoot = "SIGSTORE_ROOT_FILE"
)

func Get() *x509.CertPool {
func Get() (*x509.CertPool, error) {
rootsOnce.Do(func() {
var err error
roots, intermediates, err = initRoots()
if err != nil {
panic(err)
roots, intermediates, singletonRootErr = initRoots()
if singletonRootErr != nil {
return
}
})
return roots
return roots, singletonRootErr
}

func GetIntermediates() *x509.CertPool {
func GetIntermediates() (*x509.CertPool, error) {
rootsOnce.Do(func() {
var err error
roots, intermediates, err = initRoots()
if err != nil {
panic(err)
roots, intermediates, singletonRootErr = initRoots()
if singletonRootErr != nil {
return
}
})
return intermediates
return intermediates, singletonRootErr
}

func initRoots() (*x509.CertPool, *x509.CertPool, error) {
Expand Down
14 changes: 8 additions & 6 deletions cmd/cosign/cli/fulcio/fulcioroots/fulcioroots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ func TestGetFulcioRoots(t *testing.T) {
}
t.Setenv("SIGSTORE_ROOT_FILE", tmpCertFile.Name())

rootCertPool := Get()
// ignore deprecation error because certificates do not contain from SystemCertPool
if len(rootCertPool.Subjects()) != 1 { // nolint:staticcheck
if rootCertPool, re := Get(); err != nil {
t.Fatalf("failed to get roots: %v", re)
} else if len(rootCertPool.Subjects()) != 1 { // nolint:staticcheck
// ignore deprecation error because certificates do not contain from SystemCertPool
t.Errorf("expected 1 root certificate, got 0")
}

subCertPool := GetIntermediates()
// ignore deprecation error because certificates do not contain from SystemCertPool
if len(subCertPool.Subjects()) != 1 { // nolint:staticcheck
if subCertPool, ie := GetIntermediates(); err != nil {
t.Fatalf("failed to get intermediates: %v", ie)
} else if len(subCertPool.Subjects()) != 1 { // nolint:staticcheck
// ignore deprecation error because certificates do not contain from SystemCertPool
t.Errorf("expected 1 intermediate certificate, got 0")
}
}
10 changes: 8 additions & 2 deletions cmd/cosign/cli/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ func (c *VerifyCommand) Exec(ctx context.Context, images []string) (err error) {
}
co.RekorClient = rekorClient
}
co.RootCerts = fulcio.GetRoots()
co.IntermediateCerts = fulcio.GetIntermediates()
co.RootCerts, err = fulcio.GetRoots()
if err != nil {
return fmt.Errorf("getting Fulcio roots: %w", err)
}
co.IntermediateCerts, err = fulcio.GetIntermediates()
if err != nil {
return fmt.Errorf("getting Fulcio intermediates: %w", err)
}
}
keyRef := c.KeyRef
certRef := c.CertRef
Expand Down
10 changes: 8 additions & 2 deletions cmd/cosign/cli/verify/verify_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ func (c *VerifyAttestationCommand) Exec(ctx context.Context, images []string) (e
}
co.RekorClient = rekorClient
}
co.RootCerts = fulcio.GetRoots()
co.IntermediateCerts = fulcio.GetIntermediates()
co.RootCerts, err = fulcio.GetRoots()
if err != nil {
return fmt.Errorf("getting Fulcio roots: %w", err)
}
co.IntermediateCerts, err = fulcio.GetIntermediates()
if err != nil {
return fmt.Errorf("getting Fulcio intermediates: %w", err)
}
}
keyRef := c.KeyRef

Expand Down
18 changes: 13 additions & 5 deletions cmd/cosign/cli/verify/verify_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,20 @@ func verifySigByUUID(ctx context.Context, ko options.KeyOpts, rClient *client.Re
}

co := &cosign.CheckOpts{
RootCerts: fulcio.GetRoots(),
IntermediateCerts: fulcio.GetIntermediates(),
CertEmail: certEmail,
CertOidcIssuer: certOidcIssuer,
EnforceSCT: enforceSCT,
CertEmail: certEmail,
CertOidcIssuer: certOidcIssuer,
EnforceSCT: enforceSCT,
}

co.RootCerts, err = fulcio.GetRoots()
if err != nil {
return fmt.Errorf("getting Fulcio roots: %w", err)
}
co.IntermediateCerts, err = fulcio.GetIntermediates()
if err != nil {
return fmt.Errorf("getting Fulcio intermediates: %w", err)
}

cert := certs[0]
verifier, err := cosign.ValidateAndUnpackCert(cert, co)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/cosign/kubernetes/webhook/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import (
func valid(ctx context.Context, ref name.Reference, rekorClient *client.Rekor, keys []crypto.PublicKey, opts ...ociremote.Option) ([]oci.Signature, error) {
if len(keys) == 0 {
// If there are no keys, then verify against the fulcio root.
sps, err := validSignaturesWithFulcio(ctx, ref, fulcioroots.Get(), nil /* rekor */, nil /* no identities */, opts...)
fulcioRoots, err := fulcioroots.Get()
if err != nil {
return nil, err
}
sps, err := validSignaturesWithFulcio(ctx, ref, fulcioRoots, nil /* rekor */, nil /* no identities */, opts...)
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/sget/sget.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package sget
import (
"context"
"errors"
"fmt"
"io"

"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -90,8 +91,14 @@ func (sg *SecureGet) Do(ctx context.Context) error {
// was performed so we don't need to use this fragile logic here.
fulcioVerified := (co.SigVerifier == nil)

co.RootCerts = fulcio.GetRoots()
co.IntermediateCerts = fulcio.GetIntermediates()
co.RootCerts, err = fulcio.GetRoots()
if err != nil {
return fmt.Errorf("getting Fulcio roots: %w", err)
}
co.IntermediateCerts, err = fulcio.GetIntermediates()
if err != nil {
return fmt.Errorf("getting Fulcio intermediates: %w", err)
}

sp, bundleVerified, err := cosign.VerifyImageSignatures(ctx, ref, co)
if err != nil {
Expand Down

0 comments on commit 75b2bd1

Please sign in to comment.