-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix(git): add warning of the mismatch of git cred and url #3136
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright 2020 The Tekton Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package git | ||
|
||
import "testing" | ||
|
||
func TestValidateGitSSHURLFormat(t *testing.T) { | ||
tests := []struct { | ||
url string | ||
want bool | ||
}{ | ||
{ | ||
url: "git@github.com:user/project.git", | ||
want: true, | ||
}, | ||
{ | ||
url: "git@127.0.0.1:user/project.git", | ||
want: true, | ||
}, | ||
{ | ||
url: "http://github.com/user/project.git", | ||
want: false, | ||
}, | ||
{ | ||
url: "https://github.com/user/project.git", | ||
want: false, | ||
}, | ||
{ | ||
url: "http://127.0.0.1/user/project.git", | ||
want: false, | ||
}, | ||
{ | ||
url: "https://127.0.0.1/user/project.git", | ||
want: false, | ||
}, | ||
{ | ||
url: "http://host.xz/path/to/repo.git/", | ||
want: false, | ||
}, | ||
{ | ||
url: "https://host.xz/path/to/repo.git/", | ||
want: false, | ||
}, | ||
{ | ||
url: "ssh://user@host.xz:port/path/to/repo.git/", | ||
want: true, | ||
}, | ||
{ | ||
url: "ssh://user@host.xz/path/to/repo.git/", | ||
want: true, | ||
}, | ||
{ | ||
url: "ssh://host.xz:port/path/to/repo.git/", | ||
want: true, | ||
}, | ||
{ | ||
url: "ssh://host.xz/path/to/repo.git/", | ||
want: true, | ||
}, | ||
{ | ||
url: "git://host.xz/path/to/repo.git/", | ||
want: false, | ||
}, | ||
{ | ||
url: "/path/to/repo.git/", | ||
want: false, | ||
}, | ||
{ | ||
url: "file://~/path/to/repo.git/", | ||
want: false, | ||
}, | ||
{ | ||
url: "user@host.xz:/path/to/repo.git/", | ||
want: true, | ||
}, | ||
{ | ||
url: "host.xz:/path/to/repo.git/", | ||
want: true, | ||
}, | ||
{ | ||
url: "user@host.xz:path/to/repo.git", | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
got := ValidateGitSSHURLFormat(tt.url) | ||
if got != tt.want { | ||
t.Errorf("Validate URL(%v)'s SSH format got %v, want %v", tt.url, got, tt.want) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this some more I'm not sure this is enough to correctly validate SSH credentials. A user can mix SSH and non-SSH git credentials in a single Step. For example, with two Secrets:
In this example YAML both a
~/.ssh
directory and a~/.git-credentials
file will exist side by side.~/.git-credentials
is created for the gitlab Secret. So this code will print a warning because the gitlab basic auth URL is not SSH-formatted. But the user never intends to use SSH auth with a gitlab URL.The full solution to this problem is probably quite a lot more complicated - it would need to read the
~/.ssh/config
file and figure out whether the givenurl
is intended to be used with SSH authentication.We could work around this somewhat by updating the warning messages to be more relaxed. Something like:
But this feels like it would lead to more user confusion and I don't think this is a great workaround either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I met the similar problem when I tested the code: if the given URL is a public repo and the cred is SSH type, the warning log will be shown and the pod can still pull the code successfully.
But it will be more too complex if we check the URL is public or not. So my point is we can just let the warning show since it's only a warning and people will not care about the warning if the job success. 🤔 WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, tricky problem! OK, I think we can make this work with an even longer log message:
@vdemeester what do you think of this? Is it too vague? The other thing we could do is drop this message and only print a warning if a URL is definitely an SSH url but the user has 0 SSH credentials available in the Step. That is 100% of the time a legitimate error I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change the message first but I'm also not quite sure that if we need that message when SSH credentials are provided but the URL is non-SSH. Let me know if there is better solutions!