Skip to content

Commit b730217

Browse files
committed
Refactor SSH init code, fix directory creation for TrustedUserCAKeys file
1 parent 36353e2 commit b730217

File tree

4 files changed

+56
-29
lines changed

4 files changed

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

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)