-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add sign and verify test with LocalStack
The mocks made the wrong assumption. Let's test with more realism.
- Loading branch information
Showing
3 changed files
with
134 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package kmsjwt_test | ||
|
||
import ( | ||
"context" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/kms" | ||
"github.com/aws/aws-sdk-go-v2/service/kms/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/spacelift-io/kmsjwt/v6" | ||
) | ||
|
||
func TestWithLocalStack(t *testing.T) { | ||
const in = "sign me, please" | ||
ctx := context.Background() | ||
client := newClient(t, ctx) | ||
keyID := client.CreateKey(t, ctx) | ||
publicKey := client.GetPublicKey(t, ctx, keyID) | ||
|
||
t.Run("new", func(t *testing.T) { | ||
signer := kmsjwt.New(client.KMS, keyID) | ||
|
||
signature, err := signer.Sign(in, ctx) | ||
require.NoError(t, err, "sign") | ||
|
||
err = signer.Verify(in, signature, ctx) | ||
assert.NoError(t, err, "verify") | ||
}) | ||
|
||
t.Run("new with public key", func(t *testing.T) { | ||
signer := kmsjwt.NewWithPublicKey(client.KMS, keyID, publicKey) | ||
|
||
signature, err := signer.Sign(in, ctx) | ||
require.NoError(t, err, "sign") | ||
|
||
err = signer.Verify(in, signature, ctx) | ||
assert.NoError(t, err, "verify") | ||
}) | ||
} | ||
|
||
func newClient(t *testing.T, ctx context.Context) Client { | ||
t.Helper() | ||
|
||
cfg, err := config.LoadDefaultConfig(ctx, | ||
config.WithRegion("eu-west-1"), | ||
config.WithBaseEndpoint("http://localhost:4566"), | ||
config.WithCredentialsProvider( | ||
credentials.NewStaticCredentialsProvider("dummy", "dummy", "dummy"), | ||
), | ||
) | ||
require.NoError(t, err, "load AWS config") | ||
|
||
return Client{KMS: kms.NewFromConfig(cfg)} | ||
} | ||
|
||
type Client struct { | ||
KMS *kms.Client | ||
} | ||
|
||
func (c Client) CreateKey(t *testing.T, ctx context.Context) (id string) { | ||
t.Helper() | ||
result, err := c.KMS.CreateKey(ctx, &kms.CreateKeyInput{ | ||
KeySpec: types.KeySpecRsa4096, | ||
KeyUsage: types.KeyUsageTypeSignVerify, | ||
}) | ||
require.NoError(t, err, "creating KMS key") | ||
return *result.KeyMetadata.KeyId | ||
} | ||
|
||
func (c Client) GetPublicKey(t *testing.T, ctx context.Context, id string) *rsa.PublicKey { | ||
t.Helper() | ||
response, err := c.KMS.GetPublicKey(ctx, &kms.GetPublicKeyInput{ | ||
KeyId: &id, | ||
}) | ||
require.NoError(t, err, "get KMS public key") | ||
|
||
key, err := x509.ParsePKIXPublicKey(response.PublicKey) | ||
require.NoError(t, err, "parsing fetched pubic key") | ||
|
||
return key.(*rsa.PublicKey) | ||
} |