Skip to content

Commit ef13bba

Browse files
authored
Don't rewrite non-gitea public keys (#906)
* don't rewrite non-gitea public keys * add comment for public key
1 parent 341b3a0 commit ef13bba

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ var migrations = []Migration{
9090
NewMigration("generate and migrate Git hooks", generateAndMigrateGitHooks),
9191
// v20 -> v21
9292
NewMigration("use new avatar path name for security reason", useNewNameAvatars),
93+
// v21 -> v22
94+
NewMigration("rewrite authorized_keys file via new format", useNewPublickeyFormat),
9395
}
9496

9597
// Migrate database to current version

models/migrations/v21.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2017 Gitea. 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+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"path/filepath"
11+
12+
"code.gitea.io/gitea/modules/setting"
13+
14+
"github.com/go-xorm/xorm"
15+
)
16+
17+
const (
18+
tplCommentPrefix = `# gitea public key`
19+
tplPublicKey = tplCommentPrefix + "\n" + `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n"
20+
)
21+
22+
func useNewPublickeyFormat(x *xorm.Engine) error {
23+
fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
24+
tmpPath := fpath + ".tmp"
25+
f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
26+
if err != nil {
27+
return err
28+
}
29+
defer func() {
30+
f.Close()
31+
os.Remove(tmpPath)
32+
}()
33+
34+
type PublicKey struct {
35+
ID int64
36+
Content string
37+
}
38+
39+
err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
40+
key := bean.(*PublicKey)
41+
_, err = f.WriteString(fmt.Sprintf(tplPublicKey, setting.AppPath, key.ID, setting.CustomConf, key.Content))
42+
return err
43+
})
44+
if err != nil {
45+
return err
46+
}
47+
48+
f.Close()
49+
if err = os.Rename(tmpPath, fpath); err != nil {
50+
return err
51+
}
52+
return nil
53+
}

models/ssh_key.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package models
66

77
import (
8+
"bufio"
89
"encoding/base64"
910
"encoding/binary"
1011
"errors"
@@ -28,7 +29,8 @@ import (
2829
)
2930

3031
const (
31-
tplPublicKey = `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n"
32+
tplCommentPrefix = `# gitea public key`
33+
tplPublicKey = tplCommentPrefix + "\n" + `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n"
3234
)
3335

3436
var sshOpLocker sync.Mutex
@@ -553,22 +555,46 @@ func RewriteAllPublicKeys() error {
553555
if err != nil {
554556
return err
555557
}
556-
defer os.Remove(tmpPath)
558+
defer func() {
559+
f.Close()
560+
os.Remove(tmpPath)
561+
}()
557562

558563
err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
559564
_, err = f.WriteString((bean.(*PublicKey)).AuthorizedString())
560565
return err
561566
})
562-
f.Close()
563567
if err != nil {
564568
return err
565569
}
566570

567571
if com.IsExist(fpath) {
568-
if err = os.Remove(fpath); err != nil {
572+
bakPath := fpath + fmt.Sprintf("_%d.gitea_bak", time.Now().Unix())
573+
if err = com.Copy(fpath, bakPath); err != nil {
574+
return err
575+
}
576+
577+
p, err := os.Open(bakPath)
578+
if err != nil {
569579
return err
570580
}
581+
defer p.Close()
582+
583+
scanner := bufio.NewScanner(p)
584+
for scanner.Scan() {
585+
line := scanner.Text()
586+
if strings.HasPrefix(line, tplCommentPrefix) {
587+
scanner.Scan()
588+
continue
589+
}
590+
_, err = f.WriteString(line + "\n")
591+
if err != nil {
592+
return err
593+
}
594+
}
571595
}
596+
597+
f.Close()
572598
if err = os.Rename(tmpPath, fpath); err != nil {
573599
return err
574600
}

0 commit comments

Comments
 (0)