aws / aws/aws-cdk

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

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

Description

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

```typescript
// 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:<>:/v1/apis/<>/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:
```typescript
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:<>:apis/<>
```
Needed to grant a Lambda:
```
arn:aws:appsync:eu-central-1:<>:/v1/apis/<>
```

### 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.
```typescript
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

typescript@5.0.4

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

No repository files or tests are named. Start by locating the AppSync GraphqlApi ARN implementation and the grant or IAM resource helpers, then reproduce the CreateApiKey policy from the report. Done means the generated API ARN and the ARN used for granting CreateApiKey permissions consistently include the required /v1 segment, with coverage for the grant path.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api, authorization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.