Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add md5 option #179

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 54 additions & 62 deletions JamfUploaderProcessors/JamfUploaderLib/JamfAccountUploaderBase.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/local/autopkg/python

# pylint: disable=invalid-name
"""
Copyright 2023 Graham Pugh

Expand Down Expand Up @@ -77,7 +77,7 @@ def prepare_account_template(self, account_name, account_template):
"""prepare the account contents"""
# import template from file and replace any keys in the template
if os.path.exists(account_template):
with open(account_template, "r") as file:
with open(account_template, "r", encoding="utf-8") as file:
template_contents = file.read()
else:
raise ProcessorError("Template does not exist!")
Expand All @@ -102,21 +102,20 @@ def upload_account(
object_type,
template_xml,
token,
sleep_time,
obj_id=0,
):
"""Upload account"""

self.output(f"Uploading {object_type}...")

# if we find an object ID we put, if not, we post
url = "{}/JSSResource/accounts/{}id/{}".format(jamf_url, object_type, obj_id)
url = f"{jamf_url}/JSSResource/accounts/{object_type}id/{obj_id}"

count = 0
while True:
count += 1
self.output(
"{} upload attempt {}".format(object_type, count), verbose_level=2
)
self.output(f"{object_type} upload attempt {count}", verbose_level=2)
request = "PUT" if obj_id else "POST"
r = self.curl(
request=request,
Expand All @@ -131,93 +130,85 @@ def upload_account(
self.output(
f"WARNING: {object_type} upload did not succeed after 5 attempts"
)
self.output("\nHTTP POST Response Code: {}".format(r.status_code))
self.output(f"\nHTTP POST Response Code: {r.status_code}")
raise ProcessorError(f"ERROR: {object_type} upload failed ")
if int(self.sleep) > 30:
sleep(int(self.sleep))
if int(sleep_time) > 30:
sleep(int(sleep_time))
else:
sleep(30)
return r

def execute(self):
"""Upload the account"""
self.jamf_url = self.env.get("JSS_URL")
self.jamf_user = self.env.get("API_USERNAME")
self.jamf_password = self.env.get("API_PASSWORD")
self.client_id = self.env.get("CLIENT_ID")
self.client_secret = self.env.get("CLIENT_SECRET")
self.account_name = self.env.get("account_name")
self.account_type = self.env.get("account_type")
self.domain = self.env.get("domain")
self.account_template = self.env.get("account_template")
self.replace = self.env.get("replace_account")
self.sleep = self.env.get("sleep")
jamf_url = self.env.get("JSS_URL")
jamf_user = self.env.get("API_USERNAME")
jamf_password = self.env.get("API_PASSWORD")
client_id = self.env.get("CLIENT_ID")
client_secret = self.env.get("CLIENT_SECRET")
account_name = self.env.get("account_name")
account_type = self.env.get("account_type")
domain = self.env.get("domain")
account_template = self.env.get("account_template")
replace_account = self.env.get("replace_account")
sleep_time = self.env.get("sleep")
# handle setting replace in overrides
if not self.replace or self.replace == "False":
self.replace = False
self.account_updated = False
if not replace_account or replace_account == "False":
replace_account = False
account_updated = False

# clear any pre-existing summary result
if "jamfaccountuploader_summary_result" in self.env:
del self.env["jamfaccountuploader_summary_result"]

# handle files with a relative path
if not self.account_template.startswith("/"):
found_template = self.get_path_to_file(self.account_template)
if not account_template.startswith("/"):
found_template = self.get_path_to_file(account_template)
if found_template:
self.account_template = found_template
account_template = found_template
else:
raise ProcessorError(
f"ERROR: Policy file {self.account_template} not found"
)
raise ProcessorError(f"ERROR: Policy file {account_template} not found")

# now start the process of uploading the object
self.output(f"Checking for existing '{self.account_name}' on {self.jamf_url}")
self.output(f"Checking for existing '{account_name}' on {jamf_url}")

# get token using oauth or basic auth depending on the credentials given
if self.jamf_url and self.client_id and self.client_secret:
token = self.handle_oauth(self.jamf_url, self.client_id, self.client_secret)
elif self.jamf_url and self.jamf_user and self.jamf_password:
token = self.handle_api_auth(
self.jamf_url, self.jamf_user, self.jamf_password
)
if jamf_url and client_id and client_secret:
token = self.handle_oauth(jamf_url, client_id, client_secret)
elif jamf_url and jamf_user and jamf_password:
token = self.handle_api_auth(jamf_url, jamf_user, jamf_password)
else:
raise ProcessorError("ERROR: Credentials not supplied")

# check for existing account - requires obj_name and account type
obj_id = self.get_account_id_from_name(
self.jamf_url,
self.account_name,
self.account_type,
jamf_url,
account_name,
account_type,
token=token,
)

# check for existing domain - requires obj_name and account type
if self.domain:
if domain:
domain_id = self.get_api_obj_id_from_name(
self.jamf_url,
self.domain,
jamf_url,
domain,
"ldap_server",
token=token,
)
self.env["domain"] = self.domain
self.env["domain"] = domain
self.env["domain_id"] = domain_id

# we need to substitute the values in the account name and template now to
# account for version strings in the name
self.account_name, template_xml = self.prepare_account_template(
self.account_name, self.account_template
account_name, template_xml = self.prepare_account_template(
account_name, account_template
)

if obj_id:
self.output(
"account '{}' already exists: ID {}".format(self.account_name, obj_id)
)
if self.replace:
self.output(f"account '{account_name}' already exists: ID {obj_id}")
if replace_account:
self.output(
"Replacing existing account as 'replace_account' is set to {}".format(
self.replace
),
f"Replacing existing account as 'replace_account' is set to {replace_account}",
verbose_level=1,
)
else:
Expand All @@ -229,25 +220,26 @@ def execute(self):

# upload the account
self.upload_account(
self.jamf_url,
self.account_name,
self.account_type,
jamf_url,
account_name,
account_type,
template_xml,
token,
sleep_time,
obj_id=obj_id,
)
self.account_updated = True
account_updated = True

# output the summary
self.env["account_name"] = self.account_name
self.env["account_type"] = self.account_type
self.env["account_updated"] = self.account_updated
if self.account_updated:
self.env["account_name"] = account_name
self.env["account_type"] = account_type
self.env["account_updated"] = account_updated
if account_updated:
self.env["jamfaccountuploader_summary_result"] = {
"summary_text": "The following accounts were updated in Jamf Pro:",
"report_fields": ["account", "template"],
"data": {
"account": self.account_name,
"template": self.account_template,
"account": account_name,
"template": account_template,
},
}
73 changes: 38 additions & 35 deletions JamfUploaderProcessors/JamfUploaderLib/JamfCategoryUploaderBase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/local/autopkg/python
# pylint: disable=invalid-name

"""
Copyright 2023 Graham Pugh
Expand Down Expand Up @@ -38,7 +39,9 @@
class JamfCategoryUploaderBase(JamfUploaderBase):
"""Class for functions used to upload a category to Jamf"""

