-
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.
fix: v6, wrong public key encoding assumed
The KMS GetPublicKey endpoint returns DER encoded key. The library previously assumed PEM encoding.
- Loading branch information
Showing
9 changed files
with
230 additions
and
50 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,52 @@ | ||
name: CI | ||
|
||
on: [push] | ||
|
||
env: | ||
GO_VERSION: '1.23' | ||
|
||
|
||
jobs: | ||
lint: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: "${{ env.GO_VERSION }}" | ||
|
||
- name: Mark source directory as safe | ||
run: git config --global --add safe.directory $GITHUB_WORKSPACE | ||
|
||
- name: go.mod check | ||
run: | | ||
go mod tidy | ||
git diff --no-patch --exit-code go.mod go.sum | ||
if [ $? -ne 0 ]; then | ||
echo "Please run go mod tidy and commit the changes." | ||
exit 1 | ||
fi | ||
- uses: golangci/golangci-lint-action@v6 | ||
with: | ||
version: v1.63 | ||
|
||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: "${{ env.GO_VERSION }}" | ||
- name: Start LocalStack | ||
uses: LocalStack/[email protected] | ||
with: | ||
image-tag: '4.0.3' | ||
env: | ||
LOCALSTACK_CI_PROJECT: "${{ env.CI_PROJECT }}" | ||
- run: go test -race ./... |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
|
||
.PHONY: localstack | ||
localstack: | ||
docker run --rm -it -e SERVICES="kms" -p 4566:4566 localstack/localstack:4.0.3 |
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,101 @@ | ||
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/golang-jwt/jwt/v4" | ||
"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") | ||
}) | ||
|
||
t.Run("RFC compliance", func(t *testing.T) { | ||
signer := kmsjwt.New(client.KMS, keyID) | ||
|
||
signature, err := signer.Sign(in, ctx) | ||
require.NoError(t, err, "sign") | ||
|
||
builtinSigner := jwt.GetSigningMethod(signer.Alg()) | ||
require.NotNil(t, builtinSigner, "unknown algorithm") | ||
|
||
err = builtinSigner.Verify(in, signature, publicKey) | ||
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) | ||
} |