| 1 |
package signaturediscovery |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/google/go-tpm-tools/verifier/oci" |
| 8 |
"github.com/google/go-tpm-tools/verifier/oci/cosign" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
// FakeRepoWithSignatures represents an OCI registry with container image signatures for testing. |
| 13 |
FakeRepoWithSignatures = "repo with signatures" |
| 14 |
// FakeRepoWithNoSignatures represents an OCI registry with no container image signatures for testing. |
| 15 |
FakeRepoWithNoSignatures = "repo with no signatures" |
| 16 |
// FakeNonExistRepo represents a non-exist OCI registry for testing. |
| 17 |
FakeNonExistRepo = "nonexist repo" |
| 18 |
// FakeRepoWithAllInvalidSignatures represents an OCI registry with all invalid container image signatures for testing. |
| 19 |
FakeRepoWithAllInvalidSignatures = "repo with all invalid signatures" |
| 20 |
// FakeRepoWithPartialValidSignatures represents an OCI registry with parital valid container image signatures for testing. |
| 21 |
FakeRepoWithPartialValidSignatures = "repo with parital valid signatures" |
| 22 |
) |
| 23 |
|
| 24 |
type fakeClient struct{} |
| 25 |
|
| 26 |
// NewFakeClient constructs a new fake signature discovery client. |
| 27 |
func NewFakeClient() Fetcher { |
| 28 |
return &fakeClient{} |
| 29 |
} |
| 30 |
|
| 31 |
// FetchImageSignatures returns hardcoded signatures based on the given target repository. |
| 32 |
func (f *fakeClient) FetchImageSignatures(_ context.Context, targetRepository string) ([]oci.Signature, error) { |
| 33 |
switch targetRepository { |
| 34 |
case FakeRepoWithSignatures: |
| 35 |
return []oci.Signature{ |
| 36 |
cosign.NewFakeSignature("test data", oci.ECDSAP256SHA256), |
| 37 |
cosign.NewFakeSignature("hello world", oci.RSASSAPKCS1V152048SHA256), |
| 38 |
}, nil |
| 39 |
case FakeRepoWithNoSignatures, FakeNonExistRepo: |
| 40 |
return nil, fmt.Errorf("cannot fetch the signature object from target repository [%s]", targetRepository) |
| 41 |
case FakeRepoWithAllInvalidSignatures: |
| 42 |
return []oci.Signature{ |
| 43 |
cosign.NewFakeSignature("invalid signature", "unsupported"), |
| 44 |
cosign.NewFakeSignature("invalid signature", "unsupported"), |
| 45 |
}, nil |
| 46 |
case FakeRepoWithPartialValidSignatures: |
| 47 |
return []oci.Signature{ |
| 48 |
cosign.NewFakeSignature("test data", oci.ECDSAP256SHA256), |
| 49 |
cosign.NewFakeSignature("invalid signature", "unsupported"), |
| 50 |
}, nil |
| 51 |
default: |
| 52 |
return []oci.Signature{}, nil |
| 53 |
} |
| 54 |
} |