diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 16:14:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 16:14:24 +0800 |
commit | 3ca5dc7e32b372d14ff80d96f14b8f6a805862f1 (patch) | |
tree | 50d193ed0dacf2888d57b193a9b0d36065aff205 /modules | |
parent | 0a9fcf63a49799ad3b0f146c54879161bac61e10 (diff) | |
download | gitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.tar.gz gitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.zip |
Move keys to models/asymkey (#17917)
* Move keys to models/keys
* Rename models/keys -> models/asymkey
* change the missed package name
* Fix package alias
* Fix test
* Fix docs
* Fix test
* Fix test
* merge
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/repo.go | 7 | ||||
-rw-r--r-- | modules/convert/convert.go | 13 | ||||
-rw-r--r-- | modules/doctor/authorizedkeys.go | 8 | ||||
-rw-r--r-- | modules/gitgraph/graph_models.go | 9 | ||||
-rw-r--r-- | modules/private/serv.go | 8 | ||||
-rw-r--r-- | modules/repository/init.go | 3 | ||||
-rw-r--r-- | modules/ssh/ssh.go | 10 |
7 files changed, 32 insertions, 26 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index 694c483bd4..1725cb724d 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -25,6 +25,7 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + asymkey_service "code.gitea.io/gitea/services/asymkey" "github.com/editorconfig/editorconfig-core-go/v2" "github.com/unknwon/com" @@ -120,7 +121,7 @@ func (r *Repository) CanCommitToBranch(doer *user_model.User) (CanCommitToBranch requireSigned = protectedBranch.RequireSignedCommits } - sign, keyID, _, err := models.SignCRUDAction(r.Repository, doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName) + sign, keyID, _, err := asymkey_service.SignCRUDAction(r.Repository.RepoPath(), doer, r.Repository.RepoPath(), git.BranchPrefix+r.BranchName) canCommit := r.CanEnableEditor() && userCanPush if requireSigned { @@ -128,8 +129,8 @@ func (r *Repository) CanCommitToBranch(doer *user_model.User) (CanCommitToBranch } wontSignReason := "" if err != nil { - if models.IsErrWontSign(err) { - wontSignReason = string(err.(*models.ErrWontSign).Reason) + if asymkey_service.IsErrWontSign(err) { + wontSignReason = string(err.(*asymkey_service.ErrWontSign).Reason) err = nil } else { wontSignReason = "error" diff --git a/modules/convert/convert.go b/modules/convert/convert.go index eda9f23cd6..1c78c35cda 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -12,6 +12,7 @@ import ( "time" "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/login" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" @@ -152,7 +153,7 @@ func ToTag(repo *repo_model.Repository, t *git.Tag) *api.Tag { // ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification func ToVerification(c *git.Commit) *api.PayloadCommitVerification { - verif := models.ParseCommitWithSignature(c) + verif := asymkey_model.ParseCommitWithSignature(c) commitVerification := &api.PayloadCommitVerification{ Verified: verif.Verified, Reason: verif.Reason, @@ -170,8 +171,8 @@ func ToVerification(c *git.Commit) *api.PayloadCommitVerification { return commitVerification } -// ToPublicKey convert models.PublicKey to api.PublicKey -func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey { +// ToPublicKey convert asymkey_model.PublicKey to api.PublicKey +func ToPublicKey(apiLink string, key *asymkey_model.PublicKey) *api.PublicKey { return &api.PublicKey{ ID: key.ID, Key: key.Content, @@ -183,7 +184,7 @@ func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey { } // ToGPGKey converts models.GPGKey to api.GPGKey -func ToGPGKey(key *models.GPGKey) *api.GPGKey { +func ToGPGKey(key *asymkey_model.GPGKey) *api.GPGKey { subkeys := make([]*api.GPGKey, len(key.SubsKey)) for id, k := range key.SubsKey { subkeys[id] = &api.GPGKey{ @@ -264,8 +265,8 @@ func ToGitHook(h *git.Hook) *api.GitHook { } } -// ToDeployKey convert models.DeployKey to api.DeployKey -func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey { +// ToDeployKey convert asymkey_model.DeployKey to api.DeployKey +func ToDeployKey(apiLink string, key *asymkey_model.DeployKey) *api.DeployKey { return &api.DeployKey{ ID: key.ID, KeyID: key.KeyID, diff --git a/modules/doctor/authorizedkeys.go b/modules/doctor/authorizedkeys.go index d9bc70bb77..1a9b60e248 100644 --- a/modules/doctor/authorizedkeys.go +++ b/modules/doctor/authorizedkeys.go @@ -12,7 +12,7 @@ import ( "path/filepath" "strings" - "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -32,7 +32,7 @@ func checkAuthorizedKeys(logger log.Logger, autofix bool) error { return fmt.Errorf("Unable to open authorized_keys file. ERROR: %v", err) } logger.Warn("Unable to open authorized_keys. (ERROR: %v). Attempting to rewrite...", err) - if err = models.RewriteAllPublicKeys(); err != nil { + if err = asymkey_model.RewriteAllPublicKeys(); err != nil { logger.Critical("Unable to rewrite authorized_keys file. ERROR: %v", err) return fmt.Errorf("Unable to rewrite authorized_keys file. ERROR: %v", err) } @@ -53,7 +53,7 @@ func checkAuthorizedKeys(logger log.Logger, autofix bool) error { // now we regenerate and check if there are any lines missing regenerated := &bytes.Buffer{} - if err := models.RegeneratePublicKeys(regenerated); err != nil { + if err := asymkey_model.RegeneratePublicKeys(regenerated); err != nil { logger.Critical("Unable to regenerate authorized_keys file. ERROR: %v", err) return fmt.Errorf("Unable to regenerate authorized_keys file. ERROR: %v", err) } @@ -75,7 +75,7 @@ func checkAuthorizedKeys(logger log.Logger, autofix bool) error { return fmt.Errorf(`authorized_keys is out of date and should be regenerated with "gitea admin regenerate keys" or "gitea doctor --run authorized_keys --fix"`) } logger.Warn("authorized_keys is out of date. Attempting rewrite...") - err = models.RewriteAllPublicKeys() + err = asymkey_model.RewriteAllPublicKeys() if err != nil { logger.Critical("Unable to rewrite authorized_keys file. ERROR: %v", err) return fmt.Errorf("Unable to rewrite authorized_keys file. ERROR: %v", err) diff --git a/modules/gitgraph/graph_models.go b/modules/gitgraph/graph_models.go index e7280e2adc..4cfa96816a 100644 --- a/modules/gitgraph/graph_models.go +++ b/modules/gitgraph/graph_models.go @@ -10,6 +10,7 @@ import ( "strings" "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -113,9 +114,11 @@ func (graph *Graph) LoadAndProcessCommits(repository *repo_model.Repository, git } } - c.Verification = models.ParseCommitWithSignature(c.Commit) + c.Verification = asymkey_model.ParseCommitWithSignature(c.Commit) - _ = models.CalculateTrustStatus(c.Verification, repository, &keyMap) + _ = asymkey_model.CalculateTrustStatus(c.Verification, repository.GetTrustModel(), func(user *user_model.User) (bool, error) { + return models.IsUserRepoAdmin(repository, user) + }, &keyMap) statuses, err := models.GetLatestCommitStatus(repository.ID, c.Commit.ID.String(), db.ListOptions{}) if err != nil { @@ -236,7 +239,7 @@ func newRefsFromRefNames(refNames []byte) []git.Reference { type Commit struct { Commit *git.Commit User *user_model.User - Verification *models.CommitVerification + Verification *asymkey_model.CommitVerification Status *models.CommitStatus Flow int64 Row int diff --git a/modules/private/serv.go b/modules/private/serv.go index 4f5b19c597..a7a56df7b3 100644 --- a/modules/private/serv.go +++ b/modules/private/serv.go @@ -10,7 +10,7 @@ import ( "net/http" "net/url" - "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/json" @@ -19,12 +19,12 @@ import ( // KeyAndOwner is the response from ServNoCommand type KeyAndOwner struct { - Key *models.PublicKey `json:"key"` - Owner *user_model.User `json:"user"` + Key *asymkey_model.PublicKey `json:"key"` + Owner *user_model.User `json:"user"` } // ServNoCommand returns information about the provided key -func ServNoCommand(ctx context.Context, keyID int64) (*models.PublicKey, *user_model.User, error) { +func ServNoCommand(ctx context.Context, keyID int64) (*asymkey_model.PublicKey, *user_model.User, error) { reqURL := setting.LocalURL + fmt.Sprintf("api/internal/serv/none/%d", keyID) resp, err := newInternalRequest(ctx, reqURL, "GET").Response() diff --git a/modules/repository/init.go b/modules/repository/init.go index cfee1a3215..08c5aac1b8 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" + asymkey_service "code.gitea.io/gitea/services/asymkey" "github.com/unknwon/com" ) @@ -134,7 +135,7 @@ func initRepoCommit(tmpPath string, repo *repo_model.Repository, u *user_model.U } if git.CheckGitVersionAtLeast("1.7.9") == nil { - sign, keyID, signer, _ := models.SignInitialCommit(tmpPath, u) + sign, keyID, signer, _ := asymkey_service.SignInitialCommit(tmpPath, u) if sign { args = append(args, "-S"+keyID) diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 5f19dd4a5c..a3756fd2af 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -22,7 +22,7 @@ import ( "sync" "syscall" - "code.gitea.io/gitea/models" + asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -172,9 +172,9 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool { // look for the exact principal principalLoop: for _, principal := range cert.ValidPrincipals { - pkey, err := models.SearchPublicKeyByContentExact(principal) + pkey, err := asymkey_model.SearchPublicKeyByContentExact(principal) if err != nil { - if models.IsErrKeyNotExist(err) { + if asymkey_model.IsErrKeyNotExist(err) { log.Debug("Principal Rejected: %s Unknown Principal: %s", ctx.RemoteAddr(), principal) continue principalLoop } @@ -232,9 +232,9 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool { log.Debug("Handle Public Key: %s Fingerprint: %s is not a certificate", ctx.RemoteAddr(), gossh.FingerprintSHA256(key)) } - pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key)))) + pkey, err := asymkey_model.SearchPublicKeyByContent(strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key)))) if err != nil { - if models.IsErrKeyNotExist(err) { + if asymkey_model.IsErrKeyNotExist(err) { if log.IsWarn() { log.Warn("Unknown public key: %s from %s", gossh.FingerprintSHA256(key), ctx.RemoteAddr()) log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr()) |