Skip to content

Commit

Permalink
fix(AWS ALB): Allow multiple http-header conditions (#11888)
Browse files Browse the repository at this point in the history
  • Loading branch information
Inqnuam authored May 31, 2023
1 parent 41e90c3 commit 72b27cb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
20 changes: 11 additions & 9 deletions lib/plugins/aws/package/compile/events/alb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ function defineArray(schema) {
return { type: 'array', items: schema };
}

const ALB_HTTP_HEADER_SCHEMA = {
type: 'object',
properties: {
name: { type: 'string', maxLength: 40 },
values: defineArray({ type: 'string', maxLength: 128 }, { uniqueItems: true }),
},
additionalProperties: false,
required: ['name', 'values'],
};

class AwsCompileAlbEvents {
constructor(serverless, options) {
this.serverless = serverless;
Expand Down Expand Up @@ -39,15 +49,7 @@ class AwsCompileAlbEvents {
conditions: {
type: 'object',
properties: {
header: {
type: 'object',
properties: {
name: { type: 'string', maxLength: 40 },
values: { type: 'array', items: { type: 'string', maxLength: 128 } },
},
additionalProperties: false,
required: ['name', 'values'],
},
header: { anyOf: [defineArray(ALB_HTTP_HEADER_SCHEMA), ALB_HTTP_HEADER_SCHEMA] },
host: defineArray({
type: 'string',
pattern: '^[A-Za-z0-9*?.-]+$',
Expand Down
14 changes: 8 additions & 6 deletions lib/plugins/aws/package/compile/events/alb/lib/listener-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ module.exports = {
});
}
if (event.conditions.header) {
Conditions.push({
Field: 'http-header',
HttpHeaderConfig: {
HttpHeaderName: event.conditions.header.name,
Values: event.conditions.header.values,
},
event.conditions.header.forEach(({ name, values }) => {
Conditions.push({
Field: 'http-header',
HttpHeaderConfig: {
HttpHeaderName: name,
Values: values,
},
});
});
}
if (event.conditions.query) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/aws/package/compile/events/alb/lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
albObj.conditions.method = [].concat(event.alb.conditions.method);
}
if (event.alb.conditions.header) {
albObj.conditions.header = event.alb.conditions.header;
albObj.conditions.header = [].concat(event.alb.conditions.header);
}
if (event.alb.conditions.query) {
albObj.conditions.query = event.alb.conditions.query;
Expand Down
71 changes: 71 additions & 0 deletions test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ describe('test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js', (
},
],
},
fnSingleHeaderCondition: {
handler: 'index.handler',
events: [
{
alb: {
...validBaseEventConfig,
priority: 8,
conditions: { header: { name: 'dummyName', values: ['dummyValue'] } },
},
},
],
},
fnMultiHeaderCondition: {
handler: 'index.handler',
events: [
{
alb: {
...validBaseEventConfig,
priority: 9,
conditions: {
header: [
{ name: 'dummyName1', values: ['dummyValue1'] },
{ name: 'dummyName2', values: ['dummyValue2'] },
{ name: 'dummyName3', values: ['dummyMultiValue1', 'dummyMultiValue2'] },
],
},
},
},
],
},
},
},
});
Expand Down Expand Up @@ -276,4 +306,45 @@ describe('test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js', (
.and.have.property('code', 'ALB_TARGET_GROUP_NAME_EXCLUSIVE');
});
});

describe('should set alb header conditions', () => {
it('should convert single header condition to array', () => {
const albListenerRuleLogicalId = naming.getAlbListenerRuleLogicalId(
'fnSingleHeaderCondition',
8
);
const conditions = cfResources[albListenerRuleLogicalId].Properties.Conditions;
expect(conditions).to.have.length(1);
const config = conditions[0].HttpHeaderConfig;
expect(config.HttpHeaderName).to.equal('dummyName');
expect(config.Values).to.have.length(1);
expect(config.Values[0]).to.equal('dummyValue');
});

it('should support multi header conditions', () => {
const albListenerRuleLogicalId = naming.getAlbListenerRuleLogicalId(
'fnMultiHeaderCondition',
9
);
const conditions = cfResources[albListenerRuleLogicalId].Properties.Conditions;
expect(conditions).to.have.length(3);
const [cond1, cond2, cond3] = conditions;

const config1 = cond1.HttpHeaderConfig;
expect(config1.HttpHeaderName).to.equal('dummyName1');
expect(config1.Values).to.have.length(1);
expect(config1.Values[0]).to.equal('dummyValue1');

const config2 = cond2.HttpHeaderConfig;
expect(config2.HttpHeaderName).to.equal('dummyName2');
expect(config2.Values).to.have.length(1);
expect(config2.Values[0]).to.equal('dummyValue2');

const config3 = cond3.HttpHeaderConfig;
expect(config3.HttpHeaderName).to.equal('dummyName3');
expect(config3.Values).to.have.length(2);
expect(config3.Values[0]).to.equal('dummyMultiValue1');
expect(config3.Values[1]).to.equal('dummyMultiValue2');
});
});
});

0 comments on commit 72b27cb

Please sign in to comment.