Skip to content

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

Open
@orekav

Description

@orekav

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

    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.

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 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-appsyncRelated to AWS AppSyncbugThis issue is a bug.documentationThis is a problem with documentation.effort/mediumMedium work item – several days of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions