-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add verification helpers * fix build Signed-off-by: rgnote <[email protected]>
- Loading branch information
Showing
21 changed files
with
351 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# VS Code | ||
.vscode | ||
|
||
# Custom | ||
.cover/ | ||
# Code Editors | ||
.vscode | ||
.idea | ||
*.sublime-project | ||
*.sublime-workspace | ||
|
||
# Custom | ||
.cover/ | ||
.test/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package verification | ||
|
||
// ErrorVerificationInconclusive is used when signature verification fails due to a runtime error (e.g. a network error) | ||
type ErrorVerificationInconclusive struct { | ||
msg string | ||
} | ||
|
||
func (e ErrorVerificationInconclusive) Error() string { | ||
if e.msg != "" { | ||
return e.msg | ||
} | ||
return "signature verification was inclusive due to an unexpected error" | ||
} | ||
|
||
// ErrorNoApplicableTrustPolicy is used when there is no trust policy that applies to the given artifact | ||
type ErrorNoApplicableTrustPolicy struct { | ||
msg string | ||
} | ||
|
||
func (e ErrorNoApplicableTrustPolicy) Error() string { | ||
if e.msg != "" { | ||
return e.msg | ||
} | ||
return "there is no applicable trust policy for the given artifact" | ||
} | ||
|
||
// ErrorSignatureRetrievalFailed is used when notation is unable to retrieve the digital signature/s for the given artifact | ||
type ErrorSignatureRetrievalFailed struct { | ||
msg string | ||
} | ||
|
||
func (e ErrorSignatureRetrievalFailed) Error() string { | ||
if e.msg != "" { | ||
return e.msg | ||
} | ||
return "unable to retrieve the digital signature from the registry" | ||
} | ||
|
||
// ErrorVerificationFailed is used when it is determined that the digital signature/s is not valid for the given artifact | ||
type ErrorVerificationFailed struct { | ||
msg string | ||
} | ||
|
||
func (e ErrorVerificationFailed) Error() string { | ||
if e.msg != "" { | ||
return e.msg | ||
} | ||
return "signature verification failed" | ||
} |
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,79 @@ | ||
package verification | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestGetArtifactDigestFromUri(t *testing.T) { | ||
|
||
tests := []struct { | ||
artifactUri string | ||
digest string | ||
wantErr bool | ||
}{ | ||
{"domain.com/repository@sha256:digest", "sha256:digest", false}, | ||
{"domain.com:80/repository:digest", "", true}, | ||
{"domain.com/repository", "", true}, | ||
{"domain.com/repository@sha256", "", true}, | ||
{"domain.com/repository@sha256:", "", true}, | ||
{"", "", true}, | ||
{"domain.com", "", true}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
digest, err := getArtifactDigestFromUri(tt.artifactUri) | ||
|
||
if tt.wantErr != (err != nil) { | ||
t.Fatalf("TestGetArtifactDigestFromUri Error: %q WantErr: %v Input: %q", err, tt.wantErr, tt.artifactUri) | ||
} else if digest != tt.digest { | ||
t.Fatalf("TestGetArtifactDigestFromUri Want: %q Got: %v", tt.digest, digest) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLoadPolicyDocument(t *testing.T) { | ||
// non-existing policy file | ||
_, err := loadPolicyDocument(filepath.FromSlash("/non/existent")) | ||
if err == nil { | ||
t.Fatalf("TestLoadPolicyDocument should throw error for non existent policy") | ||
} | ||
// existing invalid json file | ||
path := filepath.Join(t.TempDir(), "invalid.json") | ||
err = ioutil.WriteFile(path, []byte(`{"invalid`), 0644) | ||
_, err = loadPolicyDocument(path) | ||
if err == nil { | ||
t.Fatalf("TestLoadPolicyDocument should throw error for invalid policy file. Error: %v", err) | ||
} | ||
|
||
// existing policy file | ||
path = filepath.Join(t.TempDir(), "trustpolicy.json") | ||
policyDoc1 := dummyPolicyDocument() | ||
policyJson, _ := json.Marshal(policyDoc1) | ||
err = ioutil.WriteFile(path, policyJson, 0644) | ||
_, err = loadPolicyDocument(path) | ||
if err != nil { | ||
t.Fatalf("TestLoadPolicyDocument should not throw error for an existing policy file. Error: %v", err) | ||
} | ||
} | ||
|
||
func TestLoadX509TrustStore(t *testing.T) { | ||
caStore := "ca:valid-trust-store" | ||
anotherStore := "ca:valid-trust-store-2" | ||
dummyPolicy := dummyPolicyStatement() | ||
dummyPolicy.TrustStores = []string{caStore, anotherStore} | ||
trustStores, err := loadX509TrustStores(&dummyPolicy, filepath.FromSlash("testdata/trust-store/")) | ||
if err != nil { | ||
t.Fatalf("TestLoadX509TrustStore should not throw error for a valid trust store. Error: %v", err) | ||
} | ||
if (len(trustStores)) != 2 { | ||
t.Fatalf("TestLoadX509TrustStore must load two trust stores") | ||
} | ||
if trustStores[caStore] == nil || trustStores[anotherStore] == nil { | ||
t.Fatalf("TestLoadX509TrustStore must load trust stores") | ||
} | ||
} |
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.