-
Notifications
You must be signed in to change notification settings - Fork 0
/
jamf_saastenancy_cloud_formation_template.yaml
488 lines (451 loc) · 23.5 KB
/
jamf_saastenancy_cloud_formation_template.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# Copyright 2024, Jamf
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the “Software”),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NON-INFRINGEMENT. IN NO EVENT SHALL JAMF SOFTWARE, LLC OR ANY OF ITS
# AFFILIATES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER DEALINGS IN THE
# SOFTWARE, INCLUDING BUT NOT LIMITED TO DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES AND OTHER DAMAGES SUCH AS
# LOSS OF USE, PROFITS, SAVINGS, TIME OR DATA, BUSINESS INTERRUPTION,
# OR PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES.
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple EC2 instance with Nginx and Elastic IP.
Mappings:
RegionAMIs:
us-east-1:
AMI: ami-0eb01a520e67f7f20
us-east-2:
AMI: ami-07a5db12eede6ff87
us-west-1:
AMI: ami-05f45e0f5aeac9a24
us-west-2:
AMI: ami-00a0b62a1660255c0
ap-southeast-2:
AMI: ami-01b5f7a30f320f409
ap-northeast-1:
AMI: ami-09ff6f432d0ee628e
eu-central-1:
AMI: ami-00068b9d3a9643636
eu-west-2:
AMI: ami-05e77069ed898709c
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: Must be the name of an existing EC2 KeyPair.
InstanceType:
Description: EC2 instance type
Type: String
Default: t4g.micro
AllowedValues:
- t4g.micro
- t4g.small
- t4g.medium
CertificateBody:
Description: The body of the SSL/TLS certificate base64 encoded (leave empty for self signed)
Type: String
Default: ""
CertificatePrivateKey:
Description: The private key of the SSL/TLS certificate base64 encoded (leave empty for self signed)
Type: String
Default: ""
Domain:
Description: internal domain to add to the header if multiple seperated by space
Type: String
Default: "example.com example.dev"
SaaSApplication:
Description: "Choose which application to allow for the domain"
Type: String
Default: Google
AllowedValues:
- Google
- Microsoft
- Slack
- Dropbox
VPCId:
Description: VPC Id where the instance will be launched
Type: AWS::EC2::VPC::Id
SubnetId:
Description: Subnet Id where the instance will be launched
Type: AWS::EC2::Subnet::Id
Resources:
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow access to port 443
VpcId: !Ref VPCId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
EC2Instance:
Type: AWS::EC2::Instance
Properties:
Tags:
- Key: Name
Value: !Ref AWS::StackName
InstanceType: !Ref InstanceType
SecurityGroupIds:
- !Ref InstanceSecurityGroup
KeyName: !Ref KeyName
ImageId: !FindInMap [RegionAMIs, !Ref "AWS::Region", AMI]
SubnetId: !Ref SubnetId
UserData:
Fn::Base64: !Sub
- |
#!/bin/bash
yum update -y
yum install -y nginx openssl
mkdir -p /var/www/html/
header_type='${SaaSApplication}'
domain='${Domain}'
cat <<EOFINTER > /etc/nginx/intermediateCA.ext
basicConstraints = critical,CA:TRUE,pathlen:0
keyUsage = critical,keyCertSign,cRLSign
EOFINTER
CERTIFICATE_BODY='${CertificateBody}'
CERTIFICATE_PRIVATE_KEY='${CertificatePrivateKey}'
# Checking for SSL Certificate and if not present creating Self Signed Root CA
cert_regex="^-----BEGIN CERTIFICATE-----"
private_key_regex="^-----BEGIN PRIVATE KEY-----"
if [[ ! $CERTIFICATE_BODY =~ $cert_regex ]]; then
CERTIFICATE_BODY=""
fi
if [[ ! $CERTIFICATE_PRIVATE_KEY =~ $private_key_regex ]]; then
CERTIFICATE_PRIVATE_KEY=""
fi
if [[ -z "$CERTIFICATE_BODY" || -z "$CERTIFICATE_PRIVATE_KEY" ]]; then
# Generate a self-signed certificate root CA
openssl genrsa -out /etc/nginx/rootCA.key 4096
openssl genrsa -out /etc/nginx/intermediateCA.key 4096
openssl req -x509 -new -nodes -key /etc/nginx/rootCA.key -sha256 -days 365 -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=jscproxy-root" -out /etc/nginx/rootCA.pem
openssl req -new -key /etc/nginx/intermediateCA.key -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=jscproxy-intermediate" -out /etc/nginx/intermediateCA.csr
openssl x509 -req -in /etc/nginx/intermediateCA.csr -CA /etc/nginx/rootCA.pem -CAkey /etc/nginx/rootCA.key -CAcreateserial -out /etc/nginx/intermediateCA.pem -days 365 -sha256 -extfile /etc/nginx/intermediateCA.ext
else
# Use provided certificate
echo "$CERTIFICATE_BODY" > /etc/nginx/server.crt
echo "$CERTIFICATE_PRIVATE_KEY" > /etc/nginx/server.key
fi
if [[ $header_type == "Google" ]]; then
cat <<GOOGLE > /etc/nginx/server.ext
basicConstraints = CA:FALSE
nsCertType = server
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = DNS:accounts.google.com, DNS:*.google.com
GOOGLE
fi
if [[ $header_type == "Microsoft" ]]; then
cat <<MICROSOFT > /etc/nginx/server.ext
basicConstraints = CA:FALSE
nsCertType = server
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = DNS:stamp2.login.microsoftonline.com, DNS:login.microsoftonline-int.com, DNS:login.microsoftonline-p.com, DNS:login.microsoftonline.com, DNS:login2.microsoftonline-int.com, DNS:login2.microsoftonline.com, DNS:loginex.microsoftonline-int.com, DNS:loginex.microsoftonline.com, DNS:stamp2.login.microsoftonline-int.com
MICROSOFT
fi
if [[ $header_type == "Slack" ]]; then
cat <<SLACK > /etc/nginx/server.ext
basicConstraints = CA:FALSE
nsCertType = server
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = DNS:slack.com, DNS:*.slack.com
SLACK
fi
if [[ $header_type == "Dropbox" ]]; then
cat <<DROPBOX > /etc/nginx/server.ext
basicConstraints = CA:FALSE
nsCertType = server
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = DNS:dropbox.com, DNS:*.dropbox.com
DROPBOX
fi
# Checking if domain is multiple
# Check if the string contains a space and if so convert to array
if echo "$domain" | grep -q " "; then
echo "Multiple Domains splitting into Array" > /tmp/log.txt
# If it does, split the string into an array using IFS and read
IFS=' ' read -r -a array <<< "$domain"
# Loop through the array and append each element to the new variable
for element in "${!array[@]}"
do
if [[ $header_type == "Google" ]]; then
# Creating CSR for Google
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/google.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=*.google.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/google.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header+="
proxy_set_header 'X-GooGApps-Allowed-Domains' $element;"
nginx_proxy_pass="
proxy_pass https://accounts.google.com;"
elif [[ $header_type == "Microsoft" ]]; then
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/microsoft.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=stamp2.login.microsoftonline.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/microsoft.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header+="
proxy_set_header 'Restrict-Access-To-Tenants' $element;
proxy_set_header 'Restrict-Access-Context' $element;"
nginx_proxy_pass="
proxy_pass https://login.microsoftonline.com;"
elif [[ $header_type == "Slack" ]]; then
# Creating CSR for Slack
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/slack.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=slack.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/slack.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header+="
proxy_set_header 'X-Slack-Allowed-Workspaces-Requester' $element;
X-Slack-Allowed-Workspaces' $element;"
nginx_proxy_pass="
proxy_pass https://slack.com/signin;"
elif [[ $header_type == "Dropbox" ]]; then
# Creating CSR for Dropbox
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/dropbox.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=*.dropbox.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/dropbox.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header+="
proxy_set_header 'X-Dropbox-allowed-Team-Ids' $element;"
nginx_proxy_pass="
proxy_pass https://www.dropbox.com/login;"
fi
done
else
if [[ $header_type == "Google" ]]; then
# Creating CSR for Google
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/google.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=*.google.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/google.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header="
proxy_set_header 'X-GooGApps-Allowed-Domains' $domain;"
nginx_proxy_pass="
proxy_pass https://accounts.google.com;"
elif [[ $header_type == "Microsoft" ]]; then
# Creating CSR for Microsoft
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/microsoft.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=stamp2.login.microsoftonline.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/microsoft.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header="
proxy_set_header 'Restrict-Access-To-Tenants' $domain;
proxy_set_header 'Restrict-Access-Context' $domain;"
nginx_proxy_pass="
proxy_pass https://login.microsoftonline.com;"
elif [[ $header_type == "Slack" ]]; then
# Creating CSR for Slack
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/slack.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=slack.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/slack.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header="
proxy_set_header 'X-Slack-Allowed-Workspaces-Requester' $domain;
X-Slack-Allowed-Workspaces' $domain;"
nginx_proxy_pass="
proxy_pass https://slack.com/signin;"
elif [[ $header_type == "Dropbox" ]]; then
# Creating CSR for Dropbox
openssl req -newkey rsa:2048 -keyout /etc/nginx/server.key -nodes -out /etc/nginx/dropbox.csr -subj "/C=US/ST=MN/L=Minneapolis/O=Jamf/OU=Security/CN=*.dropbox.com"
# Signing the CSR with internal CA
openssl x509 -req -in /etc/nginx/dropbox.csr -CA /etc/nginx/intermediateCA.pem -CAkey /etc/nginx/intermediateCA.key -CAcreateserial -out /etc/nginx/server.crt -days 365 -sha256 -extfile /etc/nginx/server.ext
nginx_header="
proxy_set_header 'X-Dropbox-allowed-Team-Ids' $domain;"
nginx_proxy_pass="
proxy_pass https://accounts.google.com;"
fi
fi
# Create MobileConfig to upload
root_cert_body=$(openssl base64 -in /etc/nginx/rootCA.pem | tr -d '\n')
intermediate_cert_body=$(openssl base64 -in /etc/nginx/intermediateCA.pem | tr -d '\n')
leaf_cert_body=$(openssl base64 -in /etc/nginx/server.crt | tr -d '\n')
cat <<PROFILE > /var/www/html/JSCP_Proxy_Cert.mobileconfig
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1">
<dict>
<key>PayloadUUID</key>
<string>954D9214-C6A3-467B-90F6-9E705665CAE8</string>
<key>PayloadType</key>
<string>Configuration</string>
<key>PayloadOrganization</key>
<string>Jamf</string>
<key>PayloadIdentifier</key>
<string>954D9214-C6A3-467B-90F6-9E705665CAE8</string>
<key>PayloadDisplayName</key>
<string>JSCP Proxy Cert</string>
<key>PayloadDescription</key>
<string/>
<key>PayloadVersion</key>
<integer>1</integer>
<key>PayloadEnabled</key>
<true/>
<key>PayloadRemovalDisallowed</key>
<true/>
<key>PayloadScope</key>
<string>System</string>
<key>PayloadContent</key>
<array>
<dict>
<key>PayloadUUID</key>
<string>F866D848-8C52-42A9-92CF-3E639ABBAFCD</string>
<key>PayloadType</key>
<string>com.apple.security.root</string>
<key>PayloadOrganization</key>
<string>Jamf</string>
<key>PayloadIdentifier</key>
<string>F866D848-8C52-42A9-92CF-3E639ABBAFCD</string>
<key>PayloadDisplayName</key>
<string>root</string>
<key>PayloadDescription</key>
<string/>
<key>PayloadVersion</key>
<integer>1</integer>
<key>PayloadEnabled</key>
<true/>
<key>PayloadCertificateFileName</key>
<string>root.cer</string>
<key>PayloadContent</key>
<data>$root_cert_body</data>
<key>AllowAllAppsAccess</key>
<true/>
<key>KeyIsExtractable</key>
<true/>
</dict>
<dict>
<key>PayloadUUID</key>
<string>F4CAC5C7-3B70-42AB-B29D-79A531D251AE</string>
<key>PayloadType</key>
<string>com.apple.security.pkcs1</string>
<key>PayloadOrganization</key>
<string>Jamf</string>
<key>PayloadIdentifier</key>
<string>F4CAC5C7-3B70-42AB-B29D-79A531D251AE</string>
<key>PayloadDisplayName</key>
<string>intermediate</string>
<key>PayloadDescription</key>
<string/>
<key>PayloadVersion</key>
<integer>1</integer>
<key>PayloadEnabled</key>
<true/>
<key>PayloadCertificateFileName</key>
<string>intermediate.cer</string>
<key>PayloadContent</key>
<data>$intermediate_cert_body</data>
<key>AllowAllAppsAccess</key>
<false/>
<key>KeyIsExtractable</key>
<true/>
</dict>
<dict>
<key>PayloadUUID</key>
<string>605B9E0B-E31A-4132-8148-8FF396D9E483</string>
<key>PayloadType</key>
<string>com.apple.security.pkcs1</string>
<key>PayloadOrganization</key>
<string>Jamf</string>
<key>PayloadIdentifier</key>
<string>605B9E0B-E31A-4132-8148-8FF396D9E483</string>
<key>PayloadDisplayName</key>
<string>leaf</string>
<key>PayloadDescription</key>
<string/>
<key>PayloadVersion</key>
<integer>1</integer>
<key>PayloadEnabled</key>
<true/>
<key>PayloadCertificateFileName</key>
<string>leaf.cer</string>
<key>PayloadContent</key>
<data>$leaf_cert_body</data>
<key>AllowAllAppsAccess</key>
<false/>
<key>KeyIsExtractable</key>
<true/>
</dict>
</array>
</dict>
</plist>
PROFILE
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
echo $nginx_header > /tmp/nginx_header
echo $nginx_proxy_pass > /tmp/nginx_proxy_pass
cat <<EOF > /etc/nginx/conf.d/jscproxy.conf
server {
listen 80;
# Define the specific path for the .mobileconfig file download
location = /download {
root /var/www/html; # Update this to the directory containing your .mobileconfig file
default_type application/x-apple-aspen-config; # MIME type for .mobileconfig files.
add_header Content-Disposition "attachment; filename=JSCP_Proxy_Cert.mobileconfig";
try_files /JSCP_Proxy_Cert.mobileconfig =404;
}
# Redirect all other traffic from HTTP to HTTPS
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
location / {
# Disable checking of body size
client_max_body_size 0;
# Set Proxy version to 1.1
proxy_http_version 1.1;
# Disable client request buffering (useful for large requests)
proxy_request_buffering off;
# Disable response buffering (useful for large requests)
proxy_buffering off;
# Sets the number and size of the buffers used for reading a response from the proxied server. In this case, 16 buffers of 32 kilobytes each
proxy_buffers 16 32k;
# Sets the size of the buffer used for the first part of the response received from the proxied server
proxy_buffer_size 64k;
# Sets the maximum size of the buffers that can be busy sending a response to the client while the response is not fully read
proxy_busy_buffers_size 64k;
# Sets the DNS resolver to the IP address
resolver 1.1.1.1 ipv6=off;
# Sets the Host header of the request to the host of the incoming client request
proxy_set_header Host \$host;
# Passes the original client IP address to the proxied server
proxy_set_header X-Real-IP \$remote_addr;
# Adding custom headers for domain
$nginx_header
$nginx_proxy_pass
}
}
EOF
systemctl start nginx
systemctl enable nginx
- { SaaSApplication: !Ref SaaSApplication, Domain: !Ref Domain }
ElasticIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
InstanceId: !Ref EC2Instance
Outputs:
InstanceId:
Description: The Instance ID
Value: !Ref EC2Instance
PublicIP:
Description: The Public IP address of the instance please add this as your JSC custom gateway address
Value: !GetAtt ElasticIP.PublicIp
ProfileDownload:
Description: Please click and download the mobile profile and then upload to Jamf Pro
Value:
!Sub "http://${ElasticIP.PublicIp}/download"