-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
171 lines (149 loc) · 4.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Official writine serverless plugins
* Ref: https://serverless.com/blog/writing-serverless-plugins/
*
* Sample list of hooks available
* Ref: https://gist.github.com/HyperBrain/50d38027a8f57778d5b0f135d80ea406
*/
const API_KEY_HEADER = 'x-api-key';
// Capitalise first character
const ucFirst = string => {
return string.charAt(0).toUpperCase() + string.slice(1);
};
// Construct httplistener name based on function i.e AlbpingAlbListenerRule2
const listenerName = (functionName, alb) => {
return `${ucFirst(functionName)}AlbListenerRule${alb.priority}`;
};
const ruleName = (functionName, priority) => {
return `${ucFirst(functionName)}InvalidApiKeyRule${priority}`;
};
/**
* Scan through all function that has event as alb and has apiKey property
* @param {} serverless
*/
const getAllEventWithAlb = serverless => {
const functions = [];
Object.keys(serverless.service.functions).forEach(key => {
const func = serverless.service.functions[key];
func.events.forEach(element => {
const { alb } = element;
if (alb && alb.apiKey) {
functions.push({
...alb,
funcName: key,
listenerName: listenerName(key, alb),
apiKey: alb.apiKey,
});
}
});
});
return functions;
};
/**
* Add Http-header condition to existing Listener
* @param {listener, apiKey} functions Functions which requires apiKey to be added
* @param {*} template CloudFormation Template
*/
const addNewListenerForFunctionAlb = (func, cloudFormation) => {
const listener = cloudFormation.Resources[func.listenerName];
listener.Properties.Conditions.push({
Field: 'http-header',
HttpHeaderConfig: {
HttpHeaderName: API_KEY_HEADER,
Values: func.apiKey,
},
});
};
const addInvalidApiKeyRule = (func, cloudFormation) => {
const { funcName, priority, listenerArn, conditions, actions } = func;
const rulePriority = 3000 + priority;
const newRuleName = ruleName(funcName, rulePriority);
const newRule = {
Type: 'AWS::ElasticLoadBalancingV2::ListenerRule',
Properties: {
ListenerArn: listenerArn,
Priority: 3000 + rulePriority,
},
};
const newConditions = [];
// create condition based on serverless functions
if (conditions.path) {
newConditions.push({
Field: 'path-pattern',
PathPatternConfig: {
Values:
conditions.path instanceof Array
? conditions.path
: [conditions.path],
},
});
}
if (conditions.method) {
newConditions.push({
Field: 'http-request-method',
HttpRequestMethodConfig: {
Values:
conditions.method instanceof Array
? conditions.method
: [conditions.method],
},
});
}
newRule.Properties = { ...newRule.Properties, Conditions: newConditions };
// if no actions is defined then we create a default action
if (!actions) {
const defaultAction = {
Type: 'fixed-response',
FixedResponseConfig: {
StatusCode: 403,
ContentType: 'application/json',
MessageBody: '{ "message": "Forbidden: invalid api key" }',
},
};
newRule.Properties = { ...newRule.Properties, Actions: [defaultAction] };
} else {
newRule.Properties = { ...newRule.Properties, Actions: actions };
}
// eslint-disable-next-line no-param-reassign
cloudFormation.Resources = {
...cloudFormation.Resources,
[newRuleName]: newRule,
};
};
/**
* Modify cloud formation to allow more listener rule that requires headers
* with specified apiKey
*
* @param {*} serverless
*/
const beforeDeploy = serverless => {
const functions = getAllEventWithAlb(serverless);
const cloudFormation =
serverless.service.provider.compiledCloudFormationTemplate;
functions.forEach(func => {
addNewListenerForFunctionAlb(func, cloudFormation);
addInvalidApiKeyRule(func, cloudFormation);
});
// process.exit(1);
};
const provider = serverless => {
return serverless ? serverless.getProvider('aws') : null;
};
/* eslint-disable class-methods-use-this */
class EventAlbApiKeyPlugin {
constructor(serverless, options) {
this.commands = {
usage: 'Add listener rule to HTTP Listener for x-api-key header',
deploy: {
lifecycleEvents: ['resources', 'functions'],
},
};
this.hooks = {
'before:deploy:deploy': beforeDeploy.bind(null, serverless, options),
};
if (!provider(serverless)) {
throw new Error('This plugin must be used with AWS');
}
}
}
module.exports = EventAlbApiKeyPlugin;