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

AWS: Add API Gateway stage name validation. #5639

Merged
merged 7 commits into from Jan 4, 2019
@@ -19,6 +19,8 @@ const constants = {
providerName: 'aws', providerName: 'aws',
}; };


const validAPIGatewayStageNamePattern = /^[a-zA-Z0-9_]+$/;

PromiseQueue.configure(BbPromise.Promise); PromiseQueue.configure(BbPromise.Promise);


const impl = { const impl = {
@@ -199,6 +201,19 @@ class AwsProvider {
} }
} }
} }

const stage = this.getStage();
this.serverless.service.getAllFunctions().forEach(funcName => {
_.forEach(this.serverless.service.getAllEventsInFunction(funcName), event => {
if (_.has(event, 'http') && !validAPIGatewayStageNamePattern.test(stage)) {
throw new Error([
`Invalid stage name ${stage}: `,
'it should contains only [a-zA-Z0-9] for AWS provider if http event are present ',
'since API Gateway stage name cannot contains hyphens.',
].join(''));
}
});
});
} }


/** /**
@@ -90,6 +90,54 @@ describe('AwsProvider', () => {
delete process.env.AWS_CLIENT_TIMEOUT; delete process.env.AWS_CLIENT_TIMEOUT;
}); });


describe('validation on construction', () => {
it('should not throw an error if stage name contains only alphanumeric', () => {
const config = {
stage: 'configStage',
};
serverless = new Serverless(config);
expect(() => new AwsProvider(serverless, config)).to.not.throw(Error);
});

it('should not throw an error if stage contains hyphen but http events are absent', () => {
const config = {
stage: 'config-stage',
};
serverless = new Serverless(config);
expect(() => new AwsProvider(serverless, config)).to.not.throw(Error);
});

it('should throw an error if stage contains hyphen and http events are present', () => {
const config = {
stage: 'config-stage',
};
serverless = new Serverless(config);

const serverlessYml = {
service: 'new-service',
provider: {
name: 'aws',
stage: 'config-stage',
},
functions: {
first: {
events: [
{
http: {
path: 'foo',
method: 'GET',
},
},
],
},
},
};
serverless.service = new serverless.classes.Service(serverless, serverlessYml);

expect(() => new AwsProvider(serverless, config)).to.throw(Error);
});
});

describe('certificate authority - environment variable', () => { describe('certificate authority - environment variable', () => {
afterEach('Environment Variable Cleanup', () => { afterEach('Environment Variable Cleanup', () => {
// clear env // clear env
ProTip! Use n and p to navigate between commits in a pull request.