-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split sagoodkey.NewKeyPolicy from goodkey.NewKeyPolicy
... so that goodkey no longer depends on google.golang.org/grpc and github.com/letsencrypt/boulder/sa/proto , making it cheaper to use from external Go code. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
- Loading branch information
Showing
9 changed files
with
110 additions
and
26 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
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
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,31 @@ | ||
package sagoodkey | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/letsencrypt/boulder/goodkey" | ||
sapb "github.com/letsencrypt/boulder/sa/proto" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// BlockedKeyCheckFunc is used to pass in the sa.BlockedKey method to KeyPolicy, | ||
// rather than storing a full sa.SQLStorageAuthority. This makes testing | ||
// significantly simpler. | ||
type BlockedKeyCheckFunc func(context.Context, *sapb.KeyBlockedRequest, ...grpc.CallOption) (*sapb.Exists, error) | ||
|
||
// NewKeyPolicy returns a KeyPolicy that uses a sa.BlockedKey method. | ||
// See goodkey.NewKeyPolicy for more details about the policy itself. | ||
func NewKeyPolicy(config *goodkey.Config, bkc BlockedKeyCheckFunc) (goodkey.KeyPolicy, error) { | ||
var genericCheck goodkey.BlockedKeyCheckFunc | ||
if bkc != nil { | ||
genericCheck = func(ctx context.Context, keyHash []byte) (bool, error) { | ||
exists, err := bkc(ctx, &sapb.KeyBlockedRequest{KeyHash: keyHash}) | ||
if err != nil { | ||
return false, err | ||
} | ||
return exists.Exists, nil | ||
} | ||
} | ||
|
||
return goodkey.NewKeyPolicy(config, genericCheck) | ||
} |
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,47 @@ | ||
package sagoodkey | ||
|
||
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"testing" | ||
|
||
"github.com/letsencrypt/boulder/goodkey" | ||
sapb "github.com/letsencrypt/boulder/sa/proto" | ||
"github.com/letsencrypt/boulder/test" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestDBBlocklistAccept(t *testing.T) { | ||
for _, testCheck := range []BlockedKeyCheckFunc{ | ||
nil, | ||
func(context.Context, *sapb.KeyBlockedRequest, ...grpc.CallOption) (*sapb.Exists, error) { | ||
return &sapb.Exists{Exists: false}, nil | ||
}, | ||
} { | ||
policy, err := NewKeyPolicy(&goodkey.Config{}, testCheck) | ||
test.AssertNotError(t, err, "NewKeyPolicy failed") | ||
|
||
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
test.AssertNotError(t, err, "ecdsa.GenerateKey failed") | ||
err = policy.GoodKey(context.Background(), k.Public()) | ||
test.AssertNotError(t, err, "GoodKey failed with a non-blocked key") | ||
} | ||
} | ||
|
||
func TestDBBlocklistReject(t *testing.T) { | ||
testCheck := func(context.Context, *sapb.KeyBlockedRequest, ...grpc.CallOption) (*sapb.Exists, error) { | ||
return &sapb.Exists{Exists: true}, nil | ||
} | ||
|
||
policy, err := NewKeyPolicy(&goodkey.Config{}, testCheck) | ||
test.AssertNotError(t, err, "NewKeyPolicy failed") | ||
|
||
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
test.AssertNotError(t, err, "ecdsa.GenerateKey failed") | ||
err = policy.GoodKey(context.Background(), k.Public()) | ||
test.AssertError(t, err, "GoodKey didn't fail with a blocked key") | ||
test.AssertErrorIs(t, err, goodkey.ErrBadKey) | ||
test.AssertEquals(t, err.Error(), "public key is forbidden") | ||
} |