-
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 layout package for writing and loading signatures from disk #1040
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
96d2154
Add layout package for writing and loading signatures from disk
9a634cb
Create siglayer package, which is used by and
e104ca4
Address code review comments
f5a015d
Merge in master
daf3f43
Get signatures by layer rather than using partialg
d1b6d07
Add unit test for reading and writing from disk
f8ffe71
Use ImageIndex to store image related signatures etc
add535a
Code review comments, also support for passing in an empty hash to Si…
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// Copyright 2021 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 layout | ||
|
||
import ( | ||
"fmt" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/layout" | ||
"github.com/google/go-containerregistry/pkg/v1/partial" | ||
"github.com/sigstore/cosign/pkg/oci" | ||
"github.com/sigstore/cosign/pkg/oci/siglayer" | ||
) | ||
|
||
// SignedImage provides access to a remote image reference, and its signatures. | ||
func SignedImage(path string) (oci.SignedImage, error) { | ||
p, err := layout.FromPath(imagePath(path)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
img, err := p.Image(v1.Hash{}) | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return &image{ | ||
Image: img, | ||
path: path, | ||
}, nil | ||
} | ||
|
||
type image struct { | ||
path string | ||
v1.Image | ||
} | ||
|
||
var _ oci.SignedImage = (*image)(nil) | ||
|
||
type sigs struct { | ||
v1.Image | ||
} | ||
|
||
var _ oci.Signatures = (*sigs)(nil) | ||
|
||
// Get implements oci.Signatures | ||
func (s *sigs) Get() ([]oci.Signature, error) { | ||
layers, err := s.Image.Layers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var signatures []oci.Signature | ||
for _, l := range layers { | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
d, err := partial.Descriptor(l) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if d == nil { | ||
continue | ||
} | ||
signatures = append(signatures, siglayer.New(l, s, *d)) | ||
} | ||
return signatures, nil | ||
} | ||
|
||
// Signatures implements oci.SignedImage | ||
func (i *image) Signatures() (oci.Signatures, error) { | ||
sigPath, err := layout.FromPath(signaturesPath(i.path)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
img, err := sigPath.Image(v1.Hash{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &sigs{ | ||
Image: img, | ||
}, nil | ||
} | ||
|
||
// Attestations implements oci.SignedImage | ||
// TODO (priyawadhwa@) | ||
func (i *image) Attestations() (oci.Signatures, error) { | ||
return nil, fmt.Errorf("not yet implemented") | ||
} | ||
|
||
// Attestations implements oci.SignedImage | ||
// TODO (priyawadhwa@) | ||
func (i *image) Attachment(name string) (oci.File, error) { | ||
return nil, fmt.Errorf("not yet implemented") | ||
} |
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,67 @@ | ||
// | ||
// Copyright 2021 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 layout | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/layout" | ||
"github.com/pkg/errors" | ||
"github.com/sigstore/cosign/pkg/oci" | ||
) | ||
|
||
// WriteSignedImage writes the image and all related signatures, attestations and attachments | ||
func WriteSignedImage(path string, si oci.SignedImage) error { | ||
// First, write the image | ||
if err := write(path, imagePath, si); err != nil { | ||
return errors.Wrap(err, "writing image") | ||
} | ||
sigs, err := si.Signatures() | ||
if err != nil { | ||
return errors.Wrap(err, "getting signatures") | ||
} | ||
if err := write(path, signaturesPath, sigs); err != nil { | ||
return errors.Wrap(err, "writing signatures") | ||
} | ||
// TODO (priyawadhwa@) write attestations and attachments | ||
return nil | ||
} | ||
|
||
func imagePath(path string) string { | ||
return filepath.Join(path, "image") | ||
} | ||
|
||
func signaturesPath(path string) string { | ||
return filepath.Join(path, "signatures") | ||
} | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type pathFunc func(string) string | ||
|
||
func write(path string, pf pathFunc, img v1.Image) error { | ||
p := pf(path) | ||
// write empty image | ||
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. index? 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. Can you elaborate on why this is necessary? |
||
layoutPath, err := layout.Write(p, empty.Index) | ||
if err != nil { | ||
return err | ||
} | ||
// write image to disk | ||
if err := layoutPath.AppendImage(img); err != nil { | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
return nil | ||
priyawadhwa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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
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
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.
Is this ~always an index? If so, I'm wondering if we should only expose
SignedIndex
🤔 (or at least start there instead, and add "sugar" 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.
The index is the entrypoint into this layout -- you could interpret the entire layout as a single index and push that to a registry, but I think it's more interesting to just use this index data structure as a list of what's contained within the directory. You can have a bunch of cosign-isms around annotations on the top-level
index.json
index that are meaningful to cosign without them leaking from this implementation.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.
It might be interesting to have an idea of a "snapshot" of a
SignedImage
, that is implemented as an index that points to every piece of relevant metadata?