You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_gpg.go 1.8KB

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.
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/modules/process"
  10. )
  11. // LoadPublicKeyContent will load the key from gpg
  12. func (gpgSettings *GPGSettings) LoadPublicKeyContent() error {
  13. content, stderr, err := process.GetManager().Exec(
  14. "gpg -a --export",
  15. "gpg", "-a", "--export", gpgSettings.KeyID)
  16. if err != nil {
  17. return fmt.Errorf("Unable to get default signing key: %s, %s, %v", gpgSettings.KeyID, stderr, err)
  18. }
  19. gpgSettings.PublicKeyContent = content
  20. return nil
  21. }
  22. // GetDefaultPublicGPGKey will return and cache the default public GPG settings for this repository
  23. func (repo *Repository) GetDefaultPublicGPGKey(forceUpdate bool) (*GPGSettings, error) {
  24. if repo.gpgSettings != nil && !forceUpdate {
  25. return repo.gpgSettings, nil
  26. }
  27. gpgSettings := &GPGSettings{
  28. Sign: true,
  29. }
  30. value, _ := NewCommand("config", "--get", "commit.gpgsign").RunInDir(repo.Path)
  31. sign, valid := ParseBool(strings.TrimSpace(value))
  32. if !sign || !valid {
  33. gpgSettings.Sign = false
  34. repo.gpgSettings = gpgSettings
  35. return gpgSettings, nil
  36. }
  37. signingKey, _ := NewCommand("config", "--get", "user.signingkey").RunInDir(repo.Path)
  38. gpgSettings.KeyID = strings.TrimSpace(signingKey)
  39. defaultEmail, _ := NewCommand("config", "--get", "user.email").RunInDir(repo.Path)
  40. gpgSettings.Email = strings.TrimSpace(defaultEmail)
  41. defaultName, _ := NewCommand("config", "--get", "user.name").RunInDir(repo.Path)
  42. gpgSettings.Name = strings.TrimSpace(defaultName)
  43. if err := gpgSettings.LoadPublicKeyContent(); err != nil {
  44. return nil, err
  45. }
  46. repo.gpgSettings = gpgSettings
  47. return repo.gpgSettings, nil
  48. }