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

Modified to use Git with PAT #4571

Open
wants to merge 44 commits into
base: master
Choose a base branch
from

Conversation

sZma5a
Copy link

@sZma5a sZma5a commented Aug 27, 2023

What this PR does / why we need it: Modified to use Personal Access Token since currently only SSH can control the Git repository.

Which issue(s) this PR fixes:

Fixes #4106

Does this PR introduce a user-facing change?: Yes

  • How are users affected by this change: Be able to use Personal Access Token setting like this:
apiVersion: pipecd.dev/v1beta1
kind: Piped
spec:
  git:
     personalAccessToken:
        userName: <user-name>
        userToken: <user-token>
  • Is this breaking change: No
  • How to migrate (if breaking change):

@sZma5a
Copy link
Author

sZma5a commented Aug 27, 2023

@kentakozuka

Sorry...
I accidentally closed a previously created PR, but was unable to reopen it, so I created a side...

#4534

@sZma5a sZma5a marked this pull request as ready for review August 27, 2023 16:32
@sZma5a sZma5a force-pushed the feat/add-pat-setting branch 2 times, most recently from 0cbfcc1 to 028e54d Compare August 27, 2023 16:33
@codecov
Copy link

codecov bot commented Aug 27, 2023

Codecov Report

Attention: Patch coverage is 54.16667% with 22 lines in your changes are missing coverage. Please review.

Project coverage is 28.93%. Comparing base (14eb473) to head (87be877).

❗ Current head 87be877 differs from pull request most recent head 1291bab. Consider uploading reports for the commit 1291bab to get more accurate results

Files Patch % Lines
pkg/git/client.go 40.00% 12 Missing and 3 partials ⚠️
pkg/app/piped/cmd/piped/piped.go 0.00% 4 Missing ⚠️
pkg/config/piped.go 84.21% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4571      +/-   ##
==========================================
- Coverage   29.23%   28.93%   -0.30%     
==========================================
  Files         318      317       -1     
  Lines       40597    40413     -184     
==========================================
- Hits        11870    11695     -175     
+ Misses      27787    27786       -1     
+ Partials      940      932       -8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@kentakozuka kentakozuka left a comment

Choose a reason for hiding this comment

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

Thanks! Left a comment. PTAL 👀

Comment on lines 1390 to 1454
func TestPipeGitValidate(t *testing.T) {
t.Parallel()
testcases := []struct {
git PipedGit
err error
}{
{
git: PipedGit{
SSHKeyData: "sshkey1",
PersonalAccessToken: PipedGitPersonalAccessToken{
UserName: "UserName",
UserToken: "UserToken",
},
},
err: errors.New("cannot configure both sshKeyData or sshKeyFile and personalAccessToken"),
},
{
git: PipedGit{
SSHKeyData: "sshkey2",
},
err: nil,
},
{
git: PipedGit{
PersonalAccessToken: PipedGitPersonalAccessToken{
UserName: "UserName",
UserToken: "UserToken",
},
},
err: nil,
},
{
git: PipedGit{ },
err: nil,
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.git.SSHKeyData, func(t *testing.T) {
t.Parallel()
err := tc.git.Validate()
assert.Equal(t, tc.err, err)
})
}
}
Copy link
Member

Choose a reason for hiding this comment

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

IMHO, the test above misses some cases.
Can you copy and paste the test bellow, fix TODOs, and run it?

