-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sign without pulling from the registry
This allows signing in a disconnected environment. Signed-off-by: Brandon Mitchell <git@bmitch.net>
- Loading branch information
1 parent
75c326b
commit ed2bd43
Showing
3 changed files
with
207 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// 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 empty | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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/sigstore/cosign/pkg/oci" | ||
) | ||
|
||
type signedImage struct { | ||
v1.Image | ||
digest v1.Hash | ||
signature oci.Signatures | ||
attestations oci.Signatures | ||
} | ||
|
||
func (se *signedImage) Signatures() (oci.Signatures, error) { | ||
return se.signature, nil | ||
} | ||
|
||
func (se *signedImage) Attestations() (oci.Signatures, error) { | ||
return se.attestations, nil | ||
} | ||
|
||
func (se *signedImage) Digest() (v1.Hash, error) { | ||
if se.digest.Hex == "" { | ||
return v1.Hash{}, fmt.Errorf("digest not available") | ||
} | ||
return se.digest, nil | ||
} | ||
|
||
func SignedImage(ref name.Reference) (oci.SignedImage, error) { | ||
var err error | ||
d := v1.Hash{} | ||
base := empty.Image | ||
if digest, ok := ref.(name.Digest); ok { | ||
d, err = v1.NewHash(digest.DigestStr()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &signedImage{ | ||
Image: base, | ||
digest: d, | ||
signature: Signatures(), | ||
attestations: Signatures(), | ||
}, 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,68 @@ | ||
// | ||
// 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 empty | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
) | ||
|
||
func TestSignedImage(t *testing.T) { | ||
tests := []struct { | ||
ref string | ||
digestStr string | ||
digestErr string | ||
}{ | ||
{ | ||
ref: "hello-world:latest", | ||
digestErr: "digest not available", | ||
}, | ||
{ | ||
ref: "hello-world@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a", | ||
digestStr: "sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a", | ||
}, | ||
} | ||
for _, test := range tests { | ||
ref, err := name.ParseReference(test.ref) | ||
if err != nil { | ||
t.Errorf("failed to parse ref \"%s\": %v", test.ref, err) | ||
continue | ||
} | ||
se, err := SignedImage(ref) | ||
if err != nil { | ||
t.Errorf("failed to create signed image for \"%s\": %v", test.ref, err) | ||
continue | ||
} | ||
d, err := se.Digest() | ||
if (err == nil && test.digestErr != "") || | ||
(err != nil && test.digestErr == "") || | ||
(err != nil && test.digestErr != "" && err.Error() != test.digestErr) { | ||
t.Errorf("digest error mismatch for \"%s\": expected %s, saw %v", test.ref, test.digestErr, err) | ||
} | ||
if test.digestStr != "" && d.String() != test.digestStr { | ||
t.Errorf("digest mismatch for \"%s\": expected %s, saw %s", test.ref, test.digestStr, d.String()) | ||
} | ||
_, err = se.Signatures() | ||
if err != nil { | ||
t.Errorf("failed to get signatures for %s: %v", test.ref, err) | ||
} | ||
_, err = se.Attestations() | ||
if err != nil { | ||
t.Errorf("failed to get attestations for %s: %v", test.ref, err) | ||
} | ||
} | ||
} |