summaryrefslogtreecommitdiffstats
path: root/integrations/git_helper_for_declarative_test.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-02-03 23:56:53 +0000
committerGitHub <noreply@github.com>2019-02-03 23:56:53 +0000
commit01c10a951b9db0b9f020ef657ca71eb5e5882a84 (patch)
tree6cb29db94edda9d90e9a509028bada2a61ade928 /integrations/git_helper_for_declarative_test.go
parent634cbaad2b8d091a2a3065de81e0b2b76daa5ef7 (diff)
downloadgitea-01c10a951b9db0b9f020ef657ca71eb5e5882a84.tar.gz
gitea-01c10a951b9db0b9f020ef657ca71eb5e5882a84.zip
Fix ssh deploy and user key constraints (#1357) (#5939)
1. A key can either be an ssh user key or a deploy key. It cannot be both. 2. If a key is a user key - it can only be associated with one user. 3. If a key is a deploy key - it can be used in multiple repositories and the permissions it has on those repositories can be different. 4. If a repository is deleted, its deploy keys must be deleted too. We currently don't enforce any of this and multiple repositories access with different permissions doesn't work at all. This PR enforces the following constraints: - [x] You should not be able to add the same user key as another user - [x] You should not be able to add a ssh user key which is being used as a deploy key - [x] You should not be able to add a ssh deploy key which is being used as a user key - [x] If you add an ssh deploy key to another repository you should be able to use it in different modes without losing the ability to use it in the other mode. - [x] If you delete a repository you must delete all its deploy keys. Fix #1357
Diffstat (limited to 'integrations/git_helper_for_declarative_test.go')
-rw-r--r--integrations/git_helper_for_declarative_test.go127
1 files changed, 127 insertions, 0 deletions
diff --git a/integrations/git_helper_for_declarative_test.go b/integrations/git_helper_for_declarative_test.go
new file mode 100644
index 0000000000..572abe95a2
--- /dev/null
+++ b/integrations/git_helper_for_declarative_test.go
@@ -0,0 +1,127 @@
+// 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 (
+ "context"
+ "fmt"
+ "io/ioutil"
+ "net"
+ "net/http"
+ "net/url"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "code.gitea.io/git"
+ "code.gitea.io/gitea/modules/setting"
+ "github.com/Unknwon/com"
+ "github.com/stretchr/testify/assert"
+)
+
+func withKeyFile(t *testing.T, keyname string, callback func(string)) {
+ keyFile := filepath.Join(setting.AppDataPath, keyname)
+ err := exec.Command("ssh-keygen", "-f", keyFile, "-t", "rsa", "-N", "").Run()
+ assert.NoError(t, err)
+
+ //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")
+
+ callback(keyFile)
+
+ defer os.RemoveAll(keyFile)
+ defer os.RemoveAll(keyFile + ".pub")
+}
+
+func createSSHUrl(gitPath string, u *url.URL) *url.URL {
+ u2 := *u
+ u2.Scheme = "ssh"
+ u2.User = url.User("git")
+ u2.Host = fmt.Sprintf("%s:%d", setting.SSH.ListenHost, setting.SSH.ListenPort)
+ u2.Path = gitPath
+ return &u2
+}
+
+func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
+ prepareTestEnv(t)
+ s := http.Server{
+ Handler: mac,
+ }
+
+ u, err := url.Parse(setting.AppURL)
+ assert.NoError(t, err)
+ listener, err := net.Listen("tcp", u.Host)
+ assert.NoError(t, err)
+
+ defer func() {
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
+ s.Shutdown(ctx)
+ cancel()
+ }()
+
+ go s.Serve(listener)
+ //Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
+
+ callback(t, u)
+}
+
+func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
+ return func(t *testing.T) {
+ assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
+ assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
+ }
+}
+
+func doGitCloneFail(dstLocalPath string, u *url.URL) func(*testing.T) {
+ return func(t *testing.T) {
+ assert.Error(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
+ assert.False(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
+ }
+}
+
+func doGitInitTestRepository(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: "test@example.com",
+ Name: "test",
+ When: time.Now(),
+ }
+ assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
+ Committer: &signature,
+ Author: &signature,
+ Message: "Initial Commit",
+ }))
+ }
+}
+
+func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
+ return func(t *testing.T) {
+ _, err := git.NewCommand("remote", "add", remoteName, u.String()).RunInDir(dstPath)
+ assert.NoError(t, err)
+ }
+}
+
+func doGitPushTestRepository(dstPath, remoteName, branch string) func(*testing.T) {
+ return func(t *testing.T) {
+ _, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
+ assert.NoError(t, err)
+ }
+}
+
+func doGitPushTestRepositoryFail(dstPath, remoteName, branch string) func(*testing.T) {
+ return func(t *testing.T) {
+ _, err := git.NewCommand("push", "-u", remoteName, branch).RunInDir(dstPath)
+ assert.Error(t, err)
+ }
+}