-
Notifications
You must be signed in to change notification settings - Fork 545
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
Add COSIGN_OCI_EXPERIMENTAL, push .sig/.sbom using OCI 1.1+ digest tag #2684
Merged
+410
−17
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d118235
Add COSIGN_OCI_EXPERIMENTAL, push .sig/.sbom using OCI 1.1+ digest tag
jdolitsky 9dee558
Merge branch 'main' of github.com:sigstore/cosign into oci-1-1-digest…
jdolitsky 42868b1
upgrade to latest ggcr with referrers API support
jdolitsky a2542ea
unexport methods whereever possible
jdolitsky 4c18c95
Merge branch 'sigstore:main' into oci-1-1-digest-tag
jdolitsky 6120b11
Use ui logger wherever possible
jdolitsky af86ae9
add TODO about using created annotations
jdolitsky 4bd1d04
remove logging about unable to to find new type of attachment
jdolitsky 2ce0564
fixes from review
jdolitsky e899a84
push the config blob
jdolitsky e79a1c2
run docgen
jdolitsky d337642
fix for e2e tests
jdolitsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,18 +20,31 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/google/go-containerregistry/pkg/logs" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/google/go-containerregistry/pkg/v1/remote/transport" | ||
ocistatic "github.com/google/go-containerregistry/pkg/v1/static" | ||
ocitypes "github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options" | ||
ociexperimental "github.com/sigstore/cosign/v2/internal/pkg/oci/remote" | ||
"github.com/sigstore/cosign/v2/internal/ui" | ||
ociremote "github.com/sigstore/cosign/v2/pkg/oci/remote" | ||
"github.com/sigstore/cosign/v2/pkg/oci/static" | ||
) | ||
|
||
func SBOMCmd(ctx context.Context, regOpts options.RegistryOptions, sbomRef string, sbomType types.MediaType, imageRef string) error { | ||
func SBOMCmd(ctx context.Context, regOpts options.RegistryOptions, regExpOpts options.RegistryExperimentalOptions, sbomRef string, sbomType ocitypes.MediaType, imageRef string) error { | ||
if regExpOpts.RegistryReferrersMode == options.RegistryReferrersModeOCI11 { | ||
return sbomCmdOCIExperimental(ctx, regOpts, sbomRef, sbomType, imageRef) | ||
} | ||
|
||
ref, err := name.ParseReference(imageRef, regOpts.NameOptions()...) | ||
if err != nil { | ||
return err | ||
|
@@ -52,14 +65,75 @@ func SBOMCmd(ctx context.Context, regOpts options.RegistryOptions, sbomRef strin | |
return err | ||
} | ||
|
||
fmt.Fprintf(os.Stderr, "Uploading SBOM file for [%s] to [%s] with mediaType [%s].\n", ref.Name(), dstRef.Name(), sbomType) | ||
ui.Infof(ctx, "Uploading SBOM file for [%s] to [%s] with mediaType [%s].\n", ref.Name(), dstRef.Name(), sbomType) | ||
img, err := static.NewFile(b, static.WithLayerMediaType(sbomType)) | ||
if err != nil { | ||
return err | ||
} | ||
return remote.Write(dstRef, img, regOpts.GetRegistryClientOpts(ctx)...) | ||
} | ||
|
||
func sbomCmdOCIExperimental(ctx context.Context, regOpts options.RegistryOptions, sbomRef string, sbomType ocitypes.MediaType, imageRef string) error { | ||
var dig name.Digest | ||
ref, err := name.ParseReference(imageRef, regOpts.NameOptions()...) | ||
if err != nil { | ||
return err | ||
} | ||
if digr, ok := ref.(name.Digest); ok { | ||
dig = digr | ||
} else { | ||
desc, err := remote.Head(ref, regOpts.GetRegistryClientOpts(ctx)...) | ||
if err != nil { | ||
return err | ||
} | ||
dig = ref.Context().Digest(desc.Digest.String()) | ||
} | ||
|
||
artifactType := ociexperimental.ArtifactType("sbom") | ||
|
||
desc, err := remote.Head(dig, regOpts.GetRegistryClientOpts(ctx)...) | ||
var terr *transport.Error | ||
if errors.As(err, &terr) && terr.StatusCode == http.StatusNotFound { | ||
h, err := v1.NewHash(dig.DigestStr()) | ||
if err != nil { | ||
return err | ||
} | ||
// The subject doesn't exist, attach to it as if it's an empty OCI image. | ||
logs.Progress.Println("subject doesn't exist, attaching to empty image") | ||
desc = &v1.Descriptor{ | ||
ArtifactType: artifactType, | ||
MediaType: ocitypes.OCIManifestSchema1, | ||
Size: 0, | ||
Digest: h, | ||
} | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
b, err := sbomBytes(sbomRef) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
empty := mutate.MediaType( | ||
mutate.ConfigMediaType(empty.Image, ocitypes.MediaType(artifactType)), | ||
ocitypes.OCIManifestSchema1) | ||
att, err := mutate.AppendLayers(empty, ocistatic.NewLayer(b, sbomType)) | ||
if err != nil { | ||
return err | ||
} | ||
att = mutate.Subject(att, *desc).(v1.Image) | ||
attdig, err := att.Digest() | ||
if err != nil { | ||
return err | ||
} | ||
dstRef := ref.Context().Digest(attdig.String()) | ||
|
||
fmt.Fprintf(os.Stderr, "Uploading SBOM file for [%s] to [%s] with config.mediaType [%s] layers[0].mediaType [%s].\n", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ref.Name(), dstRef.String(), artifactType, sbomType) | ||
return remote.Write(dstRef, att, regOpts.GetRegistryClientOpts(ctx)...) | ||
} | ||
|
||
func sbomBytes(sbomRef string) ([]byte, error) { | ||
// sbomRef can be "-", a string or a file. | ||
switch signatureType(sbomRef) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Copyright 2023 The Sigstore Authors. | ||
// | ||
// 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. | ||
|
||
package remote | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// ArtifactType converts a attachment name (sig/sbom/att/etc.) into a valid artifactType (OCI 1.1+). | ||
func ArtifactType(attName string) string { | ||
return fmt.Sprintf("application/vnd.dev.cosign.artifact.%s.v1+json", attName) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit long for the
cmd/
package. Can we move some of it intopkg/
?There's also a fair bit of overlap with
SBOMCmd
. I bet if we reorder things you can actually stick theif EnableOCIExperimental()
at the end ofSBOMCmd
and call this something likewriteSBOM
.