-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fallback verifier and make verifiers context aware (#85)
- Loading branch information
Showing
5 changed files
with
97 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package certverify | ||
|
||
import ( | ||
"context" | ||
"crypto/x509" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// CertVerifier is a simple interface for verifying a certificate. | ||
type CertVerifier interface { | ||
Verify(context.Context, *x509.Certificate) error | ||
} | ||
|
||
// FallbackVerifier verfies certificate validity using multiple verifiers. | ||
type FallbackVerifier struct { | ||
verifiers []CertVerifier | ||
} | ||
|
||
// NewFallbackVerifier creates a new verifier using other verifiers. | ||
func NewFallbackVerifier(verifiers ...CertVerifier) *FallbackVerifier { | ||
return &FallbackVerifier{verifiers: verifiers} | ||
} | ||
|
||
// Verify performs certificate verification. | ||
// Any verifier returning nil ("passes") will pass (return nil) and not | ||
// check any other verifier. | ||
// If all verifiers return non-nil ("fail") then an error for all | ||
// verifiers will be returned. | ||
func (v *FallbackVerifier) Verify(ctx context.Context, cert *x509.Certificate) error { | ||
var errs []string | ||
for i, verifier := range v.verifiers { | ||
err := verifier.Verify(ctx, cert) | ||
if err == nil { | ||
return nil | ||
} | ||
errs = append(errs, fmt.Sprintf("fallback error (%d): %v", i, err)) | ||
} | ||
return errors.New(strings.Join(errs, "; ")) | ||
} |
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,50 @@ | ||
package certverify | ||
|
||
import ( | ||
"context" | ||
"crypto/x509" | ||
"errors" | ||
"testing" | ||
) | ||
|
||
type errVerifier struct{ err error } | ||
|
||
func (v *errVerifier) Verify(_ context.Context, _ *x509.Certificate) error { | ||
return v.err | ||
} | ||
|
||
var nilErroringVerifier = &errVerifier{} | ||
var errErroringVerifier = &errVerifier{err: errors.New("verifier error")} | ||
|
||
func TestFallbackVerifier(t *testing.T) { | ||
v := NewFallbackVerifier(nilErroringVerifier) | ||
err := v.Verify(nil, nil) | ||
if err != nil { | ||
t.Errorf("should not have errored: %v", err) | ||
} | ||
|
||
v = NewFallbackVerifier(nilErroringVerifier, nilErroringVerifier) | ||
if err = v.Verify(nil, nil); err != nil { | ||
t.Errorf("should not have errored: %v", err) | ||
} | ||
|
||
v = NewFallbackVerifier(errErroringVerifier) | ||
if err = v.Verify(nil, nil); err == nil { | ||
t.Error("should have errored") | ||
} | ||
|
||
v = NewFallbackVerifier(errErroringVerifier, nilErroringVerifier) | ||
if err = v.Verify(nil, nil); err != nil { | ||
t.Errorf("should not have errored: %v", err) | ||
} | ||
|
||
v = NewFallbackVerifier(nilErroringVerifier, errErroringVerifier) | ||
if err = v.Verify(nil, nil); err != nil { | ||
t.Errorf("should not have errored: %v", err) | ||
} | ||
|
||
v = NewFallbackVerifier(errErroringVerifier, errErroringVerifier) | ||
if err = v.Verify(nil, nil); err == nil { | ||
t.Error("should have errored") | ||
} | ||
} |
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