Skip to content

Commit b63fc8e

Browse files
authored
Merge pull request #86 from grokability/80-Jamf-Token-Auth
80 jamf token auth
2 parents d0d434f + f644623 commit b63fc8e

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

jamf2snipe

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,14 @@ logging.info("Great, we found a settings file. Let's get started by parsing all
107107
# This is the address, cname, or FQDN for your JamfPro instance.
108108
jamfpro_base = config['jamf']['url']
109109
logging.info("The configured JAMFPro base url is: {}".format(jamfpro_base))
110-
jamf_api_user = config['jamf']['username']
111-
logging.info("The configured JAMFPro username we'll be connecting with is: {}".format(jamf_api_user))
112-
jamf_api_password = config['jamf']['password']
113-
logging.debug("The configured password to access the API is: {}".format(jamf_api_password))
110+
jamf_apiKey = config['jamf']['apikey']
111+
logging.debug("The API key you provided for Jamf is: {}".format(jamf_apiKey))
114112

115113
# This is the address, cname, or FQDN for your snipe-it instance.
116114
snipe_base = config['snipe-it']['url']
117115
logging.info("The configured Snipe-IT base url is: {}".format(snipe_base))
118-
apiKey = config['snipe-it']['apiKey']
119-
logging.debug("The API key you provided for Snipe is: {}".format(apiKey))
116+
snipe_apiKey = config['snipe-it']['apikey']
117+
logging.debug("The API key you provided for Snipe is: {}".format(snipe_apiKey))
120118
defaultStatus = config['snipe-it']['defaultStatus']
121119
logging.info("The default status we'll be setting updated computer to is: {} (I sure hope this is a number or something is probably wrong)".format(defaultStatus))
122120
apple_manufacturer_id = config['snipe-it']['manufacturer_id']
@@ -125,8 +123,8 @@ logging.info("The configured JAMFPro base url is: {} (Pretty sure this needs to
125123
# Headers for the API call.
126124

127125
logging.info("Creating the headers we'll need for API calls")
128-
jamfheaders = {'Accept': 'application/json'}
129-
snipeheaders = {'Authorization': 'Bearer {}'.format(apiKey),'Accept': 'application/json','Content-Type':'application/json'}
126+
jamfheaders = {'Authorization': 'Bearer {}'.format(jamf_apiKey),'Accept': 'application/json','Content-Type':'application/json'}
127+
snipeheaders = {'Authorization': 'Bearer {}'.format(snipe_apiKey),'Accept': 'application/json','Content-Type':'application/json'}
130128
logging.debug('Request headers for JamfPro will be: {}\nRequest headers for Snipe will be: {}'.format(jamfheaders, snipeheaders))
131129

132130
# Check the config file for correct headers
@@ -189,7 +187,7 @@ def request_handler(r, *args, **kwargs):
189187
def get_jamf_computers():
190188
api_url = '{0}/JSSResource/computers'.format(jamfpro_base)
191189
logging.debug('Calling for JAMF computers against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
192-
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
190+
response = requests.get(api_url, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
193191
if response.status_code == 200:
194192
logging.debug("Got back a valid 200 response code.")
195193
return response.json()
@@ -209,7 +207,7 @@ def get_jamf_computers():
209207
def get_jamf_computers_by_group(jamf_id):
210208
api_url = '{0}/JSSResource/computergroups/id/{1}'.format(jamfpro_base, jamf_id)
211209
logging.debug('Calling for JAMF computers against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
212-
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
210+
response = requests.get(api_url, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
213211
if response.status_code == 200:
214212
logging.debug("Got back a valid 200 response code.")
215213
jsonresponse = response.json()
@@ -231,7 +229,7 @@ def get_jamf_computers_by_group(jamf_id):
231229
def get_jamf_mobiles():
232230
api_url = '{0}/JSSResource/mobiledevices'.format(jamfpro_base)
233231
logging.debug('Calling for JAMF mobiles against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
234-
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
232+
response = requests.get(api_url, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
235233
if response.status_code == 200:
236234
logging.debug("Got back a valid 200 response code.")
237235
return response.json()
@@ -251,7 +249,7 @@ def get_jamf_mobiles():
251249
def get_jamf_mobiles_by_group(jamf_id):
252250
api_url = '{0}/JSSResource/mobiledevicegroups/id/{1}'.format(jamfpro_base, jamf_id)
253251
logging.debug('Calling for JAMF mobiles against: {}\n The username, passwords, and headers for this GET requestcan be found near the beginning of the output.'.format(api_url))
254-
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
252+
response = requests.get(api_url, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
255253
if response.status_code == 200:
256254
logging.debug("Got back a valid 200 response code.")
257255
jsonresponse = response.json()
@@ -272,7 +270,7 @@ def get_jamf_mobiles_by_group(jamf_id):
272270
# Function to lookup a JAMF asset by id.
273271
def search_jamf_asset(jamf_id):
274272
api_url = "{}/JSSResource/computers/id/{}".format(jamfpro_base, jamf_id)
275-
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
273+
response = requests.get(api_url, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
276274
if response.status_code == 200:
277275
logging.debug("Got back a valid 200 response code.")
278276
jsonresponse = response.json()
@@ -293,7 +291,7 @@ def search_jamf_asset(jamf_id):
293291
# Function to lookup a JAMF mobile asset by id.
294292
def search_jamf_mobile(jamf_id):
295293
api_url = "{}/JSSResource/mobiledevices/id/{}".format(jamfpro_base, jamf_id)
296-
response = requests.get(api_url, auth=(jamf_api_user, jamf_api_password), headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
294+
response = requests.get(api_url, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
297295
if response.status_code == 200:
298296
logging.debug("Got back a valid 200 response code.")
299297
jsonresponse = response.json()
@@ -316,7 +314,7 @@ def update_jamf_asset_tag(jamf_id, asset_tag):
316314
api_url = "{}/JSSResource/computers/id/{}".format(jamfpro_base, jamf_id)
317315
payload = """<?xml version="1.0" encoding="UTF-8"?><computer><general><id>{}</id><asset_tag>{}</asset_tag></general></computer>""".format(jamf_id, asset_tag)
318316
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
319-
response = requests.put(api_url, auth=(jamf_api_user, jamf_api_password), data=payload, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
317+
response = requests.put(api_url, data=payload, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
320318
if response.status_code == 201:
321319
logging.debug("Got a 201 response. Returning: True")
322320
return True
@@ -339,7 +337,7 @@ def update_jamf_mobiledevice_asset_tag(jamf_id, asset_tag):
339337
api_url = "{}/JSSResource/mobiledevices/id/{}".format(jamfpro_base, jamf_id)
340338
payload = """<?xml version="1.0" encoding="UTF-8"?><mobile_device><general><id>{}</id><asset_tag>{}</asset_tag></general></mobile_device>""".format(jamf_id, asset_tag)
341339
logging.debug('Making Get request against: {}\nPayload for the PUT request is: {}\nThe username, password, and headers can be found near the beginning of the output.'.format(api_url, payload))
342-
response = requests.put(api_url, auth=(jamf_api_user, jamf_api_password), data=payload, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
340+
response = requests.put(api_url, data=payload, headers=jamfheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
343341
if response.status_code == 201:
344342
logging.debug("Got a 201 response. Returning: True")
345343
return True
@@ -492,7 +490,7 @@ def update_snipe_asset(snipe_id, payload):
492490
if response.status_code == 200:
493491
logging.debug("Got back status code: 200 - Checking the payload updated properly: If you error here it's because you configure the API mapping right.")
494492
jsonresponse = response.json()
495-
# Check if there's an Error and Log it, or parse the payload.
493+
# Check if there's an Error and Log it, or parse the payload.
496494
if jsonresponse['status'] == "error":
497495
logging.error('Unable to update ID: {}. Error "{}"'.format(snipe_id, jsonresponse['messages']))
498496
goodupdate = False
@@ -749,7 +747,7 @@ for jamf_type in jamf_types:
749747
newasset[snipekey] = jamf_value
750748
except KeyError:
751749
continue
752-
# Reset the payload without the asset_tag if auto_incrementing flag is set.
750+
# Reset the payload without the asset_tag if auto_incrementing flag is set.
753751
if user_args.auto_incrementing:
754752
newasset.pop('asset_tag', None)
755753
new_snipe_asset = create_snipe_asset(newasset)
@@ -828,7 +826,7 @@ for jamf_type in jamf_types:
828826

829827
if updates:
830828
update_snipe_asset(snipe_id, updates)
831-
829+
832830
if ((user_args.users or user_args.users_inverse) and (snipe['rows'][0]['assigned_to'] == None) == user_args.users) or user_args.users_force:
833831

834832
if snipe['rows'][0]['status_label']['status_meta'] in ('deployable', 'deployed'):
@@ -845,7 +843,7 @@ for jamf_type in jamf_types:
845843
logging.debug("Not updating the Snipe asset because Snipe has a more recent timestamp: {} < {}".format(jamf_time, snipe_time))
846844

847845
# Update/Sync the Snipe Asset Tag Number back to JAMF
848-
# The user arg below is set to false if it's called, so this would fail if the user called it.
846+
# The user arg below is set to false if it's called, so this would fail if the user called it.
849847
if (jamf['general']['asset_tag'] != snipe['rows'][0]['asset_tag']) and user_args.do_not_update_jamf :
850848
logging.info("JAMF doesn't have the same asset tag as SNIPE so we'll update it because it should be authoritative.")
851849
if snipe['rows'][0]['asset_tag'][0]:

settings.conf.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[jamf]
22
url = https://yourinstance.jamfcloud.com
3-
username = a-valid-username
4-
password = a-valid-password
3+
apikey = YOUR-API-KEY-HERE
54

65
[snipe-it]
76
#Required

0 commit comments

Comments
 (0)