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

Fix(git): add warning of the mismatch of git cred and url #3136

Merged
merged 1 commit into from
Sep 22, 2020

Conversation

FogDong
Copy link
Member

@FogDong FogDong commented Aug 26, 2020

Changes

Fix(git): add warning of the mismatch of git cred and url
Fix #3094

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

  • Includes tests (if functionality changed/added)
  • Includes docs (if user facing)
  • Commit messages follow commit message best practices
  • Release notes block has been filled in or deleted (only if no user facing changes)

See the contribution guide for more details.

Double check this list of stuff that's easy to miss:

Reviewer Notes

If API changes are included, additive changes must be approved by at least two OWNERS and backwards incompatible changes must be approved by more than 50% of the OWNERS, and they must first be added in a backwards compatible way.

Release Notes

Fix(git): Tekton's credentials initialization now detects when an SSH credential is used with a non-SSH URL (and vice versa) in Git PipelineResources and will log a warning in Step containers.

@tekton-robot tekton-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Aug 26, 2020
@tekton-robot tekton-robot requested review from imjasonh and a user August 26, 2020 08:16
@tekton-robot tekton-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 26, 2020
@tekton-robot
Copy link
Collaborator

Hi @FogDong. Thanks for your PR.

I'm waiting for a tektoncd member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@withlin
Copy link
Member

withlin commented Aug 26, 2020

/kind feature
/ok-to-test

@tekton-robot tekton-robot added kind/feature Categorizes issue or PR as related to a new feature. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 26, 2020
@FogDong
Copy link
Member Author

FogDong commented Aug 27, 2020

PTAL 😉 @sbwsg

pkg/git/git.go Outdated Show resolved Hide resolved
pkg/git/git.go Outdated Show resolved Hide resolved
@FogDong FogDong force-pushed the git branch 2 times, most recently from 684e542 to d99c607 Compare August 31, 2020 02:59
@FogDong
Copy link
Member Author

FogDong commented Sep 2, 2020

Fixed. 😆 Please review @sbwsg

@FogDong
Copy link
Member Author

FogDong commented Sep 7, 2020

Can anybody take a look at this PR? 🤔

/cc @vdemeester

pkg/git/git.go Outdated Show resolved Hide resolved
pkg/git/git.go Outdated Show resolved Hide resolved
pkg/git/git.go Outdated Show resolved Hide resolved
func validateGitAuth(logger *zap.SugaredLogger, url string) error {
homeenv := os.Getenv("HOME")
sshCred := true
if _, err := os.Stat(homeenv + "/.ssh"); os.IsNotExist(err) {
Copy link

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:

apiVersion: v1
kind: Secret
metadata:
  name: example-gitlab-creds
  annotations:
    tekton.dev/git-0: https://gitlab.com
type: kubernetes.io/basic-auth
stringData:
  username: myuser
  password: hunter2
---
apiVersion: v1
kind: Secret
metadata:
  name: example-github-creds
  annotations:
    tekton.dev/git-0: github.com
type: kubernetes.io/ssh-auth
stringData:
  ssh-privatekey: blahblahblah
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: example-service-account
secrets:
  - name: example-gitlab-creds
  - name: example-github-creds
---
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
  name: foobar
spec:
  serviceAccountName: example-service-account
  taskSpec:
    steps:
    - name: wait
      image: alpine:latest
      script: |
        echo "foo"

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 given url 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:

logger.Warnf("SSH credentials have been provided but the URL %q is not a valid SSH URL", url)

But this feels like it would lead to more user confusion and I don't think this is a great workaround either.

Copy link
Member Author

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?

Copy link

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:

SSH credentials have been provided but the URL %q is not a valid SSH URL. This warning can be safely ignored if the URL is for a public repo or you are using basic auth

@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?

Copy link
Member Author

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!

pkg/git/git.go Outdated Show resolved Hide resolved
pkg/git/git.go Outdated Show resolved Hide resolved
@tekton-robot tekton-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 9, 2020
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/git/git.go Do not exist 2.4%

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/git/git.go Do not exist 2.4%

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/git/git.go Do not exist 2.4%

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/git/git.go Do not exist 2.4%

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Sorry, I've left more comments. I'm not totally sure if we want the warning message when SSH credentials are provided but the URL is non-SSH. Might be too vague? Curious what others think about this!

pkg/git/git.go Outdated Show resolved Hide resolved
pkg/git/git_test.go Outdated Show resolved Hide resolved
func validateGitAuth(logger *zap.SugaredLogger, url string) error {
homeenv := os.Getenv("HOME")
sshCred := true
if _, err := os.Stat(homeenv + "/.ssh"); os.IsNotExist(err) {
Copy link

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:

SSH credentials have been provided but the URL %q is not a valid SSH URL. This warning can be safely ignored if the URL is for a public repo or you are using basic auth

@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?

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/git/git.go Do not exist 2.4%

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/git/git.go Do not exist 2.4%

@FogDong
Copy link
Member Author

FogDong commented Sep 11, 2020

/retest

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/git/git.go Do not exist 2.4%

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Thanks @FogDong !

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 14, 2020
@FogDong
Copy link
Member Author

FogDong commented Sep 16, 2020

Can I get a lgtm? /cc @vdemeester 😉

Copy link
Member

@withlin withlin left a comment

Choose a reason for hiding this comment

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

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Sep 22, 2020
@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbwsg, withlin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot merged commit 53f761f into tektoncd:master Sep 22, 2020
@bobcatfish
Copy link
Collaborator

@FogDong @sbwsg can you update the release note with more information about what this change is doing?

@ghost
Copy link

ghost commented Sep 23, 2020

@bobcatfish sure thing; updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If a git secret has ssh-auth type, warn about urls starting with https: and vice versa
5 participants