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

Support SSH endpoints and basic auth for HTTP endpoints #98

Merged
merged 2 commits into from
Feb 7, 2024

Conversation

smoores-dev
Copy link
Contributor

At the moment, it's challenging to use private git repos as Scaffolds. This change adds two ways to do this:

  • Allows users to pass SSH endpoints, like [email protected]:smoores-dev/scaffold to scaffold new
  • Prompts users for HTTP auth credentials (username and password or personal access token) when we receive an authentication required error from the git provider.

The new URL tools are ported straight from go-git, and match the way they check and parse URLs.

This fixes #97.

Comment on lines 107 to 110
if pkgUrl.IsRemoteEndpoint(str) {
return str, true
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We check for remoteness after checking for short codes, because strings like gh:smoores-dev/scaffold can be valid SCP endpoints, since user and password are optional and gh is a valid host name.

In practice this is probably what folks want, anyway: if I set a short code, I would expect anything matching it to be replaced first, even if that short code is technically a valid URL itself.

Copy link
Owner

@hay-kot hay-kot left a comment

Choose a reason for hiding this comment

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

This looks great! Thanks for the PR. Super happy to get this merged, just left a few notes.

Let me know what you think about the basic auth config stuff. I think I'd also be okay merging as is and I can circle back and take a stab at the config stuff as well.

@@ -11,10 +11,12 @@ import (
"path/filepath"
"strings"

pkgUrl "github.com/hay-kot/scaffold/app/scaffold/pkgs/url"
Copy link
Owner

Choose a reason for hiding this comment

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

I think we can just name the package pkgurl

@@ -49,7 +51,24 @@ func (r *Resolver) Resolve(arg string, checkDirs []string) (path string, err err
Progress: os.Stdout,
})
if err != nil {
return "", fmt.Errorf("failed to clone repository: %w", err)
if err.Error() == "authentication required" {
Copy link
Owner

Choose a reason for hiding this comment

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

Should use the error comparison here

I think this is the one you're looking for

https://github.com/go-git/go-git/blob/d9b0a0dd2f4f03e82bcd7f7dad1f7b5eb42713c6/plumbing/transport/common.go#L34

See This SO Issue if you're unfamiliar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! Awesome, this is actually new to me, thanks so much for the link

Copy link
Contributor Author

@smoores-dev smoores-dev Feb 7, 2024

Choose a reason for hiding this comment

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

Ah, it looks like plumbing/transport/common is actually internal, so I maybe can't import ErrAuthenticationRequired?

Ignore this, I figured it out!

Comment on lines +55 to +67
username, password, err := promptHTTPAuth()
if err == nil {
r, err = git.PlainClone(dir, false, &git.CloneOptions{
URL: remoteRef,
Progress: os.Stdout,
Auth: &http.BasicAuth{
Username: username,
Password: password,
},
})
}
Copy link
Owner

Choose a reason for hiding this comment

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

I must have deleted this comment on accident, whoops!


I'm wondering if this should be configuration driven in additon to being interactive? Interactive is nice, but if you're pulling down a few it would be nice to able to define these in the conf file

Here's what that could look like

auth:
  - match: ^https://github.com.*
    basic:
      username: hay-kot 
      password: my-password
  - match: ^https://mylocalgit.com.*
    basic:
      username: ${MY_LOCAL_GIT_USERNAME}
      password: ${MY_LOCAL_GIT_PASSWORD}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh very nice, yeah I think this would be great. Would you be alright with merging this as a first pass, and then adding config support as a fast follow? I'm happy to take a stab at the config work, too! But I'd also like to get this in :D

Copy link
Owner

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Owner

@hay-kot hay-kot left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@hay-kot hay-kot merged commit 5f2ed78 into hay-kot:main Feb 7, 2024
1 check passed
@smoores-dev smoores-dev deleted the ssh-and-http-auth branch February 7, 2024 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support SSH remotes and/or HTTP Auth
2 participants