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

fix: publish cmd --sbom-path not writing files #853

Merged
Merged
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
14 changes: 13 additions & 1 deletion internal/cli/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -122,6 +123,7 @@ in a keychain.`,
remoteOpts = append(remoteOpts, remote.Reuse(puller))

if err := PublishCmd(cmd.Context(), imageRefs, archs, remoteOpts,
sbomPath,
[]build.Option{
build.WithLogger(logger),
build.WithConfig(args[0]),
Expand Down Expand Up @@ -186,7 +188,7 @@ in a keychain.`,
return cmd
}

func PublishCmd(ctx context.Context, outputRefs string, archs []types.Architecture, ropt []remote.Option, buildOpts []build.Option, publishOpts []PublishOption) error {
func PublishCmd(ctx context.Context, outputRefs string, archs []types.Architecture, ropt []remote.Option, sbomPath string, buildOpts []build.Option, publishOpts []PublishOption) error {
ctx, span := otel.Tracer("apko").Start(ctx, "PublishCmd")
defer span.End()

Expand Down Expand Up @@ -353,6 +355,16 @@ func PublishCmd(ctx context.Context, outputRefs string, archs []types.Architectu
}
}

// copy sboms over to the sbomPath target directory
if sbomPath != "" {
for _, sbom := range sboms {
// because os.Rename fails across partitions, we do our own
if err := rename(sbom.Path, filepath.Join(sbomPath, filepath.Base(sbom.Path))); err != nil {
return fmt.Errorf("moving sbom: %w", err)
}
}
}

// Write the image digest to STDOUT in order to enable command
// composition e.g. kn service create --image=$(apko publish ...)
fmt.Println(finalDigest)
Expand Down
13 changes: 12 additions & 1 deletion internal/cli/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -38,6 +39,7 @@ import (

func TestPublish(t *testing.T) {
ctx := context.Background()
tmp := t.TempDir()

// Set up a registry that requires we see a magic header.
// This allows us to make sure that remote options are getting passed
Expand All @@ -63,7 +65,11 @@ func TestPublish(t *testing.T) {
opts := []build.Option{build.WithConfig(config), build.WithTags(dst), build.WithSBOMFormats(sbom.DefaultOptions.Formats)}
publishOpts := []cli.PublishOption{cli.WithTags(dst)}

err = cli.PublishCmd(ctx, outputRefs, archs, ropt, opts, publishOpts)
sbomPath := filepath.Join(tmp, "sboms")
err = os.MkdirAll(sbomPath, 0o750)
require.NoError(t, err)

err = cli.PublishCmd(ctx, outputRefs, archs, ropt, sbomPath, opts, publishOpts)
require.NoError(t, err)

ref, err := name.ParseReference(dst)
Expand Down Expand Up @@ -125,6 +131,11 @@ func TestPublish(t *testing.T) {
got := m.Layers[0].Digest.String()
require.Equal(t, wantBoms[i], got)
}

// Check that the sbomPath is not empty.
sboms, err := os.ReadDir(sbomPath)
require.NoError(t, err)
require.NotEmpty(t, sboms)
}

type sentinel struct {
Expand Down
Loading