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 galaxy to user agent #17578

Closed
wants to merge 9 commits into from

Conversation

svonworl
Copy link
Contributor

@svonworl svonworl commented Mar 1, 2024

Hi, my name is Steve, and I'm an engineer on the Dockstore team. This PR attempts to append the string "galaxy" to the User-Agent header of all Galaxy HTTP requests. For example, it modifies the "requests" package's User-Agent so it looks like:

python-requests/2.31.0 galaxy

Galaxy uses both the "requests" and the "urllib" packages, so it also changes urllib's User-Agent similarly.

This PR would allow us to differentiate Galaxy requests from other Python-based Dockstore traffic . After this change, we'd be able to better track Galaxy usage and handle its requests more appropriately. For example, it'd be more feasible for us to filter a DOS attack and not affect Galaxy traffic, or to tweak our webservice with a Galaxy-specific accommodation.

In addition to adding the word "galaxy", we could include the Galaxy version, or the host name so that different installations could be more easily differentiated. Your call!

The User-Agent is modified via the new user_agent package, which patches the appropriate methods in both the "requests" and "urllib" packages when it is imported. For it to work 100% correctly, it must be imported before any HTTP requests are made. I figured it was easiest to import at all of the entry points to the code. The main app and the data_fetch script are covered, but there may be some I've missed...

If you want me to try to cook up an automated test, please let me know.

How to test the changes?

(Select all options that apply)

  • I've included appropriate automated tests.
  • This is a refactoring of components with existing test coverage.
  • Instructions for manual testing are as follows:
    1. Create an instance of Galaxy that incorporates the changes, and induce it to make HTTP requests to servers that log the User-Agent header and you have access to. Check the logs and confirm that the User-Agent includes "galaxy".

License

  • I agree to license these and all my past contributions to the core galaxy codebase under the MIT license.

@mvdbeek
Copy link
Member

mvdbeek commented Mar 5, 2024

This looks like a good start, do you need any help ?

@svonworl
Copy link
Contributor Author

svonworl commented Mar 6, 2024

Hi Marius, I've reopened this PR and updated the description. Regarding help, thanks! The user_agent package should be imported at all of the entry points to the code, so if I missed any, please let me know. Also, I can try to improve the testing if you think it needs to be better.

@svonworl svonworl reopened this Mar 6, 2024
urllib.request.build_opener = new_build_opener


__append_word_to_user_agent("galaxy")
Copy link
Member

@hexylena hexylena Mar 6, 2024

Choose a reason for hiding this comment

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

I think we could also include the galaxy version here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we could also include the galaxy version here as well

Good idea, I added the galaxy version to the User-Agent.

Now, the primary outstanding TODO is to figure out where to import the user_agent package to get full coverage. I probably should defer to y'all on that one, there's probably a best place that those with deeper knowledge of the codebase would know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @hexylena, I'm checking in on this PR. I think it's mostly done, except that someone with deep knowledge of the codebase (probably a regular contributor such as yourself) needs to determine where to import the user_agent package so that the user agent is properly set at all entry points. Please let me know if there's any way I can help.

@svonworl svonworl requested a review from hexylena March 15, 2024 23:50
@hexylena hexylena requested a review from mvdbeek March 28, 2024 08:32
def new_default_user_agent(*args):
return f"{old_default_user_agent(*args)} {word}"

requests.utils.default_user_agent = new_default_user_agent
Copy link
Member

Choose a reason for hiding this comment

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

We'll have to find another way to do this, we shouldn't do monkey patching on a third party library

opener.addheaders = [modify_user_agent_header(header) for header in opener.addheaders]
return opener

urllib.request.build_opener = new_build_opener
Copy link
Member

Choose a reason for hiding this comment

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

same as the comment above, w.r.t. the standard library.

urllib.request.build_opener = new_build_opener


__append_word_to_user_agent(f"galaxy/{VERSION}")
Copy link
Member

Choose a reason for hiding this comment

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

This will be triggered by just importing the module, which may be a surprising side effect in many contexts. It may be better to have a function that can be imported and then called explicitly. That would also simplify testing of this module.

@mvdbeek
Copy link
Member

mvdbeek commented Mar 29, 2024

We'll likely want to do something like c0a91c3 instead. I'll tweak this a little, but (almost) all modern code should use requests at this code, and that should cover the interaction with dockstore as well.

@mvdbeek mvdbeek mentioned this pull request Apr 16, 2024
4 tasks
@mvdbeek mvdbeek removed this from the 24.1 milestone May 14, 2024
@nsoranzo
Copy link
Member

Merged as part of #18003 , thanks for the contribution @svonworl !

@nsoranzo nsoranzo closed this May 20, 2024
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.

5 participants