-
Notifications
You must be signed in to change notification settings - Fork 9
/
create_profile.py
executable file
·100 lines (85 loc) · 3.91 KB
/
create_profile.py
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
#!/usr/bin/python
"""Convert our CloudFront PEM file to a mobileconfig."""
import os
import plistlib
import argparse
import base64
PROFILE_DISPLAY_NAME = 'Munki CloudFront Settings'
BUNDLE = 'com.github.aaronburchfield.cloudfront'
PROFILE_FILENAME = 'munki_middleware_cloudfront'
def main():
"""Handle cli arguments and core processing logic."""
parser = argparse.ArgumentParser(prog='Munki middleware profile creator',
description='Create a profile to manage'
'the munki middleware script.')
parser.add_argument('-c', '--cert', required=True,
help='File path to the CloudFront pem encoded '
'certificate.')
parser.add_argument('-b', '--base64', default=False,
type=lambda x:
(str(x).lower() in ['true', 'yes', '1']),
help='Encode the certificate as a base64 encoded '
'string instead of the default data type. '
'Accepts: true')
parser.add_argument('-e', '--expire_after', required=False,
help='Time in minutes to expire '
'the requests. (Optional)')
parser.add_argument('-a', '--access_id', required=False,
help='AWS access_id associated with the CloudFront '
'identity. (Optional)')
parser.add_argument('-d', '--domain_name', required=False,
help='Set the alterative domain name if using a '
'domain. (Optional)')
parser.add_argument('--org_name', required=False, default='',
help='Set the profile organization. (Optional)')
parser.add_argument('--desc', required=False, default='',
help='Set the profile description text. (Optional)')
args = parser.parse_args()
template = {
'PayloadUUID': '8217A278-D22A-4591-9620-945E63B6D9B4',
'PayloadDescription': args.desc,
'PayloadVersion': 1,
'PayloadContent': [{
'PayloadUUID': '1FF25978-1717-4FD6-967E-DC0DCBEA20A1',
'PayloadDescription': args.desc,
'PayloadOrganization': args.org_name,
'PayloadIdentifier': '1FF25978-1717-4FD6-967E-DC0DCBEA20A1',
'PayloadDisplayName': PROFILE_DISPLAY_NAME,
'PayloadType': BUNDLE,
'PayloadEnabled': True,
'PayloadVersion': 1,
}],
'PayloadIdentifier': BUNDLE,
'PayloadDisplayName': PROFILE_DISPLAY_NAME,
'PayloadType': 'Configuration',
'PayloadScope': 'System',
'PayloadEnabled': True,
'PayloadOrganization': args.org_name,
'PayloadRemovalDisallowed': True
}
cert_file = os.path.abspath(args.cert)
print("Certificate file is: {}".format(cert_file))
with open(cert_file, 'rb') as f:
content = f.read()
# Encode as base64 string or use data type
if args.base64:
cert_data = base64.b64encode(content)
else:
cert_data = plistlib.Data(content)
# Include the certificate in the profile
template['PayloadContent'][0]['cloudfront_certificate'] = cert_data
# Include the AWS access id if passed through
if args.access_id:
template['PayloadContent'][0]['access_id'] = args.access_id
# Include a custom expire_time if passed through
if args.expire_after:
template['PayloadContent'][0]['expire_after'] = args.expire_after
# Include the alterative domain name if passed through
if args.domain_name:
template['PayloadContent'][0]['domain_name'] = args.domain_name
# Write out the profile to disk
profile_output = '{}.mobileconfig'.format(PROFILE_FILENAME)
plistlib.writePlist(template, profile_output)
print("Profile was written to: {}".format(os.path.abspath(profile_output)))
if __name__ == '__main__':
main()