-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Set PusherName and PusherID to owner on deploy key to fix pushing with deploy keys #5935
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
Merged
zeripath
merged 7 commits into
go-gitea:master
from
zeripath:issue-3795-deploy-key-settings
Feb 3, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d4058a
Set PusherName and PusherID to owner on deploy key
zeripath bc2b6ad
Add deploy key push tests
zeripath f533b3e
use repo.Owner.Name instead of "username"
zeripath 9648d87
Push to origin master
zeripath efa0e9b
Revert change to username - owner not loaded
zeripath 34aedc8
Fix integration test
zeripath e5c5094
Add FIXME note
zeripath 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 hidden or 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 hidden or 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,160 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"code.gitea.io/git" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/sdk/gitea" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func createEmptyRepository(username, reponame string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
session := loginUser(t, username) | ||
token := getTokenForLoggedInUser(t, session) | ||
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+token, &api.CreateRepoOption{ | ||
AutoInit: false, | ||
Description: "Temporary empty repo", | ||
Name: reponame, | ||
Private: false, | ||
}) | ||
session.MakeRequest(t, req, http.StatusCreated) | ||
} | ||
} | ||
|
||
func createDeployKey(username, reponame, keyname, keyFile string, readOnly bool) func(*testing.T) { | ||
return func(t *testing.T) { | ||
session := loginUser(t, username) | ||
token := getTokenForLoggedInUser(t, session) | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", username, reponame, token) | ||
|
||
dataPubKey, err := ioutil.ReadFile(keyFile + ".pub") | ||
assert.NoError(t, err) | ||
req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{ | ||
Title: keyname, | ||
Key: string(dataPubKey), | ||
ReadOnly: readOnly, | ||
}) | ||
session.MakeRequest(t, req, http.StatusCreated) | ||
} | ||
} | ||
|
||
func initTestRepository(dstPath string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
// Init repository in dstPath | ||
assert.NoError(t, git.InitRepository(dstPath, false)) | ||
assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644)) | ||
assert.NoError(t, git.AddChanges(dstPath, true)) | ||
signature := git.Signature{ | ||
Email: "[email protected]", | ||
Name: "test", | ||
When: time.Now(), | ||
} | ||
assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{ | ||
Committer: &signature, | ||
Author: &signature, | ||
Message: "Initial Commit", | ||
})) | ||
} | ||
} | ||
|
||
func pushTestRepository(dstPath, username, reponame string, u url.URL, keyFile string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
//Setup remote link | ||
u.Scheme = "ssh" | ||
u.User = url.User("git") | ||
u.Host = fmt.Sprintf("%s:%d", setting.SSH.ListenHost, setting.SSH.ListenPort) | ||
|
||
//Setup ssh wrapper | ||
os.Setenv("GIT_SSH_COMMAND", | ||
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "+ | ||
filepath.Join(setting.AppWorkPath, keyFile)) | ||
os.Setenv("GIT_SSH_VARIANT", "ssh") | ||
|
||
log.Printf("Adding remote: %s\n", u.String()) | ||
_, err := git.NewCommand("remote", "add", "origin", u.String()).RunInDir(dstPath) | ||
assert.NoError(t, err) | ||
|
||
log.Printf("Pushing to: %s\n", u.String()) | ||
_, err = git.NewCommand("push", "-u", "origin", "master").RunInDir(dstPath) | ||
assert.NoError(t, err) | ||
} | ||
} | ||
|
||
func checkRepositoryEmptyStatus(username, reponame string, isEmpty bool) func(*testing.T) { | ||
return func(t *testing.T) { | ||
session := loginUser(t, username) | ||
token := getTokenForLoggedInUser(t, session) | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", username, reponame, token) | ||
|
||
req := NewRequest(t, "GET", urlStr) | ||
resp := session.MakeRequest(t, req, http.StatusOK) | ||
|
||
var repository api.Repository | ||
DecodeJSON(t, resp, &repository) | ||
|
||
assert.Equal(t, isEmpty, repository.Empty) | ||
} | ||
} | ||
|
||
func deleteRepository(username, reponame string) func(*testing.T) { | ||
return func(t *testing.T) { | ||
session := loginUser(t, username) | ||
token := getTokenForLoggedInUser(t, session) | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", username, reponame, token) | ||
|
||
req := NewRequest(t, "DELETE", urlStr) | ||
session.MakeRequest(t, req, http.StatusNoContent) | ||
} | ||
} | ||
|
||
func TestPushDeployKeyOnEmptyRepo(t *testing.T) { | ||
onGiteaRun(t, testPushDeployKeyOnEmptyRepo) | ||
} | ||
|
||
func testPushDeployKeyOnEmptyRepo(t *testing.T, u *url.URL) { | ||
reponame := "deploy-key-empty-repo-1" | ||
username := "user2" | ||
u.Path = fmt.Sprintf("%s/%s.git", username, reponame) | ||
keyname := fmt.Sprintf("%s-push", reponame) | ||
|
||
t.Run("CreateEmptyRepository", createEmptyRepository(username, reponame)) | ||
t.Run("CheckIsEmpty", checkRepositoryEmptyStatus(username, reponame, true)) | ||
|
||
//Setup the push deploy key file | ||
keyFile := filepath.Join(setting.AppDataPath, keyname) | ||
err := exec.Command("ssh-keygen", "-f", keyFile, "-t", "rsa", "-N", "").Run() | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(keyFile) | ||
defer os.RemoveAll(keyFile + ".pub") | ||
|
||
t.Run("CreatePushDeployKey", createDeployKey(username, reponame, keyname, keyFile, false)) | ||
|
||
// Setup the testing repository | ||
dstPath, err := ioutil.TempDir("", "repo-tmp-deploy-key-empty-repo-1") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(dstPath) | ||
|
||
t.Run("InitTestRepository", initTestRepository(dstPath)) | ||
t.Run("SSHPushTestRepository", pushTestRepository(dstPath, username, reponame, *u, keyFile)) | ||
|
||
log.Println("Done Push") | ||
t.Run("CheckIsNotEmpty", checkRepositoryEmptyStatus(username, reponame, false)) | ||
|
||
t.Run("DeleteRepository", deleteRepository(username, reponame)) | ||
} |
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.
What about using
repo.Owner.NameToLower
(I think), just for consistency?Uh oh!
There was an error while loading. Please reload this page.
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.
username
isrepo.Owner.NameToLower
- it's the lowercase'd repo username that's provided to gitea serv.In the case of the user pushes we use user.Name for this so we probably shouldn't be lowercasing at all, and in fact we should probably change this so it's clear that the "user" pushing this is a deploy key not the owner of the repository at all. However, this is the minimal change that works when previously we didn't have a working system at all previously.
I have added a FIXME note to mark that further thought is needed here.