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.sh] Parsing token inside each upstream #16289

Closed
wants to merge 30 commits into from
Closed

[update.sh] Parsing token inside each upstream #16289

wants to merge 30 commits into from

Conversation

sazriel26
Copy link
Contributor

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally?
  • Have you successfully run brew typecheck with your changes locally?
  • Have you successfully run brew tests with your changes locally?

This pull request is not for formula but for update.sh script to allow local token parsing inside each upstream


Move global token HOMEBREW_GITHUB_API_TOKEN support inside each upstream
repository to parse local token (if any) and setup it accordingly
(local supersede global token - this can be reordered if needed)
@MikeMcQuaid
Copy link
Member

@sazriel26 can you explain the motivation for this PR rather than what it is doing? Thanks!

@sazriel26
Copy link
Contributor Author

@MikeMcQuaid Well I started using a private repository for hosting some formulas with access through Git token (thus ran brew tap sazriel26/private https://sazriel26:<git_token>@github.com/....)

Such token is not a "global" but rather a local dedicated to this repository (git config is able to retrieve it and brew update can update the repository accordingly)

This is not a must have just proposing you this contribution for reviewing if it can suit

@MikeMcQuaid
Copy link
Member

@sazriel26 Ok, I understand a bit more about what you're doing here but not the "why" behind the pull request. What did it do before that you didn't like and what does it do now instead?

@sazriel26
Copy link
Contributor Author

@MikeMcQuaid Sorry for the late reply, need to wait until next working day for getting access to test environment (and hopefully having a fresh mind)

About this pull request, it is aiming to help update command to check through Github API for private repository if continuing or not the process like for public ones.

To illustrate, when UPSTREAM_REPOSITORY_URL (from git config remote.origin.url) is including a token reference (https://user:[email protected]/...), then the test

      if [[ "${UPSTREAM_REPOSITORY_URL}" == "https://github.com/"* ]]

fails due to URL with user and token and skip this section of code
Then update continues to fetch the repository (see brew update --verbose)

Checking if we need to fetch /opt/homebrew/Library/Taps/sazriel26/homebrew-***...
...
Fetching /opt/homebrew/Library/Taps/sazriel26/homebrew-***..

With this "patch", if there is a token provided, it is parsed and can supersede the global one for using the API of Github to help deciding on going ahead or skipping the fetch - see

[[ -z "${HOMEBREW_UPDATE_FORCE}" ]] && [[ "${UPSTREAM_SHA_HTTP_CODE}" == "304" ]] && exit

Not a "big deal" but I wanted to share this "enhancement" for your attention.

Thanks for your attention and patience about this matter

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

I understand a bit more what you're asking for here, thanks

Library/Homebrew/cmd/update.sh Outdated Show resolved Hide resolved
Library/Homebrew/cmd/update.sh Outdated Show resolved Hide resolved
Library/Homebrew/cmd/update.sh Outdated Show resolved Hide resolved
@sazriel26
Copy link
Contributor Author

Is it ok?

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Just to set expectations: I think this feature is desirable but this PR is currently quite a long way off being an implementation we can merge and support. I find it very hard to read and understand right now and the error-handling in update.sh needs to be absolutely rock-solid otherwise we run a risk of breaking Homebrew's own update mechanism.

Thanks again!

Library/Homebrew/cmd/update.sh Outdated Show resolved Hide resolved
Library/Homebrew/utils/url.sh Outdated Show resolved Hide resolved
Library/Homebrew/utils/url.sh Outdated Show resolved Hide resolved
Library/Homebrew/utils/url.sh Outdated Show resolved Hide resolved
Library/Homebrew/utils/url.sh Outdated Show resolved Hide resolved
Library/Homebrew/utils/url.sh Outdated Show resolved Hide resolved
better wording

Co-authored-by: Mike McQuaid <[email protected]>
@sazriel26
Copy link
Contributor Author

First thank you @MikeMcQuaid for your kind support and availability about this PR.

According to #16289 (review), it looks hard to satisfy some prerequisites in contributing for a newcomer like me, isn't it?

What about to discard this PR which is not ready at this moment for merge?

Thanks again for your understanding and sorry for the annoyance (including time consuming)

@MikeMcQuaid
Copy link
Member

First thank you @MikeMcQuaid for your kind support and availability about this PR.

You're welcome ❤️

According to #16289 (review), it looks hard to satisfy some prerequisites in contributing for a newcomer like me, isn't it?

I think we can get there together; I'm just giving you the heads up that it may take a while.

Some newcomers contributions are merged very quickly, others are not. It depends on the readability of the code, the invasiveness of the change and the potential negative effects of any issues.

What about to discard this PR which is not ready at this moment for merge?

Up to you, I'm happy to keep working here if you are!

Thanks again for your understanding and sorry for the annoyance (including time consuming)

Thank you and no apologies needed; not annoyed here at all 😁

@sazriel26
Copy link
Contributor Author

Thanks @MikeMcQuaid !

Let's give a try this week and state about if it can suit or not

See you then.

Copy link

github-actions bot commented Jan 1, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@github-actions github-actions bot added the stale No recent activity label Jan 1, 2024
@sazriel26
Copy link
Contributor Author

@MikeMcQuaid With some delay, and thinking of the shell used (bash), I have rewritten this pull request accordingly (with help of BASH_REMATCH array)

@github-actions github-actions bot removed the stale No recent activity label Jan 7, 2024
Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Thanks @sazriel26. This is looking a lot clearer now. I've suggested some tweaks (that may need fixed up with brew style) to ensure your new logic is a fallback rather than taking priority or encompassing the old logic.

Library/Homebrew/cmd/update.sh Outdated Show resolved Hide resolved
Library/Homebrew/cmd/update.sh Outdated Show resolved Hide resolved

# HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning.
# shellcheck disable=SC2030
if [[ "${UPSTREAM_REPOSITORY_URL}" == "https://github.com/"* ]]
then
UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}"
UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}"
elif [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
elif [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]]
elif [[ "${DIR}" != "${HOMEBREW_REPOSITORY}" ]] && [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]]