func TestPipeGitValidate(t *testing.T) {
	t.Parallel()
	testcases := []struct {
		name string
		git  PipedGit
		err  error
	}{
		{
			name: "both SSH and PAT are valid",
			git: PipedGit{
				SSHKeyData: "sshkey1",
				PersonalAccessToken: PipedGitPersonalAccessToken{
					UserName:  "UserName",
					UserToken: "UserToken",
				},
			},
			err: errors.New("cannot configure both sshKeyData or sshKeyFile and personalAccessToken"),
		},
		{
			name: "Both SSH and PAT is not valid",
			git: PipedGit{
				SSHKeyFile: "sshkeyfile",
				SSHKeyData: "sshkeydata",
				PersonalAccessToken: PipedGitPersonalAccessToken{
					UserName:  "",
					UserToken: "UserToken",
				},
			},
			// TODO: should return error
		},
		{
			name: "SSH key data is not empty",
			git: PipedGit{
				SSHKeyData: "sshkey2",
			},
			err: nil,
		},
		{
			name: "SSH key file is not empty",
			git: PipedGit{
				SSHKeyFile: "sshkey2",
			},
			err: nil,
		},
		{
			name: "Both SSH file and data is not empty",
			git: PipedGit{
				SSHKeyData: "sshkeydata",
				SSHKeyFile: "sshkeyfile",
			},
			// TODO: should return error
		},
		{
			name: "PAT is valid",
			git: PipedGit{
				PersonalAccessToken: PipedGitPersonalAccessToken{
					UserName:  "UserName",
					UserToken: "UserToken",
				},
			},
			err: nil,
		},
		{
			name: "PAT username is empty",
			git: PipedGit{
				PersonalAccessToken: PipedGitPersonalAccessToken{
					UserName:  "UserName",
					UserToken: "",
				},
			},
			// TODO: should return error
		},
		{
			name: "PAT token is empty",
			git: PipedGit{
				PersonalAccessToken: PipedGitPersonalAccessToken{
					UserName:  "",
					UserToken: "UserToken",
				},
			},
			// TODO: should return error
		},
		{
			name: "Git config is empty",
			git:  PipedGit{},
			err:  nil,
		},
	}
	for _, tc := range testcases {
		tc := tc
		t.Run(tc.git.SSHKeyData, func(t *testing.T) {
			t.Parallel()
			err := tc.git.Validate()
			assert.Equal(t, tc.err, err)
		})
	}
}

@@ -49,6 +49,13 @@ spec:
| hostName | string | The hostname or IP address of the remote git server. Default is the same value with Host. | No |
| sshKeyFile | string | The path to the private ssh key file. This will be used to clone the source code of the specified git repositories. | No |
| sshKeyData | string | Base64 encoded string of SSH key. | No |
| personalAccessToken | [PipedGitPersonalAccessToken](#gitPersonalAccessToken) | Configuration for personal access token. | No |
Copy link
Member

Choose a reason for hiding this comment

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

Just realized that;
PipeCD supports not only GitHub but also Git services(GitLab, self-host Git server, etc.).
So, it should be how this configuration can be used for other Git services.

How about simply adding password instead of personalAccessToken?
WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

Are you suggesting replacing the Personal Access Token with a password?
Using a password might lead to confusion, so maybe we could stick with some form of access token instead?

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

You are right. It can be confusing for GitHub users. But, IMHO, using the word password should be better because Personal access tokens can be used in GitHub (and GitLab as well), but it is not required for being a Git server. PipeCD supports Git (not GitHub) so it should respect Git terminology.

Copy link
Author

@sZma5a sZma5a Dec 17, 2023

Choose a reason for hiding this comment

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

Since 'Password' overlaps with the variable name, 'PersonalAccessToken' renamed to 'PasswordAuth' and 'userToken' to 'Password'.

Copy link
Author

Choose a reason for hiding this comment

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

@kentakozuka
Sorry I forgot to add a mentions...

Copy link
Member

@ffjlabo ffjlabo Jan 30, 2024

Choose a reason for hiding this comment

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

@sZma5a @kentakozuka
To clarify this discussion, I want to organize your opinions and give my opinion 👀
Feel free to tell me if there is a misunderstanding :)

The point is whether to define a variable only for PAT or not.
For me, each opinion of yours seems to be like below 👀

