diff options
author | zeripath <art27@cantab.net> | 2019-10-16 14:42:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-16 14:42:42 +0100 |
commit | fcb535c5c3b6b782d9242028fed4cd8c027c4e41 (patch) | |
tree | 49c49fd1f040b9dcd600ec8e381df80532bc2701 /integrations | |
parent | 1b72690cb82302b24f41d2beaa5df5592709f4d3 (diff) | |
download | gitea-fcb535c5c3b6b782d9242028fed4cd8c027c4e41.tar.gz gitea-fcb535c5c3b6b782d9242028fed4cd8c027c4e41.zip |
Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631)
This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however.
## Features
- [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.)
- [x] Verify commits signed with the default gpg as valid
- [x] Signer, Committer and Author can all be different
- [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon.
- [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available
- Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg
- [x] Try to match the default key with a user on gitea - this is done at verification time
- [x] Make things configurable?
- app.ini configuration done
- [x] when checking commits are signed need to check if they're actually verifiable too
- [x] Add documentation
I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
Diffstat (limited to 'integrations')
-rw-r--r-- | integrations/api_helper_for_declarative_test.go | 35 | ||||
-rw-r--r-- | integrations/api_repo_file_create_test.go | 2 | ||||
-rw-r--r-- | integrations/api_repo_file_update_test.go | 2 | ||||
-rw-r--r-- | integrations/gpg_git_test.go | 252 | ||||
-rw-r--r-- | integrations/mssql.ini.tmpl | 3 | ||||
-rw-r--r-- | integrations/mysql.ini.tmpl | 3 | ||||
-rw-r--r-- | integrations/mysql8.ini.tmpl | 3 | ||||
-rw-r--r-- | integrations/pgsql.ini.tmpl | 3 | ||||
-rw-r--r-- | integrations/repofiles_delete_test.go | 2 | ||||
-rw-r--r-- | integrations/repofiles_update_test.go | 4 | ||||
-rw-r--r-- | integrations/sqlite.ini | 3 |
11 files changed, 307 insertions, 5 deletions
diff --git a/integrations/api_helper_for_declarative_test.go b/integrations/api_helper_for_declarative_test.go index 805a986ae3..cae7691c4b 100644 --- a/integrations/api_helper_for_declarative_test.go +++ b/integrations/api_helper_for_declarative_test.go @@ -231,3 +231,38 @@ func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64) ctx.Session.MakeRequest(t, req, 200) } } + +func doAPIGetBranch(ctx APITestContext, branch string, callback ...func(*testing.T, api.Branch)) func(*testing.T) { + return func(t *testing.T) { + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/branches/%s?token=%s", ctx.Username, ctx.Reponame, branch, ctx.Token) + if ctx.ExpectedCode != 0 { + ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) + return + } + resp := ctx.Session.MakeRequest(t, req, http.StatusOK) + + var branch api.Branch + DecodeJSON(t, resp, &branch) + if len(callback) > 0 { + callback[0](t, branch) + } + } +} + +func doAPICreateFile(ctx APITestContext, treepath string, options *api.CreateFileOptions, callback ...func(*testing.T, api.FileResponse)) func(*testing.T) { + return func(t *testing.T) { + url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", ctx.Username, ctx.Reponame, treepath, ctx.Token) + req := NewRequestWithJSON(t, "POST", url, &options) + if ctx.ExpectedCode != 0 { + ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) + return + } + resp := ctx.Session.MakeRequest(t, req, http.StatusCreated) + + var contents api.FileResponse + DecodeJSON(t, resp, &contents) + if len(callback) > 0 { + callback[0](t, contents) + } + } +} diff --git a/integrations/api_repo_file_create_test.go b/integrations/api_repo_file_create_test.go index 42898bf259..4d76ff00ce 100644 --- a/integrations/api_repo_file_create_test.go +++ b/integrations/api_repo_file_create_test.go @@ -91,7 +91,7 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon }, Verification: &api.PayloadCommitVerification{ Verified: false, - Reason: "unsigned", + Reason: "gpg.error.not_signed_commit", Signature: "", Payload: "", }, diff --git a/integrations/api_repo_file_update_test.go b/integrations/api_repo_file_update_test.go index 366eb5e918..bf695d4344 100644 --- a/integrations/api_repo_file_update_test.go +++ b/integrations/api_repo_file_update_test.go @@ -94,7 +94,7 @@ func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileRespon }, Verification: &api.PayloadCommitVerification{ Verified: false, - Reason: "unsigned", + Reason: "gpg.error.not_signed_commit", Signature: "", Payload: "", }, diff --git a/integrations/gpg_git_test.go b/integrations/gpg_git_test.go new file mode 100644 index 0000000000..12f0a138c7 --- /dev/null +++ b/integrations/gpg_git_test.go @@ -0,0 +1,252 @@ +// 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 ( + "encoding/base64" + "fmt" + "io/ioutil" + "net/url" + "os" + "path/filepath" + "testing" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/process" + "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" + "github.com/stretchr/testify/assert" + "golang.org/x/crypto/openpgp" + "golang.org/x/crypto/openpgp/armor" +) + +func TestGPGGit(t *testing.T) { + onGiteaRun(t, testGPGGit) +} + +func testGPGGit(t *testing.T, u *url.URL) { + username := "user2" + baseAPITestContext := NewAPITestContext(t, username, "repo1") + + u.Path = baseAPITestContext.GitPath() + + // OK Set a new GPG home + tmpDir, err := ioutil.TempDir("", "temp-gpg") + assert.NoError(t, err) + defer os.RemoveAll(tmpDir) + + err = os.Chmod(tmpDir, 0700) + assert.NoError(t, err) + + oldGNUPGHome := os.Getenv("GNUPGHOME") + err = os.Setenv("GNUPGHOME", tmpDir) + assert.NoError(t, err) + defer os.Setenv("GNUPGHOME", oldGNUPGHome) + + // Need to create a root key + rootKeyPair, err := createGPGKey(tmpDir, "gitea", "gitea@fake.local") + assert.NoError(t, err) + + rootKeyID := rootKeyPair.PrimaryKey.KeyIdShortString() + + oldKeyID := setting.Repository.Signing.SigningKey + oldName := setting.Repository.Signing.SigningName + oldEmail := setting.Repository.Signing.SigningEmail + defer func() { + setting.Repository.Signing.SigningKey = oldKeyID + setting.Repository.Signing.SigningName = oldName + setting.Repository.Signing.SigningEmail = oldEmail + }() + + setting.Repository.Signing.SigningKey = rootKeyID + setting.Repository.Signing.SigningName = "gitea" + setting.Repository.Signing.SigningEmail = "gitea@fake.local" + user := models.AssertExistsAndLoadBean(t, &models.User{Name: username}).(*models.User) + + t.Run("Unsigned-Initial", func(t *testing.T) { + PrintCurrentTest(t) + setting.Repository.Signing.InitialCommit = []string{"never"} + testCtx := NewAPITestContext(t, username, "initial-unsigned") + t.Run("CreateRepository", doAPICreateRepository(testCtx, false)) + t.Run("CheckMasterBranchUnsigned", doAPIGetBranch(testCtx, "master", func(t *testing.T, branch api.Branch) { + assert.NotNil(t, branch.Commit) + assert.NotNil(t, branch.Commit.Verification) + assert.False(t, branch.Commit.Verification.Verified) + assert.Empty(t, branch.Commit.Verification.Signature) + })) + setting.Repository.Signing.CRUDActions = []string{"never"} + t.Run("CreateCRUDFile-Never", crudActionCreateFile( + t, testCtx, user, "master", "never", "unsigned-never.txt", func(t *testing.T, response api.FileResponse) { + assert.False(t, response.Verification.Verified) + })) + t.Run("CreateCRUDFile-Never", crudActionCreateFile( + t, testCtx, user, "never", "never2", "unsigned-never2.txt", func(t *testing.T, response api.FileResponse) { + assert.False(t, response.Verification.Verified) + })) + setting.Repository.Signing.CRUDActions = []string{"parentsigned"} + t.Run("CreateCRUDFile-ParentSigned", crudActionCreateFile( + t, testCtx, user, "master", "parentsigned", "signed-parent.txt", func(t *testing.T, response api.FileResponse) { + assert.False(t, response.Verification.Verified) + })) + t.Run("CreateCRUDFile-ParentSigned", crudActionCreateFile( + t, testCtx, user, "parentsigned", "parentsigned2", "signed-parent2.txt", func(t *testing.T, response api.FileResponse) { + assert.False(t, response.Verification.Verified) + })) + setting.Repository.Signing.CRUDActions = []string{"never"} + t.Run("CreateCRUDFile-Never", crudActionCreateFile( + t, testCtx, user, "parentsigned", "parentsigned-never", "unsigned-never2.txt", func(t *testing.T, response api.FileResponse) { + assert.False(t, response.Verification.Verified) + })) + setting.Repository.Signing.CRUDActions = []string{"always"} + t.Run("CreateCRUDFile-Always", crudActionCreateFile( + t, testCtx, user, "master", "always", "signed-always.txt", func(t *testing.T, response api.FileResponse) { + assert.True(t, response.Verification.Verified) + assert.Equal(t, "gitea@fake.local", response.Verification.Signer.Email) + })) + t.Run("CreateCRUDFile-ParentSigned-always", crudActionCreateFile( + t, testCtx, user, "parentsigned", "parentsigned-always", "signed-parent2.txt", func(t *testing.T, response api.FileResponse) { + assert.True(t, response.Verification.Verified) + assert.Equal(t, "gitea@fake.local", response.Verification.Signer.Email) + })) + setting.Repository.Signing.CRUDActions = []string{"parentsigned"} + t.Run("CreateCRUDFile-Always-ParentSigned", crudActionCreateFile( + t, testCtx, user, "always", "always-parentsigned", "signed-always-parentsigned.txt", func(t *testing.T, response api.FileResponse) { + assert.True(t, response.Verification.Verified) + assert.Equal(t, "gitea@fake.local", response.Verification.Signer.Email) + })) + }) + t.Run("AlwaysSign-Initial", func(t *testing.T) { + PrintCurrentTest(t) + setting.Repository.Signing.InitialCommit = []string{"always"} + testCtx := NewAPITestContext(t, username, "initial-always") + t.Run("CreateRepository", doAPICreateRepository(testCtx, false)) + t.Run("CheckMasterBranchSigned", doAPIGetBranch(testCtx, "master", func(t *testing.T, branch api.Branch) { + assert.NotNil(t, branch.Commit) + assert.NotNil(t, branch.Commit.Verification) + assert.True(t, branch.Commit.Verification.Verified) + assert.Equal(t, "gitea@fake.local", branch.Commit.Verification.Signer.Email) + })) + setting.Repository.Signing.CRUDActions = []string{"never"} + t.Run("CreateCRUDFile-Never", crudActionCreateFile( + t, testCtx, user, "master", "never", "unsigned-never.txt", func(t *testing.T, response api.FileResponse) { + assert.False(t, response.Verification.Verified) + })) + setting.Repository.Signing.CRUDActions = []string{"parentsigned"} + t.Run("CreateCRUDFile-ParentSigned", crudActionCreateFile( + t, testCtx, user, "master", "parentsigned", "signed-parent.txt", func(t *testing.T, response api.FileResponse) { + assert.True(t, response.Verification.Verified) + assert.Equal(t, "gitea@fake.local", response.Verification.Signer.Email) + })) + setting.Repository.Signing.CRUDActions = []string{"always"} + t.Run("CreateCRUDFile-Always", crudActionCreateFile( + t, testCtx, user, "master", "always", "signed-always.txt", func(t *testing.T, response api.FileResponse) { + assert.True(t, response.Verification.Verified) + assert.Equal(t, "gitea@fake.local", response.Verification.Signer.Email) + })) + + }) + t.Run("UnsignedMerging", func(t *testing.T) { + PrintCurrentTest(t) + testCtx := NewAPITestContext(t, username, "initial-unsigned") + var pr api.PullRequest + var err error + t.Run("CreatePullRequest", func(t *testing.T) { + pr, err = doAPICreatePullRequest(testCtx, testCtx.Username, testCtx.Reponame, "master", "never2")(t) + assert.NoError(t, err) + }) + setting.Repository.Signing.Merges = []string{"commitssigned"} + t.Run("MergePR", doAPIMergePullRequest(testCtx, testCtx.Username, testCtx.Reponame, pr.Index)) + t.Run("CheckMasterBranchUnsigned", doAPIGetBranch(testCtx, "master", func(t *testing.T, branch api.Branch) { + assert.NotNil(t, branch.Commit) + assert.NotNil(t, branch.Commit.Verification) + assert.False(t, branch.Commit.Verification.Verified) + assert.Empty(t, branch.Commit.Verification.Signature) + })) + setting.Repository.Signing.Merges = []string{"basesigned"} + t.Run("CreatePullRequest", func(t *testing.T) { + pr, err = doAPICreatePullRequest(testCtx, testCtx.Username, testCtx.Reponame, "master", "parentsigned2")(t) + assert.NoError(t, err) + }) + t.Run("MergePR", doAPIMergePullRequest(testCtx, testCtx.Username, testCtx.Reponame, pr.Index)) + t.Run("CheckMasterBranchUnsigned", doAPIGetBranch(testCtx, "master", func(t *testing.T, branch api.Branch) { + assert.NotNil(t, branch.Commit) + assert.NotNil(t, branch.Commit.Verification) + assert.False(t, branch.Commit.Verification.Verified) + assert.Empty(t, branch.Commit.Verification.Signature) + })) + setting.Repository.Signing.Merges = []string{"commitssigned"} + t.Run("CreatePullRequest", func(t *testing.T) { + pr, err = doAPICreatePullRequest(testCtx, testCtx.Username, testCtx.Reponame, "master", "always-parentsigned")(t) + assert.NoError(t, err) + }) + t.Run("MergePR", doAPIMergePullRequest(testCtx, testCtx.Username, testCtx.Reponame, pr.Index)) + t.Run("CheckMasterBranchUnsigned", doAPIGetBranch(testCtx, "master", func(t *testing.T, branch api.Branch) { + assert.NotNil(t, branch.Commit) + assert.NotNil(t, branch.Commit.Verification) + assert.True(t, branch.Commit.Verification.Verified) + })) + + }) +} + +func crudActionCreateFile(t *testing.T, ctx APITestContext, user *models.User, from, to, path string, callback ...func(*testing.T, api.FileResponse)) func(*testing.T) { + return doAPICreateFile(ctx, path, &api.CreateFileOptions{ + FileOptions: api.FileOptions{ + BranchName: from, + NewBranchName: to, + Message: fmt.Sprintf("from:%s to:%s path:%s", from, to, path), + Author: api.Identity{ + Name: user.FullName, + Email: user.Email, + }, + Committer: api.Identity{ + Name: user.FullName, + Email: user.Email, + }, + }, + Content: base64.StdEncoding.EncodeToString([]byte("This is new text")), + }, callback...) +} + +func createGPGKey(tmpDir, name, email string) (*openpgp.Entity, error) { + keyPair, err := openpgp.NewEntity(name, "test", email, nil) + if err != nil { + return nil, err + } + + for _, id := range keyPair.Identities { + err := id.SelfSignature.SignUserId(id.UserId.Id, keyPair.PrimaryKey, keyPair.PrivateKey, nil) + if err != nil { + return nil, err + } + } + + keyFile := filepath.Join(tmpDir, "temporary.key") + keyWriter, err := os.Create(keyFile) + if err != nil { + return nil, err + } + defer keyWriter.Close() + defer os.Remove(keyFile) + + w, err := armor.Encode(keyWriter, openpgp.PrivateKeyType, nil) + if err != nil { + return nil, err + } + defer w.Close() + + keyPair.SerializePrivate(w, nil) + if err := w.Close(); err != nil { + return nil, err + } + if err := keyWriter.Close(); err != nil { + return nil, err + } + + if _, _, err := process.GetManager().Exec("gpg --import temporary.key", "gpg", "--import", keyFile); err != nil { + return nil, err + } + return keyPair, nil +} diff --git a/integrations/mssql.ini.tmpl b/integrations/mssql.ini.tmpl index d38d038a4e..931e923cf4 100644 --- a/integrations/mssql.ini.tmpl +++ b/integrations/mssql.ini.tmpl @@ -21,6 +21,9 @@ ROOT = integrations/gitea-integration-mssql/gitea-repositories LOCAL_COPY_PATH = tmp/local-repo-mssql LOCAL_WIKI_PATH = tmp/local-wiki-mssql +[repository.signing] +SIGNING_KEY = none + [server] SSH_DOMAIN = localhost HTTP_PORT = 3003 diff --git a/integrations/mysql.ini.tmpl b/integrations/mysql.ini.tmpl index 6eed7e1578..4dde212798 100644 --- a/integrations/mysql.ini.tmpl +++ b/integrations/mysql.ini.tmpl @@ -21,6 +21,9 @@ ROOT = integrations/gitea-integration-mysql/gitea-repositories LOCAL_COPY_PATH = tmp/local-repo-mysql LOCAL_WIKI_PATH = tmp/local-wiki-mysql +[repository.signing] +SIGNING_KEY = none + [server] SSH_DOMAIN = localhost HTTP_PORT = 3001 diff --git a/integrations/mysql8.ini.tmpl b/integrations/mysql8.ini.tmpl index 1e14bc1356..1b1d3d2436 100644 --- a/integrations/mysql8.ini.tmpl +++ b/integrations/mysql8.ini.tmpl @@ -21,6 +21,9 @@ ROOT = integrations/gitea-integration-mysql8/gitea-repositories LOCAL_COPY_PATH = tmp/local-repo-mysql8 LOCAL_WIKI_PATH = tmp/local-wiki-mysql8 +[repository.signing] +SIGNING_KEY = none + [server] SSH_DOMAIN = localhost HTTP_PORT = 3004 diff --git a/integrations/pgsql.ini.tmpl b/integrations/pgsql.ini.tmpl index cd5dc44ea8..6265e0d98e 100644 --- a/integrations/pgsql.ini.tmpl +++ b/integrations/pgsql.ini.tmpl @@ -21,6 +21,9 @@ ROOT = integrations/gitea-integration-pgsql/gitea-repositories LOCAL_COPY_PATH = tmp/local-repo-pgsql LOCAL_WIKI_PATH = tmp/local-wiki-pgsql +[repository.signing] +SIGNING_KEY = none + [server] SSH_DOMAIN = localhost HTTP_PORT = 3002 diff --git a/integrations/repofiles_delete_test.go b/integrations/repofiles_delete_test.go index f4cb4510be..b4c535188b 100644 --- a/integrations/repofiles_delete_test.go +++ b/integrations/repofiles_delete_test.go @@ -53,7 +53,7 @@ func getExpectedDeleteFileResponse(u *url.URL) *api.FileResponse { }, Verification: &api.PayloadCommitVerification{ Verified: false, - Reason: "", + Reason: "gpg.error.not_signed_commit", Signature: "", Payload: "", }, diff --git a/integrations/repofiles_update_test.go b/integrations/repofiles_update_test.go index a4ce16d847..c475c70008 100644 --- a/integrations/repofiles_update_test.go +++ b/integrations/repofiles_update_test.go @@ -108,7 +108,7 @@ func getExpectedFileResponseForRepofilesCreate(commitID string) *api.FileRespons }, Verification: &api.PayloadCommitVerification{ Verified: false, - Reason: "unsigned", + Reason: "gpg.error.not_signed_commit", Signature: "", Payload: "", }, @@ -175,7 +175,7 @@ func getExpectedFileResponseForRepofilesUpdate(commitID, filename string) *api.F }, Verification: &api.PayloadCommitVerification{ Verified: false, - Reason: "unsigned", + Reason: "gpg.error.not_signed_commit", Signature: "", Payload: "", }, diff --git a/integrations/sqlite.ini b/integrations/sqlite.ini index b188406ee9..de3355c166 100644 --- a/integrations/sqlite.ini +++ b/integrations/sqlite.ini @@ -17,6 +17,9 @@ ROOT = integrations/gitea-integration-sqlite/gitea-repositories LOCAL_COPY_PATH = tmp/local-repo-sqlite LOCAL_WIKI_PATH = tmp/local-wiki-sqlite +[repository.signing] +SIGNING_KEY = none + [server] SSH_DOMAIN = localhost HTTP_PORT = 3003 |