Skip to content

Commit 79daf31

Browse files
dnmgnslunny
authored andcommitted
Setting to disable authorized_keys backup (#1856)
* Add setting to disable authorized_keys backup when rewriting public keys Signed-off-by: Magnus Lindvall <[email protected]> * Update default value to comply with documentation Signed-off-by: Magnus Lindvall <[email protected]> * Use tmp-file instead of bak-file for saving manually added keys. Signed-off-by: Magnus Lindvall <[email protected]> * Change casing Signed-off-by: Magnus Lindvall <[email protected]> * Change casing and build bakpath with sprintf only Signed-off-by: Magnus Lindvall <[email protected]> * Only close file once Signed-off-by: Magnus Lindvall <[email protected]> * Do not modify calcFingerprint Signed-off-by: Magnus Lindvall <[email protected]> * Fix casing Signed-off-by: Magnus Lindvall <[email protected]> * Change style from disable to enable Signed-off-by: Magnus Lindvall <[email protected]> * Change name, just SSH_BACKUP_AUTHORIZED_KEYS Signed-off-by: Magnus Lindvall <[email protected]> * Do not check for directory existence if backup is disabled Signed-off-by: Magnus Lindvall <[email protected]>
1 parent a037cd8 commit 79daf31

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

conf/app.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ SSH_ROOT_PATH =
124124
SSH_KEY_TEST_PATH =
125125
; Path to ssh-keygen, default is 'ssh-keygen' and let shell find out which one to call.
126126
SSH_KEYGEN_PATH = ssh-keygen
127+
; Enable SSH Authorized Key Backup when rewriting all keys, default is true
128+
SSH_BACKUP_AUTHORIZED_KEYS = true
127129
; Indicate whether to check minimum key size with corresponding type
128130
MINIMUM_KEY_SIZE_CHECK = false
129131
; Disable CDN even in "prod" mode

models/ssh_key.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error {
324324
sshOpLocker.Lock()
325325
defer sshOpLocker.Unlock()
326326

327-
fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
328-
f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
327+
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
328+
f, err := os.OpenFile(fPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
329329
if err != nil {
330330
return err
331331
}
@@ -558,53 +558,53 @@ func RewriteAllPublicKeys() error {
558558
sshOpLocker.Lock()
559559
defer sshOpLocker.Unlock()
560560

561-
fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
562-
tmpPath := fpath + ".tmp"
563-
f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
561+
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
562+
tmpPath := fPath + ".tmp"
563+
t, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
564564
if err != nil {
565565
return err
566566
}
567567
defer func() {
568-
f.Close()
568+
t.Close()
569569
os.Remove(tmpPath)
570570
}()
571571

572+
if setting.SSH.AuthorizedKeysBackup && com.IsExist(fPath) {
573+
bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())
574+
if err = com.Copy(fPath, bakPath); err != nil {
575+
return err
576+
}
577+
}
578+
572579
err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
573-
_, err = f.WriteString((bean.(*PublicKey)).AuthorizedString())
580+
_, err = t.WriteString((bean.(*PublicKey)).AuthorizedString())
574581
return err
575582
})
576583
if err != nil {
577584
return err
578585
}
579586

580-
if com.IsExist(fpath) {
581-
bakPath := fpath + fmt.Sprintf("_%d.gitea_bak", time.Now().Unix())
582-
if err = com.Copy(fpath, bakPath); err != nil {
583-
return err
584-
}
585-
586-
p, err := os.Open(bakPath)
587+
if com.IsExist(fPath) {
588+
f, err := os.Open(fPath)
587589
if err != nil {
588590
return err
589591
}
590-
defer p.Close()
591-
592-
scanner := bufio.NewScanner(p)
592+
scanner := bufio.NewScanner(f)
593593
for scanner.Scan() {
594594
line := scanner.Text()
595595
if strings.HasPrefix(line, tplCommentPrefix) {
596596
scanner.Scan()
597597
continue
598598
}
599-
_, err = f.WriteString(line + "\n")
599+
_, err = t.WriteString(line + "\n")
600600
if err != nil {
601601
return err
602602
}
603603
}
604+
defer f.Close()
604605
}
605606

606-
f.Close()
607-
if err = os.Rename(tmpPath, fpath); err != nil {
607+
if err = os.Rename(tmpPath, fPath); err != nil {
608608
return err
609609
}
610610

modules/setting/setting.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,18 @@ var (
8787
EnablePprof bool
8888

8989
SSH = struct {
90-
Disabled bool `ini:"DISABLE_SSH"`
91-
StartBuiltinServer bool `ini:"START_SSH_SERVER"`
92-
Domain string `ini:"SSH_DOMAIN"`
93-
Port int `ini:"SSH_PORT"`
94-
ListenHost string `ini:"SSH_LISTEN_HOST"`
95-
ListenPort int `ini:"SSH_LISTEN_PORT"`
96-
RootPath string `ini:"SSH_ROOT_PATH"`
97-
KeyTestPath string `ini:"SSH_KEY_TEST_PATH"`
98-
KeygenPath string `ini:"SSH_KEYGEN_PATH"`
99-
MinimumKeySizeCheck bool `ini:"-"`
100-
MinimumKeySizes map[string]int `ini:"-"`
90+
Disabled bool `ini:"DISABLE_SSH"`
91+
StartBuiltinServer bool `ini:"START_SSH_SERVER"`
92+
Domain string `ini:"SSH_DOMAIN"`
93+
Port int `ini:"SSH_PORT"`
94+
ListenHost string `ini:"SSH_LISTEN_HOST"`
95+
ListenPort int `ini:"SSH_LISTEN_PORT"`
96+
RootPath string `ini:"SSH_ROOT_PATH"`
97+
KeyTestPath string `ini:"SSH_KEY_TEST_PATH"`
98+
KeygenPath string `ini:"SSH_KEYGEN_PATH"`
99+
AuthorizedKeysBackup bool `ini:"SSH_AUTHORIZED_KEYS_BACKUP"`
100+
MinimumKeySizeCheck bool `ini:"-"`
101+
MinimumKeySizes map[string]int `ini:"-"`
101102
}{
102103
Disabled: false,
103104
StartBuiltinServer: false,
@@ -703,6 +704,7 @@ func NewContext() {
703704
SSH.MinimumKeySizes[strings.ToLower(key.Name())] = key.MustInt()
704705
}
705706
}
707+
SSH.AuthorizedKeysBackup = sec.Key("SSH_AUTHORIZED_KEYS_BACKUP").MustBool(true)
706708

707709
if err = Cfg.Section("server").MapTo(&LFS); err != nil {
708710
log.Fatal(4, "Failed to map LFS settings: %v", err)

0 commit comments

Comments
 (0)