Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appsync: the ARN needed to grant access to the API has to contain 'v1' #26746

Open
baartch opened this issue Aug 14, 2023 · 2 comments
Open

appsync: the ARN needed to grant access to the API has to contain 'v1' #26746

baartch opened this issue Aug 14, 2023 · 2 comments
Labels
@aws-cdk/aws-appsync Related to AWS AppSync bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@baartch
Copy link

baartch commented Aug 14, 2023

Describe the bug

I want to grant a Lambda function to rotate the API_KEY of the AppSync API. If I provide the ARN of the AppSync API to the PolicyStatement of the Lambda, I get the following error.

    // Creates the AppSync API
    const api = new aws_appsync.GraphqlApi(this, 'Api', {...});
    //Lambda function to rotate API key
    const funcAPIKeyRotate = new PythonFunction(this, 'funcAPIKeyRotate', {...});
    //Grant the Lambda permission to create a new API key
    funcAPIKeyRotate.addToRolePolicy(
      new aws_iam.PolicyStatement({
        actions: ['appsync:CreateApiKey'],
        resources: [api.arn + '/apikeys'],
      }),
    );
... is not authorized to perform: appsync:CreateApiKey on resource:arn:aws:appsync:eu-central-1:<<accountId>>:/v1/apis/<<myApiId>>/apikeys

Please note the v1 before apis

Oh, and just for completeness. There is the same behaviour when I try to grant via the API object:

api.grant(funcAPIKeyRotate, aws_appsync.IamResource.all(), 'appsync:CreateApiKey');

Expected Behavior

I expect the ARN coming from the API constructor being the same as the one needed to grant permission to.

Current Behavior

The ARN coming from the API constructor is without v1, and the one needed to grant a Lambda permission to the API has to contain a v1.

Coming from API Constructor:

arn:aws:appsync:eu-central-1:<<accountId>>:apis/<<myApiId>>

Needed to grant a Lambda:

arn:aws:appsync:eu-central-1:<<accountId>>:/v1/apis/<<myApiId>>

Reproduction Steps

To reproduce, see my bug description.

Possible Solution

My workaround is to concat the strings manually. I don't know if there is any better solution.

    const splittedApiArn = Fn.split('apis', api.arn);
    const joinedArn = Fn.join('', [Fn.select(0, splittedApiArn), '/v1/apis/', api.apiId, '/apikeys']);
    funcAPIKeyRotate.addToRolePolicy(
      new aws_iam.PolicyStatement({
        actions: ['appsync:CreateApiKey'],
        resources: [joinedArn], 
      }),
    );

Additional Information/Context

No response

CDK CLI Version

2.91.0 (build eea12ea)

Framework Version

No response

Node.js Version

Node.js v18.17.0

OS

Ubuntu 22.04 - Linux version 5.15.90.1-microsoft-standard-WSL2

Language

Typescript

Language Version

[email protected]

Other information

No response

@baartch baartch added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 14, 2023
@github-actions github-actions bot added the @aws-cdk/aws-appsync Related to AWS AppSync label Aug 14, 2023
@pahud pahud added p2 effort/small Small work item – less than a day of effort labels Aug 14, 2023
@pahud
Copy link
Contributor

pahud commented Aug 14, 2023

The api.arn is actually from the cloudformation returned values:

this.arn = this.api.attrArn;

Arn
The Amazon Resource Name (ARN) of the API key, such as arn:aws:appsync:us-east-1:123456789012:apis/graphqlapiid.

If the createApiKey does expected versioned arn, maybe we should return a versioned arn attribute from the constructor but I am not sure if this is a good idea.

@pahud pahud removed the needs-triage This issue or PR still needs to be triaged. label Aug 14, 2023
@cm-dyoshikawa
Copy link

@pahud
It may be more convenient for users. So, could we add new propety .versionedArn to GraphqlApi class?
if it seems ok, I'll take it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-appsync Related to AWS AppSync bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

No branches or pull requests

3 participants