salopensource / sal

Modular reporting for Endpoints
Apache License 2.0
212 stars 64 forks source link

Cant search on any item other than Machine in Advanced Search #398

Closed johnmikee closed 4 years ago

johnmikee commented 4 years ago

Describe the bug When trying to compose an advanced search the only Search item that populates information in Search Field is Machine. If switched to Facter the Search Field is empty. This information is visible in the admin site and device detail pages. This appears to be the same issue as noted in this Slack thread https://macadmins.slack.com/archives/C061B9XGS/p1585857553015600

This only appears to happen in the sal-saml container. If I spin up a fresh vanilla Sal instance the Advanced Search works as expected.

I have tested this in Debug mode and can't seem to find an error. I don't believe anything is erroring out or failing per se, but the value is being returned as null. That's my best guess at least.

Steps to reproduce the behavior:

  1. Go to Advanced Search
  2. Click the drop-down on Search item and switch to any option other than Machine
  3. Click the Search field to modify the field and nothing appears

Screenshot Screen Shot 2020-07-09 at 11 44 53 AM

Server

Client (please complete the following information):

NoahRJ commented 4 years ago

I'll second that I'm seeing this in my environment as well. Same Sal-Saml version (4.1.4.2149), host env is Docker, and behavior is consistent across all browsers.

grahamgilbert commented 4 years ago

Enable debug mode and look at the web inspector in your browser for errors please.

johnmikee commented 4 years ago

Looking at the Advanced Search page with Machine selected I get the following

<span class="select2-container select2-container--default select2-container--open" style="position: absolute; left: 297.222px; top: 220.76px;"><span class="select2-dropdown select2-dropdown--below" dir="ltr" style="width: 2501px;"><span class="select2-search select2-search--dropdown"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox"></span><span class="select2-results"><ul class="select2-results__options" role="tree" id="select2-id_search_field-results" aria-expanded="true" aria-hidden="false"><li class="select2-results__option" id="select2-id_search_field-result-m7pn-broken_client" role="treeitem" aria-selected="true">broken_client</li><li class="select2-results__option" id="select2-id_search_field-result-uw4v-console_user" role="treeitem" aria-selected="false">console_user</li><li class="select2-results__option" id="select2-id_search_field-result-w6eq-cpu_speed" role="treeitem" aria-selected="false">cpu_speed</li><li class="select2-results__option" id="select2-id_search_field-result-67ir-cpu_type" role="treeitem" aria-selected="false">cpu_type</li><li class="select2-results__option" id="select2-id_search_field-result-jqd2-deployed" role="treeitem" aria-selected="false">deployed</li><li class="select2-results__option select2-results__option--highlighted" id="select2-id_search_field-result-3j3c-first_checkin" role="treeitem" aria-selected="false">first_checkin</li><li class="select2-results__option" id="select2-id_search_field-result-471y-hd_percent" role="treeitem" aria-selected="false">hd_percent</li><li class="select2-results__option" id="select2-id_search_field-result-4mak-hd_space" role="treeitem" aria-selected="false">hd_space</li><li class="select2-results__option" id="select2-id_search_field-result-1pb3-hd_total" role="treeitem" aria-selected="false">hd_total</li><li class="select2-results__option" id="select2-id_search_field-result-ofks-hostname" role="treeitem" aria-selected="false">hostname</li><li class="select2-results__option" id="select2-id_search_field-result-dj0v-last_checkin" role="treeitem" aria-selected="false">last_checkin</li><li class="select2-results__option" id="select2-id_search_field-result-xgez-machine_group" role="treeitem" aria-selected="false">machine_group</li><li class="select2-results__option" id="select2-id_search_field-result-xpvj-machine_model" role="treeitem" aria-selected="false">machine_model</li><li class="select2-results__option" id="select2-id_search_field-result-p1jz-machine_model_friendly" role="treeitem" aria-selected="false">machine_model_friendly</li><li class="select2-results__option" id="select2-id_search_field-result-khd8-manifest" role="treeitem" aria-selected="false">manifest</li><li class="select2-results__option" id="select2-id_search_field-result-s6qy-memory" role="treeitem" aria-selected="false">memory</li><li class="select2-results__option" id="select2-id_search_field-result-ztd6-memory_kb" role="treeitem" aria-selected="false">memory_kb</li><li class="select2-results__option" id="select2-id_search_field-result-dxgt-munki_version" role="treeitem" aria-selected="false">munki_version</li><li class="select2-results__option" id="select2-id_search_field-result-27m3-operating_system" role="treeitem" aria-selected="false">operating_system</li><li class="select2-results__option" id="select2-id_search_field-result-g41j-os_family" role="treeitem" aria-selected="false">os_family</li><li class="select2-results__option" id="select2-id_search_field-result-2nsf-sal_version" role="treeitem" aria-selected="false">sal_version</li><li class="select2-results__option" id="select2-id_search_field-result-ljd2-serial" role="treeitem" aria-selected="false">serial</li></ul></span></span></span>

