Skip to content

Commit 54ef658

Browse files
authored
Refactor SSH init code, fix directory creation for TrustedUserCAKeys file (#20299) (#20306)
Backport #20299. Follow #20298. Only the `GlobalInitInstalled` function should prepare the SSH files for external server or starts the builtin server. * `trustedUserCaKeys` is removed, use `SSH.TrustedUserCAKeys` directly * introduce `ssh.Init`, move the SSH init code from `routers/init.go` to it * `ssh.Init` will start builtin SSH server or prepare external SSH server files
1 parent c556a83 commit 54ef658

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
@@ -843,16 +843,17 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
843843
SSH.StartBuiltinServer = false
844844
}
845845

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

853854
SSH.TrustedUserCAKeysParsed = append(SSH.TrustedUserCAKeysParsed, pubKey)
854855
}
855-
if len(trustedUserCaKeys) > 0 {
856+
if len(SSH.TrustedUserCAKeys) > 0 {
856857
// Set the default as email,username otherwise we can leave it empty
857858
sec.Key("SSH_AUTHORIZED_PRINCIPALS_ALLOW").MustString("username,email")
858859
} else {
@@ -861,20 +862,6 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
861862

862863
SSH.AuthorizedPrincipalsAllow, SSH.AuthorizedPrincipalsEnabled = parseAuthorizedPrincipalsAllow(sec.Key("SSH_AUTHORIZED_PRINCIPALS_ALLOW").Strings(","))
863864

864-
if !SSH.Disabled && !SSH.StartBuiltinServer {
865-
if err = os.MkdirAll(SSH.KeyTestPath, 0o644); err != nil {
866-
log.Fatal("Failed to create '%s': %v", SSH.KeyTestPath, err)
867-
}
868-
869-
if len(trustedUserCaKeys) > 0 && SSH.AuthorizedPrincipalsEnabled {
870-
fname := sec.Key("SSH_TRUSTED_USER_CA_KEYS_FILENAME").MustString(filepath.Join(SSH.RootPath, "gitea-trusted-user-ca-keys.pem"))
871-
if err := os.WriteFile(fname,
872-
[]byte(strings.Join(trustedUserCaKeys, "\n")), 0o600); err != nil {
873-
log.Fatal("Failed to create '%s': %v", fname, err)
874-
}
875-
}
876-
}
877-
878865
SSH.MinimumKeySizeCheck = sec.Key("MINIMUM_KEY_SIZE_CHECK").MustBool(SSH.MinimumKeySizeCheck)
879866
minimumKeySizes := Cfg.Section("ssh.minimum_key_sizes").Keys()
880867
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)