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.
389 lines
11 KiB
389 lines
11 KiB
3 years ago
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||
5 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package asymkey
|
||
5 years ago
|
|
||
|
import (
|
||
3 years ago
|
"fmt"
|
||
5 years ago
|
"strings"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models"
|
||
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
"code.gitea.io/gitea/models/login"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/git"
|
||
|
"code.gitea.io/gitea/modules/log"
|
||
|
"code.gitea.io/gitea/modules/process"
|
||
|
"code.gitea.io/gitea/modules/setting"
|
||
|
)
|
||
|
|
||
|
type signingMode string
|
||
|
|
||
|
const (
|
||
|
never signingMode = "never"
|
||
|
always signingMode = "always"
|
||
|
pubkey signingMode = "pubkey"
|
||
|
twofa signingMode = "twofa"
|
||
|
parentSigned signingMode = "parentsigned"
|
||
|
baseSigned signingMode = "basesigned"
|
||
|
headSigned signingMode = "headsigned"
|
||
|
commitsSigned signingMode = "commitssigned"
|
||
5 years ago
|
approved signingMode = "approved"
|
||
5 years ago
|
noKey signingMode = "nokey"
|
||
5 years ago
|
)
|
||
|
|
||
|
func signingModeFromStrings(modeStrings []string) []signingMode {
|
||
|
returnable := make([]signingMode, 0, len(modeStrings))
|
||
|
for _, mode := range modeStrings {
|
||
4 years ago
|
signMode := signingMode(strings.ToLower(strings.TrimSpace(mode)))
|
||
5 years ago
|
switch signMode {
|
||
|
case never:
|
||
|
return []signingMode{never}
|
||
|
case always:
|
||
|
return []signingMode{always}
|
||
|
case pubkey:
|
||
|
fallthrough
|
||
|
case twofa:
|
||
|
fallthrough
|
||
|
case parentSigned:
|
||
|
fallthrough
|
||
|
case baseSigned:
|
||
|
fallthrough
|
||
|
case headSigned:
|
||
|
fallthrough
|
||
5 years ago
|
case approved:
|
||
|
fallthrough
|
||
5 years ago
|
case commitsSigned:
|
||
|
returnable = append(returnable, signMode)
|
||
|
}
|
||
|
}
|
||
|
if len(returnable) == 0 {
|
||
|
return []signingMode{never}
|
||
|
}
|
||
|
return returnable
|
||
|
}
|
||
|
|
||
3 years ago
|
// ErrWontSign explains the first reason why a commit would not be signed
|
||
|
// There may be other reasons - this is just the first reason found
|
||
|
type ErrWontSign struct {
|
||
|
Reason signingMode
|
||
|
}
|
||
|
|
||
|
func (e *ErrWontSign) Error() string {
|
||
|
return fmt.Sprintf("wont sign: %s", e.Reason)
|
||
|
}
|
||
|
|
||
|
// IsErrWontSign checks if an error is a ErrWontSign
|
||
|
func IsErrWontSign(err error) bool {
|
||
|
_, ok := err.(*ErrWontSign)
|
||
|
return ok
|
||
|
}
|
||
|
|
||
4 years ago
|
// SigningKey returns the KeyID and git Signature for the repo
|
||
|
func SigningKey(repoPath string) (string, *git.Signature) {
|
||
5 years ago
|
if setting.Repository.Signing.SigningKey == "none" {
|
||
4 years ago
|
return "", nil
|
||
5 years ago
|
}
|
||
|
|
||
|
if setting.Repository.Signing.SigningKey == "default" || setting.Repository.Signing.SigningKey == "" {
|
||
|
// Can ignore the error here as it means that commit.gpgsign is not set
|
||
|
value, _ := git.NewCommand("config", "--get", "commit.gpgsign").RunInDir(repoPath)
|
||
|
sign, valid := git.ParseBool(strings.TrimSpace(value))
|
||
|
if !sign || !valid {
|
||
4 years ago
|
return "", nil
|
||
5 years ago
|
}
|
||
|
|
||
|
signingKey, _ := git.NewCommand("config", "--get", "user.signingkey").RunInDir(repoPath)
|
||
4 years ago
|
signingName, _ := git.NewCommand("config", "--get", "user.name").RunInDir(repoPath)
|
||
|
signingEmail, _ := git.NewCommand("config", "--get", "user.email").RunInDir(repoPath)
|
||
|
return strings.TrimSpace(signingKey), &git.Signature{
|
||
|
Name: strings.TrimSpace(signingName),
|
||
|
Email: strings.TrimSpace(signingEmail),
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
return setting.Repository.Signing.SigningKey, &git.Signature{
|
||
|
Name: setting.Repository.Signing.SigningName,
|
||
|
Email: setting.Repository.Signing.SigningEmail,
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
// PublicSigningKey gets the public signing key within a provided repository directory
|
||
|
func PublicSigningKey(repoPath string) (string, error) {
|
||
4 years ago
|
signingKey, _ := SigningKey(repoPath)
|
||
5 years ago
|
if signingKey == "" {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
content, stderr, err := process.GetManager().ExecDir(-1, repoPath,
|
||
|
"gpg --export -a", "gpg", "--export", "-a", signingKey)
|
||
|
if err != nil {
|
||
|
log.Error("Unable to get default signing key in %s: %s, %s, %v", repoPath, signingKey, stderr, err)
|
||
|
return "", err
|
||
|
}
|
||
|
return content, nil
|
||
|
}
|
||
|
|
||
|
// SignInitialCommit determines if we should sign the initial commit to this repository
|
||
3 years ago
|
func SignInitialCommit(repoPath string, u *user_model.User) (bool, string, *git.Signature, error) {
|
||
5 years ago
|
rules := signingModeFromStrings(setting.Repository.Signing.InitialCommit)
|
||
4 years ago
|
signingKey, sig := SigningKey(repoPath)
|
||
5 years ago
|
if signingKey == "" {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{noKey}
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
Loop:
|
||
5 years ago
|
for _, rule := range rules {
|
||
|
switch rule {
|
||
|
case never:
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{never}
|
||
5 years ago
|
case always:
|
||
4 years ago
|
break Loop
|
||
5 years ago
|
case pubkey:
|
||
3 years ago
|
keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{})
|
||
5 years ago
|
if err != nil {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if len(keys) == 0 {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{pubkey}
|
||
5 years ago
|
}
|
||
|
case twofa:
|
||
3 years ago
|
twofaModel, err := login.GetTwoFactorByUID(u.ID)
|
||
|
if err != nil && !login.IsErrTwoFactorNotEnrolled(err) {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if twofaModel == nil {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{twofa}
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
4 years ago
|
return true, signingKey, sig, nil
|
||
5 years ago
|
}
|
||
|
|
||
|
// SignWikiCommit determines if we should sign the commits to this repository wiki
|
||
3 years ago
|
func SignWikiCommit(repoWikiPath string, u *user_model.User) (bool, string, *git.Signature, error) {
|
||
5 years ago
|
rules := signingModeFromStrings(setting.Repository.Signing.Wiki)
|
||
3 years ago
|
signingKey, sig := SigningKey(repoWikiPath)
|
||
5 years ago
|
if signingKey == "" {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{noKey}
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
Loop:
|
||
5 years ago
|
for _, rule := range rules {
|
||
|
switch rule {
|
||
|
case never:
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{never}
|
||
5 years ago
|
case always:
|
||
4 years ago
|
break Loop
|
||
5 years ago
|
case pubkey:
|
||
3 years ago
|
keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{})
|
||
5 years ago
|
if err != nil {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if len(keys) == 0 {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{pubkey}
|
||
5 years ago
|
}
|
||
|
case twofa:
|
||
3 years ago
|
twofaModel, err := login.GetTwoFactorByUID(u.ID)
|
||
|
if err != nil && !login.IsErrTwoFactorNotEnrolled(err) {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if twofaModel == nil {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{twofa}
|
||
5 years ago
|
}
|
||
|
case parentSigned:
|
||
3 years ago
|
gitRepo, err := git.OpenRepository(repoWikiPath)
|
||
5 years ago
|
if err != nil {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
5 years ago
|
defer gitRepo.Close()
|
||
5 years ago
|
commit, err := gitRepo.GetCommit("HEAD")
|
||
|
if err != nil {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if commit.Signature == nil {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{parentSigned}
|
||
5 years ago
|
}
|
||
3 years ago
|
verification := asymkey_model.ParseCommitWithSignature(commit)
|
||
5 years ago
|
if !verification.Verified {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{parentSigned}
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
4 years ago
|
return true, signingKey, sig, nil
|
||
5 years ago
|
}
|
||
|
|
||
|
// SignCRUDAction determines if we should sign a CRUD commit to this repository
|
||
3 years ago
|
func SignCRUDAction(repoPath string, u *user_model.User, tmpBasePath, parentCommit string) (bool, string, *git.Signature, error) {
|
||
5 years ago
|
rules := signingModeFromStrings(setting.Repository.Signing.CRUDActions)
|
||
3 years ago
|
signingKey, sig := SigningKey(repoPath)
|
||
5 years ago
|
if signingKey == "" {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{noKey}
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
Loop:
|
||
5 years ago
|
for _, rule := range rules {
|
||
|
switch rule {
|
||
|
case never:
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{never}
|
||
5 years ago
|
case always:
|
||
4 years ago
|
break Loop
|
||
5 years ago
|
case pubkey:
|
||
3 years ago
|
keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{})
|
||
5 years ago
|
if err != nil {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if len(keys) == 0 {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{pubkey}
|
||
5 years ago
|
}
|
||
|
case twofa:
|
||
3 years ago
|
twofaModel, err := login.GetTwoFactorByUID(u.ID)
|
||
|
if err != nil && !login.IsErrTwoFactorNotEnrolled(err) {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if twofaModel == nil {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{twofa}
|
||
5 years ago
|
}
|
||
|
case parentSigned:
|
||
|
gitRepo, err := git.OpenRepository(tmpBasePath)
|
||
|
if err != nil {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
5 years ago
|
defer gitRepo.Close()
|
||
5 years ago
|
commit, err := gitRepo.GetCommit(parentCommit)
|
||
|
if err != nil {
|
||
4 years ago
|
return false, "", nil, err
|
||
5 years ago
|
}
|
||
|
if commit.Signature == nil {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{parentSigned}
|
||
5 years ago
|
}
|
||
3 years ago
|
verification := asymkey_model.ParseCommitWithSignature(commit)
|
||
5 years ago
|
if !verification.Verified {
|
||
4 years ago
|
return false, "", nil, &ErrWontSign{parentSigned}
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
4 years ago
|
return true, signingKey, sig, nil
|
||
5 years ago
|
}
|
||
3 years ago
|
|
||
|
// SignMerge determines if we should sign a PR merge commit to the base repository
|
||
|
func SignMerge(pr *models.PullRequest, u *user_model.User, tmpBasePath, baseCommit, headCommit string) (bool, string, *git.Signature, error) {
|
||
|
if err := pr.LoadBaseRepo(); err != nil {
|
||
|
log.Error("Unable to get Base Repo for pull request")
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
repo := pr.BaseRepo
|
||
|
|
||
|
signingKey, signer := SigningKey(repo.RepoPath())
|
||
|
if signingKey == "" {
|
||
|
return false, "", nil, &ErrWontSign{noKey}
|
||
|
}
|
||
|
rules := signingModeFromStrings(setting.Repository.Signing.Merges)
|
||
|
|
||
|
var gitRepo *git.Repository
|
||
|
var err error
|
||
|
|
||
|
Loop:
|
||
|
for _, rule := range rules {
|
||
|
switch rule {
|
||
|
case never:
|
||
|
return false, "", nil, &ErrWontSign{never}
|
||
|
case always:
|
||
|
break Loop
|
||
|
case pubkey:
|
||
|
keys, err := asymkey_model.ListGPGKeys(db.DefaultContext, u.ID, db.ListOptions{})
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
if len(keys) == 0 {
|
||
|
return false, "", nil, &ErrWontSign{pubkey}
|
||
|
}
|
||
|
case twofa:
|
||
|
twofaModel, err := login.GetTwoFactorByUID(u.ID)
|
||
|
if err != nil && !login.IsErrTwoFactorNotEnrolled(err) {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
if twofaModel == nil {
|
||
|
return false, "", nil, &ErrWontSign{twofa}
|
||
|
}
|
||
|
case approved:
|
||
|
protectedBranch, err := models.GetProtectedBranchBy(repo.ID, pr.BaseBranch)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
if protectedBranch == nil {
|
||
|
return false, "", nil, &ErrWontSign{approved}
|
||
|
}
|
||
|
if protectedBranch.GetGrantedApprovalsCount(pr) < 1 {
|
||
|
return false, "", nil, &ErrWontSign{approved}
|
||
|
}
|
||
|
case baseSigned:
|
||
|
if gitRepo == nil {
|
||
|
gitRepo, err = git.OpenRepository(tmpBasePath)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
defer gitRepo.Close()
|
||
|
}
|
||
|
commit, err := gitRepo.GetCommit(baseCommit)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
verification := asymkey_model.ParseCommitWithSignature(commit)
|
||
|
if !verification.Verified {
|
||
|
return false, "", nil, &ErrWontSign{baseSigned}
|
||
|
}
|
||
|
case headSigned:
|
||
|
if gitRepo == nil {
|
||
|
gitRepo, err = git.OpenRepository(tmpBasePath)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
defer gitRepo.Close()
|
||
|
}
|
||
|
commit, err := gitRepo.GetCommit(headCommit)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
verification := asymkey_model.ParseCommitWithSignature(commit)
|
||
|
if !verification.Verified {
|
||
|
return false, "", nil, &ErrWontSign{headSigned}
|
||
|
}
|
||
|
case commitsSigned:
|
||
|
if gitRepo == nil {
|
||
|
gitRepo, err = git.OpenRepository(tmpBasePath)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
defer gitRepo.Close()
|
||
|
}
|
||
|
commit, err := gitRepo.GetCommit(headCommit)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
verification := asymkey_model.ParseCommitWithSignature(commit)
|
||
|
if !verification.Verified {
|
||
|
return false, "", nil, &ErrWontSign{commitsSigned}
|
||
|
}
|
||
|
// need to work out merge-base
|
||
|
mergeBaseCommit, _, err := gitRepo.GetMergeBase("", baseCommit, headCommit)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
commitList, err := commit.CommitsBeforeUntil(mergeBaseCommit)
|
||
|
if err != nil {
|
||
|
return false, "", nil, err
|
||
|
}
|
||
|
for _, commit := range commitList {
|
||
|
verification := asymkey_model.ParseCommitWithSignature(commit)
|
||
|
if !verification.Verified {
|
||
|
return false, "", nil, &ErrWontSign{commitsSigned}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return true, signingKey, signer, nil
|
||
|
}
|