-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
validate.js
160 lines (140 loc) · 4.97 KB
/
validate.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
'use strict';
const _ = require('lodash');
module.exports = {
validate() {
const events = [];
_.forEach(this.serverless.service.functions, (functionObject, functionName) => {
_.forEach(functionObject.events, (event) => {
if (_.has(event, 'http')) {
const http = this.getHttp(event, functionName);
http.path = this.getHttpPath(http, functionName);
http.method = this.getHttpMethod(http, functionName);
if (http.authorizer) {
http.authorizer = this.getAuthorizer(http, functionName);
}
events.push({
functionName,
http,
});
}
});
});
return {
events,
};
},
getHttp(event, functionName) {
if (typeof event.http === 'object') {
return event.http;
} else if (typeof event.http === 'string') {
return {
method: event.http.split(' ')[0],
path: event.http.split(' ')[1],
};
}
const errorMessage = [
`Invalid http event in function "${functionName}"`,
' in serverless.yml.',
' If you define an http event, make sure you pass a valid value for it,',
' either as string syntax, or object syntax.',
' Please check the docs for more options.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
},
getHttpPath(http, functionName) {
if (typeof http.path === 'string') {
return http.path.replace(/^\//, '').replace(/\/$/, '');
}
const errorMessage = [
`Missing or invalid "path" property in function "${functionName}"`,
' for http event in serverless.yml.',
' If you define an http event, make sure you pass a valid value for it,',
' either as string syntax, or object syntax.',
' Please check the docs for more options.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
},
getHttpMethod(http, functionName) {
if (typeof http.method === 'string') {
const method = http.method.toLowerCase();
const allowedMethods = [
'get', 'post', 'put', 'patch', 'options', 'head', 'delete', 'any',
];
if (allowedMethods.indexOf(method) === -1) {
const errorMessage = [
`Invalid APIG method "${http.method}" in function "${functionName}".`,
` AWS supported methods are: ${allowedMethods.join(', ')}.`,
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
return method;
}
const errorMessage = [
`Missing or invalid "method" property in function "${functionName}"`,
' for http event in serverless.yml.',
' If you define an http event, make sure you pass a valid value for it,',
' either as string syntax, or object syntax.',
' Please check the docs for more options.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
},
getAuthorizer(http, functionName) {
const authorizer = http.authorizer;
let name;
let arn;
let identitySource;
let resultTtlInSeconds;
let identityValidationExpression;
if (typeof authorizer === 'string') {
if (authorizer.indexOf(':') === -1) {
name = authorizer;
arn = this.getLambdaArn(authorizer);
} else {
arn = authorizer;
name = this.getLambdaName(arn);
}
} else if (typeof authorizer === 'object') {
if (authorizer.arn) {
arn = authorizer.arn;
name = this.getLambdaName(arn);
} else if (authorizer.name) {
name = authorizer.name;
arn = this.getLambdaArn(name);
} else {
throw new this.serverless.classes.Error('Please provide either an authorizer name or ARN');
}
resultTtlInSeconds = Number.parseInt(authorizer.resultTtlInSeconds, 10);
resultTtlInSeconds = Number.isNaN(resultTtlInSeconds) ? 300 : resultTtlInSeconds;
identitySource = authorizer.identitySource;
identityValidationExpression = authorizer.identityValidationExpression;
} else {
const errorMessage = [
`authorizer property in function ${functionName} is not an object nor a string.`,
' The correct format is: authorizer: functionName',
' OR an object containing a name property.',
' Please check the docs for more info.',
].join('');
throw new this.serverless.classes.Error(errorMessage);
}
if (typeof identitySource === 'undefined') {
identitySource = 'method.request.header.Authorization';
}
return {
name,
arn,
resultTtlInSeconds,
identitySource,
identityValidationExpression,
};
},
getLambdaArn(name) {
this.serverless.service.getFunction(name);
const normalizedName = name[0].toUpperCase() + name.substr(1);
return { 'Fn::GetAtt': [`${normalizedName}LambdaFunction`, 'Arn'] };
},
getLambdaName(arn) {
const splitArn = arn.split(':');
const splitLambdaName = splitArn[splitArn.length - 1].split('-');
return splitLambdaName[splitLambdaName.length - 1];
},
};