def upload_category(self, jamf_url, category_name, priority, token, obj_id=0):
def upload_category(
self, jamf_url, category_name, priority, token, sleep_time, obj_id=0
):
"""Update category metadata."""

# build the object
Expand All @@ -49,9 +52,9 @@ def upload_category(self, jamf_url, category_name, priority, token, obj_id=0):
# if we find an object ID we put, if not, we post
object_type = "category"
if obj_id:
url = "{}/{}/{}".format(jamf_url, self.api_endpoints(object_type), obj_id)
url = f"{jamf_url}/{self.api_endpoints(object_type)}/{obj_id}"
else:
url = "{}/{}".format(jamf_url, self.api_endpoints(object_type))
url = f"{jamf_url}/{self.api_endpoints(object_type)}"

# write the category.
count = 0
Expand All @@ -72,57 +75,56 @@ def upload_category(self, jamf_url, category_name, priority, token, obj_id=0):
self.output("ERROR: Category creation did not succeed after 5 attempts")
self.output(f"\nHTTP POST Response Code: {r.status_code}")
raise ProcessorError("ERROR: Category upload failed ")
if int(self.sleep) > 30:
sleep(int(self.sleep))
if int(sleep_time) > 30:
sleep(int(sleep_time))
else:
sleep(30)

def execute(self):
"""Upload a category"""
self.jamf_url = self.env.get("JSS_URL")
self.jamf_user = self.env.get("API_USERNAME")
self.jamf_password = self.env.get("API_PASSWORD")
self.client_id = self.env.get("CLIENT_ID")
self.client_secret = self.env.get("CLIENT_SECRET")
self.category_name = self.env.get("category_name")
self.category_priority = self.env.get("category_priority")
self.replace = self.env.get("replace_category")
self.sleep = self.env.get("sleep")
jamf_url = self.env.get("JSS_URL")
jamf_user = self.env.get("API_USERNAME")
jamf_password = self.env.get("API_PASSWORD")
client_id = self.env.get("CLIENT_ID")
client_secret = self.env.get("CLIENT_SECRET")
category_name = self.env.get("category_name")
category_priority = self.env.get("category_priority")
replace_category = self.env.get("replace_category")
sleep_time = self.env.get("sleep")
# handle setting replace_pkg in overrides
if not self.replace or self.replace == "False":
self.replace = False
if not replace_category or replace_category == "False":
replace_category = False