showing all of the Search fields as expected. When switching to any other Search item it changes to the following with no errors other than No results found.

<span class="select2-container select2-container--default select2-container--open" style="position: absolute; left: 297.222px; top: 220.76px;"><span class="select2-dropdown select2-dropdown--below" dir="ltr" style="width: 2484px;"><span class="select2-search select2-search--dropdown"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox"></span><span class="select2-results"><ul class="select2-results__options" role="tree" id="select2-id_search_field-results" aria-expanded="true" aria-hidden="false"><li role="treeitem" aria-live="assertive" class="select2-results__option select2-results__message">No results found</li></ul></span></span></span>

What's interesting is that if you switch back to Machine as the Search item after choosing one of the other non-working Search fields the Search field options for Machine do not repopulate. I'm trying to configure one of the Django Chrome debuggers to see if I can pull anything more useful than the limited information the inspector showed.

grahamgilbert commented 4 years ago

I would expect http errors from the Ajax response here. Turning on debug in the settings and looking at the console would surface these.

johnmikee commented 4 years ago

Screen Shot 2020-07-23 at 11 16 08 AM For whatever reason these werent showing up in Chrome but in Firefox the error became more apparent.

A 301 error is occurring for the search field when switching the search item.

grahamgilbert commented 4 years ago

That sounds like a problem with your webserver, Sal wouldn't send a 301 (I'm right here, aren't I @sheagcraig ?). What happens if you hit that URL?

johnmikee commented 4 years ago

I think the 301 might have been a red herring. Looking in the nginx logs it almost immediately gets a 200. This is the same for Machine which works.

"GET /search/get_fields/Facter HTTP/1.1" 301 0 "https://sal4.megapcorp.com/search/new_search_row/24/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15" "-"
"GET /search/get_fields/Facter/ HTTP/1.1" 200 14 "https://sal.megacorp.com/search/new_search_row/24/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15" "-

At the same that is occuring the sal debug logs look like everything should be working

2020-07-23 17:11:29,998 - django.db.backends - DEBUG - (0.001) SELECT "search_searchfieldcache"."id", "search_searchfieldcache"."search_model", "search_searchfieldcache"."search_field" FROM "search_searchfieldcache" WHERE "search_searchfieldcache"."search_model" = 'Facter'; args=('Facter',)
[23/Jul/2020 17:11:29] INFO [django.server:157] "GET /search/get_fields/Facter/ HTTP/1.0" 200 14

If I query the url https://sal.megacorp.com/search/get_fields/Facter/ or https://sal.megacorp.com/search/get_fields/Machine/ I get {"fields": []} . Should there be data returned for the fields with debug turned on?

grahamgilbert commented 4 years ago

Okay, there should be data there. Is this running with docker? Looks like the maintenance task isn't populating the cache.

johnmikee commented 4 years ago

Yeah it is. Is the maintenance task manage.py search_maintenance ? Looking at the sal docker logs Im seeing

2020-07-23 18:50:18,233 INFO success: searchmaint entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2020-07-23 18:55:17,887 INFO exited: servermaint (exit status 0; expected)
2020-07-23 18:55:17,941 INFO spawned: 'servermaint' with pid 44
2020-07-23 18:55:18,221 INFO success: servermaint entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2020-07-23 18:55:20,385 INFO exited: searchmaint (exit status 1; not expected)
2020-07-23 18:55:20,523 INFO spawned: 'searchmaint' with pid 45
2020-07-23 18:55:21,228 INFO success: searchmaint entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)

which may correlate to that. Would docker exec'ing the container and running that maintenance be of potential value?

grahamgilbert commented 4 years ago

Yep, that will be helpful. Something isn't working right

2020-07-23 18:55:20,385 INFO exited: searchmaint (exit status 1; not expected)

johnmikee commented 4 years ago

This looks like an issue I ran into a few years back

/home/docker/sal# python3 manage.py search_maintenance
/usr/local/lib/python3.7/site-packages/django/db/models/fields/__init__.py:1368: RuntimeWarning: DateTimeField SavedSearch.created received a naive datetime (2020-06-23 20:22:06.214539) while time zone support is active.
  RuntimeWarning)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(254)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/docker/sal/search/management/commands/search_maintenance.py", line 105, in handle
    SearchFieldCache.objects.bulk_create(search_fields)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 493, in bulk_create
    objs_without_pk, fields, batch_size, ignore_conflicts=ignore_conflicts,
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1223, in _batched_insert
    ignore_conflicts=ignore_conflicts,
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1204, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1391, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(254)

Previously I had changed the max length from 254 to 300 and that solved the problem but that seems like more of a band-aid on the matter. I need to figure out what is coming in at such an extreme length.

I don't know if this is the same issue @NoahRJ is experiencing but once I address the length I will update this thread.

grahamgilbert commented 4 years ago

The job should probably truncate long entries and log an error to allow it to continue

grahamgilbert commented 4 years ago

Or just skip them completely and log an error, since we would be making assumptions about which ones should be saved. Or another option is to just make it a text field, but it would slow down database operations.