diff --git a/internal/conf/client_assertion.go b/internal/conf/client_assertion.go new file mode 100644 index 0000000000..1aabf33287 --- /dev/null +++ b/internal/conf/client_assertion.go @@ -0,0 +1,153 @@ +package conf + +import ( + "context" + "crypto" + "fmt" + "slices" + + "github.com/lestrrat-go/jwx/v2/jwa" + "github.com/lestrrat-go/jwx/v2/jwk" +) + +// ClientAssertionConfiguration configures the keys used to sign client +// assertions (private_key_jwt, RFC 7523) that Auth sends to custom OAuth +// providers' token endpoints instead of a client secret. +type ClientAssertionConfiguration struct { + // Enabled allows custom OAuth providers to use private_key_jwt. + // Env: GOTRUE_EXPERIMENTAL_CLIENT_ASSERTION_ENABLED=true + Enabled bool `json:"enabled" default:"false"` + + // Keys is a JWK set in the same format as GOTRUE_JWT_KEYS, used only to + // sign client assertions. Plain RSA private keys and KMS-backed keys + // (aws:kms:arn) are supported, and every key must be RS256. As with + // GOTRUE_JWT_KEYS, the active signing key is the single key with key_ops + // ["sign"]; other keys are kept for rotation. No key may be shared with + // GOTRUE_JWT_KEYS. Keys are validated whenever they are set, even if + // Enabled is false. + // Env: GOTRUE_EXPERIMENTAL_CLIENT_ASSERTION_KEYS='[{...}]' + Keys JwtKeysDecoder `json:"-"` + + // Issuer overrides the iss claim of client assertions, which defaults to + // the provider's client_id. Must not equal GOTRUE_JWT_ISSUER. + // Env: GOTRUE_EXPERIMENTAL_CLIENT_ASSERTION_ISSUER=https://auth.example.com + Issuer string `json:"issuer"` + + // SigningJwk is the active key from Keys. Set in ApplyDefaults. + SigningJwk jwk.Key `ignored:"true" json:"-"` + + // SigningKey returns the key to sign client assertions with. Set in + // ApplyDefaults. + SigningKey func(context.Context) (any, error) `ignored:"true" json:"-"` +} + +// applyDefaults checks the client assertion keys on their own and sets up the +// active signing key. Checks that need other parts of the configuration are in +// validate. +func (c *ClientAssertionConfiguration) applyDefaults() error { + c.SigningJwk = nil + c.SigningKey = nil + + if len(c.Keys) == 0 { + return nil + } + + if err := c.Keys.Validate(); err != nil { + return fmt.Errorf("conf: invalid client assertion keys: %w", err) + } + + var signingJwk jwk.Key + for kid, key := range c.Keys { + if err := validateClientAssertionKey(kid, key.PrivateKey); err != nil { + return err + } + if slices.Contains(key.PrivateKey.KeyOps(), jwk.KeyOpSign) { + signingJwk = key.PrivateKey + } + } + + signingKey, err := getSigningKey(signingJwk) + if err != nil { + return fmt.Errorf("conf: client assertion signing key: %w", err) + } + + c.SigningJwk = signingJwk + c.SigningKey = signingKey + return nil +} + +func validateClientAssertionKey(kid string, key jwk.Key) error { + if kid == "" { + return fmt.Errorf("conf: client assertion keys must have a kid") + } + if key.KeyType() != jwa.RSA { + return fmt.Errorf("conf: client assertion key %q must be an RSA key, got %q", kid, key.KeyType()) + } + if key.Algorithm().String() != jwa.RS256.String() { + return fmt.Errorf("conf: client assertion key %q must use alg RS256, got %q", kid, key.Algorithm()) + } + + _, _, isKMS, err := kmsKeyARN(key) + if err != nil { + return fmt.Errorf("conf: client assertion key %q: %w", kid, err) + } + if isKMS { + return nil + } + + if slices.Contains(key.KeyOps(), jwk.KeyOpSign) { + if _, ok := key.(jwk.RSAPrivateKey); !ok { + return fmt.Errorf("conf: client assertion signing key %q must be a private key", kid) + } + } + return nil +} + +// validate checks the client assertion configuration against the rest of the +// configuration. The assertion keys must never be usable as +// session keys, so they may not share a kid or key material with the JWT keys. +func (c *ClientAssertionConfiguration) validate(jwtConfig *JWTConfiguration) error { + if c.Issuer != "" && c.Issuer == jwtConfig.Issuer { + return fmt.Errorf("conf: client assertion issuer must not equal the JWT issuer") + } + + if len(c.Keys) == 0 { + if c.Enabled { + return fmt.Errorf("conf: client assertion is enabled but no client assertion keys are configured") + } + return nil + } + + jwtThumbprints := make(map[string]struct{}, len(jwtConfig.Keys)) + for _, key := range jwtConfig.Keys { + k := key.PublicKey + if k == nil { + k = key.PrivateKey + } + tp, err := k.Thumbprint(crypto.SHA256) + if err != nil { + return fmt.Errorf("conf: computing JWT key thumbprint: %w", err) + } + jwtThumbprints[string(tp)] = struct{}{} + } + + for kid, key := range c.Keys { + if _, ok := jwtConfig.Keys[kid]; ok || kid == jwtConfig.KeyID { + return fmt.Errorf("conf: client assertion key %q has the same kid as a JWT key", kid) + } + + tp, err := key.PublicKey.Thumbprint(crypto.SHA256) + if err != nil { + return fmt.Errorf("conf: computing client assertion key thumbprint: %w", err) + } + if _, ok := jwtThumbprints[string(tp)]; ok { + return fmt.Errorf("conf: client assertion key %q is also used as a JWT key", kid) + } + } + + if c.Enabled && c.SigningKey == nil { + return fmt.Errorf("conf: client assertion is enabled but no client assertion signing key is configured") + } + + return nil +} diff --git a/internal/conf/client_assertion_test.go b/internal/conf/client_assertion_test.go new file mode 100644 index 0000000000..5a0105c226 --- /dev/null +++ b/internal/conf/client_assertion_test.go @@ -0,0 +1,302 @@ +package conf + +import ( + "context" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/rsa" + "encoding/json" + "testing" + + "github.com/kelseyhightower/envconfig" + "github.com/lestrrat-go/jwx/v2/jwk" + "github.com/stretchr/testify/require" + "github.com/supabase/auth/internal/conf/awskmsjwk" +) + +func newTestJwk(t *testing.T, raw any, fields map[string]any) jwk.Key { + t.Helper() + k, err := jwk.FromRaw(raw) + require.NoError(t, err) + for name, value := range fields { + require.NoError(t, k.Set(name, value)) + } + return k +} + +func newTestRSAKey(t *testing.T) *rsa.PrivateKey { + t.Helper() + k, err := rsa.GenerateKey(rand.Reader, 2048) + require.NoError(t, err) + return k +} + +func signingFields(kid string) map[string]any { + return map[string]any{ + jwk.KeyIDKey: kid, + jwk.AlgorithmKey: "RS256", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpSign}, + } +} + +func encodeTestKeys(t *testing.T, keys ...jwk.Key) string { + t.Helper() + b, err := json.Marshal(keys) + require.NoError(t, err) + return string(b) +} + +// newClientAssertionTestConfig returns a config whose JWT keys hold one RSA +// key (kid "session-key"), so the disjointness checks have something to +// compare against. +func newClientAssertionTestConfig(t *testing.T, jwtKey *rsa.PrivateKey) *GlobalConfiguration { + t.Helper() + c := &GlobalConfiguration{} + c.JWT.Issuer = "https://project.example.com/auth/v1" + require.NoError(t, c.JWT.Keys.Decode(encodeTestKeys(t, + newTestJwk(t, jwtKey, signingFields("session-key")), + ))) + return c +} + +func applyAndValidateClientAssertion(c *GlobalConfiguration) error { + if err := c.ApplyDefaults(); err != nil { + return err + } + return c.Experimental.ClientAssertion.validate(&c.JWT) +} + +func TestClientAssertionConfig(t *testing.T) { + jwtKey := newTestRSAKey(t) + assertionKey := newTestRSAKey(t) + ecKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + require.NoError(t, err) + + t.Run("unset and disabled loads unchanged", func(t *testing.T) { + c := newClientAssertionTestConfig(t, jwtKey) + require.NoError(t, applyAndValidateClientAssertion(c)) + require.Nil(t, c.Experimental.ClientAssertion.SigningKey) + require.Nil(t, c.Experimental.ClientAssertion.SigningJwk) + }) + + t.Run("valid plain RSA key", func(t *testing.T) { + c := newClientAssertionTestConfig(t, jwtKey) + c.Experimental.ClientAssertion.Enabled = true + c.Experimental.ClientAssertion.Issuer = "https://assertion-issuer.example.com" + require.NoError(t, c.Experimental.ClientAssertion.Keys.Decode(encodeTestKeys(t, + newTestJwk(t, assertionKey, signingFields("assertion-key")), + ))) + require.NoError(t, applyAndValidateClientAssertion(c)) + + require.Equal(t, "assertion-key", c.Experimental.ClientAssertion.SigningJwk.KeyID()) + require.Equal(t, "RS256", GetSigningAlg(c.Experimental.ClientAssertion.SigningJwk).Alg()) + sk, err := c.Experimental.ClientAssertion.SigningKey(context.Background()) + require.NoError(t, err) + require.True(t, assertionKey.Equal(sk)) + }) + + t.Run("active key is the one with sign key_ops", func(t *testing.T) { + c := newClientAssertionTestConfig(t, jwtKey) + c.Experimental.ClientAssertion.Enabled = true + require.NoError(t, c.Experimental.ClientAssertion.Keys.Decode(encodeTestKeys(t, + newTestJwk(t, newTestRSAKey(t), map[string]any{ + jwk.KeyIDKey: "old-key", + jwk.AlgorithmKey: "RS256", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpVerify}, + }), + newTestJwk(t, assertionKey, signingFields("assertion-key")), + ))) + require.NoError(t, applyAndValidateClientAssertion(c)) + require.Equal(t, "assertion-key", c.Experimental.ClientAssertion.SigningJwk.KeyID()) + }) + + t.Run("KMS-backed key", func(t *testing.T) { + pub := newTestJwk(t, &assertionKey.PublicKey, signingFields("kms-key")) + require.NoError(t, pub.Set("aws:kms:arn", "arn:aws:kms:us-east-1:123456789012:key/00000000-0000-0000-0000-000000000000")) + + c := newClientAssertionTestConfig(t, jwtKey) + c.Experimental.ClientAssertion.Enabled = true + require.NoError(t, c.Experimental.ClientAssertion.Keys.Decode(encodeTestKeys(t, pub))) + require.NoError(t, applyAndValidateClientAssertion(c)) + + require.Equal(t, awskmsjwk.SigningMethodRS256KMS, GetSigningAlg(c.Experimental.ClientAssertion.SigningJwk)) + sk, err := c.Experimental.ClientAssertion.SigningKey(context.Background()) + require.NoError(t, err) + require.IsType(t, &awskmsjwk.RS256Key{}, sk) + }) + + rejections := []struct { + name string + keys func(t *testing.T) []jwk.Key + enabled bool + issuer string + contains string + }{ + { + name: "enabled without keys", + enabled: true, + contains: "no client assertion keys are configured", + }, + { + name: "HMAC key", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, []byte("0123456789abcdef0123456789abcdef"), map[string]any{ + jwk.KeyIDKey: "hmac-key", + jwk.AlgorithmKey: "HS256", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpSign}, + })} + }, + enabled: true, + contains: "must be an RSA key", + }, + { + name: "HMAC key is rejected even when disabled", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, []byte("0123456789abcdef0123456789abcdef"), map[string]any{ + jwk.KeyIDKey: "hmac-key", + jwk.AlgorithmKey: "HS256", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpSign}, + })} + }, + contains: "must be an RSA key", + }, + { + name: "EC key", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, ecKey, map[string]any{ + jwk.KeyIDKey: "ec-key", + jwk.AlgorithmKey: "ES256", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpSign}, + })} + }, + enabled: true, + contains: "must be an RSA key", + }, + { + name: "RSA key with a non-RS256 alg", + keys: func(t *testing.T) []jwk.Key { + f := signingFields("rs512-key") + f[jwk.AlgorithmKey] = "RS512" + return []jwk.Key{newTestJwk(t, assertionKey, f)} + }, + enabled: true, + contains: "must use alg RS256", + }, + { + name: "RSA key without alg", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, assertionKey, map[string]any{ + jwk.KeyIDKey: "no-alg-key", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpSign}, + })} + }, + enabled: true, + contains: "must use alg RS256", + }, + { + name: "key without kid", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, assertionKey, map[string]any{ + jwk.AlgorithmKey: "RS256", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpSign}, + })} + }, + enabled: true, + contains: "must have a kid", + }, + { + name: "no signing key", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, assertionKey, map[string]any{ + jwk.KeyIDKey: "verify-only", + jwk.AlgorithmKey: "RS256", + jwk.KeyOpsKey: jwk.KeyOperationList{jwk.KeyOpVerify}, + })} + }, + enabled: true, + contains: "no signing key detected", + }, + { + name: "more than one signing key", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{ + newTestJwk(t, assertionKey, signingFields("key-1")), + newTestJwk(t, newTestRSAKey(t), signingFields("key-2")), + } + }, + enabled: true, + contains: "multiple signing keys detected", + }, + { + name: "signing key is a public key without KMS", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, &assertionKey.PublicKey, signingFields("public-key"))} + }, + enabled: true, + contains: "must be a private key", + }, + { + name: "malformed KMS ARN", + keys: func(t *testing.T) []jwk.Key { + k := newTestJwk(t, &assertionKey.PublicKey, signingFields("kms-key")) + require.NoError(t, k.Set("aws:kms:arn", "not-an-arn")) + return []jwk.Key{k} + }, + enabled: true, + contains: "invalid aws:kms:arn", + }, + { + name: "kid shared with a JWT key", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, assertionKey, signingFields("session-key"))} + }, + enabled: true, + contains: "same kid as a JWT key", + }, + { + name: "key material shared with a JWT key", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, jwtKey, signingFields("assertion-key"))} + }, + enabled: true, + contains: "also used as a JWT key", + }, + { + name: "issuer override equals the JWT issuer", + keys: func(t *testing.T) []jwk.Key { + return []jwk.Key{newTestJwk(t, assertionKey, signingFields("assertion-key"))} + }, + enabled: true, + issuer: "https://project.example.com/auth/v1", + contains: "must not equal the JWT issuer", + }, + } + + for _, tc := range rejections { + t.Run("rejects "+tc.name, func(t *testing.T) { + c := newClientAssertionTestConfig(t, jwtKey) + c.Experimental.ClientAssertion.Enabled = tc.enabled + c.Experimental.ClientAssertion.Issuer = tc.issuer + if tc.keys != nil { + require.NoError(t, c.Experimental.ClientAssertion.Keys.Decode(encodeTestKeys(t, tc.keys(t)...))) + } + err := applyAndValidateClientAssertion(c) + require.Error(t, err) + require.Contains(t, err.Error(), tc.contains) + }) + } +} + +func TestClientAssertionConfigFromEnv(t *testing.T) { + key := newTestJwk(t, newTestRSAKey(t), signingFields("assertion-key")) + t.Setenv("GOTRUE_EXPERIMENTAL_CLIENT_ASSERTION_ENABLED", "true") + t.Setenv("GOTRUE_EXPERIMENTAL_CLIENT_ASSERTION_KEYS", encodeTestKeys(t, key)) + t.Setenv("GOTRUE_EXPERIMENTAL_CLIENT_ASSERTION_ISSUER", "https://assertion-issuer.example.com") + + var c ExperimentalConfiguration + require.NoError(t, envconfig.Process("gotrue_experimental", &c)) + require.True(t, c.ClientAssertion.Enabled) + require.Equal(t, "https://assertion-issuer.example.com", c.ClientAssertion.Issuer) + require.Contains(t, c.ClientAssertion.Keys, "assertion-key") +} diff --git a/internal/conf/configuration.go b/internal/conf/configuration.go index 1e5fb3af79..a61329e2e5 100644 --- a/internal/conf/configuration.go +++ b/internal/conf/configuration.go @@ -431,6 +431,11 @@ type ExperimentalConfiguration struct { // one-time token is created. Ships dark: nothing reads the column yet. // Env: GOTRUE_EXPERIMENTAL_ONE_TIME_TOKEN_EXPIRES_AT_WRITE_ENABLED=true OneTimeTokenExpiresAtWriteEnabled bool `split_words:"true" default:"false"` + + // ClientAssertion configures signed client assertions (private_key_jwt) + // for custom OAuth providers. + // Env: GOTRUE_EXPERIMENTAL_CLIENT_ASSERTION_* + ClientAssertion ClientAssertionConfiguration `split_words:"true"` } // ReloadingConfiguration holds the configuration values for runtime @@ -1185,6 +1190,10 @@ func (config *GlobalConfiguration) ApplyDefaults() error { config.JWT.SigningKey = sk } + if err := config.Experimental.ClientAssertion.applyDefaults(); err != nil { + return err + } + if config.JWT.ValidMethods == nil { config.JWT.ValidMethods = []string{} for _, key := range config.JWT.Keys { @@ -1392,6 +1401,10 @@ func (c *GlobalConfiguration) Validate() error { } } + if err := c.Experimental.ClientAssertion.validate(&c.JWT); err != nil { + return err + } + if c.Passkey.Enabled || c.MFA.WebAuthn.EnrollEnabled || c.MFA.WebAuthn.VerifyEnabled { if err := c.WebAuthn.Validate(); err != nil { logrus.WithError(err).Warn("WebAuthn configuration is invalid") diff --git a/internal/conf/jwk.go b/internal/conf/jwk.go index ede982de04..cb74b343f3 100644 --- a/internal/conf/jwk.go +++ b/internal/conf/jwk.go @@ -134,16 +134,33 @@ func GetSigningJwk(config *JWTConfiguration) (jwk.Key, error) { return nil, fmt.Errorf("no signing key found") } +// kmsKeyARN returns the aws:kms:arn of a KMS-backed key and its region. +// isKMS is false if the key has no aws:kms:arn. An error is returned if the +// value is not a string or not a full ARN. +func kmsKeyARN(k jwk.Key) (arn, region string, isKMS bool, err error) { + value, isKMS := k.Get("aws:kms:arn") + if !isKMS { + return "", "", false, nil + } + + arn, ok := value.(string) + if !ok { + return "", "", true, fmt.Errorf("invalid aws:kms:arn: not a string") + } + // arn:partition:service:region:account-id:resource + parts := strings.SplitN(arn, ":", 5) + if len(parts) != 5 { + return "", "", true, fmt.Errorf("invalid aws:kms:arn %q", arn) + } + return arn, parts[3], true, nil +} + func getSigningKey(k jwk.Key) (func(context.Context) (any, error), error) { - if value, isKMS := k.Get("aws:kms:arn"); isKMS && k.KeyOps()[0] == jwk.KeyOpSign { - kmsARN, ok := value.(string) - if !ok { - return nil, fmt.Errorf("conf: jwk key has aws:kms:arn but is not a string %v", value) + if kmsARN, region, isKMS, err := kmsKeyARN(k); isKMS && k.KeyOps()[0] == jwk.KeyOpSign { + if err != nil { + return nil, fmt.Errorf("conf: jwk key: %w", err) } - parts := strings.SplitN(kmsARN, ":", 5) - region := parts[3] - cfg, err := config.LoadDefaultConfig( context.Background(), config.WithRegion(region), diff --git a/internal/conf/jwk_test.go b/internal/conf/jwk_test.go index 18cd72e4d4..a4b49593e8 100644 --- a/internal/conf/jwk_test.go +++ b/internal/conf/jwk_test.go @@ -2,6 +2,8 @@ package conf import ( "context" + "crypto/rand" + "crypto/rsa" "encoding/json" "errors" "fmt" @@ -421,3 +423,24 @@ func (o *mockKey) PublicKey() (jwk.Key, error) { } const testJwtKey = `[{"kty":"oct","k":"9Sj51i2YvfY85NJZFD6rAl9fKDxSKjFgW6W6ZXOJLnU","kid":"1","key_ops":["verify"],"alg":"HS256"},{"kty":"RSA","n":"4slQjr-XoU6I1KXFWOeeJi387RIUxjhyzXX3GUVNb75a0SPKoGShlJEbpvuXqkDLGDweLcIZy-01nqgjSzMY_tUO3L78MxVfIVn7MByJ4_zbrVf5rjKeAk9EEMl6pb8nKJGArph9sOwL68LLioNySt_WNo_hMfuxUuVkRagh5gLjYoQ4odkULQrgwlMcXxXNnvg0aYURUr2SDmncHNuZQ3adebRlI164mUZPPWui2fg72R7c9qhVaAEzbdG-JAuC3zn5iL4zZk-8pOwZkM7Qb_2lrcXwdTl_Qz6fMdAHz_3rggac5oeKkdvO2x7_XiUwGxIBYSghxg5BBxcyqd6WrQ","e":"AQAB","d":"FjJo7uH4aUoktO8kHhbHbY_KSdQpHDjKyc7yTS_0DWYgUfdozzubJfRDF42vI-KsXssF-NoB0wJf0uP0L8ip6G326XPuoMQRTMgcaF8j6swTwsapSOEagr7BzcECx1zpc2-ojhwbLHSvRutWDzPJkbrUccF8vRC6BsiAUG4Hapiumbot7JtJGwU8ZUhxico7_OEJ_MtkRrHByXgrOMnzNLrmViI9rzvtWOhVc8sNDzLogDDi01AP0j6WeBhbOpaZ_1BMLQ9IeeN5Iiy-7Qj-q4-8kBXIPXpYaKMFnDTmhB0GAVUFimF6ojhZNAJvV81VMHPjrEmmps0_qBfIlKAB","p":"9G7wBpiSJHAl-w47AWvW60v_hye50lte4Ep2P3KeRyinzgxtEMivzldoqirwdoyPCJWwU7nNsv7AjdXVoHFy3fJvJeV5mhArxb2zA36OS_Tr3CQXtB3OO-RFwVcG7AGO7XvA54PK28siXY2VvkG2Xn_ZrbVebJnHQprn7ddUIIE","q":"7YSaG2E_M9XpgUJ0izwKdfGew6Hz5utPUdwMWjqr81BjtLkUtQ3tGYWs2tdaRYUTK4mNFyR2MjLYnMK-F37rue4LSKitmEu2N6RD9TwzcqwiEL_vuQTC985iJ0hzUC58LcbhYtTLU3KqZXXUqaeBXEwQAWxK1NRf6rQRhOGk4C0","dp":"fOV-sfAdpI7FaW3RCp3euGYh0B6lXW4goXyKxUq8w2FrtOY2iH_zDP0u1tyP-BNENr-91Fo5V__BxfeAa7XsWqo4zuVdaDJhG24d3Wg6L2ebaOXsUrV0Hrg6SFs-hzMYpBI69FEsQ3idO65P2GJdXBX51T-6WsWMwmTCo44GR4E","dq":"O2DrJe0p38ualLYIbMaV1uaQyleyoggxzEU20VfZpPpz8rpScvEIVVkV3Z_48WhTYo8AtshmxCXyAT6uRzFzvQfFymRhAbHr2_01ABoMwp5F5eoWBCsskscFwsxaB7GXWdpefla0figscTED-WXm8SwS1Eg-bParBAIAXzgKAAE","qi":"Cezqw8ECfMmwnRXJuiG2A93lzhixHxXISvGC-qbWaRmCfetheSviZlM0_KxF6dsvrw_aNfIPa8rv1TbN-5F04v_RU1CD79QuluzXWLkZVhPXorkK5e8sUi_odzAJXOwHKQzal5ndInl4XYctDHQr8jXcFW5Un65FhPwdAC6-aek","kid":"2","key_ops":["verify"],"alg":"RS256"},{"kty":"EC","x":"GwbnH57MUhgL14dJfayyzuI6o2_mB_Pm8xIuauHXtQs","y":"cYqN0VAcv0BC9wrg3vNgHlKhGP8ZEedUC2A8jXpaGwA","crv":"P-256","d":"4STEXq7W4UY0piCGPueMaQqAAZ5jVRjjA_b1Hq7YgmM","kid":"3","key_ops":["sign","verify"],"alg":"ES256"},{"crv":"Ed25519","d":"T179kXSOJHE8CNbqaI2HNdG8r3YbSoKYxNRSzTkpEcY","x":"iDYagELzmD4z6uaW7eAZLuQ9fiUlnLqtrh7AfNbiNiI","kty":"OKP","kid":"4","key_ops":["verify"],"alg":"EdDSA"}]` + +func TestGetSigningKeyRejectsInvalidKMSARN(t *testing.T) { + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) + require.NoError(t, err) + + for name, arn := range map[string]any{ + "too few parts": "arn:aws:kms", + "not a string": 123, + } { + t.Run(name, func(t *testing.T) { + k, err := jwk.FromRaw(rsaKey.Public()) + require.NoError(t, err) + require.NoError(t, k.Set(jwk.KeyOpsKey, jwk.KeyOperationList{jwk.KeyOpSign})) + require.NoError(t, k.Set("aws:kms:arn", arn)) + + // used to panic with index out of range + _, err = getSigningKey(k) + require.ErrorContains(t, err, "invalid aws:kms:arn") + }) + } +}