| 1 |
package client |
| 2 |
|
| 3 |
import ( |
| 4 |
pb "github.com/moby/buildkit/frontend/gateway/pb" |
| 5 |
"github.com/moby/buildkit/solver/result" |
| 6 |
digest "github.com/opencontainers/go-digest" |
| 7 |
"github.com/pkg/errors" |
| 8 |
) |
| 9 |
|
| 10 |
func AttestationToPB[T any](a *result.Attestation[T]) (*pb.Attestation, error) { |
| 11 |
if a.ContentFunc != nil { |
| 12 |
return nil, errors.Errorf("attestation callback cannot be sent through gateway") |
| 13 |
} |
| 14 |
|
| 15 |
subjects := make([]*pb.InTotoSubject, len(a.InToto.Subjects)) |
| 16 |
for i, subject := range a.InToto.Subjects { |
| 17 |
subjects[i] = &pb.InTotoSubject{ |
| 18 |
Kind: subject.Kind, |
| 19 |
Name: subject.Name, |
| 20 |
Digest: digestSliceToPB(subject.Digest), |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
return &pb.Attestation{ |
| 25 |
Kind: a.Kind, |
| 26 |
Metadata: a.Metadata, |
| 27 |
Path: a.Path, |
| 28 |
InTotoPredicateType: a.InToto.PredicateType, |
| 29 |
InTotoSubjects: subjects, |
| 30 |
}, nil |
| 31 |
} |
| 32 |
|
| 33 |
func AttestationFromPB[T any](a *pb.Attestation) (*result.Attestation[T], error) { |
| 34 |
if a == nil { |
| 35 |
return nil, errors.Errorf("invalid nil attestation") |
| 36 |
} |
| 37 |
subjects := make([]result.InTotoSubject, len(a.InTotoSubjects)) |
| 38 |
for i, subject := range a.InTotoSubjects { |
| 39 |
if subject == nil { |
| 40 |
return nil, errors.Errorf("invalid nil attestation subject") |
| 41 |
} |
| 42 |
subjects[i] = result.InTotoSubject{ |
| 43 |
Kind: subject.Kind, |
| 44 |
Name: subject.Name, |
| 45 |
Digest: digestSliceFromPB(subject.Digest), |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return &result.Attestation[T]{ |
| 50 |
Kind: a.Kind, |
| 51 |
Metadata: a.Metadata, |
| 52 |
Path: a.Path, |
| 53 |
InToto: result.InTotoAttestation{ |
| 54 |
PredicateType: a.InTotoPredicateType, |
| 55 |
Subjects: subjects, |
| 56 |
}, |
| 57 |
}, nil |
| 58 |
} |
| 59 |
|
| 60 |
func digestSliceToPB(elems []digest.Digest) []string { |
| 61 |
clone := make([]string, len(elems)) |
| 62 |
for i, e := range elems { |
| 63 |
clone[i] = string(e) |
| 64 |
} |
| 65 |
return clone |
| 66 |
} |
| 67 |
|
| 68 |
func digestSliceFromPB(elems []string) []digest.Digest { |
| 69 |
clone := make([]digest.Digest, len(elems)) |
| 70 |
for i, e := range elems { |
| 71 |
clone[i] = digest.Digest(e) |
| 72 |
} |
| 73 |
return clone |
| 74 |
} |