-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.yaml
252 lines (244 loc) · 9.23 KB
/
api.yaml
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
AWSTemplateFormatVersion: '2010-09-09'
Description: Backend to interact with ServiceNow
Transform: 'AWS::Serverless-2016-10-31'
Parameters:
SNOWInstance:
Type: String
MinLength: 1
MaxLength: 127
Description: Name of the ServiceNow instance
SNOWID:
Type: String
MinLength: 1
MaxLength: 127
Description: ServiceNow ClientID to call the API
SNOWSecret:
Type: String
MinLength: 1
MaxLength: 127
Description: ServiceNow Client Secret to call the API
SNOWUser:
Type: String
MinLength: 1
MaxLength: 127
Description: login of a valid SNOW API user
SNOWPassword:
Type: String
MinLength: 1
MaxLength: 127
Description: password of a valid SNOW API user
APIUser:
Type: String
MinLength: 1
MaxLength: 127
Description: login to authenticate in API Gateway, must be in a SNOW Basic Auth Configuration element
APIPassword:
Type: String
MinLength: 1
MaxLength: 127
Description: password to authenticate in API Gateway, must be in a SNOW Basic Auth Configuration element
LambdaBackendBucket:
Type: String
MinLength: 1
MaxLength: 127
Description: Name of the S3 bucket where is the located backend lambda
LambdaBackendKey:
Type: String
Default: /path/to/api.zip
MinLength: 1
MaxLength: 127
Description: Full path of the backend lambda function in the S3 bucket
Resources:
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: cloudwatchLogging
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
BackendLambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler: api.lambda_handler
Runtime: python3.6
CodeUri:
Bucket: !Ref LambdaBackendBucket
Key: !Ref LambdaBackendKey
Description: 'Get request from SNOW, process and update'
MemorySize: 128
Timeout: 3
Role: !GetAtt LambdaExecutionRole.Arn
Environment:
Variables:
Instance: !Ref SNOWInstance
clientid: !Ref SNOWID
psswd: !Ref SNOWPassword
secret: !Ref SNOWSecret
user: !Ref SNOWUser
AuthorizerLambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler: authorizer.handler
Runtime: nodejs6.10
CodeUri:
Bucket: !Ref LambdaBackendBucket
Key: authorizer.zip
Description: 'API Gateway Basic http authorizer'
MemorySize: 128
Timeout: 3
Role: !GetAtt LambdaExecutionRole.Arn
Environment:
Variables:
LOGIN: !Ref APIUser
PASWD: !Ref APIPassword
ConfigLambdaPermissionAPI:
Type: "AWS::Lambda::Permission"
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref BackendLambda
Principal: apigateway.amazonaws.com
SourceArn: !Join
- ""
- - "arn:aws:execute-api:"
- !Ref "AWS::Region"
- ":"
- !Ref "AWS::AccountId"
- ":"
- !Ref ServiceNowAPI
- "/*/POST/"
ConfigLambdaPermissionAuth:
Type: "AWS::Lambda::Permission"
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref AuthorizerLambda
Principal: apigateway.amazonaws.com
SourceArn: !Join
- ""
- - "arn:aws:execute-api:"
- !Ref "AWS::Region"
- ":"
- !Ref "AWS::AccountId"
- ":"
- !Ref ServiceNowAPI
- "/authorizers/*"
ServiceNowAPI:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: "2.0"
info:
version: "1.0"
title: "ServiceNowAPI"
basePath: "/prod"
schemes:
- "https"
paths:
/:
post:
consumes:
- "application/json"
produces:
- "application/json"
responses:
'200':
description: "200 response"
schema:
$ref: "#/definitions/Empty"
security:
- snow_lamb: []
x-amazon-apigateway-integration:
uri: !Join
- ""
- - "arn:aws:apigateway:"
- !Ref "AWS::Region"
- ":lambda:path/2015-03-31/functions/"
- !GetAtt BackendLambda.Arn
- "/invocations"
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_templates"
httpMethod: "POST"
requestTemplates:
application/json: "## See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html\n\
## This template will pass through all parameters including path, querystring,\
\ header, stage variables, and context through to the integration endpoint\
\ via the body/payload\n#set($allParams = $input.params())\n{\n\"body-json\"\
\ : $input.json('$'),\n\"params\" : {\n#foreach($type in $allParams.keySet())\n\
\ #set($params = $allParams.get($type))\n\"$type\" : {\n #foreach($paramName\
\ in $params.keySet())\n \"$paramName\" : \"$util.escapeJavaScript($params.get($paramName))\"\
\n #if($foreach.hasNext),#end\n #end\n}\n #if($foreach.hasNext),#end\n\
#end\n},\n\"stage-variables\" : {\n#foreach($key in $stageVariables.keySet())\n\
\"$key\" : \"$util.escapeJavaScript($stageVariables.get($key))\"\n \
\ #if($foreach.hasNext),#end\n#end\n},\n\"context\" : {\n \"account-id\"\
\ : \"$context.identity.accountId\",\n \"api-id\" : \"$context.apiId\"\
,\n \"api-key\" : \"$context.identity.apiKey\",\n \"authorizer-principal-id\"\
\ : \"$context.authorizer.principalId\",\n \"caller\" : \"$context.identity.caller\"\
,\n \"cognito-authentication-provider\" : \"$context.identity.cognitoAuthenticationProvider\"\
,\n \"cognito-authentication-type\" : \"$context.identity.cognitoAuthenticationType\"\
,\n \"cognito-identity-id\" : \"$context.identity.cognitoIdentityId\"\
,\n \"cognito-identity-pool-id\" : \"$context.identity.cognitoIdentityPoolId\"\
,\n \"http-method\" : \"$context.httpMethod\",\n \"stage\" : \"\
$context.stage\",\n \"source-ip\" : \"$context.identity.sourceIp\"\
,\n \"user\" : \"$context.identity.user\",\n \"user-agent\" : \"\
$context.identity.userAgent\",\n \"user-arn\" : \"$context.identity.userArn\"\
,\n \"request-id\" : \"$context.requestId\",\n \"resource-id\" :\
\ \"$context.resourceId\",\n \"resource-path\" : \"$context.resourcePath\"\
\n }\n}\n"
contentHandling: "CONVERT_TO_TEXT"
type: "aws"
securityDefinitions:
snow_lamb:
type: "apiKey"
name: "Authorization"
in: "header"
x-amazon-apigateway-authtype: "custom"
x-amazon-apigateway-authorizer:
authorizerUri: !Join
- ""
- - "arn:aws:apigateway:"
- !Ref "AWS::Region"
- ":lambda:path/2015-03-31/functions/"
- !GetAtt AuthorizerLambda.Arn
- "/invocations"
authorizerResultTtlInSeconds: 0
identitySource: "method.request.header.Authorization"
type: "request"
definitions:
Empty:
type: "object"
title: "Empty Schema"
x-amazon-apigateway-gateway-responses:
UNAUTHORIZED:
statusCode: 401
responseParameters:
gatewayresponse.header.WWW-Authenticate: "'Basic'"
responseTemplates:
application/json: "{\"message\":$context.error.messageString}"
Outputs:
API:
Description: API for SNOW interraction
Value:
Ref: ServiceNowAPI
BackendFunction:
Description: Function receiving and parsing Output REST message from SNOW
Value:
Ref: BackendLambda