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

61 lines
1.6 KiB
Go

package utils
import (
"sync"
sigstoreRoot "github.com/sigstore/sigstore-go/pkg/root"
sigstoreTUF "github.com/sigstore/sigstore-go/pkg/tuf"
)
var (
// cache the default Sigstore TUF client.
defaultSigstoreTUFClient *sigstoreTUF.Client
// defaultSigstoreTUFClientOnce is used for initializing the defaultSigstoreTUFClient.
defaultSigstoreTUFClientOnce = new(sync.Once)
// cache the trusted root.
trustedRoot *sigstoreRoot.LiveTrustedRoot
// trustedRootOnce is used for initializing the trustedRoot.
trustedRootOnce = new(sync.Once)
)
// SigstoreTUFClient is the interface for the Sigstore TUF client.
type SigstoreTUFClient interface {
// GetTarget retrieves the target file from the TUF repository.
GetTarget(target string) ([]byte, error)
}
// GetDefaultSigstoreTUFClient returns the default Sigstore TUF client.
// The client will be cached in memory.
func GetDefaultSigstoreTUFClient() (*sigstoreTUF.Client, error) {
var err error
defaultSigstoreTUFClientOnce.Do(func() {
defaultSigstoreTUFClient, err = sigstoreTUF.DefaultClient()
if err != nil {
defaultSigstoreTUFClientOnce = new(sync.Once)
return
}
})
if err != nil {
return nil, err
}
return defaultSigstoreTUFClient, nil
}
// GetSigstoreTrustedRoot returns the trusted root for the Sigstore TUF client.
func GetSigstoreTrustedRoot() (*sigstoreRoot.LiveTrustedRoot, error) {
var err error
trustedRootOnce.Do(func() {
opts := sigstoreTUF.DefaultOptions()
trustedRoot, err = sigstoreRoot.NewLiveTrustedRoot(opts)
if err != nil {
trustedRootOnce = new(sync.Once)
return
}
})
if err != nil {
return nil, err
}
return trustedRoot, nil
}