-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_resources.yaml
More file actions
236 lines (206 loc) · 6.2 KB
/
base_resources.yaml
File metadata and controls
236 lines (206 loc) · 6.2 KB
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
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Creates the required resources for Jamf Step Functions.
Parameters:
JamfProUrl:
Type: String
Description: https://example.jamfcloud.com (No trailing slash)
JamfApiClientId:
Type: String
JamfApiClientSecret:
Type: String
NoEcho: true
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "Jamf Pro"
Parameters:
- JamfProUrl
- JamfApiClientId
- JamfApiClientSecret
ParameterLabels:
JamfProUrl:
default: "Jamf Pro URL"
JamfApiClientId:
default: "API Client ID"
JamfApiClientSecret:
default: "API Client Secret"
Globals:
Function:
Architectures:
- arm64
Runtime: python3.11
MemorySize: 128
Handler: index.handler
Resources:
JamfApiConnection:
Type: AWS::Events::Connection
Properties:
Description: !Sub "API Client for ${JamfProUrl}"
AuthorizationType: OAUTH_CLIENT_CREDENTIALS
AuthParameters:
OAuthParameters:
AuthorizationEndpoint: !Sub "${JamfProUrl}/api/oauth/token"
HttpMethod: POST
ClientParameters:
ClientID: !Ref JamfApiClientId
ClientSecret: !Ref JamfApiClientSecret
OAuthHttpParameters:
BodyParameters:
- Key: grant_type
Value: client_credentials
IsValueSecret: false
# Event Bus with Logging Resources
WebhooksEventBus:
Type: AWS::Events::EventBus
Properties:
Name: !Ref AWS::StackName
WebhooksEventBusLogging:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 7
LogGroupName: !Sub /${AWS::StackName}/event-bus-logs
WebhooksEventBusLoggingPolicy:
Type: AWS::Logs::ResourcePolicy
Properties:
PolicyName: !Sub EventBridgeToLogsPolicy-${AWS::StackName}-${AWS::Region}
PolicyDocument: !Sub >
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EventBridgeToLogsPolicy-${AWS::StackName}-${AWS::Region}",
"Effect": "Allow",
"Principal": {
"Service": [
"delivery.logs.amazonaws.com",
"events.amazonaws.com"
]
},
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": ["${WebhooksEventBusLogging.Arn}"]
}
]
}
WebhooksEventBusLoggingRule:
Type: AWS::Events::Rule
Properties:
EventBusName: !Ref WebhooksEventBus
EventPattern:
account:
- !Ref AWS::AccountId
Targets:
- Id: CloudwatchLogsTarget
Arn: !GetAtt WebhooksEventBusLogging.Arn
# HTTP API Resources
TokenGenerator:
Type: AWS::Serverless::Function
Properties:
InlineCode: |
from secrets import token_urlsafe
import cfnresponse
def handler(event, context):
return cfnresponse.send(
event,
context,
cfnresponse.SUCCESS,
{"Value": token_urlsafe(32)}
)
WebhooksApiKey:
Type: AWS::CloudFormation::CustomResource
Properties:
ServiceToken: !GetAtt TokenGenerator.Arn
WebhooksApiPath:
Type: AWS::CloudFormation::CustomResource
Properties:
ServiceToken: !GetAtt TokenGenerator.Arn
ApiKeyAuthorizer:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
API_KEY: !GetAtt WebhooksApiKey.Value
InlineCode: |
from secrets import compare_digest
import os
def handler(event, context):
api_key = event["headers"].get("x-api-key")
if compare_digest(api_key, os.environ["API_KEY"]):
return {"isAuthorized": True}
else:
return {"isAuthorized": False}
WebhooksApi:
Type: AWS::Serverless::HttpApi
Properties:
FailOnWarnings: true
StageName: !GetAtt WebhooksApiPath.Value
Auth:
DefaultAuthorizer: ApiKeyAuth
Authorizers:
ApiKeyAuth:
AuthorizerPayloadFormatVersion: 2.0
EnableFunctionDefaultPermissions: true
EnableSimpleResponses: true
FunctionArn: !GetAtt ApiKeyAuthorizer.Arn
Identity:
ReauthorizeEvery: 300
Headers:
- x-api-key
DefinitionBody:
openapi: "3.0.1"
info:
title: "Jamf Pro Webhooks API"
version: "1.0"
paths:
/:
post:
responses:
default:
description: EventBridge response
x-amazon-apigateway-integration:
connectionType: INTERNET
type: aws_proxy
integrationSubtype: EventBridge-PutEvents
payloadFormatVersion: "1.0"
credentials: !GetAtt WebhooksApiRole.Arn
requestParameters:
EventBusName: !Ref WebhooksEventBus
Detail: $request.body
DetailType: Jamf Pro Webhook
Source: Webhooks API
WebhooksApiRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: ApiDirectWriteEventBridge
PolicyDocument:
Version: 2012-10-17
Statement:
Action:
- events:PutEvents
Effect: Allow
Resource:
- !GetAtt WebhooksEventBus.Arn
Outputs:
JamfProUrl:
Value: !Ref JamfProUrl
JamfApiConnectionArn:
Value: !GetAtt JamfApiConnection.Arn
WebhooksEventBusName:
Value: !Ref WebhooksEventBus
WebhooksApiUrl:
Value: !Sub https://${WebhooksApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${WebhooksApiPath.Value}/
WebhooksApiKey:
Value: !GetAtt WebhooksApiKey.Value