-
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.
Implements the notation signature verification for container images. fixes #312
- Loading branch information
Showing
34 changed files
with
1,077 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ connaisseur.yaml | |
*.key | ||
*.pem | ||
*.csr | ||
!test/testdata/**/*.pem |
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
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,173 @@ | ||
package notation | ||
|
||
import ( | ||
"connaisseur/internal/image" | ||
"connaisseur/internal/policy" | ||
"connaisseur/internal/utils" | ||
"connaisseur/internal/validator/auth" | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/notaryproject/notation-go" | ||
"github.com/notaryproject/notation-go/log" | ||
"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 | ||
RootCA *x509.CertPool | ||
TrustStore truststore.X509TrustStore | ||
} | ||
|
||
type NotationValidatorYaml struct { | ||
Name string `yaml:"name"` | ||
Type string `yaml:"type"` | ||
Auth auth.Auth `yaml:"auth"` | ||
Cert string `yaml:"cert"` | ||
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) | ||
} | ||
|
||
imts, err := NewInMemoryTrustStore(valData.TrustRoots) | ||
if err != nil { | ||
return fmt.Errorf("failed to create trust store: %s", err) | ||
} | ||
|
||
if valData.Cert != "" { | ||
rootCA := x509.NewCertPool() | ||
if !rootCA.AppendCertsFromPEM([]byte(valData.Cert)) { | ||
return fmt.Errorf("failed to parse certificate") | ||
} | ||
nv.RootCA = rootCA | ||
} | ||
|
||
nv.Name = valData.Name | ||
nv.Type = valData.Type | ||
nv.Auth = valData.Auth | ||
nv.TrustStore = imts | ||
|
||
return nil | ||
} | ||
|
||
func (nv *NotationValidator) ValidateImage( | ||
ctx context.Context, | ||
image *image.Image, | ||
args policy.RuleOptions, | ||
) (string, error) { | ||
trustPolicy, err := nv.setUpTrustPolicy(image, 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) | ||
} | ||
|
||
client := orasAuth.DefaultClient | ||
if nv.RootCA != nil { | ||
client.Client = &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
RootCAs: nv.RootCA, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
if authn := nv.Auth.LookUp(image.Context().Name()); authn.Username != "" && | ||
authn.Password != "" { | ||
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) | ||
|
||
// notation needs digests for signature verification | ||
// thus we resolve the digest if it is not set | ||
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, | ||
} | ||
|
||
notation_ctx := log.WithLogger(ctx, logrus.StandardLogger()) | ||
digest, _, err := notation.Verify(notation_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 *image.Image, | ||
args policy.RuleOptions, | ||
) (*trustpolicy.Document, error) { | ||
imts := nv.TrustStore.(*InMemoryTrustStore) | ||
trs, err := auth.GetTrustRoots([]string{args.TrustRoot}, imts.trustRoots, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get trust roots: %s", err) | ||
} | ||
|
||
vl := args.VerificationLevel | ||
if vl == "" { | ||
vl = trustpolicy.LevelStrict.Name | ||
} | ||
|
||
return &trustpolicy.Document{ | ||
Version: "1.0", | ||
TrustPolicies: []trustpolicy.TrustPolicy{ | ||
{ | ||
Name: "default", | ||
RegistryScopes: []string{image.Context().String()}, | ||
SignatureVerification: trustpolicy.SignatureVerification{ | ||
VerificationLevel: vl, | ||
}, | ||
TrustStores: utils.Map( | ||
trs, | ||
func(tr auth.TrustRoot) string { return fmt.Sprintf("ca:%s", tr.Name) }, | ||
), | ||
TrustedIdentities: []string{"*"}, | ||
}, | ||
}, | ||
}, nil | ||
} |
Oops, something went wrong.