Skip to content

Commit f97acdc

Browse files
update to UserSSHKeypair per feedback
1 parent cee2ca0 commit f97acdc

File tree

8 files changed

+65
-60
lines changed

8 files changed

+65
-60
lines changed

models/migrations/migrations.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"code.gitea.io/gitea/models/migrations/v1_22"
2525
"code.gitea.io/gitea/models/migrations/v1_23"
2626
"code.gitea.io/gitea/models/migrations/v1_24"
27+
"code.gitea.io/gitea/models/migrations/v1_25"
2728
"code.gitea.io/gitea/models/migrations/v1_6"
2829
"code.gitea.io/gitea/models/migrations/v1_7"
2930
"code.gitea.io/gitea/models/migrations/v1_8"
@@ -382,6 +383,10 @@ func prepareMigrationTasks() []*migration {
382383
newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode),
383384
newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable),
384385
newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor),
386+
387+
// Gitea 1.24.0 ends at migration ID number 320 (database version 321)
388+
newMigration(321, "Use LONGTEXT for some columns and fix review_state.updated_files column", v1_25.UseLongTextInSomeColumnsAndFixBugs),
389+
newMigration(322, "Add Mirror SSH keypair table", v1_25.AddUserSSHKeypairTable),
385390
}
386391
return preparedMigrations
387392
}

models/migrations/v1_25/v322.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"xorm.io/xorm"
1010
)
1111

12-
func AddMirrorSSHKeypairTable(x *xorm.Engine) error {
13-
type MirrorSSHKeypair struct {
12+
func AddUserSSHKeypairTable(x *xorm.Engine) error {
13+
type UserSSHKeypair struct {
1414
ID int64 `xorm:"pk autoincr"`
1515
OwnerID int64 `xorm:"INDEX NOT NULL"`
1616
PrivateKeyEncrypted string `xorm:"TEXT NOT NULL"`
@@ -20,5 +20,5 @@ func AddMirrorSSHKeypairTable(x *xorm.Engine) error {
2020
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
2121
}
2222

23-
return x.Sync(new(MirrorSSHKeypair))
23+
return x.Sync(new(UserSSHKeypair))
2424
}

models/repo/mirror_ssh_keypair.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"golang.org/x/crypto/ssh"
2323
)
2424

25-
// MirrorSSHKeypair represents an SSH keypair for repository mirroring
26-
type MirrorSSHKeypair struct {
25+
// UserSSHKeypair represents an SSH keypair for repository mirroring
26+
type UserSSHKeypair struct {
2727
ID int64 `xorm:"pk autoincr"`
2828
OwnerID int64 `xorm:"INDEX NOT NULL"`
2929
PrivateKeyEncrypted string `xorm:"TEXT NOT NULL"`
@@ -34,12 +34,12 @@ type MirrorSSHKeypair struct {
3434
}
3535

3636
func init() {
37-
db.RegisterModel(new(MirrorSSHKeypair))
37+
db.RegisterModel(new(UserSSHKeypair))
3838
}
3939

40-
// GetMirrorSSHKeypairByOwner gets the most recent SSH keypair for the given owner
41-
func GetMirrorSSHKeypairByOwner(ctx context.Context, ownerID int64) (*MirrorSSHKeypair, error) {
42-
keypair := &MirrorSSHKeypair{}
40+
// GetUserSSHKeypairByOwner gets the most recent SSH keypair for the given owner
41+
func GetUserSSHKeypairByOwner(ctx context.Context, ownerID int64) (*UserSSHKeypair, error) {
42+
keypair := &UserSSHKeypair{}
4343
has, err := db.GetEngine(ctx).Where("owner_id = ?", ownerID).
4444
Desc("created_unix").Get(keypair)
4545
if err != nil {
@@ -51,8 +51,8 @@ func GetMirrorSSHKeypairByOwner(ctx context.Context, ownerID int64) (*MirrorSSHK
5151
return keypair, nil
5252
}
5353

54-
// CreateMirrorSSHKeypair creates a new SSH keypair for mirroring
55-
func CreateMirrorSSHKeypair(ctx context.Context, ownerID int64) (*MirrorSSHKeypair, error) {
54+
// CreateUserSSHKeypair creates a new SSH keypair for mirroring
55+
func CreateUserSSHKeypair(ctx context.Context, ownerID int64) (*UserSSHKeypair, error) {
5656
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
5757
if err != nil {
5858
return nil, fmt.Errorf("failed to generate Ed25519 keypair: %w", err)
@@ -73,7 +73,7 @@ func CreateMirrorSSHKeypair(ctx context.Context, ownerID int64) (*MirrorSSHKeypa
7373
return nil, fmt.Errorf("failed to encrypt private key: %w", err)
7474
}
7575

76-
keypair := &MirrorSSHKeypair{
76+
keypair := &UserSSHKeypair{
7777
OwnerID: ownerID,
7878
PrivateKeyEncrypted: privateKeyEncrypted,
7979
PublicKey: publicKeyStr,
@@ -84,7 +84,7 @@ func CreateMirrorSSHKeypair(ctx context.Context, ownerID int64) (*MirrorSSHKeypa
8484
}
8585

8686
// GetDecryptedPrivateKey returns the decrypted private key
87-
func (k *MirrorSSHKeypair) GetDecryptedPrivateKey() (ed25519.PrivateKey, error) {
87+
func (k *UserSSHKeypair) GetDecryptedPrivateKey() (ed25519.PrivateKey, error) {
8888
decrypted, err := secret.DecryptSecret(setting.SecretKey, k.PrivateKeyEncrypted)
8989
if err != nil {
9090
return nil, fmt.Errorf("failed to decrypt private key: %w", err)
@@ -93,7 +93,7 @@ func (k *MirrorSSHKeypair) GetDecryptedPrivateKey() (ed25519.PrivateKey, error)
9393
}
9494

9595
// GetPublicKeyWithComment returns the public key with a descriptive comment (namespace-fingerprint@domain)
96-
func (k *MirrorSSHKeypair) GetPublicKeyWithComment(ctx context.Context) (string, error) {
96+
func (k *UserSSHKeypair) GetPublicKeyWithComment(ctx context.Context) (string, error) {
9797
owner, err := user_model.GetUserByID(ctx, k.OwnerID)
9898
if err != nil {
9999
return k.PublicKey, nil
@@ -113,14 +113,14 @@ func (k *MirrorSSHKeypair) GetPublicKeyWithComment(ctx context.Context) (string,
113113
return strings.TrimSpace(k.PublicKey) + " " + comment, nil
114114
}
115115

116-
// DeleteMirrorSSHKeypair deletes an SSH keypair
117-
func DeleteMirrorSSHKeypair(ctx context.Context, ownerID int64) error {
118-
_, err := db.GetEngine(ctx).Where("owner_id = ?", ownerID).Delete(&MirrorSSHKeypair{})
116+
// DeleteUserSSHKeypair deletes an SSH keypair
117+
func DeleteUserSSHKeypair(ctx context.Context, ownerID int64) error {
118+
_, err := db.GetEngine(ctx).Where("owner_id = ?", ownerID).Delete(&UserSSHKeypair{})
119119
return err
120120
}
121121

122-
// RegenerateMirrorSSHKeypair regenerates an SSH keypair for the given owner
123-
func RegenerateMirrorSSHKeypair(ctx context.Context, ownerID int64) (*MirrorSSHKeypair, error) {
122+
// RegenerateUserSSHKeypair regenerates an SSH keypair for the given owner
123+
func RegenerateUserSSHKeypair(ctx context.Context, ownerID int64) (*UserSSHKeypair, error) {
124124
// TODO: This creates a new one old ones will be garbage collected later, as the user may accidentally regenerate
125-
return CreateMirrorSSHKeypair(ctx, ownerID)
125+
return CreateUserSSHKeypair(ctx, ownerID)
126126
}

models/repo/mirror_ssh_keypair_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717
"github.com/stretchr/testify/require"
1818
)
1919

20-
func TestMirrorSSHKeypair(t *testing.T) {
20+
func TestUserSSHKeypair(t *testing.T) {
2121
require.NoError(t, unittest.PrepareTestDatabase())
2222

23-
t.Run("CreateMirrorSSHKeypair", func(t *testing.T) {
23+
t.Run("CreateUserSSHKeypair", func(t *testing.T) {
2424
// Test creating a new SSH keypair for a user
25-
keypair, err := repo_model.CreateMirrorSSHKeypair(db.DefaultContext, 1)
25+
keypair, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 1)
2626
require.NoError(t, err)
2727
assert.NotNil(t, keypair)
2828
assert.Equal(t, int64(1), keypair.OwnerID)
@@ -36,7 +36,7 @@ func TestMirrorSSHKeypair(t *testing.T) {
3636
assert.Contains(t, keypair.PublicKey, "ssh-ed25519")
3737

3838
// Test creating a keypair for an organization
39-
orgKeypair, err := repo_model.CreateMirrorSSHKeypair(db.DefaultContext, 2)
39+
orgKeypair, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 2)
4040
require.NoError(t, err)
4141
assert.NotNil(t, orgKeypair)
4242
assert.Equal(t, int64(2), orgKeypair.OwnerID)
@@ -46,20 +46,20 @@ func TestMirrorSSHKeypair(t *testing.T) {
4646
assert.NotEqual(t, keypair.Fingerprint, orgKeypair.Fingerprint)
4747
})
4848

49-
t.Run("GetMirrorSSHKeypairByOwner", func(t *testing.T) {
49+
t.Run("GetUserSSHKeypairByOwner", func(t *testing.T) {
5050
// Create a keypair first
51-
created, err := repo_model.CreateMirrorSSHKeypair(db.DefaultContext, 3)
51+
created, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 3)
5252
require.NoError(t, err)
5353

5454
// Test retrieving the keypair
55-
retrieved, err := repo_model.GetMirrorSSHKeypairByOwner(db.DefaultContext, 3)
55+
retrieved, err := repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 3)
5656
require.NoError(t, err)
5757
assert.Equal(t, created.ID, retrieved.ID)
5858
assert.Equal(t, created.PublicKey, retrieved.PublicKey)
5959
assert.Equal(t, created.Fingerprint, retrieved.Fingerprint)
6060

6161
// Test retrieving non-existent keypair
62-
_, err = repo_model.GetMirrorSSHKeypairByOwner(db.DefaultContext, 999)
62+
_, err = repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 999)
6363
assert.ErrorIs(t, err, util.ErrNotExist)
6464
})
6565

@@ -70,7 +70,7 @@ func TestMirrorSSHKeypair(t *testing.T) {
7070
}
7171

7272
// Create a keypair
73-
keypair, err := repo_model.CreateMirrorSSHKeypair(db.DefaultContext, 4)
73+
keypair, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 4)
7474
require.NoError(t, err)
7575

7676
// Test decrypting the private key
@@ -84,31 +84,31 @@ func TestMirrorSSHKeypair(t *testing.T) {
8484
assert.Len(t, publicKey, ed25519.PublicKeySize)
8585
})
8686

87-
t.Run("DeleteMirrorSSHKeypair", func(t *testing.T) {
87+
t.Run("DeleteUserSSHKeypair", func(t *testing.T) {
8888
// Create a keypair
89-
_, err := repo_model.CreateMirrorSSHKeypair(db.DefaultContext, 5)
89+
_, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 5)
9090
require.NoError(t, err)
9191

9292
// Verify it exists
93-
_, err = repo_model.GetMirrorSSHKeypairByOwner(db.DefaultContext, 5)
93+
_, err = repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 5)
9494
require.NoError(t, err)
9595

9696
// Delete it
97-
err = repo_model.DeleteMirrorSSHKeypair(db.DefaultContext, 5)
97+
err = repo_model.DeleteUserSSHKeypair(db.DefaultContext, 5)
9898
require.NoError(t, err)
9999

100100
// Verify it's gone
101-
_, err = repo_model.GetMirrorSSHKeypairByOwner(db.DefaultContext, 5)
101+
_, err = repo_model.GetUserSSHKeypairByOwner(db.DefaultContext, 5)
102102
assert.ErrorIs(t, err, util.ErrNotExist)
103103
})
104104

105-
t.Run("RegenerateMirrorSSHKeypair", func(t *testing.T) {
105+
t.Run("RegenerateUserSSHKeypair", func(t *testing.T) {
106106
// Create initial keypair
107-
original, err := repo_model.CreateMirrorSSHKeypair(db.DefaultContext, 6)
107+
original, err := repo_model.CreateUserSSHKeypair(db.DefaultContext, 6)
108108
require.NoError(t, err)
109109

110110
// Regenerate it
111-
regenerated, err := repo_model.RegenerateMirrorSSHKeypair(db.DefaultContext, 6)
111+
regenerated, err := repo_model.RegenerateUserSSHKeypair(db.DefaultContext, 6)
112112
require.NoError(t, err)
113113

114114
// Verify it's different
@@ -119,7 +119,7 @@ func TestMirrorSSHKeypair(t *testing.T) {
119119
})
120120
}
121121

122-
func TestMirrorSSHKeypairConcurrency(t *testing.T) {
122+
func TestUserSSHKeypairConcurrency(t *testing.T) {
123123
require.NoError(t, unittest.PrepareTestDatabase())
124124

125125
if setting.SecretKey == "" {
@@ -134,7 +134,7 @@ func TestMirrorSSHKeypairConcurrency(t *testing.T) {
134134
// Start multiple goroutines creating keypairs for different owners
135135
for i := range 10 {
136136
go func(ownerID int64) {
137-
_, err := repo_model.CreateMirrorSSHKeypair(ctx, ownerID+100)
137+
_, err := repo_model.CreateUserSSHKeypair(ctx, ownerID+100)
138138
results <- err
139139
}(int64(i))
140140
}

modules/ssh/mirror.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ func IsSSHURL(url string) bool {
2020
}
2121

2222
// GetOrCreateSSHKeypairForUser gets or creates an SSH keypair for the given user
23-
func GetOrCreateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.MirrorSSHKeypair, error) {
24-
keypair, err := repo_model.GetMirrorSSHKeypairByOwner(ctx, userID)
23+
func GetOrCreateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.UserSSHKeypair, error) {
24+
keypair, err := repo_model.GetUserSSHKeypairByOwner(ctx, userID)
2525
if err != nil {
2626
if db.IsErrNotExist(err) {
2727
log.Debug("Creating new SSH keypair for user %d", userID)
28-
return repo_model.CreateMirrorSSHKeypair(ctx, userID)
28+
return repo_model.CreateUserSSHKeypair(ctx, userID)
2929
}
3030
return nil, fmt.Errorf("failed to get SSH keypair for user %d: %w", userID, err)
3131
}
3232
return keypair, nil
3333
}
3434

3535
// GetOrCreateSSHKeypairForOrg gets or creates an SSH keypair for the given organization
36-
func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.MirrorSSHKeypair, error) {
37-
keypair, err := repo_model.GetMirrorSSHKeypairByOwner(ctx, orgID)
36+
func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.UserSSHKeypair, error) {
37+
keypair, err := repo_model.GetUserSSHKeypairByOwner(ctx, orgID)
3838
if err != nil {
3939
if db.IsErrNotExist(err) {
4040
log.Debug("Creating new SSH keypair for organization %d", orgID)
41-
return repo_model.CreateMirrorSSHKeypair(ctx, orgID)
41+
return repo_model.CreateUserSSHKeypair(ctx, orgID)
4242
}
4343
return nil, fmt.Errorf("failed to get SSH keypair for organization %d: %w", orgID, err)
4444
}
@@ -48,7 +48,7 @@ func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.
4848
// GetSSHKeypairForRepository gets the appropriate SSH keypair for a repository
4949
// If the repository belongs to an organization, it uses the org's keypair,
5050
// otherwise it uses the user's keypair
51-
func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*repo_model.MirrorSSHKeypair, error) {
51+
func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*repo_model.UserSSHKeypair, error) {
5252
if repo.Owner == nil {
5353
owner, err := user_model.GetUserByID(ctx, repo.OwnerID)
5454
if err != nil {
@@ -65,7 +65,7 @@ func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository
6565

6666
// GetSSHKeypairForURL gets the appropriate SSH keypair for a given repository and URL
6767
// Returns nil if the URL is not an SSH URL
68-
func GetSSHKeypairForURL(ctx context.Context, repo *repo_model.Repository, url string) (*repo_model.MirrorSSHKeypair, error) {
68+
func GetSSHKeypairForURL(ctx context.Context, repo *repo_model.Repository, url string) (*repo_model.UserSSHKeypair, error) {
6969
if !IsSSHURL(url) {
7070
return nil, nil
7171
}

routers/web/user/setting/keys.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ func loadKeysData(ctx *context.Context) {
353353
// Create a struct with the public key including comment
354354
publicKeyWithComment, _ := mirrorKeypair.GetPublicKeyWithComment(ctx)
355355
mirrorKeyData := struct {
356-
*repo_model.MirrorSSHKeypair
356+
*repo_model.UserSSHKeypair
357357
PublicKeyWithComment string
358358
}{
359-
MirrorSSHKeypair: mirrorKeypair,
359+
UserSSHKeypair: mirrorKeypair,
360360
PublicKeyWithComment: publicKeyWithComment,
361361
}
362362

@@ -366,8 +366,8 @@ func loadKeysData(ctx *context.Context) {
366366
}
367367
}
368368

369-
// RegenerateMirrorSSHKeyPair regenerates the SSH keypair for repository mirroring
370-
func RegenerateMirrorSSHKeyPair(ctx *context.Context) {
369+
// RegenerateUserSSHKeypair regenerates the SSH keypair for repository mirroring
370+
func RegenerateUserSSHKeypair(ctx *context.Context) {
371371
_, err := mirror_service.RegenerateSSHKeypairForUser(ctx, ctx.Doer.ID)
372372
if err != nil {
373373
ctx.ServerError("RegenerateSSHKeypairForUser", err)

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ func registerWebRoutes(m *web.Router) {
641641
m.Combo("/keys").Get(user_setting.Keys).
642642
Post(web.Bind(forms.AddKeyForm{}), user_setting.KeysPost)
643643
m.Post("/keys/delete", user_setting.DeleteKey)
644-
m.Post("/keys/mirror-ssh/regenerate", user_setting.RegenerateMirrorSSHKeyPair)
644+
m.Post("/keys/mirror-ssh/regenerate", user_setting.RegenerateUserSSHKeypair)
645645
m.Group("/packages", func() {
646646
m.Get("", user_setting.Packages)
647647
m.Group("/rules", func() {

services/mirror/ssh_keypair.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@ import (
1212
)
1313

1414
// GetOrCreateSSHKeypairForUser gets or creates an SSH keypair for the given user
15-
func GetOrCreateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.MirrorSSHKeypair, error) {
15+
func GetOrCreateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.UserSSHKeypair, error) {
1616
return ssh_module.GetOrCreateSSHKeypairForUser(ctx, userID)
1717
}
1818

1919
// GetOrCreateSSHKeypairForOrg gets or creates an SSH keypair for the given organization
20-
func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.MirrorSSHKeypair, error) {
20+
func GetOrCreateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.UserSSHKeypair, error) {
2121
return ssh_module.GetOrCreateSSHKeypairForOrg(ctx, orgID)
2222
}
2323

2424
// GetSSHKeypairForRepository gets the appropriate SSH keypair for a repository
2525
// If the repository belongs to an organization, it uses the org's keypair,
2626
// otherwise it uses the user's keypair
27-
func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*repo_model.MirrorSSHKeypair, error) {
27+
func GetSSHKeypairForRepository(ctx context.Context, repo *repo_model.Repository) (*repo_model.UserSSHKeypair, error) {
2828
return ssh_module.GetSSHKeypairForRepository(ctx, repo)
2929
}
3030

3131
// RegenerateSSHKeypairForUser regenerates the SSH keypair for a user
32-
func RegenerateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.MirrorSSHKeypair, error) {
32+
func RegenerateSSHKeypairForUser(ctx context.Context, userID int64) (*repo_model.UserSSHKeypair, error) {
3333
log.Info("Regenerating SSH keypair for user %d", userID)
34-
return repo_model.RegenerateMirrorSSHKeypair(ctx, userID)
34+
return repo_model.RegenerateUserSSHKeypair(ctx, userID)
3535
}
3636

3737
// RegenerateSSHKeypairForOrg regenerates the SSH keypair for an organization
38-
func RegenerateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.MirrorSSHKeypair, error) {
38+
func RegenerateSSHKeypairForOrg(ctx context.Context, orgID int64) (*repo_model.UserSSHKeypair, error) {
3939
log.Info("Regenerating SSH keypair for organization %d", orgID)
40-
return repo_model.RegenerateMirrorSSHKeypair(ctx, orgID)
40+
return repo_model.RegenerateUserSSHKeypair(ctx, orgID)
4141
}
4242

4343
// IsSSHURL checks if a URL is an SSH URL
@@ -47,6 +47,6 @@ func IsSSHURL(url string) bool {
4747

4848
// GetSSHKeypairForURL gets the appropriate SSH keypair for a given repository and URL
4949
// Returns nil if the URL is not an SSH URL
50-
func GetSSHKeypairForURL(ctx context.Context, repo *repo_model.Repository, url string) (*repo_model.MirrorSSHKeypair, error) {
50+
func GetSSHKeypairForURL(ctx context.Context, repo *repo_model.Repository, url string) (*repo_model.UserSSHKeypair, error) {
5151
return ssh_module.GetSSHKeypairForURL(ctx, repo, url)
5252
}

0 commit comments

Comments
 (0)