-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This type is currently just an alias for `v1.Image`, which is the unit of storage we use for files uploaded with `UploadFile[s]`. This changes `static.NewFile` to create an `oci.File` (full image vs. just the layer previously), and `UploadFile` to use this (it's where the rest of the image came from). Signed-off-by: Matt Moore <[email protected]>
- Loading branch information
Showing
9 changed files
with
230 additions
and
119 deletions.
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
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,23 @@ | ||
// | ||
// 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 oci | ||
|
||
// File is a degenerate form of SignedImage that stores a single file as a v1.Layer | ||
type File interface { | ||
SignedImage | ||
|
||
// TODO(mattmoor): Consider adding useful helpers. | ||
} |
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,44 @@ | ||
// | ||
// 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 static | ||
|
||
import ( | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/sigstore/cosign/pkg/oci" | ||
"github.com/sigstore/cosign/pkg/oci/signed" | ||
) | ||
|
||
// NewFile constructs a new v1.Image with the provided payload. | ||
func NewFile(payload []byte, opts ...Option) (oci.File, error) { | ||
o, err := makeOptions(opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
base := mutate.MediaType(empty.Image, types.OCIManifestSchema1) | ||
base = mutate.ConfigMediaType(base, o.ConfigMediaType) | ||
img, err := mutate.Append(base, mutate.Addendum{ | ||
Layer: &staticLayer{ | ||
b: payload, | ||
opts: o, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return signed.Image(img), nil | ||
} |
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,115 @@ | ||
// | ||
// 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 static | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
) | ||
|
||
func TestNewFile(t *testing.T) { | ||
payload := "this is the content!" | ||
img, err := NewFile([]byte(payload), WithLayerMediaType("foo")) | ||
if err != nil { | ||
t.Fatalf("NewFile() = %v", err) | ||
} | ||
|
||
layers, err := img.Layers() | ||
if err != nil { | ||
t.Fatalf("Layers() = %v", err) | ||
} else if got, want := len(layers), 1; got != want { | ||
t.Fatalf("len(Layers()) = %d, wanted %d", got, want) | ||
} | ||
l := layers[0] | ||
|
||
t.Run("check size", func(t *testing.T) { | ||
wantSize := int64(len(payload)) | ||
gotSize, err := l.Size() | ||
if err != nil { | ||
t.Fatalf("Size() = %v", err) | ||
} | ||
if gotSize != wantSize { | ||
t.Errorf("Size() = %d, wanted %d", gotSize, wantSize) | ||
} | ||
}) | ||
|
||
t.Run("check media type", func(t *testing.T) { | ||
wantMT := types.MediaType("foo") | ||
gotMT, err := l.MediaType() | ||
if err != nil { | ||
t.Fatalf("MediaType() = %v", err) | ||
} | ||
if gotMT != wantMT { | ||
t.Errorf("MediaType() = %s, wanted %s", gotMT, wantMT) | ||
} | ||
}) | ||
|
||
t.Run("check hashes", func(t *testing.T) { | ||
wantHash, _, err := v1.SHA256(strings.NewReader(payload)) | ||
if err != nil { | ||
t.Fatalf("SHA256() = %v", err) | ||
} | ||
|
||
gotDigest, err := l.Digest() | ||
if err != nil { | ||
t.Fatalf("Digest() = %v", err) | ||
} | ||
if !cmp.Equal(gotDigest, wantHash) { | ||
t.Errorf("Digest = %s", cmp.Diff(gotDigest, wantHash)) | ||
} | ||
|
||
gotDiffID, err := l.DiffID() | ||
if err != nil { | ||
t.Fatalf("DiffID() = %v", err) | ||
} | ||
if !cmp.Equal(gotDiffID, wantHash) { | ||
t.Errorf("DiffID = %s", cmp.Diff(gotDiffID, wantHash)) | ||
} | ||
}) | ||
|
||
t.Run("check content", func(t *testing.T) { | ||
comp, err := l.Compressed() | ||
if err != nil { | ||
t.Fatalf("Compressed() = %v", err) | ||
} | ||
defer comp.Close() | ||
compContent, err := io.ReadAll(comp) | ||
if err != nil { | ||
t.Fatalf("ReadAll() = %v", err) | ||
} | ||
if got, want := string(compContent), payload; got != want { | ||
t.Errorf("Compressed() = %s, wanted %s", got, want) | ||
} | ||
|
||
uncomp, err := l.Uncompressed() | ||
if err != nil { | ||
t.Fatalf("Uncompressed() = %v", err) | ||
} | ||
defer uncomp.Close() | ||
uncompContent, err := io.ReadAll(uncomp) | ||
if err != nil { | ||
t.Fatalf("ReadAll() = %v", err) | ||
} | ||
if got, want := string(uncompContent), payload; got != want { | ||
t.Errorf("Uncompressed() = %s, wanted %s", got, want) | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.