-
Notifications
You must be signed in to change notification settings - Fork 195
Add rekor harness tests #945
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,211 @@ | ||
| // Copyright 2022 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. | ||
|
|
||
| //go:build e2e | ||
| // +build e2e | ||
|
|
||
| package e2e | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/ecdsa" | ||
| "crypto/sha256" | ||
| "crypto/x509" | ||
| "encoding/base64" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "encoding/pem" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/in-toto/in-toto-golang/in_toto" | ||
| slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v0.2" | ||
| "github.com/secure-systems-lab/go-securesystemslib/dsse" | ||
| "github.com/sigstore/rekor/pkg/generated/models" | ||
| "github.com/sigstore/rekor/pkg/types" | ||
| ) | ||
|
|
||
| // Make sure we can add an entry | ||
| func TestHarnessAddEntry(t *testing.T) { | ||
| // Create a random artifact and sign it. | ||
| artifactPath := filepath.Join(t.TempDir(), "artifact") | ||
| sigPath := filepath.Join(t.TempDir(), "signature.asc") | ||
|
|
||
| createdX509SignedArtifact(t, artifactPath, sigPath) | ||
| dataBytes, _ := ioutil.ReadFile(artifactPath) | ||
| h := sha256.Sum256(dataBytes) | ||
| dataSHA := hex.EncodeToString(h[:]) | ||
|
|
||
| // Write the public key to a file | ||
| pubPath := filepath.Join(t.TempDir(), "pubKey.asc") | ||
| if err := ioutil.WriteFile(pubPath, []byte(rsaCert), 0644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Verify should fail initially | ||
| runCliErr(t, "verify", "--type=hashedrekord", "--pki-format=x509", "--artifact-hash", dataSHA, "--signature", sigPath, "--public-key", pubPath) | ||
|
|
||
| // It should upload successfully. | ||
| out := runCli(t, "upload", "--type=hashedrekord", "--pki-format=x509", "--artifact-hash", dataSHA, "--signature", sigPath, "--public-key", pubPath) | ||
| outputContains(t, out, "Created entry at") | ||
|
|
||
| // Now we should be able to verify it. | ||
| out = runCli(t, "verify", "--type=hashedrekord", "--pki-format=x509", "--artifact-hash", dataSHA, "--signature", sigPath, "--public-key", pubPath) | ||
| outputContains(t, out, "Inclusion Proof:") | ||
| } | ||
|
|
||
| // Make sure we can add an intoto entry | ||
| func TestHarnessAddIntoto(t *testing.T) { | ||
| td := t.TempDir() | ||
| attestationPath := filepath.Join(td, "attestation.json") | ||
| pubKeyPath := filepath.Join(td, "pub.pem") | ||
|
|
||
| // Get some random data so it's unique each run | ||
| d := randomData(t, 10) | ||
| id := base64.StdEncoding.EncodeToString(d) | ||
|
|
||
| it := in_toto.ProvenanceStatement{ | ||
| StatementHeader: in_toto.StatementHeader{ | ||
| Type: in_toto.StatementInTotoV01, | ||
| PredicateType: slsa.PredicateSLSAProvenance, | ||
| Subject: []in_toto.Subject{ | ||
| { | ||
| Name: "foobar", | ||
| Digest: slsa.DigestSet{ | ||
| "foo": "bar", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Predicate: slsa.ProvenancePredicate{ | ||
| Builder: slsa.ProvenanceBuilder{ | ||
| ID: "foo" + id, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| b, err := json.Marshal(it) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| pb, _ := pem.Decode([]byte(ecdsaPriv)) | ||
| priv, err := x509.ParsePKCS8PrivateKey(pb.Bytes) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| signer, err := dsse.NewEnvelopeSigner(&IntotoSigner{ | ||
| priv: priv.(*ecdsa.PrivateKey), | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| env, err := signer.SignPayload("application/vnd.in-toto+json", b) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| eb, err := json.Marshal(env) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| write(t, string(eb), attestationPath) | ||
| write(t, ecdsaPub, pubKeyPath) | ||
|
|
||
| // If we do it twice, it should already exist | ||
| out := runCli(t, "upload", "--artifact", attestationPath, "--type", "intoto", "--public-key", pubKeyPath) | ||
| outputContains(t, out, "Created entry at") | ||
| uuid := getUUIDFromUploadOutput(t, out) | ||
|
|
||
| out = runCli(t, "get", "--uuid", uuid, "--format=json") | ||
| g := getOut{} | ||
| if err := json.Unmarshal([]byte(out), &g); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| // The attestation should be stored at /var/run/attestations/sha256:digest | ||
|
|
||
| got := in_toto.ProvenanceStatement{} | ||
| if err := json.Unmarshal([]byte(g.Attestation), &got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if diff := cmp.Diff(it, got); diff != "" { | ||
| t.Errorf("diff: %s", diff) | ||
| } | ||
|
|
||
| attHash := sha256.Sum256(b) | ||
|
|
||
| intotoModel := &models.IntotoV001Schema{} | ||
| if err := types.DecodeEntry(g.Body.(map[string]interface{})["IntotoObj"], intotoModel); err != nil { | ||
| t.Errorf("could not convert body into intoto type: %v", err) | ||
| } | ||
| if intotoModel.Content == nil || intotoModel.Content.PayloadHash == nil { | ||
| t.Errorf("could not find hash over attestation %v", intotoModel) | ||
| } | ||
| recordedPayloadHash, err := hex.DecodeString(*intotoModel.Content.PayloadHash.Value) | ||
| if err != nil { | ||
| t.Errorf("error converting attestation hash to []byte: %v", err) | ||
| } | ||
|
|
||
| if !bytes.Equal(attHash[:], recordedPayloadHash) { | ||
| t.Fatal(fmt.Errorf("attestation hash %v doesnt match the payload we sent %v", hex.EncodeToString(attHash[:]), | ||
| *intotoModel.Content.PayloadHash.Value)) | ||
| } | ||
|
|
||
| out = runCli(t, "upload", "--artifact", attestationPath, "--type", "intoto", "--public-key", pubKeyPath) | ||
| outputContains(t, out, "Entry already exists") | ||
| } | ||
|
|
||
| // Make sure we can get and verify all entries | ||
| // For attestations, make sure we can see the attestation | ||
| func TestHarnessGetAllEntriesLogIndex(t *testing.T) { | ||
| treeSize := activeTreeSize(t) | ||
| if treeSize == 0 { | ||
| t.Fatal("There are 0 entries in the log, there should be at least 2") | ||
| } | ||
| for i := 0; i < treeSize; i++ { | ||
| out := runCli(t, "get", "--log-index", fmt.Sprintf("%d", i), "--format", "json") | ||
| if !strings.Contains(out, "IntotoObj") { | ||
| continue | ||
| } | ||
| var intotoObj struct { | ||
| Attestation string | ||
| } | ||
| if err := json.Unmarshal([]byte(out), &intotoObj); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if intotoObj.Attestation == "" { | ||
| t.Log(out) | ||
| t.Fatalf("intotoObj attestation is empty for log index %d", i) | ||
| } | ||
| t.Log("Got IntotoObj type with attestation") | ||
| } | ||
| } | ||
|
|
||
| func activeTreeSize(t *testing.T) int { | ||
| out := runCliStdout(t, "loginfo", "--format", "json", "--store_tree_state", "false") | ||
| t.Log(string(out)) | ||
| var s struct { | ||
| ActiveTreeSize int | ||
| } | ||
| if err := json.Unmarshal([]byte(out), &s); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return s.ActiveTreeSize | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.