public_keys.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2023 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "os"
  7. "path/filepath"
  8. "github.com/pkg/errors"
  9. "gorm.io/gorm"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/osutil"
  12. )
  13. // PublicKeysStore is the persistent interface for public keys.
  14. type PublicKeysStore interface {
  15. // RewriteAuthorizedKeys rewrites the "authorized_keys" file under the SSH root
  16. // path with all public keys stored in the database.
  17. RewriteAuthorizedKeys() error
  18. }
  19. var PublicKeys PublicKeysStore
  20. var _ PublicKeysStore = (*publicKeys)(nil)
  21. type publicKeys struct {
  22. *gorm.DB
  23. }
  24. // NewPublicKeysStore returns a persistent interface for public keys with given
  25. // database connection.
  26. func NewPublicKeysStore(db *gorm.DB) PublicKeysStore {
  27. return &publicKeys{DB: db}
  28. }
  29. func authorizedKeysPath() string {
  30. return filepath.Join(conf.SSH.RootPath, "authorized_keys")
  31. }
  32. func (db *publicKeys) RewriteAuthorizedKeys() error {
  33. sshOpLocker.Lock()
  34. defer sshOpLocker.Unlock()
  35. err := os.MkdirAll(conf.SSH.RootPath, os.ModePerm)
  36. if err != nil {
  37. return errors.Wrap(err, "create SSH root path")
  38. }
  39. fpath := authorizedKeysPath()
  40. tempPath := fpath + ".tmp"
  41. f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  42. if err != nil {
  43. return errors.Wrap(err, "create temporary file")
  44. }
  45. defer func() {
  46. _ = f.Close()
  47. _ = os.Remove(tempPath)
  48. }()
  49. // NOTE: More recently updated keys are more likely to be used more frequently,
  50. // putting them in the earlier lines could speed up the key lookup by SSHD.
  51. rows, err := db.Model(&PublicKey{}).Order("updated_unix DESC").Rows()
  52. if err != nil {
  53. return errors.Wrap(err, "iterate public keys")
  54. }
  55. defer func() { _ = rows.Close() }()
  56. for rows.Next() {
  57. var key PublicKey
  58. err = db.ScanRows(rows, &key)
  59. if err != nil {
  60. return errors.Wrap(err, "scan rows")
  61. }
  62. _, err = f.WriteString(key.AuthorizedString())
  63. if err != nil {
  64. return errors.Wrapf(err, "write key %d", key.ID)
  65. }
  66. }
  67. if err = rows.Err(); err != nil {
  68. return errors.Wrap(err, "check rows.Err")
  69. }
  70. err = f.Close()
  71. if err != nil {
  72. return errors.Wrap(err, "close temporary file")
  73. }
  74. if osutil.IsExist(fpath) {
  75. err = os.Remove(fpath)
  76. if err != nil {
  77. return errors.Wrap(err, "remove")
  78. }
  79. }
  80. err = os.Rename(tempPath, fpath)
  81. if err != nil {
  82. return errors.Wrap(err, "rename")
  83. }
  84. return nil
  85. }