Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: push oci attestations #110

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions cmd/oci/oci.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package oci

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/file"

"github.com/buildsafedev/bsf/cmd/build"
binit "github.com/buildsafedev/bsf/cmd/init"
Expand All @@ -18,6 +24,7 @@ import (
nixcmd "github.com/buildsafedev/bsf/pkg/nix/cmd"
"github.com/buildsafedev/bsf/pkg/oci"
"github.com/buildsafedev/bsf/pkg/platformutils"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

var (
Expand Down Expand Up @@ -208,6 +215,8 @@ var OCICmd = &cobra.Command{
fmt.Println(styles.SucessStyle.Render(fmt.Sprintf("Image %s loaded to podman", artifact.Name)))
}

// artifactType := "application/json"

if push {
fmt.Println(styles.HighlightStyle.Render("Pushing image to registry..."))
if digest {
Expand All @@ -232,10 +241,113 @@ var OCICmd = &cobra.Command{
}
fmt.Println(styles.SucessStyle.Render(fmt.Sprintf("Image %s pushed to registry", artifact.Name)))
}
ctx := context.Background()
attestationFile := "bsf-result/attestation.jsonl"
artifactType := "application/json"

store, err := file.New("")
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}
defer store.Close()

descs, err := loadFiles(ctx, store, nil, []string{attestationFile})
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error:", err.Error()))
os.Exit(1)
}

fetchOpts := oras.DefaultResolveOptions
subject, err := oras.Resolve(ctx, &file.Store{}, artifact.Name, fetchOpts)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("failed to resolve subject image:", err.Error()))
os.Exit(1)
}

packOpts := oras.PackManifestOptions{
Subject: &subject,
Layers: descs,
}
root, err := oras.PackManifest(ctx, store, oras.PackManifestVersion1_1, artifactType, packOpts)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error packing manifest:", err.Error()))
os.Exit(1)
}

err = oras.CopyGraph(ctx, store, nil, root, oras.DefaultCopyGraphOptions)
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error copying graph:", err.Error()))
os.Exit(1)
}
}

},
}

func loadFiles(ctx context.Context, store *file.Store, annotations map[string]map[string]string, fileRefs []string) ([]ocispec.Descriptor, error) {
var files []ocispec.Descriptor
for _, fileRef := range fileRefs {
filename, mediaType, err := Parse(fileRef, "")
if err != nil {
return nil, err
}

// get shortest absolute path as unique name
name := filepath.Clean(filename)
if !filepath.IsAbs(name) {
name = filepath.ToSlash(name)
}

file, err := addFile(ctx, store, name, mediaType, filename)
if err != nil {
return nil, err
}
if value, ok := annotations[filename]; ok {
if file.Annotations == nil {
file.Annotations = value
} else {
for k, v := range value {
file.Annotations[k] = v
}
}
}
files = append(files, file)
}

if len(files) == 0 {
return nil, nil
}

return files, nil
}

func addFile(ctx context.Context, store *file.Store, name string, mediaType string, filename string) (ocispec.Descriptor, error) {
file, err := store.Add(ctx, name, mediaType, filename)
if err != nil {
var pathErr *fs.PathError
if errors.As(err, &pathErr) {
err = pathErr
}
return ocispec.Descriptor{}, err
}
return file, nil
}

// Parse parses file reference on unix.
func Parse(reference string, defaultMetadata string) (filePath, metadata string, err error) {
i := strings.LastIndex(reference, ":")
if i < 0 {
filePath, metadata = reference, defaultMetadata
} else {
filePath, metadata = reference[:i], reference[i+1:]
}
if filePath == "" {
return "", "", fmt.Errorf("found empty file path in %q", reference)
}
return filePath, metadata, nil
}

// ProcessPlatformAndConfig processes the platform and config file
func ProcessPlatformAndConfig(conf *hcl2nix.Config, plat string, envName string) (hcl2nix.OCIArtifact, string, error) {
if plat == "" {
Expand Down
52 changes: 42 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/buildsafedev/bsf

go 1.21.0
go 1.22.4

toolchain go1.22.2
toolchain go1.22.5

require (
github.com/BurntSushi/toml v1.2.1
Expand All @@ -15,7 +15,6 @@ require (
github.com/elewis787/boa v0.1.2
github.com/go-git/go-git/v5 v5.11.0
github.com/google/go-cmp v0.6.0
github.com/google/go-containerregistry v0.20.1
github.com/google/go-github/v62 v62.0.0
github.com/hashicorp/hcl/v2 v2.19.1
github.com/in-toto/in-toto-golang v0.9.0
Expand All @@ -28,33 +27,66 @@ require (
golang.org/x/mod v0.17.0
golang.org/x/oauth2 v0.21.0
golang.org/x/sync v0.7.0
golang.org/x/term v0.21.0
golang.org/x/term v0.22.0
google.golang.org/api v0.190.0
google.golang.org/grpc v1.64.1
google.golang.org/protobuf v1.34.2
oras.land/oras-go v1.2.6
oras.land/oras-go/v2 v2.5.0
zombiezen.com/go/nix v0.0.0-20240320004700-0734e137f7fe
)

require (
cloud.google.com/go/auth v0.7.3 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.3 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/containerd/containerd v1.7.20 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/docker/cli v27.0.3+incompatible // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/cli v27.1.0+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v27.1.0+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.2 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/go-containerregistry v0.20.1 // indirect
github.com/google/go-github/v61 v61.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/buildkit v0.14.0 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/puzpuzpuz/xsync v1.5.2 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
)

require (
Expand All @@ -80,7 +112,7 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-billy/v5 v5.5.0
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down Expand Up @@ -123,12 +155,12 @@ require (
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/zclconf/go-cty v1.13.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.6 // indirect
Expand Down
Loading
Loading