# clear any pre-existing summary result
if "jamfcategoryuploader_summary_result" in self.env:
del self.env["jamfcategoryuploader_summary_result"]

# get token using oauth or basic auth depending on the credentials given
if self.jamf_url and self.client_id and self.client_secret:
token = self.handle_oauth(self.jamf_url, self.client_id, self.client_secret)
elif self.jamf_url and self.jamf_user and self.jamf_password:
token = self.handle_api_auth(
self.jamf_url, self.jamf_user, self.jamf_password
)
if jamf_url and client_id and client_secret:
token = self.handle_oauth(jamf_url, client_id, client_secret)
elif jamf_url and jamf_user and jamf_password:
token = self.handle_api_auth(jamf_url, jamf_user, jamf_password)
else:
raise ProcessorError("ERROR: Credentials not supplied")

# now process the category
# check for existing category
self.output(f"Checking for existing '{self.category_name}' on {self.jamf_url}")
self.output(f"Checking for existing '{category_name}' on {jamf_url}")
obj_type = "category"
obj_name = self.category_name
obj_name = category_name
obj_id = self.get_uapi_obj_id_from_name(
self.jamf_url,
jamf_url,
obj_type,
obj_name,
token,
)

if obj_id:
self.output(f"Category '{self.category_name}' already exists: ID {obj_id}")
if self.replace:
self.output(f"Category '{category_name}' already exists: ID {obj_id}")
if replace_category:
self.output(
f"Replacing existing category as 'replace_category' is set to {self.replace}",
"Replacing existing category as 'replace_category' is set to "
+ replace_category,
verbose_level=1,
)
else:
Expand All @@ -132,24 +134,25 @@ def execute(self):
)
return
else:
self.output(f"Category '{self.category_name}' not found: ID {obj_id}")
self.output(f"Category '{category_name}' not found: ID {obj_id}")

# upload the category
self.upload_category(
self.jamf_url,
self.category_name,
self.category_priority,
jamf_url,
category_name,
category_priority,
token,
sleep_time,
obj_id,
)

# output the summary
self.env["category"] = self.category_name
self.env["category"] = category_name
self.env["jamfcategoryuploader_summary_result"] = {
"summary_text": "The following categories were created or updated in Jamf Pro:",
"report_fields": ["category", "priority"],
"data": {
"category": self.category_name,
"priority": str(self.category_priority),
"category": category_name,
"priority": str(category_priority),
},
}
Loading