Skip to content

Commit 671781e

Browse files
committed
add comment for public key
1 parent d230fc2 commit 671781e

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
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: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
)
3030

3131
const (
32-
tplCommentPrefix = `command="%s serv`
33-
tplPublicKey = tplCommentPrefix + ` 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"
3434
)
3535

3636
var sshOpLocker sync.Mutex
@@ -555,43 +555,45 @@ func RewriteAllPublicKeys() error {
555555
if err != nil {
556556
return err
557557
}
558-
defer os.Remove(tmpPath)
558+
defer func() {
559+
f.Close()
560+
os.Remove(tmpPath)
561+
}()
559562

560563
err = x.Iterate(new(PublicKey), func(idx int, bean interface{}) (err error) {
561564
_, err = f.WriteString((bean.(*PublicKey)).AuthorizedString())
562565
return err
563566
})
564-
565567
if err != nil {
566-
f.Close()
567568
return err
568569
}
569570

570571
if com.IsExist(fpath) {
571-
if err = os.Rename(fpath, fpath+".gitea_bak"); err != nil {
572-
f.Close()
572+
bakPath := fpath + fmt.Sprintf("_%d.gitea_bak", time.Now().Unix())
573+
if err = com.Copy(fpath, bakPath); err != nil {
573574
return err
574575
}
575576

576-
p, err := os.Open(fpath + ".gitea_bak")
577+
p, err := os.Open(bakPath)
577578
if err != nil {
578-
f.Close()
579579
return err
580580
}
581581
defer p.Close()
582+
582583
scanner := bufio.NewScanner(p)
583-
prefix := fmt.Sprintf(tplCommentPrefix, setting.AppPath)
584584
for scanner.Scan() {
585585
line := scanner.Text()
586-
if !strings.HasPrefix(line, prefix) {
587-
_, err = f.WriteString(line + "\n")
588-
if err != nil {
589-
f.Close()
590-
return err
591-
}
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
592593
}
593594
}
594595
}
596+
595597
f.Close()
596598
if err = os.Rename(tmpPath, fpath); err != nil {
597599
return err

0 commit comments

Comments
 (0)