macadmins / jamf-pro-sdk-python

A client library for the Jamf Pro APIs and webhooks.
https://macadmins.github.io/jamf-pro-sdk-python/
MIT License
50 stars 10 forks source link

[Bug] Create/Update Advanced Computer Search Methods Sending Empty Criteria #55

Open mhrono opened 1 week ago

mhrono commented 1 week ago

tl;dr when attempting to create or update an advanced computer search (using the create_advanced_computer_search or update_advanced_computer_search_by_id methods, respectively), the operation is completed successfully, except for the search criteria. Name and display fields are set as configured, but any criteria sent are seemingly ignored, and the search is created/updated with no criteria. This effectively places every device into the results for the advanced computer search.

Reproduction details below will be for creating a search, but the result is the same when attempting to update an existing search.

Steps to Reproduce

from jamf_pro_sdk import JamfProClient, SessionConfig
from jamf_pro_sdk.models.classic import computer_groups, advanced_computer_searches
from jamf_pro_sdk.models.classic.criteria import ClassicCriterion
from jamf_pro_sdk.clients.auth import ApiClientCredentialsProvider

jamfClient = JamfProClient(
    server=jamfURL,
    credentials=ApiClientCredentialsProvider(jamfClientID, jamfClientSecret),
    session_config=SessionConfig(**{"timeout": 30, "max_retries": 3}),
)

notUpdatedSearchData = advanced_computer_searches.ClassicAdvancedComputerSearch(
    name="macOS Version Not Current",
    criteria=[
        ClassicCriterion(
            name='Managed',
            priority=0,
            and_or='and',
            search_type='is',
            value='Managed',
            opening_paren=False,
            closing_paren=False
        ),
        ClassicCriterion(
            name='Operating System Version',
            priority=1,
            and_or='and',
            search_type='less than',
            value='15.1',
            opening_paren=False,
            closing_paren=False
        )
    ],
    display_fields=[
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="JSS Computer ID"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Last Check-in"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Serial Number"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Operating System Version"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Username"
        ),
    ],
)

searchID = jamfClient.classic_api.create_advanced_computer_search(data=notUpdatedSearchData)
searchData = jamfClient.classic_api.get_advanced_computer_search_by_id(advanced_search=searchID)
print(searchData)

Expected Result

Expected an advanced computer search to be created with the following criteria:

image

Actual Result

Advanced computer search created with no criteria:

image
id=6 name='macOS Version Not Current' site=ClassicSite(id=-1, name='None') criteria=[] display_fields=[ClassicAdvancedComputerSearchDisplayField(name='Username'), ClassicAdvancedComputerSearchDisplayField(name='Operating System Version'), ClassicAdvancedComputerSearchDisplayField(name='JSS Computer ID'), ClassicAdvancedComputerSearchDisplayField(name='Serial Number'), ClassicAdvancedComputerSearchDisplayField(name='Last Check-in')] 

(omitted computers from above output)

Additional Data

If I retrieve the criteria from an existing advanced computer search (with correct criteria) and feed that directly into the create method, I would definitely expect this criteria to be valid and accepted. Even doing this, the new search is created with no criteria:

knownGoodSearchCriteria = jamfClient.classic_api.get_advanced_computer_search_by_id(advanced_search=5).criteria
print(knownGoodSearchCriteria)

####
[ClassicCriterion(name='Managed', priority=0, and_or='and', search_type='is', value='Managed', opening_paren=False, closing_paren=False), ClassicCriterion(name='Operating System Version', priority=1, and_or='and', search_type='less than', value='15.1', opening_paren=False, closing_paren=False)]
####

notUpdatedSearchData = advanced_computer_searches.ClassicAdvancedComputerSearch(
    name="macOS Version Not Current",
    criteria=knownGoodSearchCriteria,
    display_fields=[
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="JSS Computer ID"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Last Check-in"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Serial Number"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Operating System Version"
        ),
        advanced_computer_searches.ClassicAdvancedComputerSearchDisplayField(
            name="Username"
        ),
    ],
)

searchID = jamfClient.classic_api.create_advanced_computer_search(data=notUpdatedSearchData)
searchData = jamfClient.classic_api.get_advanced_computer_search_by_id(advanced_search=searchID)

print(searchData)

####
id=7 name='macOS Version Not Current' site=ClassicSite(id=-1, name='None') criteria=[] display_fields=[ClassicAdvancedComputerSearchDisplayField(name='Username'), ClassicAdvancedComputerSearchDisplayField(name='JSS Computer ID'), ClassicAdvancedComputerSearchDisplayField(name='Last Check-in'), ClassicAdvancedComputerSearchDisplayField(name='Serial Number'), ClassicAdvancedComputerSearchDisplayField(name='Operating System Version')]
####

(again omitted computers)

System Information

macOS 15.1 Python 3.12.1 SDK 0.6a2 Jamf Pro 11.10.2

mhrono commented 1 week ago

After discussing with @brysontyrrell in Slack, I attempted to create a search by dumping the model to XML and using the classic_api_request method:

jamfClient.classic_api_request(method="POST", resource_path="advancedcomputersearches/id/0", data=notUpdatedSearchData.xml())

Unfortunately, this search was also created with no criteria. Examining the output of notUpdatedSearchData.xml() seems to point to the issue:

<?xml version="1.0" encoding="UTF-8" ?><advanced_computer_search><name>testxml</name><criteria><None><name>Managed</name><priority>0</priority><and_or>and</and_or><search_type>is</search_type><value>Managed</value><opening_paren>false</opening_paren><closing_paren>false</closing_paren></None><None><name>Operating System Version</name><priority>1</priority><and_or>and</and_or><search_type>less than</search_type><value>15.1</value><opening_paren>false</opening_paren><closing_paren>false</closing_paren></None></criteria><display_fields><display_field><name>JSS Computer ID</name></display_field><display_field><name>Last Check-in</name></display_field><display_field><name>Serial Number</name></display_field><display_field><name>Operating System Version</name></display_field><display_field><name>Username</name></display_field></display_fields></advanced_computer_search>

It looks like each criterion is being surrounded by a <None> tag.