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_sign.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. package models
  5. import (
  6. "strings"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/process"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. type signingMode string
  13. const (
  14. never signingMode = "never"
  15. always signingMode = "always"
  16. pubkey signingMode = "pubkey"
  17. twofa signingMode = "twofa"
  18. parentSigned signingMode = "parentsigned"
  19. baseSigned signingMode = "basesigned"
  20. headSigned signingMode = "headsigned"
  21. commitsSigned signingMode = "commitssigned"
  22. approved signingMode = "approved"
  23. noKey signingMode = "nokey"
  24. )
  25. func signingModeFromStrings(modeStrings []string) []signingMode {
  26. returnable := make([]signingMode, 0, len(modeStrings))
  27. for _, mode := range modeStrings {
  28. signMode := signingMode(strings.ToLower(strings.TrimSpace(mode)))
  29. switch signMode {
  30. case never:
  31. return []signingMode{never}
  32. case always:
  33. return []signingMode{always}
  34. case pubkey:
  35. fallthrough
  36. case twofa:
  37. fallthrough
  38. case parentSigned:
  39. fallthrough
  40. case baseSigned:
  41. fallthrough
  42. case headSigned:
  43. fallthrough
  44. case approved:
  45. fallthrough
  46. case commitsSigned:
  47. returnable = append(returnable, signMode)
  48. }
  49. }
  50. if len(returnable) == 0 {
  51. return []signingMode{never}
  52. }
  53. return returnable
  54. }
  55. // SigningKey returns the KeyID and git Signature for the repo
  56. func SigningKey(repoPath string) (string, *git.Signature) {
  57. if setting.Repository.Signing.SigningKey == "none" {
  58. return "", nil
  59. }
  60. if setting.Repository.Signing.SigningKey == "default" || setting.Repository.Signing.SigningKey == "" {
  61. // Can ignore the error here as it means that commit.gpgsign is not set
  62. value, _ := git.NewCommand("config", "--get", "commit.gpgsign").RunInDir(repoPath)
  63. sign, valid := git.ParseBool(strings.TrimSpace(value))
  64. if !sign || !valid {
  65. return "", nil
  66. }
  67. signingKey, _ := git.NewCommand("config", "--get", "user.signingkey").RunInDir(repoPath)
  68. signingName, _ := git.NewCommand("config", "--get", "user.name").RunInDir(repoPath)
  69. signingEmail, _ := git.NewCommand("config", "--get", "user.email").RunInDir(repoPath)
  70. return strings.TrimSpace(signingKey), &git.Signature{
  71. Name: strings.TrimSpace(signingName),
  72. Email: strings.TrimSpace(signingEmail),
  73. }
  74. }
  75. return setting.Repository.Signing.SigningKey, &git.Signature{
  76. Name: setting.Repository.Signing.SigningName,
  77. Email: setting.Repository.Signing.SigningEmail,
  78. }
  79. }
  80. // PublicSigningKey gets the public signing key within a provided repository directory
  81. func PublicSigningKey(repoPath string) (string, error) {
  82. signingKey, _ := SigningKey(repoPath)
  83. if signingKey == "" {
  84. return "", nil
  85. }
  86. content, stderr, err := process.GetManager().ExecDir(-1, repoPath,
  87. "gpg --export -a", "gpg", "--export", "-a", signingKey)
  88. if err != nil {
  89. log.Error("Unable to get default signing key in %s: %s, %s, %v", repoPath, signingKey, stderr, err)
  90. return "", err
  91. }
  92. return content, nil
  93. }
  94. // SignInitialCommit determines if we should sign the initial commit to this repository
  95. func SignInitialCommit(repoPath string, u *User) (bool, string, *git.Signature, error) {
  96. rules := signingModeFromStrings(setting.Repository.Signing.InitialCommit)
  97. signingKey, sig := SigningKey(repoPath)
  98. if signingKey == "" {
  99. return false, "", nil, &ErrWontSign{noKey}
  100. }
  101. Loop:
  102. for _, rule := range rules {
  103. switch rule {
  104. case never:
  105. return false, "", nil, &ErrWontSign{never}
  106. case always:
  107. break Loop
  108. case pubkey:
  109. keys, err := ListGPGKeys(u.ID, ListOptions{})
  110. if err != nil {
  111. return false, "", nil, err
  112. }
  113. if len(keys) == 0 {
  114. return false, "", nil, &ErrWontSign{pubkey}
  115. }
  116. case twofa:
  117. twofaModel, err := GetTwoFactorByUID(u.ID)
  118. if err != nil && !IsErrTwoFactorNotEnrolled(err) {
  119. return false, "", nil, err
  120. }
  121. if twofaModel == nil {
  122. return false, "", nil, &ErrWontSign{twofa}
  123. }
  124. }
  125. }
  126. return true, signingKey, sig, nil
  127. }
  128. // SignWikiCommit determines if we should sign the commits to this repository wiki
  129. func (repo *Repository) SignWikiCommit(u *User) (bool, string, *git.Signature, error) {
  130. rules := signingModeFromStrings(setting.Repository.Signing.Wiki)
  131. signingKey, sig := SigningKey(repo.WikiPath())
  132. if signingKey == "" {
  133. return false, "", nil, &ErrWontSign{noKey}
  134. }
  135. Loop:
  136. for _, rule := range rules {
  137. switch rule {
  138. case never:
  139. return false, "", nil, &ErrWontSign{never}
  140. case always:
  141. break Loop
  142. case pubkey:
  143. keys, err := ListGPGKeys(u.ID, ListOptions{})
  144. if err != nil {
  145. return false, "", nil, err
  146. }
  147. if len(keys) == 0 {
  148. return false, "", nil, &ErrWontSign{pubkey}
  149. }
  150. case twofa:
  151. twofaModel, err := GetTwoFactorByUID(u.ID)
  152. if err != nil && !IsErrTwoFactorNotEnrolled(err) {
  153. return false, "", nil, err
  154. }
  155. if twofaModel == nil {
  156. return false, "", nil, &ErrWontSign{twofa}
  157. }
  158. case parentSigned:
  159. gitRepo, err := git.OpenRepository(repo.WikiPath())
  160. if err != nil {
  161. return false, "", nil, err
  162. }
  163. defer gitRepo.Close()
  164. commit, err := gitRepo.GetCommit("HEAD")
  165. if err != nil {
  166. return false, "", nil, err
  167. }
  168. if commit.Signature == nil {
  169. return false, "", nil, &ErrWontSign{parentSigned}
  170. }
  171. verification := ParseCommitWithSignature(commit)
  172. if !verification.Verified {
  173. return false, "", nil, &ErrWontSign{parentSigned}
  174. }
  175. }
  176. }
  177. return true, signingKey, sig, nil
  178. }
  179. // SignCRUDAction determines if we should sign a CRUD commit to this repository
  180. func (repo *Repository) SignCRUDAction(u *User, tmpBasePath, parentCommit string) (bool, string, *git.Signature, error) {
  181. rules := signingModeFromStrings(setting.Repository.Signing.CRUDActions)
  182. signingKey, sig := SigningKey(repo.RepoPath())
  183. if signingKey == "" {
  184. return false, "", nil, &ErrWontSign{noKey}
  185. }
  186. Loop:
  187. for _, rule := range rules {
  188. switch rule {
  189. case never:
  190. return false, "", nil, &ErrWontSign{never}
  191. case always:
  192. break Loop
  193. case pubkey:
  194. keys, err := ListGPGKeys(u.ID, ListOptions{})
  195. if err != nil {
  196. return false, "", nil, err
  197. }
  198. if len(keys) == 0 {
  199. return false, "", nil, &ErrWontSign{pubkey}
  200. }
  201. case twofa:
  202. twofaModel, err := GetTwoFactorByUID(u.ID)
  203. if err != nil && !IsErrTwoFactorNotEnrolled(err) {
  204. return false, "", nil, err
  205. }
  206. if twofaModel == nil {
  207. return false, "", nil, &ErrWontSign{twofa}
  208. }
  209. case parentSigned:
  210. gitRepo, err := git.OpenRepository(tmpBasePath)
  211. if err != nil {
  212. return false, "", nil, err
  213. }
  214. defer gitRepo.Close()
  215. commit, err := gitRepo.GetCommit(parentCommit)
  216. if err != nil {
  217. return false, "", nil, err
  218. }
  219. if commit.Signature == nil {
  220. return false, "", nil, &ErrWontSign{parentSigned}
  221. }
  222. verification := ParseCommitWithSignature(commit)
  223. if !verification.Verified {
  224. return false, "", nil, &ErrWontSign{parentSigned}
  225. }
  226. }
  227. }
  228. return true, signingKey, sig, nil
  229. }