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

[THOG-204] Use oauth2 as username when cloning #441

Merged
merged 3 commits into from
Apr 22, 2022

Conversation

ahrav
Copy link
Collaborator

@ahrav ahrav commented Apr 21, 2022

What?

Fix error when cloning private gitlab repos.

Why?

In order to scan repos for secret we need to be able to clone them first.

How?

Use oauth2 as the username when constructing the url for cloning a repo.

Testing?

Updated unit tests and they are also now working.

Screenshots (optional)

Anything Else?

@@ -324,7 +324,7 @@ func (s *Source) scanRepos(ctx context.Context, chunksChan chan *sources.Chunk,
}
s.SetProgressComplete(i, len(repos), fmt.Sprintf("Repo: %s", repoURL), "")

path, repo, err := git.CloneRepoUsingToken(s.token, repoURL.String(), s.user)
path, repo, err := git.CloneRepoUsingToken(s.token, repoURL.String(), "oauth2")
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe "placeholder" instead? We don't support oauth2 for gitlab, this is kind of confusing.

Copy link
Collaborator Author

@ahrav ahrav Apr 21, 2022

Choose a reason for hiding this comment

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

well the "oauth2" string is actually need in order to server as the user here:

func CloneRepoUsingToken(token, gitUrl, user string) (string, *git.Repository, error) {
userInfo := url.UserPassword(user, token)
return CloneRepo(userInfo, gitUrl)
}

which is then used here:

cloneURL.User = userInfo
cloneCmd := exec.Command("git", "clone", cloneURL.String(), clonePath)
//cloneCmd := exec.Command("date")
output, err := cloneCmd.CombinedOutput()

That way when we construct the url to clone it has the correct format of:
git clone https://oauth2:${Personal Access Tokens}@gitlab.com/username/myrepo.git Without oauth2 as part of the url string the clone command fails because it requires a username and password. Not sure if there is an alternative way to clone a private repo without setting the url string as in the example above.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you saying the specific string "oauth2" is required and a string that is "placeholder" (or similar) won't work? As far as I can tell, there is nothing related to oauth2 happening here so it's confusing.

Copy link
Collaborator Author

@ahrav ahrav Apr 21, 2022

Choose a reason for hiding this comment

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

ahh got it, yea I was under the impression it needed to be oauth2 as the string before : in the url because that's what i was reading SO answers

but with some additional testing as long as it's a string it works, it just can't be an empty string. Interesting and weird 🤔

Updated to "placeholder"

@@ -324,7 +324,7 @@ func (s *Source) scanRepos(ctx context.Context, chunksChan chan *sources.Chunk,
}
s.SetProgressComplete(i, len(repos), fmt.Sprintf("Repo: %s", repoURL), "")

path, repo, err := git.CloneRepoUsingToken(s.token, repoURL.String(), s.user)
path, repo, err := git.CloneRepoUsingToken(s.token, repoURL.String(), "placeholder")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think basic auth used to be supported on gitlab, so we should leave the option for a username. The on prem gitlab offering could still support it. Using "placeholder" if s.user is empty should work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

makes sense. Updated to check if s.user is set in the event it isn't we can use "placeholder".

@ahrav ahrav requested a review from bill-rich April 21, 2022 23:47
@dustin-decker dustin-decker merged commit 3b35404 into main Apr 22, 2022
@dustin-decker dustin-decker deleted the THOG-204-clone-private-repos-failing branch April 22, 2022 00:49
bill-rich pushed a commit that referenced this pull request May 2, 2022
* Use oauth2 as the username for cloning private repos with a token.

* Update username string.

* Only set user to "placeholder" if no username is present.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants