Skip to content

Commit

Permalink
lazy init fulcio root (#519)
Browse files Browse the repository at this point in the history
Signed-off-by: Asra Ali <asraa@google.com>
  • Loading branch information
asraa committed Aug 4, 2021
1 parent fbc9831 commit ef05414
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
17 changes: 14 additions & 3 deletions cmd/cosign/cli/fulcio/fulcio.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"io/ioutil"
"os"
"sync"

"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
Expand Down Expand Up @@ -179,9 +180,19 @@ func (f *Signer) PublicKey(opts ...signature.PublicKeyOption) (crypto.PublicKey,

var _ signature.Signer = &Signer{}

var Roots *x509.CertPool
var (
rootsOnce sync.Once
roots *x509.CertPool
)

func GetRoots() *x509.CertPool {
rootsOnce.Do(func() {
roots = initRoots()
})
return roots
}

func init() {
func initRoots() *x509.CertPool {
cp := x509.NewCertPool()
rootEnv := os.Getenv(altRoot)
if rootEnv != "" {
Expand All @@ -195,5 +206,5 @@ func init() {
} else if !cp.AppendCertsFromPEM([]byte(rootPem)) {
panic("error creating root cert pool")
}
Roots = cp
return cp
}
2 changes: 1 addition & 1 deletion cmd/cosign/cli/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (c *VerifyCommand) Exec(ctx context.Context, args []string) (err error) {

co := &cosign.CheckOpts{
Annotations: *c.Annotations,
RootCerts: fulcio.Roots,
RootCerts: fulcio.GetRoots(),
RegistryClientOpts: DefaultRegistryClientOpts(ctx),
}
if c.CheckClaims {
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/verify_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c *VerifyAttestationCommand) Exec(ctx context.Context, args []string) (err
}

co := &cosign.CheckOpts{
RootCerts: fulcio.Roots,
RootCerts: fulcio.GetRoots(),
RegistryClientOpts: DefaultRegistryClientOpts(ctx),
SigTagSuffixOverride: cosign.AttestationTagSuffix,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cosign/cli/verify_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func VerifyBlobCmd(ctx context.Context, ko KeyOpts, certRef, sigRef, blobRef str
}

if cert != nil { // cert
if err := cosign.TrustedCert(cert, fulcio.Roots); err != nil {
if err := cosign.TrustedCert(cert, fulcio.GetRoots()); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "Certificate is trusted by Fulcio Root CA")
Expand Down
2 changes: 1 addition & 1 deletion cmd/sget/cli/sget.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func SgetCmd(ctx context.Context, imageRef, keyRef string) (io.ReadCloser, error
co := &cosign.CheckOpts{
ClaimVerifier: cosign.SimpleClaimVerifier,
VerifyBundle: true,
RootCerts: fulcio.Roots,
RootCerts: fulcio.GetRoots(),
RegistryClientOpts: []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
remote.WithContext(ctx),
Expand Down
2 changes: 1 addition & 1 deletion copasetic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func main() {
VerifyOpts: []signature.VerifyOption{ctxOpt},
PKOpts: []signature.PublicKeyOption{ctxOpt},
ClaimVerifier: cosign.SimpleClaimVerifier,
RootCerts: fulcio.Roots,
RootCerts: fulcio.GetRoots(),
RegistryClientOpts: []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
remote.WithContext(bctx.Context),
Expand Down

0 comments on commit ef05414

Please sign in to comment.