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

fix(apigateway): permission error in lambda integration when function name is modified #8813

Merged
merged 6 commits into from Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/integration.ts
Expand Up @@ -134,6 +134,18 @@ export interface IntegrationProps {
readonly options?: IntegrationOptions;
}

/**
* Result of binding an Integration to the Method
*/
export interface IntegrationConfig {
/**
* Triggers a new Deployment if the value of this property changes.
* Use this property to trigger new deployments for changes to values in this integration.
* @default - deployments are not triggered for any change of this integration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note that it can be an arbitrary string and does not have to be a hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to index type <string, string> so the user doesn't have to do JSON.stringify() unless they're adding a nested struct. The input to the hash gen will also be a bit nicer without the serialized objects.

*/
readonly triggerDeployment?: any;
nija-at marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Base class for backend integrations for an API Gateway method.
*
Expand All @@ -156,8 +168,8 @@ export class Integration {
* Can be overridden by subclasses to allow the integration to interact with the method
* being integrated, access the REST API object, method ARNs, etc.
*/
public bind(_method: Method) {
return;
public bind(_method: Method): IntegrationConfig {
nija-at marked this conversation as resolved.
Show resolved Hide resolved
return {};
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-apigateway/lib/integrations/aws.ts
@@ -1,5 +1,5 @@
import * as cdk from '@aws-cdk/core';
import { Integration, IntegrationOptions, IntegrationType } from '../integration';
import { Integration, IntegrationConfig, IntegrationOptions, IntegrationType } from '../integration';
import { Method } from '../method';
import { parseAwsApiCall } from '../util';

Expand Down Expand Up @@ -92,7 +92,8 @@ export class AwsIntegration extends Integration {
});
}

public bind(method: Method) {
public bind(method: Method): IntegrationConfig {
this.scope = method;
return {};
}
}
15 changes: 12 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/integrations/lambda.ts
@@ -1,7 +1,7 @@
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { Lazy } from '@aws-cdk/core';
import { IntegrationOptions } from '../integration';
import { Lazy, Token } from '@aws-cdk/core';
import { IntegrationConfig, IntegrationOptions } from '../integration';
import { Method } from '../method';
import { AwsIntegration } from './aws';

Expand Down Expand Up @@ -52,7 +52,7 @@ export class LambdaIntegration extends AwsIntegration {
this.enableTest = options.allowTestInvoke === undefined ? true : false;
}

public bind(method: Method) {
public bind(method: Method): IntegrationConfig {
super.bind(method);
const principal = new iam.ServicePrincipal('apigateway.amazonaws.com');

Expand All @@ -72,5 +72,14 @@ export class LambdaIntegration extends AwsIntegration {
sourceArn: method.testMethodArn,
});
}

const cfnFunction = this.handler.node.defaultChild as lambda.CfnFunction;
let triggerDeployment;
if (!Token.isUnresolved(cfnFunction.functionName)) {
triggerDeployment = { functionName: cfnFunction.functionName };
}
return {
triggerDeployment,
};
}
}
26 changes: 11 additions & 15 deletions packages/@aws-cdk/aws-apigateway/lib/method.ts
Expand Up @@ -192,6 +192,9 @@ export class Method extends Resource {
authorizer._attachToApi(this.api);
}

const integration = props.integration ?? this.resource.defaultIntegration ?? new MockIntegration();
eladb marked this conversation as resolved.
Show resolved Hide resolved
const bindResult = integration.bind(this);

const methodProps: CfnMethodProps = {
resourceId: props.resource.resourceId,
restApiId: this.api.restApiId,
Expand All @@ -201,7 +204,7 @@ export class Method extends Resource {
authorizationType,
authorizerId,
requestParameters: options.requestParameters || defaultMethodOptions.requestParameters,
integration: this.renderIntegration(props.integration),
integration: this.renderIntegration(integration),
methodResponses: this.renderMethodResponses(options.methodResponses),
requestModels: this.renderRequestModels(options.requestModels),
requestValidatorId: this.requestValidatorId(options),
Expand All @@ -219,7 +222,12 @@ export class Method extends Resource {
const deployment = props.resource.api.latestDeployment;
if (deployment) {
deployment.node.addDependency(resource);
deployment.addToLogicalId({ method: methodProps });
deployment.addToLogicalId({
method: {
...methodProps,
...bindResult?.triggerDeployment,
},
});
}
}

Expand Down Expand Up @@ -255,19 +263,7 @@ export class Method extends Resource {
return this.api.arnForExecuteApi(this.httpMethod, pathForArn(this.resource.path), 'test-invoke-stage');
}

private renderIntegration(integration?: Integration): CfnMethod.IntegrationProperty {
if (!integration) {
// use defaultIntegration from API if defined
if (this.resource.defaultIntegration) {
return this.renderIntegration(this.resource.defaultIntegration);
}

// fallback to mock
return this.renderIntegration(new MockIntegration());
}

integration.bind(this);

private renderIntegration(integration: Integration): CfnMethod.IntegrationProperty {
const options = integration._props.options || { };

let credentials;
Expand Down
Expand Up @@ -120,7 +120,7 @@
"BooksApi60AC975F"
]
},
"BooksApiDeployment86CA39AF7e6c771d47a1a3777eba99bffc037822": {
"BooksApiDeployment86CA39AFdfee6753c946e6cbf8b3d649e0cf1377": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand All @@ -141,7 +141,7 @@
"Ref": "BooksApi60AC975F"
},
"DeploymentId": {
"Ref": "BooksApiDeployment86CA39AF7e6c771d47a1a3777eba99bffc037822"
"Ref": "BooksApiDeployment86CA39AFdfee6753c946e6cbf8b3d649e0cf1377"
},
"StageName": "prod"
}
Expand Down