diff --git a/routers/api/v1/org/mirror.go b/routers/api/v1/org/mirror.go index 3c117ccf54..a4a78aad29 100644 --- a/routers/api/v1/org/mirror.go +++ b/routers/api/v1/org/mirror.go @@ -7,8 +7,9 @@ import ( "net/http" "gitea.dev/models/db" + repo_model "gitea.dev/models/repo" + ssh_module "gitea.dev/modules/ssh" "gitea.dev/services/context" - mirror_service "gitea.dev/services/mirror" ) // GetManagedSSHKey gets the SSH public key for organization mirroring @@ -39,7 +40,7 @@ func GetManagedSSHKey(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - keypair, err := mirror_service.GetOrCreateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID) + keypair, err := ssh_module.GetOrCreateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID) if err != nil { if db.IsErrNotExist(err) { ctx.APIError(http.StatusNotFound, "SSH keypair not found") @@ -81,7 +82,7 @@ func RegenerateManagedSSHKey(ctx *context.APIContext) { // "403": // "$ref": "#/responses/forbidden" - keypair, err := mirror_service.RegenerateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID) + keypair, err := repo_model.RegenerateUserSSHKeypair(ctx, ctx.Org.Organization.ID) if err != nil { ctx.APIErrorInternal(err) return diff --git a/routers/api/v1/user/mirror.go b/routers/api/v1/user/mirror.go index 7d58883c60..f4047fdf20 100644 --- a/routers/api/v1/user/mirror.go +++ b/routers/api/v1/user/mirror.go @@ -7,8 +7,9 @@ import ( "net/http" "gitea.dev/models/db" + repo_model "gitea.dev/models/repo" + ssh_module "gitea.dev/modules/ssh" "gitea.dev/services/context" - mirror_service "gitea.dev/services/mirror" ) // GetManagedSSHKey gets the SSH public key for user mirroring @@ -31,7 +32,7 @@ func GetManagedSSHKey(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - keypair, err := mirror_service.GetOrCreateSSHKeypairForUser(ctx, ctx.Doer.ID) + keypair, err := ssh_module.GetOrCreateSSHKeypairForUser(ctx, ctx.Doer.ID) if err != nil { if db.IsErrNotExist(err) { ctx.APIError(http.StatusNotFound, "SSH keypair not found") @@ -65,7 +66,7 @@ func RegenerateManagedSSHKey(ctx *context.APIContext) { // fingerprint: // type: string - keypair, err := mirror_service.RegenerateSSHKeypairForUser(ctx, ctx.Doer.ID) + keypair, err := repo_model.RegenerateUserSSHKeypair(ctx, ctx.Doer.ID) if err != nil { ctx.APIErrorInternal(err) return diff --git a/routers/web/org/setting_ssh_keys.go b/routers/web/org/setting_ssh_keys.go index b888b5d78e..c649c5089a 100644 --- a/routers/web/org/setting_ssh_keys.go +++ b/routers/web/org/setting_ssh_keys.go @@ -10,7 +10,7 @@ import ( "gitea.dev/modules/templates" shared_user "gitea.dev/routers/web/shared/user" "gitea.dev/services/context" - mirror_service "gitea.dev/services/mirror" + ssh_module "gitea.dev/modules/ssh" ) const ( @@ -28,7 +28,7 @@ func SSHKeys(ctx *context.Context) { return } - keypair, err := mirror_service.GetOrCreateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID) + keypair, err := ssh_module.GetOrCreateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID) if err != nil { ctx.ServerError("GetOrCreateSSHKeypairForOrg", err) return @@ -45,7 +45,7 @@ func SSHKeys(ctx *context.Context) { // RegenerateSSHKey regenerates the SSH keypair for organization mirror operations func RegenerateSSHKey(ctx *context.Context) { - _, err := mirror_service.RegenerateSSHKeypairForOrg(ctx, ctx.Org.Organization.ID) + _, err := repo_model.RegenerateUserSSHKeypair(ctx, ctx.Org.Organization.ID) if err != nil { ctx.ServerError("RegenerateSSHKeypairForOrg", err) return diff --git a/routers/web/user/setting/keys.go b/routers/web/user/setting/keys.go index 29cfe5ff37..388098809d 100644 --- a/routers/web/user/setting/keys.go +++ b/routers/web/user/setting/keys.go @@ -18,7 +18,7 @@ import ( asymkey_service "gitea.dev/services/asymkey" "gitea.dev/services/context" "gitea.dev/services/forms" - mirror_service "gitea.dev/services/mirror" + ssh_module "gitea.dev/modules/ssh" ) const ( @@ -344,7 +344,7 @@ func loadKeysData(ctx *context.Context) { ctx.Data["UserDisabledFeatures"] = user_model.DisabledFeaturesWithLoginType(ctx.Doer) // Load SSH mirror keypair if it exists - mirrorKeypair, err := mirror_service.GetOrCreateSSHKeypairForUser(ctx, ctx.Doer.ID) + mirrorKeypair, err := ssh_module.GetOrCreateSSHKeypairForUser(ctx, ctx.Doer.ID) if err == nil { ctx.Data["HasManagedSSHKey"] = true @@ -366,7 +366,7 @@ func loadKeysData(ctx *context.Context) { // RegenerateUserSSHKeypair regenerates the SSH keypair for repository mirroring func RegenerateUserSSHKeypair(ctx *context.Context) { - _, err := mirror_service.RegenerateSSHKeypairForUser(ctx, ctx.Doer.ID) + _, err := repo_model.RegenerateUserSSHKeypair(ctx, ctx.Doer.ID) if err != nil { ctx.ServerError("RegenerateSSHKeypairForUser", err) return diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 9c9a53c985..7e362c8dd1 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -23,6 +23,7 @@ import ( "gitea.dev/modules/proxy" repo_module "gitea.dev/modules/repository" "gitea.dev/modules/setting" + ssh_module "gitea.dev/modules/ssh" "gitea.dev/modules/timeutil" "gitea.dev/modules/util" "gitea.dev/services/migrations" @@ -118,7 +119,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*repo_module.SyncResu timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second // Setup SSH authentication if needed - sshAuthSock, cleanup, sshErr := SetupManagedSSHAgent(ctx, m.Repo, remoteURL.String()) + sshAuthSock, cleanup, sshErr := ssh_module.SetupManagedSSHAgent(ctx, m.Repo, remoteURL.String()) if sshErr != nil { log.Error("SyncMirrors [repo: %-v]: SSH setup error %v", m.Repo, sshErr) return nil, false diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go index 4110029ea0..5020e6a414 100644 --- a/services/mirror/mirror_push.go +++ b/services/mirror/mirror_push.go @@ -21,6 +21,7 @@ import ( "gitea.dev/modules/proxy" "gitea.dev/modules/repository" "gitea.dev/modules/setting" + ssh_module "gitea.dev/modules/ssh" "gitea.dev/modules/timeutil" "gitea.dev/modules/util" "gitea.dev/services/migrations" @@ -167,7 +168,7 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error { } // Setup SSH authentication - sshAuthSock, cleanup, err := SetupManagedSSHAgent(ctx, repo, remoteURL.String()) + sshAuthSock, cleanup, err := ssh_module.SetupManagedSSHAgent(ctx, repo, remoteURL.String()) if err != nil { log.Error("Failed to set up SSH agent for push mirror %s: %v", repo.FullName(), err) return util.SanitizeErrorCredentialURLs(err) diff --git a/services/mirror/ssh_keypair.go b/services/mirror/ssh_keypair.go deleted file mode 100644 index b576bc8144..0000000000 --- a/services/mirror/ssh_keypair.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2025 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package mirror - -import ( - "context" - - repo_model "gitea.dev/models/repo" - "gitea.dev/modules/log" - ssh_module "gitea.dev/modules/ssh" -) - -// GetOrCreateSSHKeypairForUser gets or creates an SSH keypair for the given user -func GetOrCreateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.UserSSHKeypair, error) { - return ssh_module.GetOrCreateSSHKeypairForUser(ctx, userID) -} - -// GetOrCreateSSHKeypairForOrg gets or creates an SSH keypair for the given organization -func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.UserSSHKeypair, error) { - return ssh_module.GetOrCreateSSHKeypairForOrg(ctx, orgID) -} - -// GetSSHKeypairForRepository gets the appropriate SSH keypair for a repository -// If the repository belongs to an organization, it uses the org's keypair, -// otherwise it uses the user's keypair -func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*repo_model.UserSSHKeypair, error) { - return ssh_module.GetSSHKeypairForRepository(ctx, repo) -} - -// RegenerateSSHKeypairForUser regenerates the SSH keypair for a user -func RegenerateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.UserSSHKeypair, error) { - log.Info("Regenerating SSH keypair for user %d", userID) - return repo_model.RegenerateUserSSHKeypair(ctx, userID) -} - -// RegenerateSSHKeypairForOrg regenerates the SSH keypair for an organization -func RegenerateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.UserSSHKeypair, error) { - log.Info("Regenerating SSH keypair for organization %d", orgID) - return repo_model.RegenerateUserSSHKeypair(ctx, orgID) -} - -// IsSSHURL checks if a URL is an SSH URL -func IsSSHURL(url string) bool { - return ssh_module.IsSSHURL(url) -} - -// GetSSHKeypairForURL gets the appropriate SSH keypair for a given repository and URL -// Returns nil if the URL is not an SSH URL -func GetSSHKeypairForURL(ctx context.Context, repo *repo_model.Repository, url string) (*repo_model.UserSSHKeypair, error) { - return ssh_module.GetSSHKeypairForURL(ctx, repo, url) -} - -// SetupManagedSSHAgent prepares SSH key-based authentication for a mirror or -// migration git operation against url on behalf of repo. The returned cleanup -// is never nil and must always be called by the caller (typically via defer). -func SetupManagedSSHAgent(ctx context.Context, repo *repo_model.Repository, url string) (string, func(), error) { - return ssh_module.SetupManagedSSHAgent(ctx, repo, url) -}