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

Allow SCP-style URLs for Git #129

Merged
merged 3 commits into from
Nov 19, 2018
Merged

Allow SCP-style URLs for Git #129

merged 3 commits into from
Nov 19, 2018

Conversation

mitchellh
Copy link
Contributor

This fixes both #38 and #124, and should also address the 4 TF issues referenced there as well. First, thanks @paultyng for the failing cases as well as a stab at a solution. You can see I took from that! I also took the failing cases from #38 (now passing).

I dug into the linked Terraform issues and there were actually two issues at play, both fixed in this PR:

1.) Detector for SCP-like Git URLs:

The heuristic we use here is the git@ username. We can extend this to support more usernames in the future (the code is ready for it) but all the issues seem to use this as the user and this limits the blast radius of this change significantly.

This detects things in the form of git@foo.com:path and converts it to git::ssh://git@foo.com/path and Git will clone this properly.

Note that if a user other than "git" is required, the go-getter user can still use full forced URLs like git::ssh://admin@host.com/path.

2.) Fixing up the SCP-like URLs directly in the Git getter:

If a full, valid URL is given to go-getter, then the detectors are not run. So if a user gave the URL git::ssh://git@foo.com:path (note the colon), then the detector won't run to fix that up at all. Therefore, I also added a little fixup directly in the Git getter that looks for 1.) an SSH URL and 2.) with ":" in the host (how Go's net/url parses it) and fixes it up.

This should fix the issues where the direct URL is used.

Note to @apparentlymart: Re: your comment, I verified that forced protocols will always force. This is done here:

if getForce != "" {
So your comment should be okay, everything works as you would expect.

Backwards Compatibility

This should not break any backwards compatibility.

These SCP-like URLs were being detected as "file" URLs below and its highly unlikely that was ever the intended behavior. Users who may have intended this can force it with file::, though I strongly believe the number of users impacted by that will be zero.

Future Stuff

The groundwork is now laid (detect_ssh.go) to support an SCP-getter directly as well. So we can consider doing that one day by expanding this detector directly into SSH URLs that use an SCP lib or something.

This detects URLs in the format of git@my.custom.git:dir1/dir2 and
convert them to git::ssh://git@my.custom.git/dir1/dir2.
If a fully qualified URL like ssh://user@host:path is given, then the
detectors don't run (since the URL is valid). Therefore we must also
correct this directly in the getter.
{
"git@github.com:hashicorp/foo.git?foo=bar",
"git::ssh://git@github.com/hashicorp/foo.git?foo=bar",
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests were moved to detect_git_test.go, not removed.

Copy link

@paultyng paultyng left a comment

Choose a reason for hiding this comment

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

LGTM, some minor comments around edge cases.

detect_git_test.go Show resolved Hide resolved
get_git.go Show resolved Hide resolved
detect_git_test.go Show resolved Hide resolved
Copy link
Contributor

@apparentlymart apparentlymart 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 still not really a fan of adding more "magic" detectors here, but I agree that this seems like the minimum possible solution for right now and should get us out of the current confusing situation.

@mitchellh mitchellh merged commit bd1edc2 into master Nov 19, 2018
@mitchellh mitchellh deleted the b/git-scp branch November 19, 2018 19:45
apparentlymart added a commit that referenced this pull request May 10, 2019
The "scp-style" remote URL scheme in git is ambiguous with the standard
URL syntax in our previous design where we'd add/accept the ssh://
prefix in both cases; ssh://git@example.com:222/foo could be understood
as either:

 - connect to example.com on port 222 and clone from path "foo"
 - connect to example.com on port 22 and clone from path "222/foo"

Originally we used the first interpretation. but in #129 we switched to
the second in order to support a strange hybrid form where no port number
was specified, but in the process we broke handling of the standard URL
form when a non-standard port is required, as is sometimes the case for
internal git servers running on machines where port 22 is already used for
administrative access.

git itself resolves this ambiguity by not permitting the ssh:// scheme to
be used in conjunction with the scp-like form. Here we mimic that by
supporting the scp-like form _only_ in the detector, and only allowing it
when the ssh:// prefix isn't present. That allows for the following two
scp-like forms to be accepted:

 - git@example.com:foo/bar when the username is "git"
 - git::anything@example.com:foo/bar for any other username

We no longer support using the scp-like form with the ssh:// prefix, so
git::ssh://anything.example.com:foo/bar is invalid. To use that form, the
ssh:// part must be omitted, which is consistent with git's own behavior.

The detector already rewrites the scp-like form into the standard URL form,
when no ssh: scheme is given so the getter does not need to do anything
special to deal with it.

In the git getter, we now also check to see if the port number seems to
be non-decimal and return a more actionable error if so, since that is
likely to be caused by someone incorrectly using the ssh:// scheme with
a scp-style address.
apparentlymart added a commit that referenced this pull request May 12, 2019
The "scp-style" remote URL scheme in git is ambiguous with the standard
URL syntax in our previous design where we'd add/accept the ssh://
prefix in both cases; ssh://git@example.com:222/foo could be understood
as either:

 - connect to example.com on port 222 and clone from path "foo"
 - connect to example.com on port 22 and clone from path "222/foo"

Originally we used the first interpretation. but in #129 we switched to
the second in order to support a strange hybrid form where no port number
was specified, but in the process we broke handling of the standard URL
form when a non-standard port is required, as is sometimes the case for
internal git servers running on machines where port 22 is already used for
administrative access.

git itself resolves this ambiguity by not permitting the ssh:// scheme to
be used in conjunction with the scp-like form. Here we mimic that by
supporting the scp-like form _only_ in the detector, and only allowing it
when the ssh:// prefix isn't present. That allows for the following two
scp-like forms to be accepted:

 - git@example.com:foo/bar when the username is "git"
 - git::anything@example.com:foo/bar for any other username

We no longer support using the scp-like form with the ssh:// prefix, so
git::ssh://anything.example.com:foo/bar is invalid. To use that form, the
ssh:// part must be omitted, which is consistent with git's own behavior.

The detector already rewrites the scp-like form into the standard URL form,
when no ssh: scheme is given so the getter does not need to do anything
special to deal with it.

In the git getter, we now also check to see if the port number seems to
be non-decimal and return a more actionable error if so, since that is
likely to be caused by someone incorrectly using the ssh:// scheme with
a scp-style address.
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.

3 participants