Skip to content

Commit

Permalink
all: unwrap database.PublicKeysStore interface (#7702)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Mar 24, 2024
1 parent 5cf0189 commit 895e553
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
4 changes: 4 additions & 0 deletions internal/database/database.go
Expand Up @@ -179,3 +179,7 @@ func (db *DB) Organizations() *OrganizationsStore {
func (db *DB) Permissions() *PermissionsStore {
return newPermissionsStore(db.db)
}

func (db *DB) PublicKey() *PublicKeysStore {
return newPublicKeysStore(db.db)
}
30 changes: 10 additions & 20 deletions internal/database/public_keys.go
Expand Up @@ -15,32 +15,22 @@ import (
"gogs.io/gogs/internal/osutil"
)

// PublicKeysStore is the persistent interface for public keys.
type PublicKeysStore interface {
// RewriteAuthorizedKeys rewrites the "authorized_keys" file under the SSH root
// path with all public keys stored in the database.
RewriteAuthorizedKeys() error
// PublicKeysStore is the storage layer for public keys.
type PublicKeysStore struct {
db *gorm.DB
}

var PublicKeys PublicKeysStore

var _ PublicKeysStore = (*publicKeysStore)(nil)

type publicKeysStore struct {
*gorm.DB
}

// NewPublicKeysStore returns a persistent interface for public keys with given
// database connection.
func NewPublicKeysStore(db *gorm.DB) PublicKeysStore {
return &publicKeysStore{DB: db}
func newPublicKeysStore(db *gorm.DB) *PublicKeysStore {
return &PublicKeysStore{db: db}
}

func authorizedKeysPath() string {
return filepath.Join(conf.SSH.RootPath, "authorized_keys")
}

func (s *publicKeysStore) RewriteAuthorizedKeys() error {
// RewriteAuthorizedKeys rewrites the "authorized_keys" file under the SSH root
// path with all public keys stored in the database.
func (s *PublicKeysStore) RewriteAuthorizedKeys() error {
sshOpLocker.Lock()
defer sshOpLocker.Unlock()

Expand All @@ -61,15 +51,15 @@ func (s *publicKeysStore) RewriteAuthorizedKeys() error {

// NOTE: More recently updated keys are more likely to be used more frequently,
// putting them in the earlier lines could speed up the key lookup by SSHD.
rows, err := s.Model(&PublicKey{}).Order("updated_unix DESC").Rows()
rows, err := s.db.Model(&PublicKey{}).Order("updated_unix DESC").Rows()
if err != nil {
return errors.Wrap(err, "iterate public keys")
}
defer func() { _ = rows.Close() }()

for rows.Next() {
var key PublicKey
err = s.ScanRows(rows, &key)
err = s.db.ScanRows(rows, &key)
if err != nil {
return errors.Wrap(err, "scan rows")
}
Expand Down
16 changes: 8 additions & 8 deletions internal/database/public_keys_test.go
Expand Up @@ -24,42 +24,42 @@ func TestPublicKeys(t *testing.T) {
t.Parallel()

ctx := context.Background()
db := &publicKeysStore{
DB: newTestDB(t, "publicKeysStore"),
s := &PublicKeysStore{
db: newTestDB(t, "PublicKeysStore"),
}

for _, tc := range []struct {
name string
test func(t *testing.T, ctx context.Context, db *publicKeysStore)
test func(t *testing.T, ctx context.Context, s *PublicKeysStore)
}{
{"RewriteAuthorizedKeys", publicKeysRewriteAuthorizedKeys},
} {
t.Run(tc.name, func(t *testing.T) {
t.Cleanup(func() {
err := clearTables(t, db.DB)
err := clearTables(t, s.db)
require.NoError(t, err)
})
tc.test(t, ctx, db)
tc.test(t, ctx, s)
})
if t.Failed() {
break
}
}
}

func publicKeysRewriteAuthorizedKeys(t *testing.T, ctx context.Context, db *publicKeysStore) {
func publicKeysRewriteAuthorizedKeys(t *testing.T, ctx context.Context, s *PublicKeysStore) {
// TODO: Use PublicKeys.Add to replace SQL hack when the method is available.
publicKey := &PublicKey{
OwnerID: 1,
Name: "test-key",
Fingerprint: "12:f8:7e:78:61:b4:bf:e2:de:24:15:96:4e:d4:72:53",
Content: "test-key-content",
}
err := db.DB.Create(publicKey).Error
err := s.db.Create(publicKey).Error
require.NoError(t, err)
tempSSHRootPath := filepath.Join(os.TempDir(), "publicKeysRewriteAuthorizedKeys-tempSSHRootPath")
conf.SetMockSSH(t, conf.SSHOpts{RootPath: tempSSHRootPath})
err = db.RewriteAuthorizedKeys()
err = s.RewriteAuthorizedKeys()
require.NoError(t, err)

authorizedKeys, err := os.ReadFile(authorizedKeysPath())
Expand Down
4 changes: 2 additions & 2 deletions internal/database/users.go
Expand Up @@ -645,7 +645,7 @@ func (s *usersStore) DeleteByID(ctx context.Context, userID int64, skipRewriteAu
_ = os.Remove(userutil.CustomAvatarPath(userID))

if needsRewriteAuthorizedKeys {
err = NewPublicKeysStore(s.DB).RewriteAuthorizedKeys()
err = newPublicKeysStore(s.DB).RewriteAuthorizedKeys()
if err != nil {
return errors.Wrap(err, `rewrite "authorized_keys" file`)
}
Expand All @@ -672,7 +672,7 @@ func (s *usersStore) DeleteInactivated() error {
return errors.Wrapf(err, "delete user with ID %d", userID)
}
}
err = NewPublicKeysStore(s.DB).RewriteAuthorizedKeys()
err = newPublicKeysStore(s.DB).RewriteAuthorizedKeys()
if err != nil {
return errors.Wrap(err, `rewrite "authorized_keys" file`)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/database/users_test.go
Expand Up @@ -534,7 +534,7 @@ func usersDeleteByID(t *testing.T, ctx context.Context, db *usersStore) {
require.NoError(t, err)
tempSSHRootPath := filepath.Join(os.TempDir(), "usersDeleteByID-tempSSHRootPath")
conf.SetMockSSH(t, conf.SSHOpts{RootPath: tempSSHRootPath})
err = NewPublicKeysStore(db.DB).RewriteAuthorizedKeys()
err = newPublicKeysStore(db.DB).RewriteAuthorizedKeys()
require.NoError(t, err)

// Mock issue assignee
Expand Down

0 comments on commit 895e553

Please sign in to comment.