forked from containerd/nerdctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cosign sign use executable avoid deps
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com> docs: add cosign.md Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> feat: verify image with cosign Fixes containerd#577 Signed-off-by: Furkan <furkan.turkal@trendyol.com> Co-authored-by: Batuhan <batuhan.apaydin@trendyol.com> feat: add cosign-key flag to pull command Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> docs(cosign): clarify according to reviews Signed-off-by: Furkan <furkan.turkal@trendyol.com> Co-authored-by: Batuhan <batuha.apaydin@trendyol.com> feat: updates according to code review Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> feat: add resolve digest feature while pulling the image Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> feat(cosign): cosign test for push and pull Signed-off-by: Furkan <furkan.turkal@trendyol.com> Co-authored-by: Batuhan <batuhan.apaydin@trendyol.com> Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
- Loading branch information
1 parent
8c95977
commit 48a74f9
Showing
10 changed files
with
443 additions
and
22 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,80 @@ | ||
/* | ||
Copyright The containerd 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 main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/containerd/nerdctl/pkg/testutil" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
type cosignKeyPair struct { | ||
publicKey string | ||
privateKey string | ||
cleanup func() | ||
} | ||
|
||
func newCosignKeyPair(t testing.TB) *cosignKeyPair { | ||
td, err := os.MkdirTemp(t.TempDir(), "cosign-key-pair") | ||
assert.NilError(t, err) | ||
|
||
t.Setenv("COSIGN_PASSWORD", "1") | ||
|
||
cmd := exec.Command("cosign", "generate-key-pair") | ||
if out, err := cmd.CombinedOutput(); err != nil { | ||
t.Fatalf("failed to run %v: %v (%q)", cmd.Args, err, string(out)) | ||
} | ||
|
||
publicKey, err := filepath.Abs("cosign.pub") | ||
if err != nil { | ||
t.Fatalf("failed to get public key %v", err) | ||
} | ||
|
||
privateKey, err := filepath.Abs("cosign.key") | ||
if err != nil { | ||
t.Fatalf("failed to get private key %v", err) | ||
} | ||
|
||
return &cosignKeyPair{ | ||
publicKey: publicKey, | ||
privateKey: privateKey, | ||
cleanup: func() { | ||
_ = os.RemoveAll(td) | ||
}, | ||
} | ||
} | ||
|
||
func TestImageVerifyWithCosign(t *testing.T) { | ||
testutil.DockerIncompatible(t) | ||
keyPair := newCosignKeyPair(t) | ||
defer keyPair.cleanup() | ||
base := testutil.NewBase(t) | ||
reg := newTestRegistry(base, "test-image-cosign") | ||
defer reg.cleanup() | ||
localhostIP := "127.0.0.1" | ||
t.Logf("localhost IP=%q", localhostIP) | ||
testImageRef := fmt.Sprintf("%s:%d/test-push-signed-image", | ||
localhostIP, reg.listenPort) | ||
t.Logf("testImageRef=%q", testImageRef) | ||
base.Cmd("push", testImageRef, "--sign", "--cosign-key="+keyPair.publicKey).AssertOK() | ||
base.Cmd("pull", testImageRef, "--verify", "--cosign-key="+keyPair.privateKey).AssertOK() | ||
} |
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,74 @@ | ||
# Container Image Sign and Verify with cosign tool | ||
|
||
[cosign](https://github.com/sigstore/cosign) is tool that allows you to sign and verify container images with the | ||
public/private key pairs or without them by providing | ||
a [Keyless support](https://github.com/sigstore/cosign/blob/main/KEYLESS.md). | ||
|
||
Keyless uses ephemeral keys and certificates, which are signed automatically by | ||
the [fulcio](https://github.com/sigstore/fulcio) root CA. Signatures are stored in | ||
the [rekor](https://github.com/sigstore/rekor) transparency log, which automatically provides an attestation as to when | ||
the signature was created. | ||
|
||
You can enable container signing and verifying features with `push` and `pull` commands of `nerdctl` by using `cosign` | ||
under the hood with make use of flags `--sign` while pushing the container image, and `--verify` while pulling the | ||
container image. | ||
|
||
> * Ensure cosign executable in your `$PATH`. | ||
> * You can install cosign by following this page: https://docs.sigstore.dev/cosign/installation | ||
Prepare your environment: | ||
|
||
```shell | ||
# Create a sample Dockerfile | ||
$ cat <<EOF | tee Dockerfile.dummy | ||
FROM alpine:latest | ||
CMD [ "echo", "Hello World" ] | ||
EOF | ||
``` | ||
|
||
> Please do not forget, we won't be validating the base images, which is `alpine:latest` in this case, of the container image that was built on, | ||
> we'll only verify the container image itself once we sign it. | ||
```shell | ||
|
||
# Build the image | ||
$ nerdctl build -t devopps/hello-world -f Dockerfile.dummy . | ||
|
||
# Generate a key-pair: cosign.key and cosign.pub | ||
$ cosign generate-key-pair | ||
|
||
# Export your COSIGN_PASSWORD to prevent CLI prompting | ||
$ export COSIGN_PASSWORD=$COSIGN_PASSWORD | ||
``` | ||
|
||
Sign the container image while pushing: | ||
|
||
``` | ||
# Sign the image with Keyless mode | ||
$ nerdctl push --sign=cosign devopps/hello-world | ||
# Sign the image and store the signature in the registry | ||
$ nerdctl push --sign=cosign --cosign-key cosign.key devopps/hello-world | ||
``` | ||
|
||
Verify the container image while pulling: | ||
|
||
> REMINDER: Image won't be pulled if there are no matching signatures in case you passed `--verify` flag. | ||
```shell | ||
# Verify the image with Keyless mode | ||
$ nerdctl pull --verify=cosign devopps/hello-world | ||
INFO[0004] cosign: | ||
INFO[0004] cosign: [{"critical":{"identity":...}] | ||
docker.io/devopps/nginx-new:latest: resolved |++++++++++++++++++++++++++++++++++++++| | ||
manifest-sha256:0910d404e58dd320c3c0c7ea31bf5fbfe7544b26905c5eccaf87c3af7bcf9b88: done |++++++++++++++++++++++++++++++++++++++| | ||
config-sha256:1de1c4fb5122ac8650e349e018fba189c51300cf8800d619e92e595d6ddda40e: done |++++++++++++++++++++++++++++++++++++++| | ||
elapsed: 1.4 s total: 1.3 Ki (928.0 B/s) | ||
|
||
# You can not verify the image if it is not signed | ||
$ nerdctl pull --verify=cosign --cosign-key cosign.pub devopps/hello-world-bad | ||
INFO[0003] cosign: Error: no matching signatures: | ||
INFO[0003] cosign: failed to verify signature | ||
INFO[0003] cosign: main.go:46: error during command execution: no matching signatures: | ||
INFO[0003] cosign: failed to verify signature | ||
``` |
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.