-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
328 additions
and
3 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 |
---|---|---|
|
@@ -43,6 +43,7 @@ jobs: | |
"regular", | ||
"notaryv1", | ||
"cosign", | ||
"notation", | ||
"namespaced", | ||
"deployment", | ||
"pre-config", | ||
|
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,139 @@ | ||
package notation | ||
|
||
import ( | ||
"connaisseur/internal/image" | ||
"connaisseur/internal/policy" | ||
"connaisseur/internal/utils" | ||
"connaisseur/internal/validator/auth" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/notaryproject/notation-go" | ||
"github.com/notaryproject/notation-go/registry" | ||
"github.com/notaryproject/notation-go/verifier" | ||
"github.com/notaryproject/notation-go/verifier/trustpolicy" | ||
"github.com/notaryproject/notation-go/verifier/truststore" | ||
"github.com/sirupsen/logrus" | ||
"oras.land/oras-go/v2/registry/remote" | ||
orasAuth "oras.land/oras-go/v2/registry/remote/auth" | ||
) | ||
|
||
type NotationValidator struct { | ||
Name string `validate:"required"` | ||
Type string `validate:"eq=notation"` | ||
Auth auth.Auth | ||
TrustStore truststore.X509TrustStore | ||
} | ||
|
||
type NotationValidatorYaml struct { | ||
Name string `yaml:"name"` | ||
Type string `yaml:"type"` | ||
Auth auth.Auth `yaml:"auth"` | ||
TrustRoots []auth.TrustRoot `yaml:"trustRoots"` | ||
} | ||
|
||
func (nv *NotationValidator) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var valData NotationValidatorYaml | ||
if err := unmarshal(&valData); err != nil { | ||
return err | ||
} | ||
|
||
if len(valData.TrustRoots) < 1 { | ||
return fmt.Errorf("no trust roots provided for validator %s", valData.Name) | ||
} | ||
|
||
nv.Name = valData.Name | ||
nv.Type = valData.Type | ||
nv.Auth = valData.Auth | ||
nv.TrustStore = &InMemoryTrustStore{ | ||
trustRoots: valData.TrustRoots, | ||
} | ||
return nil | ||
} | ||
|
||
func (nv *NotationValidator) ValidateImage( | ||
ctx context.Context, | ||
image *image.Image, | ||
args policy.RuleOptions, | ||
) (string, error) { | ||
|
||
trustPolicy, err := nv.setUpTrustPolicy(image.Context().String(), args) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to set up trust policy: %s", err) | ||
} | ||
|
||
verifier, err := verifier.New(trustPolicy, nv.TrustStore, nil) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create verifier: %s", err) | ||
} | ||
|
||
remoteRepo, err := remote.NewRepository(image.Context().String()) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create remote repository: %s", err) | ||
} | ||
|
||
if authn := nv.Auth.LookUp(image.Context().Name()); authn.Username != "" && | ||
authn.Password != "" { | ||
client := orasAuth.DefaultClient | ||
client.Credential = func(nv2_ctx context.Context, s string) (orasAuth.Credential, error) { | ||
return orasAuth.Credential{ | ||
Username: authn.Username, | ||
Password: authn.Password, | ||
}, nil | ||
} | ||
remoteRepo.Client = client | ||
} | ||
remoteRegisty := registry.NewRepository(remoteRepo) | ||
|
||
if image.Digest() == "" { | ||
desc, err := remoteRegisty.Resolve(ctx, image.Name()) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to resolve image tag: %s", err) | ||
} | ||
logrus.Debugf("resolved digest: %s", desc.Digest.String()) | ||
image.SetDigest(desc.Digest.String()) | ||
} | ||
|
||
verifyOptions := notation.VerifyOptions{ | ||
ArtifactReference: fmt.Sprintf("%s@%s", image.Context().String(), image.Digest()), | ||
MaxSignatureAttempts: 10, | ||
} | ||
|
||
digest, _, err := notation.Verify(ctx, verifier, remoteRegisty, verifyOptions) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to verify image: %s", err) | ||
} | ||
|
||
return string(digest.Digest), nil | ||
} | ||
|
||
func (nv *NotationValidator) setUpTrustPolicy( | ||
image string, | ||
args policy.RuleOptions, | ||
) (*trustpolicy.Document, error) { | ||
imtr := nv.TrustStore.(*InMemoryTrustStore) | ||
trs, err := auth.GetTrustRoots([]string{args.TrustRoot}, imtr.trustRoots, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get trust roots: %s", err) | ||
} | ||
|
||
return &trustpolicy.Document{ | ||
Version: "1.0", | ||
TrustPolicies: []trustpolicy.TrustPolicy{ | ||
{ | ||
Name: "default", | ||
RegistryScopes: []string{image}, | ||
SignatureVerification: trustpolicy.SignatureVerification{ | ||
VerificationLevel: trustpolicy.LevelStrict.Name, | ||
}, | ||
TrustStores: utils.Map( | ||
trs, | ||
func(tr auth.TrustRoot) string { | ||
return fmt.Sprintf("ca:%s", tr.Name) | ||
}, | ||
), | ||
TrustedIdentities: []string{"*"}, | ||
}, | ||
}, | ||
}, 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,41 @@ | ||
package notation | ||
|
||
import ( | ||
"connaisseur/internal/validator/auth" | ||
"context" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
|
||
"github.com/notaryproject/notation-go/verifier/truststore" | ||
) | ||
|
||
type InMemoryTrustStore struct { | ||
trustRoots []auth.TrustRoot | ||
truststore.X509TrustStore | ||
} | ||
|
||
func (imts *InMemoryTrustStore) GetCertificates( | ||
ctx context.Context, | ||
_ truststore.Type, | ||
namedStore string, | ||
) ([]*x509.Certificate, error) { | ||
var certs []*x509.Certificate | ||
|
||
for _, trustRoot := range imts.trustRoots { | ||
if trustRoot.Name == namedStore { | ||
block, _ := pem.Decode([]byte(trustRoot.Cert)) | ||
cert, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return nil, fmt.Errorf( | ||
"failed to parse certificate for trustRoot %s: %s", | ||
trustRoot.Name, | ||
err, | ||
) | ||
} | ||
certs = append(certs, cert) | ||
} | ||
} | ||
|
||
return certs, 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
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,8 @@ | ||
# simple test cases for notation | ||
- id: unsigned | ||
txt: Testing unsigned image... | ||
ref: ghcr.io/sse-secure-systems/testimage:notation-unsign | ||
expected_msg: error during notation validation | ||
- id: signed | ||
txt: Testing signed image... | ||
ref: ghcr.io/sse-secure-systems/testimage:notation-sign |
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,33 @@ | ||
application: | ||
validators: | ||
- name: ghcr-notation | ||
type: notation | ||
trustRoots: | ||
- name: default | ||
cert: | | ||
-----BEGIN CERTIFICATE----- | ||
MIIDrjCCApagAwIBAgIUfA/t/J6eINSu566a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o1G6f0ph/GPO4o71s8ek55Q92ZNrAqHwzGwsByGFbHcwABwtAA | ||
1gqAB5luiuokUXhmlqkH46wbQLiVLYnetqIQ8uJiSnUFrWaKQSICnCY1kxptqepv | ||
Vfgbd7LIriH+m57IWD2fil6qRXV7c0J6v+N5N7gf2TgDvRBxYRhHV+fQpxRWm+Ti | ||
fyEquTrEhfXY4yfYWnpJ/EbjsHheqK+F4EacAqBTjhzW+g== | ||
-----END CERTIFICATE----- | ||
policy: | ||
- pattern: "ghcr.io/sse-secure-systems/testimage:*" | ||
validator: ghcr-notation | ||
alerts: [] |
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,10 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
notation_test() { | ||
update_with_file "notation/install.yaml" | ||
update '(.application.validators[] | select(.name == "ghcr-notation") | .auth) += {"secretName": env(IMAGEPULLSECRET)}' | ||
install "make" | ||
multi_test "notation/cases.yaml" | ||
uninstall "make" | ||
} |