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

feat(git/auth): Support hostrules with username and password when authenticating git commands #24081

Merged
merged 9 commits into from
Sep 4, 2023

Conversation

Shegox
Copy link
Contributor

@Shegox Shegox commented Aug 25, 2023

Changes

This pull request introduces the ability to authenticate git commands using a combination of username and password through host rules in addition to the currently supported token format.

Context

Fixes #24077

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • No documentation update is required

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests but ran on a real repository, or
  • Both unit tests + ran on a real repository

@Shegox Shegox requested a review from JamieMagee as a code owner August 25, 2023 13:02
lib/util/git/auth.ts Outdated Show resolved Hide resolved
lib/util/git/auth.ts Outdated Show resolved Hide resolved
lib/util/git/auth.ts Outdated Show resolved Hide resolved
@Shegox Shegox requested a review from viceice August 28, 2023 06:52
lib/modules/manager/git-submodules/readme.md Outdated Show resolved Hide resolved
lib/modules/manager/gomod/readme.md Outdated Show resolved Hide resolved
lib/util/git/auth.spec.ts Outdated Show resolved Hide resolved
@Shegox Shegox requested a review from viceice August 29, 2023 20:11
Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

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

otherwise LGTM

lib/util/git/auth.ts Show resolved Hide resolved
@viceice viceice enabled auto-merge September 4, 2023 09:47
@viceice viceice added this pull request to the merge queue Sep 4, 2023
Merged via the queue into renovatebot:main with commit 095afcd Sep 4, 2023
@renovate-release
Copy link
Collaborator

🎉 This PR is included in version 36.79.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@lcobucci
Copy link
Contributor

lcobucci commented Sep 4, 2023

Just tested the fix and I still get the authentication errors 😢
I'll do some local debugging and come back you.

Thanks @Shegox and @viceice for working on this 👍

@lcobucci
Copy link
Contributor

lcobucci commented Sep 4, 2023

Okay, after adding a whole lot of console.log() on a few places I've managed to find the issue... it's a tricky one, I think 😭

We were previously relying on the host types git-refs and git-tags to resolve the submodules.
The authentication changes aren't considering those host types, leading the creation of environment variables using the token as auth mechanism (RENOVATE_TOKEN is automatically promoted as a host rule).

So, essentially I have the following host rules:

[
  {
    hostType: 'github',
    matchHost: 'github.com',
    token: 'github-token',
    resolvedHost: 'github.com'
  },
  {
    hostType: 'git-refs',
    matchHost: 'mygitlab.com',
    password: 'mygitlab-pwd',
    username: 'renovate-bot',
    resolvedHost: 'mygitlab.com'
  },
  {
    hostType: 'git-tags',
    matchHost: 'mygitlab.com',
    password: 'mygitlab-pwd',
    username: 'renovate-bot',
    resolvedHost: 'mygitlab.com'
  },
  {
    matchHost: 'mygitlab.com',
    token: 'mygitlab-pwd',
    hostType: 'gitlab',
    resolvedHost: 'mygitlab.com'
  }
]

The host rule created from RENOVATE_TOKEN is legit for the API requests. However, not for the Git related requests.

Filtering for git-refs, renovate creates the following env vars (contents of newEnvironmentVariables):

{
  GIT_CONFIG_KEY_0: 'url.https://ssh:[email protected]/.insteadOf',
  GIT_CONFIG_VALUE_0: 'ssh://[email protected]/',
  GIT_CONFIG_KEY_1: 'url.https://git:[email protected]/.insteadOf',
  GIT_CONFIG_VALUE_1: '[email protected]:',
  GIT_CONFIG_KEY_2: 'url.https://[email protected]/.insteadOf',
  GIT_CONFIG_VALUE_2: 'https://github.com/',
  GIT_CONFIG_COUNT: '6',
  GIT_CONFIG_KEY_3: 'url.https://renovate-bot:[email protected]/.insteadOf',
  GIT_CONFIG_VALUE_3: 'ssh://[email protected]/',
  GIT_CONFIG_KEY_4: 'url.https://renovate-bot:[email protected]/.insteadOf',
  GIT_CONFIG_VALUE_4: '[email protected]:',
  GIT_CONFIG_KEY_5: 'url.https://renovate-bot:[email protected]/.insteadOf',
  GIT_CONFIG_VALUE_5: 'https://mygitlab.com/'
}

And manually running git ls-remote --symref https://mygitlab.com/path/to/my/repo.git HEAD with those variables work just fine. However, letting the command run leads to the password prompt of a completely weird username (oauth2) - which understandably fails on non-interactive sessions.

I also confirm that running git ls-remote --symref https://[email protected]/path/to/my/repo.git HEAD with those variables doesn't work at all (which makes sense because there's no match on https://oauth2@....

What do you think? We might still be missing some scenarios for getAuthenticationRules() or getGitEnvironmentVariables() - it seems like different components are messing up with the expectations here.

@lcobucci
Copy link
Contributor

lcobucci commented Sep 4, 2023

Btw, I did find oauth2 here: https://github.com/renovatebot/renovate/blob/main/lib/modules/platform/gitlab/index.ts#L248
Is this somehow affecting the URL generation?

@viceice
Copy link
Member

viceice commented Sep 4, 2023

please open a new discussion

@lcobucci
Copy link
Contributor

lcobucci commented Sep 4, 2023

please open a new discussion

@viceice the original hasn't been concluded IMHO so I just added context here. I believe it's quite counter-productive to open a new one.

@viceice
Copy link
Member

viceice commented Sep 4, 2023

you can of cause use the existing discussion, but not the PR. this PR is wrong place to discuss things.

@renovatebot renovatebot locked and limited conversation to collaborators Sep 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants