-
-
Notifications
You must be signed in to change notification settings - Fork 6k
SSH Push/Pull Mirroring & Migrations #35089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
techknowlogick
wants to merge
19
commits into
go-gitea:main
Choose a base branch
from
techknowlogick:ssh-mirroring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4c8e222
SSH Push/Pull Mirroring & Migrations
techknowlogick c2e9532
Merge remote-tracking branch 'origin/main' into test
techknowlogick fb5e880
add repomigration form to dom-ready
techknowlogick 20e70e4
fix lint errors
techknowlogick 41afc43
more lint fixes
techknowlogick 5ca8e61
Merge branch 'main' into ssh-mirroring
techknowlogick 2830c5f
Quick quit ssh://
techknowlogick 21422e9
fmt and swagger
techknowlogick 5f8c350
fix swagger
techknowlogick 8347926
make fix
techknowlogick 95af679
skip err check
techknowlogick cecb89d
fix test
techknowlogick 795bd80
Merge branch 'main' into ssh-mirroring
techknowlogick a965cd5
Merge branch 'main' into ssh-mirroring
techknowlogick 0d7e2d1
rename migration to 322
techknowlogick cee2ca0
Merge remote-tracking branch 'upstream/main' into ssh-mirroring
techknowlogick f97acdc
update to `UserSSHKeypair` per feedback
techknowlogick 2f3f2d1
Merge branch 'ssh-mirroring' of https://github.com/techknowlogick/git…
techknowlogick 3ed601d
switch to use user_settings table per feedback
techknowlogick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo | ||
|
||
import ( | ||
"context" | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/secret" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/util" | ||
|
||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
// UserSSHKeypair represents an SSH keypair for repository mirroring | ||
type UserSSHKeypair struct { | ||
OwnerID int64 | ||
PrivateKeyEncrypted string | ||
PublicKey string | ||
Fingerprint string | ||
} | ||
|
||
// GetUserSSHKeypairByOwner gets the SSH keypair for the given owner | ||
func GetUserSSHKeypairByOwner(ctx context.Context, ownerID int64) (*UserSSHKeypair, error) { | ||
settings, err := user_model.GetSettings(ctx, ownerID, []string{ | ||
user_model.UserSSHMirrorPrivPem, | ||
user_model.UserSSHMirrorPubPem, | ||
user_model.UserSSHMirrorFingerprint, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(settings) == 0 { | ||
return nil, util.NewNotExistErrorf("SSH keypair does not exist for owner %d", ownerID) | ||
} | ||
|
||
keypair := &UserSSHKeypair{ | ||
OwnerID: ownerID, | ||
} | ||
|
||
if privSetting, exists := settings[user_model.UserSSHMirrorPrivPem]; exists { | ||
keypair.PrivateKeyEncrypted = privSetting.SettingValue | ||
} | ||
if pubSetting, exists := settings[user_model.UserSSHMirrorPubPem]; exists { | ||
keypair.PublicKey = pubSetting.SettingValue | ||
} | ||
if fpSetting, exists := settings[user_model.UserSSHMirrorFingerprint]; exists { | ||
keypair.Fingerprint = fpSetting.SettingValue | ||
} | ||
|
||
if keypair.PrivateKeyEncrypted == "" || keypair.PublicKey == "" || keypair.Fingerprint == "" { | ||
return nil, util.NewNotExistErrorf("SSH keypair incomplete for owner %d", ownerID) | ||
} | ||
|
||
return keypair, nil | ||
} | ||
|
||
// CreateUserSSHKeypair creates a new SSH keypair for mirroring | ||
func CreateUserSSHKeypair(ctx context.Context, ownerID int64) (*UserSSHKeypair, error) { | ||
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to generate Ed25519 keypair: %w", err) | ||
} | ||
|
||
sshPublicKey, err := ssh.NewPublicKey(publicKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert public key to SSH format: %w", err) | ||
} | ||
|
||
publicKeyStr := string(ssh.MarshalAuthorizedKey(sshPublicKey)) | ||
|
||
fingerprint := sha256.Sum256(sshPublicKey.Marshal()) | ||
fingerprintStr := hex.EncodeToString(fingerprint[:]) | ||
|
||
privateKeyEncrypted, err := secret.EncryptSecret(setting.SecretKey, string(privateKey)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to encrypt private key: %w", err) | ||
} | ||
|
||
err = db.WithTx(ctx, func(ctx context.Context) error { | ||
if err := user_model.SetUserSetting(ctx, ownerID, user_model.UserSSHMirrorPrivPem, privateKeyEncrypted); err != nil { | ||
return fmt.Errorf("failed to save private key: %w", err) | ||
} | ||
if err := user_model.SetUserSetting(ctx, ownerID, user_model.UserSSHMirrorPubPem, publicKeyStr); err != nil { | ||
return fmt.Errorf("failed to save public key: %w", err) | ||
} | ||
if err := user_model.SetUserSetting(ctx, ownerID, user_model.UserSSHMirrorFingerprint, fingerprintStr); err != nil { | ||
return fmt.Errorf("failed to save fingerprint: %w", err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keypair := &UserSSHKeypair{ | ||
OwnerID: ownerID, | ||
PrivateKeyEncrypted: privateKeyEncrypted, | ||
PublicKey: publicKeyStr, | ||
Fingerprint: fingerprintStr, | ||
} | ||
|
||
return keypair, nil | ||
} | ||
|
||
// GetDecryptedPrivateKey returns the decrypted private key | ||
func (k *UserSSHKeypair) GetDecryptedPrivateKey() (ed25519.PrivateKey, error) { | ||
decrypted, err := secret.DecryptSecret(setting.SecretKey, k.PrivateKeyEncrypted) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decrypt private key: %w", err) | ||
} | ||
return ed25519.PrivateKey(decrypted), nil | ||
} | ||
|
||
// GetPublicKeyWithComment returns the public key with a descriptive comment (namespace-fingerprint@domain) | ||
func (k *UserSSHKeypair) GetPublicKeyWithComment(ctx context.Context) (string, error) { | ||
owner, err := user_model.GetUserByID(ctx, k.OwnerID) | ||
if err != nil { | ||
return k.PublicKey, nil | ||
} | ||
|
||
domain := setting.Domain | ||
if domain == "" { | ||
domain = "gitea" | ||
} | ||
|
||
keyID := k.Fingerprint | ||
if len(keyID) > 8 { | ||
keyID = keyID[:8] | ||
} | ||
|
||
comment := fmt.Sprintf("%s-%s@%s", owner.Name, keyID, domain) | ||
return strings.TrimSpace(k.PublicKey) + " " + comment, nil | ||
} | ||
|
||
// DeleteUserSSHKeypair deletes an SSH keypair | ||
func DeleteUserSSHKeypair(ctx context.Context, ownerID int64) error { | ||
return db.WithTx(ctx, func(ctx context.Context) error { | ||
if err := user_model.DeleteUserSetting(ctx, ownerID, user_model.UserSSHMirrorPrivPem); err != nil { | ||
return err | ||
} | ||
if err := user_model.DeleteUserSetting(ctx, ownerID, user_model.UserSSHMirrorPubPem); err != nil { | ||
return err | ||
} | ||
return user_model.DeleteUserSetting(ctx, ownerID, user_model.UserSSHMirrorFingerprint) | ||
}) | ||
} | ||
|
||
// RegenerateUserSSHKeypair regenerates an SSH keypair for the given owner | ||
func RegenerateUserSSHKeypair(ctx context.Context, ownerID int64) (*UserSSHKeypair, error) { | ||
var keypair *UserSSHKeypair | ||
err := db.WithTx(ctx, func(ctx context.Context) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
_ = DeleteUserSSHKeypair(ctx, ownerID) | ||
|
||
newKeypair, err := CreateUserSSHKeypair(ctx, ownerID) | ||
if err != nil { | ||
return err | ||
} | ||
keypair = newKeypair | ||
return nil | ||
}) | ||
return keypair, err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2025 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package repo_test | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/util" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUserSSHKeypair(t *testing.T) { | ||
require.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
t.Run("CreateUserSSHKeypair", func(t *testing.T) { | ||
// Test creating a new SSH keypair for a user | ||
keypair, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 1) | ||
require.NoError(t, err) | ||
assert.NotNil(t, keypair) | ||
assert.Equal(t, int64(1), keypair.OwnerID) | ||
assert.NotEmpty(t, keypair.PublicKey) | ||
assert.NotEmpty(t, keypair.PrivateKeyEncrypted) | ||
assert.NotEmpty(t, keypair.Fingerprint) | ||
|
||
// Verify the public key is in SSH format | ||
assert.Contains(t, keypair.PublicKey, "ssh-ed25519") | ||
|
||
// Test creating a keypair for an organization | ||
orgKeypair, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 2) | ||
require.NoError(t, err) | ||
assert.NotNil(t, orgKeypair) | ||
assert.Equal(t, int64(2), orgKeypair.OwnerID) | ||
|
||
// Ensure different owners get different keypairs | ||
assert.NotEqual(t, keypair.PublicKey, orgKeypair.PublicKey) | ||
assert.NotEqual(t, keypair.Fingerprint, orgKeypair.Fingerprint) | ||
}) | ||
|
||
t.Run("GetUserSSHKeypairByOwner", func(t *testing.T) { | ||
// Create a keypair first | ||
created, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 3) | ||
require.NoError(t, err) | ||
|
||
// Test retrieving the keypair | ||
retrieved, err := repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 3) | ||
require.NoError(t, err) | ||
assert.Equal(t, created.OwnerID, retrieved.OwnerID) | ||
assert.Equal(t, created.PublicKey, retrieved.PublicKey) | ||
assert.Equal(t, created.Fingerprint, retrieved.Fingerprint) | ||
|
||
// Test retrieving non-existent keypair | ||
_, err = repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 999) | ||
assert.ErrorIs(t, err, util.ErrNotExist) | ||
}) | ||
|
||
t.Run("GetDecryptedPrivateKey", func(t *testing.T) { | ||
// Ensure we have a valid SECRET_KEY for testing | ||
if setting.SecretKey == "" { | ||
setting.SecretKey = "test-secret-key-for-testing" | ||
} | ||
|
||
// Create a keypair | ||
keypair, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 4) | ||
require.NoError(t, err) | ||
|
||
// Test decrypting the private key | ||
privateKey, err := keypair.GetDecryptedPrivateKey() | ||
require.NoError(t, err) | ||
assert.IsType(t, ed25519.PrivateKey{}, privateKey) | ||
assert.Len(t, privateKey, ed25519.PrivateKeySize) | ||
|
||
// Verify the private key corresponds to the public key | ||
publicKey := privateKey.Public().(ed25519.PublicKey) | ||
assert.Len(t, publicKey, ed25519.PublicKeySize) | ||
}) | ||
|
||
t.Run("DeleteUserSSHKeypair", func(t *testing.T) { | ||
// Create a keypair | ||
_, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 5) | ||
require.NoError(t, err) | ||
|
||
// Verify it exists | ||
_, err = repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 5) | ||
require.NoError(t, err) | ||
|
||
// Delete it | ||
err = repo_model.DeleteUserSSHKeypair(db.DefaultContext, 5) | ||
require.NoError(t, err) | ||
|
||
// Verify it's gone | ||
_, err = repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 5) | ||
assert.ErrorIs(t, err, util.ErrNotExist) | ||
}) | ||
|
||
t.Run("RegenerateUserSSHKeypair", func(t *testing.T) { | ||
// Create initial keypair | ||
original, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 6) | ||
require.NoError(t, err) | ||
|
||
// Regenerate it | ||
regenerated, err := repo_model.RegenerateUserSSHKeypair(db.DefaultContext, 6) | ||
require.NoError(t, err) | ||
|
||
// Verify it's different | ||
assert.NotEqual(t, original.PublicKey, regenerated.PublicKey) | ||
assert.NotEqual(t, original.PrivateKeyEncrypted, regenerated.PrivateKeyEncrypted) | ||
assert.NotEqual(t, original.Fingerprint, regenerated.Fingerprint) | ||
assert.Equal(t, original.OwnerID, regenerated.OwnerID) | ||
}) | ||
} | ||
|
||
func TestUserSSHKeypairConcurrency(t *testing.T) { | ||
require.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
if setting.SecretKey == "" { | ||
setting.SecretKey = "test-secret-key-for-testing" | ||
} | ||
|
||
// Test concurrent creation of keypairs to ensure no race conditions | ||
t.Run("ConcurrentCreation", func(t *testing.T) { | ||
ctx := db.DefaultContext | ||
results := make(chan error, 10) | ||
|
||
// Start multiple goroutines creating keypairs for different owners | ||
for i := range 10 { | ||
go func(ownerID int64) { | ||
_, err := repo_model.CreateUserSSHKeypair(ctx, ownerID+100) | ||
results <- err | ||
}(int64(i)) | ||
} | ||
|
||
// Check all creations succeeded | ||
for range 10 { | ||
err := <-results | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated change now.