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

Update query.sh to use FTL's API instead of directly interacting with the database #5361

Merged
merged 6 commits into from
Oct 16, 2023

Conversation

yubiuser
Copy link
Member

@yubiuser yubiuser commented Aug 13, 2023

What does this PR aim to accomplish?:

This is a complete rewrite of query.sh

So far, we have been using direct access to the gravity database to implement the functions provided by pihole -q (query the adlist/denylist for a given domain). With FTL v6, FTL provides an API endpoint /search/DOMAIN which will do all the low-level work for us and outputs a json containing all the relevant data (and some more).
This PR changes the implementation of pihole -q to use the provided API. To be able to do that, I added a helper file (api.sh) which provides the necessary functions to authenticate with the API and request data from it and finally to delete the session if everything is done. It's the same function that I use to access FTL's API int the v6 branch of PADD. As we need to rewrite some more function to use FTL's API they can be reused in other pihole cli calls. Both, the query.sh as well as api.sh are POSIX style. The output colors are similar to that the v6 web interface is using. As we source COL_TABLE this was made POSIX compatible as well.

Screenshot at 2023-08-13 22-07-25

Most of the code in query.sh is now the generation of the output (from the json returned by FTL). While working on it, I also changed the way we handle provided option (--exact, --all). They can be in an arbitrary position and handled gracefully. This
PR supersedes #5349


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code and I have tested my changes.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)
  6. I have checked that another pull request for this purpose does not exist.
  7. I have considered, and confirmed that this submission will be valuable to others.
  8. I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  9. I give this submission freely, and claim no ownership to its content.

  • I have read the above and my PR is ready for review. Check this box to confirm

Signed-off-by: Christian König <ckoenig@posteo.de>
Signed-off-by: Christian König <ckoenig@posteo.de>
@yubiuser yubiuser requested a review from a team August 13, 2023 20:24
Signed-off-by: Christian König <ckoenig@posteo.de>
@yubiuser
Copy link
Member Author

P.S. the function from api.sh are not in utils.sh as they are not self-contained - which is one requirement of utils.sh

@PromoFaux
Copy link
Member

I like this as an overall idea - the only "don't like" from me at the moment is that it requires entering the api password to run pihole -q, whereas this was never needed before.

Is this an issue? I don't know - perhaps one that we should discuss before moving ahead however.

@DL6ER
Copy link
Member

DL6ER commented Sep 26, 2023

It can be circumvented by excluding local API users from authentication (FTL has a dedicated option for this since the very early days). I agree this may require further discussion. We might want to add an extra option (defaulting to true?) to exclude only some local requests from auth - permitting only a few harmless things without password such as reading the lists

@pralor-bot
Copy link

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/whitelist-blacklist-options-result-in-grep-errors/65521/2

@DL6ER
Copy link
Member

DL6ER commented Oct 12, 2023

@PromoFaux @yubiuser Any comments on my last idea (opt-out: exclude local API users from authentication)?

@yubiuser
Copy link
Member Author

I like the idea of having a additional option - like "excluding light" for local users.

@DL6ER
Copy link
Member

DL6ER commented Oct 13, 2023

Dedicated option incoming via pi-hole/FTL#1662

@yubiuser
Copy link
Member Author

With the last commit, no authentication is required when one of webserver.api.loaclAPIauth or webserver.api.searchAPIauth is false.

@DL6ER
Copy link
Member

DL6ER commented Oct 14, 2023

Thanks, this code seems to work as expected (I also verified the searchAPIauth = true fallback to entering a password).

However, given that ABP searching does not go along with partial matching, I wonder if exact matching would actually be the better default? On the web interface Enter also executes exact searching.

pihole -q 0.000dn.com

Screenshot from 2023-10-14 18-48-09

pihole -q 0.000dn.com --exact

Screenshot from 2023-10-14 18-48-17

@yubiuser
Copy link
Member Author

I can change the default behavior. I just kept it as it was in v5 -> partial was the default.

Signed-off-by: Christian König <ckoenig@posteo.de>
Copy link
Member

@DL6ER DL6ER left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving how it is. Do you still want to improve the antigravity support (maybe similar to how I did it recently for web)?

@yubiuser
Copy link
Member Author

Yes I want to improve anti-gravity support and also print a warning if 'N' is to small (FTL PR needs to be merged). I was busy reviewing your PRs 😉

@yubiuser
Copy link
Member Author

I decided to merge already now - tweaking can happen later and others could use what we have in api.sh already now.

@yubiuser yubiuser merged commit aaa9acc into development-v6 Oct 16, 2023
15 checks passed
@yubiuser yubiuser deleted the query_list_v6 branch October 16, 2023 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants