Skip to content

Commit 72b27cb

Browse files
authored
fix(AWS ALB): Allow multiple http-header conditions (#11888)
1 parent 41e90c3 commit 72b27cb

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

lib/plugins/aws/package/compile/events/alb/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ function defineArray(schema) {
1111
return { type: 'array', items: schema };
1212
}
1313

14+
const ALB_HTTP_HEADER_SCHEMA = {
15+
type: 'object',
16+
properties: {
17+
name: { type: 'string', maxLength: 40 },
18+
values: defineArray({ type: 'string', maxLength: 128 }, { uniqueItems: true }),
19+
},
20+
additionalProperties: false,
21+
required: ['name', 'values'],
22+
};
23+
1424
class AwsCompileAlbEvents {
1525
constructor(serverless, options) {
1626
this.serverless = serverless;
@@ -39,15 +49,7 @@ class AwsCompileAlbEvents {
3949
conditions: {
4050
type: 'object',
4151
properties: {
42-
header: {
43-
type: 'object',
44-
properties: {
45-
name: { type: 'string', maxLength: 40 },
46-
values: { type: 'array', items: { type: 'string', maxLength: 128 } },
47-
},
48-
additionalProperties: false,
49-
required: ['name', 'values'],
50-
},
52+
header: { anyOf: [defineArray(ALB_HTTP_HEADER_SCHEMA), ALB_HTTP_HEADER_SCHEMA] },
5153
host: defineArray({
5254
type: 'string',
5355
pattern: '^[A-Za-z0-9*?.-]+$',

lib/plugins/aws/package/compile/events/alb/lib/listener-rules.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ module.exports = {
9999
});
100100
}
101101
if (event.conditions.header) {
102-
Conditions.push({
103-
Field: 'http-header',
104-
HttpHeaderConfig: {
105-
HttpHeaderName: event.conditions.header.name,
106-
Values: event.conditions.header.values,
107-
},
102+
event.conditions.header.forEach(({ name, values }) => {
103+
Conditions.push({
104+
Field: 'http-header',
105+
HttpHeaderConfig: {
106+
HttpHeaderName: name,
107+
Values: values,
108+
},
109+
});
108110
});
109111
}
110112
if (event.conditions.query) {

lib/plugins/aws/package/compile/events/alb/lib/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
albObj.conditions.method = [].concat(event.alb.conditions.method);
5151
}
5252
if (event.alb.conditions.header) {
53-
albObj.conditions.header = event.alb.conditions.header;
53+
albObj.conditions.header = [].concat(event.alb.conditions.header);
5454
}
5555
if (event.alb.conditions.query) {
5656
albObj.conditions.query = event.alb.conditions.query;

test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,36 @@ describe('test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js', (
9393
},
9494
],
9595
},
96+
fnSingleHeaderCondition: {
97+
handler: 'index.handler',
98+
events: [
99+
{
100+
alb: {
101+
...validBaseEventConfig,
102+
priority: 8,
103+
conditions: { header: { name: 'dummyName', values: ['dummyValue'] } },
104+
},
105+
},
106+
],
107+
},
108+
fnMultiHeaderCondition: {
109+
handler: 'index.handler',
110+
events: [
111+
{
112+
alb: {
113+
...validBaseEventConfig,
114+
priority: 9,
115+
conditions: {
116+
header: [
117+
{ name: 'dummyName1', values: ['dummyValue1'] },
118+
{ name: 'dummyName2', values: ['dummyValue2'] },
119+
{ name: 'dummyName3', values: ['dummyMultiValue1', 'dummyMultiValue2'] },
120+
],
121+
},
122+
},
123+
},
124+
],
125+
},
96126
},
97127
},
98128
});
@@ -276,4 +306,45 @@ describe('test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js', (
276306
.and.have.property('code', 'ALB_TARGET_GROUP_NAME_EXCLUSIVE');
277307
});
278308
});
309+
310+
describe('should set alb header conditions', () => {
311+
it('should convert single header condition to array', () => {
312+
const albListenerRuleLogicalId = naming.getAlbListenerRuleLogicalId(
313+
'fnSingleHeaderCondition',
314+
8
315+
);
316+
const conditions = cfResources[albListenerRuleLogicalId].Properties.Conditions;
317+
expect(conditions).to.have.length(1);
318+
const config = conditions[0].HttpHeaderConfig;
319+
expect(config.HttpHeaderName).to.equal('dummyName');
320+
expect(config.Values).to.have.length(1);
321+
expect(config.Values[0]).to.equal('dummyValue');
322+
});
323+
324+
it('should support multi header conditions', () => {
325+
const albListenerRuleLogicalId = naming.getAlbListenerRuleLogicalId(
326+
'fnMultiHeaderCondition',
327+
9
328+
);
329+
const conditions = cfResources[albListenerRuleLogicalId].Properties.Conditions;
330+
expect(conditions).to.have.length(3);
331+
const [cond1, cond2, cond3] = conditions;
332+
333+
const config1 = cond1.HttpHeaderConfig;
334+
expect(config1.HttpHeaderName).to.equal('dummyName1');
335+
expect(config1.Values).to.have.length(1);
336+
expect(config1.Values[0]).to.equal('dummyValue1');
337+
338+
const config2 = cond2.HttpHeaderConfig;
339+
expect(config2.HttpHeaderName).to.equal('dummyName2');
340+
expect(config2.Values).to.have.length(1);
341+
expect(config2.Values[0]).to.equal('dummyValue2');
342+
343+
const config3 = cond3.HttpHeaderConfig;
344+
expect(config3.HttpHeaderName).to.equal('dummyName3');
345+
expect(config3.Values).to.have.length(2);
346+
expect(config3.Values[0]).to.equal('dummyMultiValue1');
347+
expect(config3.Values[1]).to.equal('dummyMultiValue2');
348+
});
349+
});
279350
});

0 commit comments

Comments
 (0)