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 7.0KB

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