I think this is what we want to do here based on discussion with @MikeMcQuaid.

@sazriel26
Copy link
Contributor Author

Can it suit better now?

@MikeMcQuaid MikeMcQuaid requested review from Bo98, carlocab and cho-m and removed request for apainintheneck January 22, 2024 12:08
Copy link
Member

@carlocab carlocab left a comment

Choose a reason for hiding this comment

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

I'm convinced that this code does what it's intended and is unlikely to break anything important.

However, is this the correct API for providing repository/tap-specific credentials?

IIUC, encoding access credentials in the URL is insecure and discouraged. If so, then, at minimum, we should:

  1. at least redact the credentials from the URL we pass to curl; or,
  2. maybe consider a different API for passing these credentials.

@Bo98
Copy link
Member

Bo98 commented Feb 1, 2024

redact the credentials from the URL we pass to curl

We're not passing auth in the URL to curl. We're decoding it from an already-encoded git URL and passing it to curl via --header.

On the credential retrieval side, it isn't fully foolproof. You can supply git with a plain URL and retrieve credentials via the keychain (where available) or use .extraheader (https://github.com/Homebrew/actions/blob/d54a6744d5fcdff54b45a9659f3e17f769389952/setup-homebrew/main.sh#L129). Though anything is better than nothing.

@MikeMcQuaid
Copy link
Member

IIUC, encoding access credentials in the URL is insecure and discouraged.

In my experience: it's also the only method that 100% works across curl and git invocations cross-platform without other tooling and configuration.

That's not to say "let's encourage it" but it is a completely reasonable thing to do particularly given:

We're not passing auth in the URL to curl. We're decoding it from an already-encoded git URL and passing it to curl via --header.

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Thanks for your first PR to Homebrew, @sazriel26, you rock and I appreciate your patience with all the back-and-forth!

@p-linnane
Copy link
Member

@sazriel26 Can you please rebase this so we can get it merged? Thanks!

@sazriel26
Copy link
Contributor Author

@sazriel26 Can you please rebase this so we can get it merged? Thanks!
@p-linnane Sure, can you hint me about 🙏?
I can try to resync my forked repository so it would be as close as possible as current state of reference repository
How can I do a git rebase 🤗 ?

Thanks in advance

@p-linnane
Copy link
Member

You can check the Git manual for this.

@sazriel26 sazriel26 deleted the branch Homebrew:master February 7, 2024 13:16
@sazriel26 sazriel26 closed this Feb 7, 2024
@sazriel26 sazriel26 deleted the master branch February 7, 2024 13:16
@sazriel26 sazriel26 restored the master branch February 7, 2024 13:16
@sazriel26 sazriel26 deleted the master branch February 7, 2024 13:31
@sazriel26
Copy link
Contributor Author

Oops ! I have broken this pull request, isn't it?

When I have tried to reproduce the https://git-scm.com/book/en/v2/Git-Branching-Rebasing:

  • I have renamed current "master" (from which I started the PR) to "parse-git-token" (the experiment)
  • I have cloned the upstream/master to "master"
  • When I ran the
git rebase master

it is saying "Current branch parse-git-token is up to date."

Shall I reopen a new pull request from the parse-git-token (old master from which I have opened this PR)?

Thanks in advance and sorry for the inconvenience

@MikeMcQuaid
Copy link
Member

it is saying "Current branch parse-git-token is up to date."

You need to instead run git fetch --all; git rebase origin/master (assuming origin is Homebrew/brew and not your fork).

Shall I reopen a new pull request from the parse-git-token (old master from which I have opened this PR)?

Yes, please.

@sazriel26
Copy link
Contributor Author

@MikeMcQuaid Thanks for your answer

I have created a draft pull request #16649 as demanded

Can you check if it suits the requirements of rebase ?

Thanks in advance

@github-actions github-actions bot added the outdated PR was locked due to age label Mar 15, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants