Skip to content

Commit 440ff00

Browse files
wxiaoguangzeripathlunny
authored and
Sysoev, Vladimir
committed
Refactor SSH init code, fix directory creation for TrustedUserCAKeys file (go-gitea#20299)
* Refactor SSH init code, fix directory creation for TrustedUserCAKeys file * Update modules/ssh/init.go Co-authored-by: zeripath <[email protected]> * fix lint copyright * Update modules/ssh/init.go Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 941877a commit 440ff00

File tree

4 files changed

+63
-29
lines changed

4 files changed

+63
-29
lines changed

modules/setting/setting.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -840,16 +840,17 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
840840
SSH.StartBuiltinServer = false
841841
}
842842

843-
trustedUserCaKeys := sec.Key("SSH_TRUSTED_USER_CA_KEYS").Strings(",")
844-
for _, caKey := range trustedUserCaKeys {
843+
SSH.TrustedUserCAKeysFile = sec.Key("SSH_TRUSTED_USER_CA_KEYS_FILENAME").MustString(filepath.Join(SSH.RootPath, "gitea-trusted-user-ca-keys.pem"))
844+
845+
for _, caKey := range SSH.TrustedUserCAKeys {
845846
pubKey, _, _, _, err := gossh.ParseAuthorizedKey([]byte(caKey))
846847
if err != nil {
847848
log.Fatal("Failed to parse TrustedUserCaKeys: %s %v", caKey, err)
848849
}
849850

850851
SSH.TrustedUserCAKeysParsed = append(SSH.TrustedUserCAKeysParsed, pubKey)
851852
}
852-
if len(trustedUserCaKeys) > 0 {
853+
if len(SSH.TrustedUserCAKeys) > 0 {
853854
// Set the default as email,username otherwise we can leave it empty
854855
sec.Key("SSH_AUTHORIZED_PRINCIPALS_ALLOW").MustString("username,email")
855856
} else {
@@ -858,20 +859,6 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
858859

859860
SSH.AuthorizedPrincipalsAllow, SSH.AuthorizedPrincipalsEnabled = parseAuthorizedPrincipalsAllow(sec.Key("SSH_AUTHORIZED_PRINCIPALS_ALLOW").Strings(","))
860861

861-
if !SSH.Disabled && !SSH.StartBuiltinServer {
862-
if err = os.MkdirAll(SSH.KeyTestPath, 0o644); err != nil {
863-
log.Fatal("Failed to create '%s': %v", SSH.KeyTestPath, err)
864-
}
865-
866-
if len(trustedUserCaKeys) > 0 && SSH.AuthorizedPrincipalsEnabled {
867-
fname := sec.Key("SSH_TRUSTED_USER_CA_KEYS_FILENAME").MustString(filepath.Join(SSH.RootPath, "gitea-trusted-user-ca-keys.pem"))
868-
if err := os.WriteFile(fname,
869-
[]byte(strings.Join(trustedUserCaKeys, "\n")), 0o600); err != nil {
870-
log.Fatal("Failed to create '%s': %v", fname, err)
871-
}
872-
}
873-
}
874-
875862
SSH.MinimumKeySizeCheck = sec.Key("MINIMUM_KEY_SIZE_CHECK").MustBool(SSH.MinimumKeySizeCheck)
876863
minimumKeySizes := Cfg.Section("ssh.minimum_key_sizes").Keys()
877864
for _, key := range minimumKeySizes {

modules/ssh/init.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 The Gitea 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+
5+
package ssh
6+
7+
import (
8+
"fmt"
9+
"net"
10+
"os"
11+
"path/filepath"
12+
"strconv"
13+
"strings"
14+
15+
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/setting"
17+
)
18+
19+
func Init() error {
20+
if setting.SSH.Disabled {
21+
return nil
22+
}
23+
24+
if setting.SSH.StartBuiltinServer {
25+
Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
26+
log.Info("SSH server started on %s. Cipher list (%v), key exchange algorithms (%v), MACs (%v)",
27+
net.JoinHostPort(setting.SSH.ListenHost, strconv.Itoa(setting.SSH.ListenPort)),
28+
setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs,
29+
)
30+
return nil
31+
}
32+
33+
builtinUnused()
34+
35+
// FIXME: why 0o644 for a directory .....
36+
if err := os.MkdirAll(setting.SSH.KeyTestPath, 0o644); err != nil {
37+
return fmt.Errorf("failed to create directory %q for ssh key test: %w", setting.SSH.KeyTestPath, err)
38+
}
39+
40+
if len(setting.SSH.TrustedUserCAKeys) > 0 && setting.SSH.AuthorizedPrincipalsEnabled {
41+
caKeysFileName := setting.SSH.TrustedUserCAKeysFile
42+
caKeysFileDir := filepath.Dir(caKeysFileName)
43+
44+
err := os.MkdirAll(caKeysFileDir, 0o700) // SSH.RootPath by default (That is `~/.ssh` in most cases)
45+
if err != nil {
46+
return fmt.Errorf("failed to create directory %q for ssh trusted ca keys: %w", caKeysFileDir, err)
47+
}
48+
49+
if err := os.WriteFile(caKeysFileName, []byte(strings.Join(setting.SSH.TrustedUserCAKeys, "\n")), 0o600); err != nil {
50+
return fmt.Errorf("failed to write ssh trusted ca keys to %q: %w", caKeysFileName, err)
51+
}
52+
}
53+
54+
return nil
55+
}

modules/ssh/ssh_graceful.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func listen(server *ssh.Server) {
2929
log.Info("SSH Listener: %s Closed", server.Addr)
3030
}
3131

32-
// Unused informs our cleanup routine that we will not be using a ssh port
33-
func Unused() {
32+
// builtinUnused informs our cleanup routine that we will not be using a ssh port
33+
func builtinUnused() {
3434
graceful.GetManager().InformCleanup()
3535
}

routers/init.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ package routers
66

77
import (
88
"context"
9-
"net"
109
"reflect"
1110
"runtime"
12-
"strconv"
1311

1412
"code.gitea.io/gitea/models"
1513
asymkey_model "code.gitea.io/gitea/models/asymkey"
@@ -158,14 +156,8 @@ func GlobalInitInstalled(ctx context.Context) {
158156

159157
mustInitCtx(ctx, syncAppPathForGit)
160158

161-
if setting.SSH.StartBuiltinServer {
162-
ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
163-
log.Info("SSH server started on %s. Cipher list (%v), key exchange algorithms (%v), MACs (%v)",
164-
net.JoinHostPort(setting.SSH.ListenHost, strconv.Itoa(setting.SSH.ListenPort)),
165-
setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
166-
} else {
167-
ssh.Unused()
168-
}
159+
mustInit(ssh.Init)
160+
169161
auth.Init()
170162
svg.Init()
171163
}

0 commit comments

Comments
 (0)