Files
Ramon Petgrave c789437815 feat: refactor: use sigstore-go for fetching TrustedRoot (#791)
Uses the `sigstore-go` library for fetching the `TrustedRoot`, which
contains the Sigstore infrastructure certificates needed to validate the
leaf ephemeral certificates used to sign artifacts.

Refactors:

- replace `TrustedRootSingleton()` with `getDefaultCosignCheckOpts()`,
since only `VerifyImage()` will now need that data.
- replace `cosign.ValidateAndUnpackCert`
with`sigstoreVerify.VerifyLeafCertificate()`
- use `sync.Once` for sigstore and rekor clients, and the `TrustedRoot`

## Testing

- existing tests continue to pass
- [negative tests
](https://github.com/slsa-framework/slsa-verifier/blob/d96b9777090694fa5096ee1b9c710a46b5a66f5e/cli/slsa-verifier/main_regression_test.go#L450-L471)
against rekor TLogs
- manual invocations of `verify-artifact`.

---------

Signed-off-by: Ramon Petgrave <ramon.petgrave64@gmail.com>
2024-08-02 21:47:50 +00:00

67 lines
1.9 KiB
Go

package gha
import (
"context"
"fmt"
"sync"
"github.com/sigstore/cosign/v2/pkg/cosign"
"github.com/sigstore/sigstore/pkg/fulcioroots"
serrors "github.com/slsa-framework/slsa-verifier/v2/errors"
)
var (
// defaultCosignCheckOpts are the default options for cosign checks.
defaultCosignCheckOpts *cosign.CheckOpts
// defaultCosignCheckOptsOnce is used for initializing the defaultCosignCheckOpts.
defaultCosignCheckOptsOnce = new(sync.Once)
)
// getDefaultCosignCheckOpts returns the default cosign check options.
// This is cached in memory.
// CheckOpts.RegistryClientOpts must be added by the receiver.
func getDefaultCosignCheckOpts(ctx context.Context) (*cosign.CheckOpts, error) {
var getErr error
// Initialize the defaultCosignCheckOpts.
// defaultCosignCheckOptsOnce is reinitialized upon error.
defaultCosignCheckOptsOnce.Do(func() {
rootCerts, err := fulcioroots.Get()
if err != nil {
getErr = fmt.Errorf("%w: %s", serrors.ErrorInternal, err)
defaultCosignCheckOptsOnce = new(sync.Once)
return
}
intermediateCerts, err := fulcioroots.GetIntermediates()
if err != nil {
getErr = fmt.Errorf("%w: %s", serrors.ErrorInternal, err)
defaultCosignCheckOptsOnce = new(sync.Once)
return
}
rekorPubKeys, err := cosign.GetRekorPubs(ctx)
if err != nil {
getErr = fmt.Errorf("%w: %s", serrors.ErrorRekorPubKey, err)
defaultCosignCheckOptsOnce = new(sync.Once)
return
}
ctPubKeys, err := cosign.GetCTLogPubs(ctx)
if err != nil {
// this is unexpected, hold on to this error.
getErr = fmt.Errorf("%w: %s", serrors.ErrorInternal, err)
defaultCosignCheckOptsOnce = new(sync.Once)
return
}
defaultCosignCheckOpts = &cosign.CheckOpts{
RootCerts: rootCerts,
IntermediateCerts: intermediateCerts,
RekorPubKeys: rekorPubKeys,
CTLogPubKeys: ctPubKeys,
}
})
if getErr != nil {
return nil, getErr
}
return defaultCosignCheckOpts, nil
}