[@kentakozuka 's opinion]
GitHub's PAT behaves like a password on the git. It's just a password on the git.

[@sZma5a 's opinion]
PAT is a different role from a password. So It's confusing to define it as a Password.

I think both opinions are important points 👍
So I will try to give my opinion after considering those opinions!

[IMO]

Maybe this PR is like a supporting password for git on piped :)

WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

Understood, thank you very much!

Copy link
Author

@sZma5a sZma5a Feb 12, 2024

Choose a reason for hiding this comment

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

@ffjlabo @kentakozuka
Sorry it took so long to fix.
I have modified the PAT settings to use a password, and updated it to work with the existing Git username!
How does this look?

Copy link

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added Stale and removed Stale labels Dec 14, 2023
@sZma5a sZma5a requested a review from t-kikuc as a code owner December 17, 2023 14:43
@sZma5a sZma5a force-pushed the feat/add-pat-setting branch 2 times, most recently from a358cde to ffeea16 Compare December 17, 2023 15:05
鈴木 優耀 and others added 26 commits April 19, 2024 03:05
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: Your Name <you@example.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: swallow <masaaki@haribote-lab.net>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: Your Name <you@example.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: swallow <masaaki@haribote-lab.net>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: Your Name <you@example.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: swallow <masaaki@haribote-lab.net>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: Your Name <you@example.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: swallow <masaaki@haribote-lab.net>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: swallow <masaaki@haribote-lab.net>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
authentication instead of personal access token

Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
…ation in git client

Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: 鈴木 優耀 <suzuki_masaaki@cyberagent.co.jp>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
…ction

Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
…on-reference.md

Co-authored-by: Yoshiki Fujikane <40124947+ffjlabo@users.noreply.github.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Co-authored-by: Yoshiki Fujikane <40124947+ffjlabo@users.noreply.github.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
Signed-off-by: sZma5a <masaaki.haribote@gmail.com>
@ffjlabo
Copy link
Member

ffjlabo commented Apr 19, 2024

@sZma5a Thank you for the fix! I'm checking 👍

Comment on lines +146 to +153
authArgs := []string{}
if c.username != "" && c.password != "" {
token := fmt.Sprintf("%s:%s", c.username, c.password)
encodedToken := base64.StdEncoding.EncodeToString([]byte(token))
header := fmt.Sprintf("Authorization: Basic %s", encodedToken)
authArgs = append(authArgs, "-c", fmt.Sprintf("http.extraHeader=%s", header))
}

Copy link
Member

@ffjlabo ffjlabo Apr 22, 2024

Choose a reason for hiding this comment

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

At first, thank you for investigating and the fix 🙏 I don't know such an option! It's so helpful 👍
noted: https://git-scm.com/docs/git-config#Documentation/git-config.txt-httpextraHeader

I tried the expected command with a private repo git -c http.extraHeader="Authorization: Basic <base64 encoded value of username:password>" clone https://github.com/ffjlabo-playground/git-test.git on mac.
I encountered the behaviors written below. Does below also reproduce on your machine? Could you try it?

  • It shows the prompts for username and password like this↓
% git -c http.extraHeader="Authorization: Basic <base64 encoded value of username:password>" clone https://github.com/ffjlabo-playground/git-test.git
Cloning into 'git-test'...
Username for 'https://github.com':
  • When I input the username and password as empty, then It shows the auth error like this↓
% git -c http.extraHeader="Authorization: Basic <base64 encoded value of username:password>" clone https://github.com/ffjlabo-playground/git-test.git
Cloning into 'git-test'...
Username for 'https://github.com':
Password for 'https://github.com':
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/ffjlabo-playground/git-test.git/'

Copy link
Author

Choose a reason for hiding this comment

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

Sorry...
It worked when we looked into it before, but we will look into it again and verify.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you! Don't worry! I think this is a challenging. I'm so helpful.

Copy link
Member

Choose a reason for hiding this comment

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

Hi @sZma5a ! Do you have any updates on the above? If you want some helps, feel free to ping me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the way to access git repository by using Personal Access Token(PAT)
4 participants