aws / aws/aws-cdk

appsync: Unable have 2 or more API Keys (GraphqlApi construct)

Open
#26,296 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-appsync bug documentation effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the issue

By using the L2 Construct `GraphQLApi` we cannot create multiple API_KEYs, if we try to do so we get this error

```typescript
if (modes.filter((mode) => mode.authorizationType === AuthorizationType.API_KEY).length > 1) {
throw new Error('You can\'t duplicate API_KEY configuration. See https://docs.aws.amazon.com/appsync/latest/devguide/security.html');
}
```

My current workaround is this, I consider it a horrible thing to do but in the meantime is what I need.

```typescript
const expires = cdk.Expiration.after(
cdk.Duration.days(365)
).toEpoch();
const firstApiKey = new appsync.CfnApiKey(this, "FirstApiKey", {
apiId: this.apiId,
description: "First Light API Key",
expires,
});
const secondApiKey = new appsync.CfnApiKey(this, "SecondApiKey", {
apiId: this.apiId,
description: "Second API Key",
expires,
});

this.addSchemaDependency(firstApiKey);
this.addSchemaDependency(secondApiKey);

if (!this.modes.includes(appsync.AuthorizationType.API_KEY)) {
const authenticationProvider: appsync.CfnGraphQLApi.AdditionalAuthenticationProviderProperty =
{ authenticationType: appsync.AuthorizationType.API_KEY };
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const cfnGraphQLApi = (this as any)
.api as unknown as appsync.CfnGraphQLApi;
const additionalAuthenticationProviders: typeof cfnGraphQLApi.additionalAuthenticationProviders =
[authenticationProvider];

if (
cfnGraphQLApi.additionalAuthenticationProviders !==
undefined
) {
if (
Array.isArray(
cfnGraphQLApi.additionalAuthenticationProviders
)
) {
additionalAuthenticationProviders.push(
...cfnGraphQLApi.additionalAuthenticationProviders
);
} else {
additionalAuthenticationProviders.push(
cfnGraphQLApi.additionalAuthenticationProviders
);
}
}
cfnGraphQLApi.additionalAuthenticationProviders =
additionalAuthenticationProviders;
}
```

### Links

I have not seen anything in this [link](https://docs.aws.amazon.com/appsync/latest/devguide/security.html) saying that multiple API Keys are a problem. Furthermore, I think in that case it should be a `cdk_nag` rule instead or a warning in CDK but not an error.

Contributor guide

Open the contributing guide

Research direction

Begin at the GraphQLApi construct's API_KEY validation; the report's CfnApiKey workaround and AppSync security link are the available entry points. Verify the supported multiple-key configuration and make the construct synthesize it without throwing, with regression coverage for two keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api, cloud, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.