2024-10-17 13:40:17 -05:00
|
|
|
/*
|
2024-10-18 09:25:31 -05:00
|
|
|
Copyright Docker attest authors
|
2024-10-17 13:40:17 -05:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
2024-10-18 09:25:31 -05:00
|
|
|
|
2024-08-12 14:49:52 -05:00
|
|
|
package attestation
|
2024-07-16 10:05:17 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2024-09-30 20:53:13 +01:00
|
|
|
"github.com/docker/attest/internal/test"
|
2024-09-02 16:17:50 +01:00
|
|
|
"github.com/docker/attest/oci"
|
2024-07-16 10:05:17 +01:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-04 10:20:00 +01:00
|
|
|
// ensure MockResolver implements Resolver.
|
|
|
|
|
var _ oci.ImageDetailsResolver = MockResolver{}
|
|
|
|
|
|
2024-07-16 10:05:17 +01:00
|
|
|
type MockResolver struct {
|
2024-10-07 13:34:04 -05:00
|
|
|
Envs []*EnvelopeReference
|
2024-09-04 10:20:00 +01:00
|
|
|
Image string
|
|
|
|
|
PlatformFn func() (*v1.Platform, error)
|
|
|
|
|
DescriptorFn func() (*v1.Descriptor, error)
|
|
|
|
|
ImangeNameFn func() (string, error)
|
2024-07-16 10:05:17 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-07 13:34:04 -05:00
|
|
|
func (r MockResolver) Attestations(_ context.Context, _ string) ([]*EnvelopeReference, error) {
|
2024-07-16 10:05:17 +01:00
|
|
|
return r.Envs, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 15:35:15 +01:00
|
|
|
func (r MockResolver) ImageName(_ context.Context) (string, error) {
|
2024-09-04 10:20:00 +01:00
|
|
|
if r.Image != "" {
|
|
|
|
|
return r.Image, nil
|
|
|
|
|
}
|
|
|
|
|
if r.ImangeNameFn != nil {
|
|
|
|
|
return r.ImangeNameFn()
|
|
|
|
|
}
|
2024-07-16 10:05:17 +01:00
|
|
|
return "library/alpine:latest", nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 15:35:15 +01:00
|
|
|
func (r MockResolver) ImageDescriptor(_ context.Context) (*v1.Descriptor, error) {
|
2024-09-04 10:20:00 +01:00
|
|
|
if r.DescriptorFn != nil {
|
|
|
|
|
return r.DescriptorFn()
|
|
|
|
|
}
|
2024-09-30 20:53:13 +01:00
|
|
|
digest, err := v1.NewHash(test.UnsignedLinuxAMD64ImageDigest)
|
2024-07-16 10:05:17 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &v1.Descriptor{
|
|
|
|
|
Digest: digest,
|
|
|
|
|
Size: 1234,
|
|
|
|
|
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 15:35:15 +01:00
|
|
|
func (r MockResolver) ImagePlatform(_ context.Context) (*v1.Platform, error) {
|
2024-09-04 10:20:00 +01:00
|
|
|
if r.PlatformFn != nil {
|
|
|
|
|
return r.PlatformFn()
|
|
|
|
|
}
|
2024-08-12 14:49:52 -05:00
|
|
|
return oci.ParsePlatform("linux/amd64")
|
2024-07-16 10:05:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MockRegistryResolver struct {
|
|
|
|
|
Subject *v1.Descriptor
|
|
|
|
|
ImageNameStr string
|
|
|
|
|
*MockResolver
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 15:35:15 +01:00
|
|
|
func (r *MockRegistryResolver) ImageDescriptor(_ context.Context) (*v1.Descriptor, error) {
|
2024-07-16 10:05:17 +01:00
|
|
|
return r.Subject, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 15:35:15 +01:00
|
|
|
func (r *MockRegistryResolver) ImageName(_ context.Context) (string, error) {
|
2024-07-16 10:05:17 +01:00
|
|
|
return r.ImageNameStr, nil
|
|
|
|
|
}
|