Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Jun 26, 2024
1 parent 336502b commit 84cf365
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 12 deletions.
26 changes: 20 additions & 6 deletions attest/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
)

type DirContents struct {
types.GenericStatement[SourceDirectory]
types.GenericStatement[SourceDirectoryContents]
}

type SourceDirectory struct {
Expand All @@ -25,14 +25,16 @@ type SourceDirectory struct {
VCSEntries *types.PathCheckSummaryCollection `json:"vcsEntries"`
}

type SourceDirectoryContents struct {
SourceDirectory `json:"containedInDirectory"`
}

func MakeDirContentsStatement(dir string, entries *types.PathCheckSummaryCollection) types.Statement {
return &DirContents{
types.MakeStatement[SourceDirectory](
types.MakeStatement[SourceDirectoryContents](
ManifestDirPredicateType,
struct {
SourceDirectory `json:"containedInDirectory"`
}{
SourceDirectory{
SourceDirectoryContents{
SourceDirectory: SourceDirectory{
Path: dir,
VCSEntries: entries,
},
Expand All @@ -42,6 +44,14 @@ func MakeDirContentsStatement(dir string, entries *types.PathCheckSummaryCollect
}
}

func MakeDirContentsStatementFrom(statement types.Statement) DirContents {
dirContents := DirContents{
GenericStatement: attestTypes.GenericStatement[SourceDirectoryContents]{},
}
dirContents.ConvertFrom(statement)
return dirContents
}

func (a SourceDirectory) Compare(b SourceDirectory) types.Cmp {
if cmp := cmp.Compare(a.Path, b.Path); cmp != 0 {
return &cmp
Expand All @@ -55,3 +65,7 @@ func (a SourceDirectory) Compare(b SourceDirectory) types.Cmp {
cmp := a.VCSEntries.Compare(*b.VCSEntries)
return &cmp
}

func (a SourceDirectoryContents) Compare(b SourceDirectoryContents) types.Cmp {
return a.SourceDirectory.Compare(b.SourceDirectory)
}
29 changes: 27 additions & 2 deletions attest/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ func Export(s ExportableStatement) toto.Statement {
}
}

func FilterByPredicateType(t string, s Statements) Statements {
results := Statements{}
for i := range s {
if s[i].GetType() == t {
results = append(results, s[i])
}
}
return results
}

type StamentConverter[T any] struct {
Statement
}

func (s *GenericStatement[T]) ConvertFrom(statement Statement) error {
predicate, ok := statement.GetPredicate().(ComparablePredicate[T])
if !ok {
return fmt.Errorf("cannot convert statement with predicte of type %T into %T", statement.GetPredicate(), GenericStatement[T]{})
}

*s = MakeStatement[T](statement.GetType(), predicate, statement.GetSubject()...)
return nil
}

func (s Statements) Export() []toto.Statement {
statements := make([]toto.Statement, len(s))
for i := range s {
Expand Down Expand Up @@ -368,8 +392,9 @@ func comparePathCheckSummaries(a, b PathCheckSummary) int {
return cmp.Compare(a.Common().Path, b.Common().Path)
}

func (p Predicate[T]) GetType() string { return p.Type }
func (p Predicate[T]) GetPredicate() any { return p.ComparablePredicate }
func (p Predicate[T]) GetType() string { return p.Type }
func (p Predicate[T]) GetPredicate() any { return p.ComparablePredicate }
func (p Predicate[T]) GetUnderlyingPredicate() T { return p.ComparablePredicate.(T) }

func (p Predicate[T]) Compare(b any) Cmp {
if b, ok := b.(Predicate[T]); ok {
Expand Down
56 changes: 52 additions & 4 deletions oci/artefact.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"path/filepath"
"time"

"golang.org/x/mod/semver"

ociclient "github.com/fluxcd/pkg/oci"
"github.com/go-git/go-git/v5/utils/ioutil"
"github.com/google/go-containerregistry/pkg/compression"
Expand All @@ -23,7 +25,9 @@ import (
"github.com/google/go-containerregistry/pkg/v1/tarball"
typesv1 "github.com/google/go-containerregistry/pkg/v1/types"

"github.com/errordeveloper/tape/attest/manifest"
attestTypes "github.com/errordeveloper/tape/attest/types"
"github.com/errordeveloper/tape/attest/vcs/git"
manifestTypes "github.com/errordeveloper/tape/manifest/types"
)

Expand Down Expand Up @@ -250,7 +254,11 @@ func (c *Client) PushArtefact(ctx context.Context, destinationRef, sourceDir str
}
hash := hex.EncodeToString(c.hash.Sum(nil))
tag := repo.Tag(manifestTypes.ConfigImageTagPrefix + hash)
tagAlias := tag.Context().Tag(manifestTypes.ConfigImageTagPrefix + hash[:7])

tagAliases := append(
SemVerTagsFromAttestations(ctx, tag, sourceAttestations...),
tag.Context().Tag(manifestTypes.ConfigImageTagPrefix+hash[:7]),
)

if timestamp == nil {
timestamp = new(time.Time)
Expand Down Expand Up @@ -341,11 +349,51 @@ func (c *Client) PushArtefact(ctx context.Context, destinationRef, sourceDir str
return "", fmt.Errorf("pushing index failed: %w", err)
}

if err := remote.Tag(tagAlias, index, c.remoteWithContext(ctx)...); err != nil {
return "", fmt.Errorf("adding alias tagging failed: %w", err)
for i := range tagAliases {
if err := remote.Tag(tagAliases[i], index, c.remoteWithContext(ctx)...); err != nil {
return "", fmt.Errorf("adding alias tagging failed: %w", err)
}
}
// TODO: reteurn tag and all of its aliases
return tagAliases[0].String() + "@" + digest.String(), err
}

func SemVerTagsFromAttestations(ctx context.Context, tag name.Tag, sourceAttestations ...attestTypes.Statement) []name.Tag {
statements := attestTypes.FilterByPredicateType(manifest.ManifestDirPredicateType, sourceAttestations)
if len(statements) != 1 {
return []name.Tag{}
}

return tagAlias.String() + "@" + digest.String(), err
entries := manifest.MakeDirContentsStatementFrom(statements[0]).GetUnderlyingPredicate().VCSEntries
if len(entries.EntryGroups) != 1 && len(entries.Providers) != 1 ||
entries.Providers[0] != git.ProviderName {
return []name.Tag{}
}
if len(entries.EntryGroups[0]) == 0 {
return []name.Tag{}
}

// TODO: try to use generics for this?
groupSummary, ok := entries.EntryGroups[0][0].Full().(*git.Summary)
if !ok {
return []name.Tag{}
}
ref := groupSummary.Git.Reference
if len(ref.Tags) == 0 {
return []name.Tag{}
}
// TODO: detect tags with groupSummary.Path+"/" as prefix and priorities them
tags := make([]name.Tag, 0, len(ref.Tags))
for i := range ref.Tags {
t := ref.Tags[i].Name
if semver.IsValid(t) || semver.IsValid("v"+t) {
tags = append(tags, tag.Context().Tag(ref.Tags[i].Name))
}
}
if len(tags) == 0 {
return []name.Tag{}
}
return tags
}

func makeDescriptorWithPlatform() Descriptor {
Expand Down

0 comments on commit 84cf365

Please sign in to comment.