Skip to content

Commit 4876976

Browse files
committed
Merge remote-tracking branch 'upstream/release/v1.7' into develop
2 parents 73aa7f9 + d269179 commit 4876976

File tree

15 files changed

+722
-189
lines changed

15 files changed

+722
-189
lines changed

cmd/serv.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func checkLFSVersion() {
7070
}
7171

7272
func setup(logPath string) {
73+
log.DelLogger("console")
7374
setting.NewContext()
7475
checkLFSVersion()
7576
log.NewGitLogger(filepath.Join(setting.LogRootPath, logPath))
@@ -233,23 +234,30 @@ func runServ(c *cli.Context) error {
233234

234235
// Check deploy key or user key.
235236
if key.Type == models.KeyTypeDeploy {
236-
if key.Mode < requestedMode {
237-
fail("Key permission denied", "Cannot push with deployment key: %d", key.ID)
238-
}
239-
240-
// Check if this deploy key belongs to current repository.
241-
has, err := private.HasDeployKey(key.ID, repo.ID)
237+
// Now we have to get the deploy key for this repo
238+
deployKey, err := private.GetDeployKey(key.ID, repo.ID)
242239
if err != nil {
243240
fail("Key access denied", "Failed to access internal api: [key_id: %d, repo_id: %d]", key.ID, repo.ID)
244241
}
245-
if !has {
242+
243+
if deployKey == nil {
246244
fail("Key access denied", "Deploy key access denied: [key_id: %d, repo_id: %d]", key.ID, repo.ID)
247245
}
248246

247+
if deployKey.Mode < requestedMode {
248+
fail("Key permission denied", "Cannot push with read-only deployment key: %d to repo_id: %d", key.ID, repo.ID)
249+
}
250+
249251
// Update deploy key activity.
250252
if err = private.UpdateDeployKeyUpdated(key.ID, repo.ID); err != nil {
251253
fail("Internal error", "UpdateDeployKey: %v", err)
252254
}
255+
256+
// FIXME: Deploy keys aren't really the owner of the repo pushing changes
257+
// however we don't have good way of representing deploy keys in hook.go
258+
// so for now use the owner
259+
os.Setenv(models.EnvPusherName, username)
260+
os.Setenv(models.EnvPusherID, fmt.Sprintf("%d", repo.OwnerID))
253261
} else {
254262
user, err = private.GetUserByKeyID(key.ID)
255263
if err != nil {
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"fmt"
9+
"io/ioutil"
10+
"net/http"
11+
"testing"
12+
13+
api "code.gitea.io/sdk/gitea"
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
type APITestContext struct {
18+
Reponame string
19+
Session *TestSession
20+
Token string
21+
Username string
22+
ExpectedCode int
23+
}
24+
25+
func NewAPITestContext(t *testing.T, username, reponame string) APITestContext {
26+
session := loginUser(t, username)
27+
token := getTokenForLoggedInUser(t, session)
28+
return APITestContext{
29+
Session: session,
30+
Token: token,
31+
Username: username,
32+
Reponame: reponame,
33+
}
34+
}
35+
36+
func (ctx APITestContext) GitPath() string {
37+
return fmt.Sprintf("%s/%s.git", ctx.Username, ctx.Reponame)
38+
}
39+
40+
func doAPICreateRepository(ctx APITestContext, empty bool, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
41+
return func(t *testing.T) {
42+
createRepoOption := &api.CreateRepoOption{
43+
AutoInit: !empty,
44+
Description: "Temporary repo",
45+
Name: ctx.Reponame,
46+
Private: true,
47+
Gitignores: "",
48+
License: "WTFPL",
49+
Readme: "Default",
50+
}
51+
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos?token="+ctx.Token, createRepoOption)
52+
if ctx.ExpectedCode != 0 {
53+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
54+
return
55+
}
56+
resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
57+
58+
var repository api.Repository
59+
DecodeJSON(t, resp, &repository)
60+
if len(callback) > 0 {
61+
callback[0](t, repository)
62+
}
63+
}
64+
}
65+
66+
func doAPIGetRepository(ctx APITestContext, callback ...func(*testing.T, api.Repository)) func(*testing.T) {
67+
return func(t *testing.T) {
68+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
69+
70+
req := NewRequest(t, "GET", urlStr)
71+
if ctx.ExpectedCode != 0 {
72+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
73+
return
74+
}
75+
resp := ctx.Session.MakeRequest(t, req, http.StatusOK)
76+
77+
var repository api.Repository
78+
DecodeJSON(t, resp, &repository)
79+
if len(callback) > 0 {
80+
callback[0](t, repository)
81+
}
82+
}
83+
}
84+
85+
func doAPIDeleteRepository(ctx APITestContext) func(*testing.T) {
86+
return func(t *testing.T) {
87+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
88+
89+
req := NewRequest(t, "DELETE", urlStr)
90+
if ctx.ExpectedCode != 0 {
91+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
92+
return
93+
}
94+
ctx.Session.MakeRequest(t, req, http.StatusNoContent)
95+
}
96+
}
97+
98+
func doAPICreateUserKey(ctx APITestContext, keyname, keyFile string, callback ...func(*testing.T, api.PublicKey)) func(*testing.T) {
99+
return func(t *testing.T) {
100+
urlStr := fmt.Sprintf("/api/v1/user/keys?token=%s", ctx.Token)
101+
102+
dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
103+
assert.NoError(t, err)
104+
req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateKeyOption{
105+
Title: keyname,
106+
Key: string(dataPubKey),
107+
})
108+
if ctx.ExpectedCode != 0 {
109+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
110+
return
111+
}
112+
resp := ctx.Session.MakeRequest(t, req, http.StatusCreated)
113+
var publicKey api.PublicKey
114+
DecodeJSON(t, resp, &publicKey)
115+
if len(callback) > 0 {
116+
callback[0](t, publicKey)
117+
}
118+
}
119+
}
120+
121+
func doAPIDeleteUserKey(ctx APITestContext, keyID int64) func(*testing.T) {
122+
return func(t *testing.T) {
123+
urlStr := fmt.Sprintf("/api/v1/user/keys/%d?token=%s", keyID, ctx.Token)
124+
125+
req := NewRequest(t, "DELETE", urlStr)
126+
if ctx.ExpectedCode != 0 {
127+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
128+
return
129+
}
130+
ctx.Session.MakeRequest(t, req, http.StatusNoContent)
131+
}
132+
}
133+
134+
func doAPICreateDeployKey(ctx APITestContext, keyname, keyFile string, readOnly bool) func(*testing.T) {
135+
return func(t *testing.T) {
136+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", ctx.Username, ctx.Reponame, ctx.Token)
137+
138+
dataPubKey, err := ioutil.ReadFile(keyFile + ".pub")
139+
assert.NoError(t, err)
140+
req := NewRequestWithJSON(t, "POST", urlStr, api.CreateKeyOption{
141+
Title: keyname,
142+
Key: string(dataPubKey),
143+
ReadOnly: readOnly,
144+
})
145+
146+
if ctx.ExpectedCode != 0 {
147+
ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
148+
return
149+
}
150+
ctx.Session.MakeRequest(t, req, http.StatusCreated)
151+
}
152+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"io/ioutil"
11+
"net"
12+
"net/http"
13+
"net/url"
14+
"os"
15+
"os/exec"
16+
"path/filepath"
17+
"testing"
18+
"time"
19+
20+
"code.gitea.io/git"
21+
"code.gitea.io/gitea/modules/setting"
22+
"github.com/Unknwon/com"
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func withKeyFile(t *testing.T, keyname string, callback func(string)) {
27+
keyFile := filepath.Join(setting.AppDataPath, keyname)
28+
err := exec.Command("ssh-keygen", "-f", keyFile, "-t", "rsa", "-N", "").Run()
29+
assert.NoError(t, err)
30+
31+
//Setup ssh wrapper
32+
os.Setenv("GIT_SSH_COMMAND",
33+
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "+
34+
filepath.Join(setting.AppWorkPath, keyFile))
35+
os.Setenv("GIT_SSH_VARIANT", "ssh")
36+
37+
callback(keyFile)
38+
39+
defer os.RemoveAll(keyFile)
40+
defer os.RemoveAll(keyFile + ".pub")
41+
}
42+
43+
func createSSHUrl(gitPath string, u *url.URL) *url.URL {
44+
u2 := *u
45+
u2.Scheme = "ssh"
46+
u2.User = url.User("git")
47+
u2.Host = fmt.Sprintf("%s:%d", setting.SSH.ListenHost, setting.SSH.ListenPort)
48+
u2.Path = gitPath
49+
return &u2
50+
}
51+
52+
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
53+
prepareTestEnv(t)
54+
s := http.Server{
55+
Handler: mac,
56+
}
57+
58+
u, err := url.Parse(setting.AppURL)
59+
assert.NoError(t, err)
60+
listener, err := net.Listen("tcp", u.Host)
61+
assert.NoError(t, err)
62+
63+
defer func() {
64+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
65+
s.Shutdown(ctx)
66+
cancel()
67+
}()
68+
69+
go s.Serve(listener)
70+
//Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
71+
72+
callback(t, u)
73+
}
74+
75+
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
76+
return func(t *testing.T) {
77+
assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
78+
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
79+
}
80+
}
81+
82+
func doGitCloneFail(dstLocalPath string, u *url.URL) func(*testing.T) {
83+
return func(t *testing.T) {
84+
assert.Error(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
85+
assert.False(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
86+
}
87+
}
88+
89+
func doGitInitTestRepository(dstPath string) func(*testing.T) {
90+
return func(t *testing.T) {
91+
// Init repository in dstPath
92+
assert.NoError(t, git.InitRepository(dstPath, false))
93+
assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
94+
assert.NoError(t, git.AddChanges(dstPath, true))
95+
signature := git.Signature{
96+
97+
Name: "test",
98+
When: time.Now(),
99+
}
100+
assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
101+
Committer: &signature,
102+
Author: &signature,
103+
Message: "Initial Commit",
104+
}))
105+
}
106+
}
107+
108+
func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
109+
return func(t *testing.T) {
110+
_, err := git.NewCommand("remote", "add", remoteName, u.String()).RunInDir(dstPath)
111+
assert.NoError(t, err)
112+
}
113+
}
114+
115+
func doGitPushTestRepository(dstPath, remoteName, branch string) func(*testing.T) {
116+
return func(t *testing.T) {
117+
_, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
118+
assert.NoError(t, err)
119+
}
120+
}
121+
122+
func doGitPushTestRepositoryFail(dstPath, remoteName, branch string) func(*testing.T) {
123+
return func(t *testing.T) {
124+
_, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
125+
assert.Error(t, err)
126+
}
127+
}

0 commit comments

Comments
 (0)