Skip to content

Commit 834bb59

Browse files
committed
revert dump tmp dir, remove SSH_KEYGEN_PATH
1 parent 8373fff commit 834bb59

File tree

8 files changed

+7
-165
lines changed

8 files changed

+7
-165
lines changed

cmd/dump.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var CmdDump = &cli.Command{
4848
&cli.StringFlag{
4949
Name: "tempdir",
5050
Aliases: []string{"t"},
51-
Value: filepath.Join(setting.TempPath, "dump"),
51+
Value: os.TempDir(),
5252
Usage: "Temporary dir path",
5353
},
5454
&cli.StringFlag{
@@ -194,8 +194,8 @@ func runDump(ctx *cli.Context) error {
194194
log.Info("Skipping database")
195195
} else {
196196
tmpDir := ctx.String("tempdir")
197-
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
198-
fatal("Unable to create temporary directory: %s (%v)", tmpDir, err)
197+
if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
198+
fatal("Path does not exist: %s", tmpDir)
199199
}
200200

201201
dbDump, err := os.CreateTemp(tmpDir, "gitea-db.sql")

custom/conf/app.example.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,6 @@ RUN_USER = ; git
201201
;; relative paths are made absolute relative to the APP_DATA_PATH
202202
;SSH_SERVER_HOST_KEYS=ssh/gitea.rsa, ssh/gogs.rsa
203203
;;
204-
;; Use `ssh-keygen` to parse public SSH keys. The value is passed to the shell. By default, Gitea does the parsing itself.
205-
;SSH_KEYGEN_PATH =
206-
;;
207204
;; Enable SSH Authorized Key Backup when rewriting all keys, default is false
208205
;SSH_AUTHORIZED_KEYS_BACKUP = false
209206
;;

models/asymkey/ssh_key_fingerprint.go

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,13 @@ package asymkey
66
import (
77
"context"
88
"fmt"
9-
"strings"
109

1110
"code.gitea.io/gitea/models/db"
12-
"code.gitea.io/gitea/modules/log"
13-
"code.gitea.io/gitea/modules/process"
14-
"code.gitea.io/gitea/modules/setting"
15-
"code.gitea.io/gitea/modules/util"
1611

1712
"golang.org/x/crypto/ssh"
1813
"xorm.io/builder"
1914
)
2015

21-
// ___________.__ .__ __
22-
// \_ _____/|__| ____ ____ ________________________|__| _____/ |_
23-
// | __) | |/ \ / ___\_/ __ \_ __ \____ \_ __ \ |/ \ __\
24-
// | \ | | | \/ /_/ > ___/| | \/ |_> > | \/ | | \ |
25-
// \___ / |__|___| /\___ / \___ >__| | __/|__| |__|___| /__|
26-
// \/ \//_____/ \/ |__| \/
27-
//
28-
// This file contains functions for fingerprinting SSH keys
29-
//
3016
// The database is used in checkKeyFingerprint however most of these functions probably belong in a module
3117

3218
// checkKeyFingerprint only checks if key fingerprint has been used as public key,
@@ -41,29 +27,6 @@ func checkKeyFingerprint(ctx context.Context, fingerprint string) error {
4127
return nil
4228
}
4329

44-
func calcFingerprintSSHKeygen(publicKeyContent string) (string, error) {
45-
// Calculate fingerprint.
46-
tmpPath, err := writeTmpKeyFile(publicKeyContent)
47-
if err != nil {
48-
return "", err
49-
}
50-
defer func() {
51-
if err := util.Remove(tmpPath); err != nil {
52-
log.Warn("Unable to remove temporary key file: %s: Error: %v", tmpPath, err)
53-
}
54-
}()
55-
stdout, stderr, err := process.GetManager().Exec("AddPublicKey", "ssh-keygen", "-lf", tmpPath)
56-
if err != nil {
57-
if strings.Contains(stderr, "is not a public key file") {
58-
return "", ErrKeyUnableVerify{stderr}
59-
}
60-
return "", util.NewInvalidArgumentErrorf("'ssh-keygen -lf %s' failed with error '%s': %s", tmpPath, err, stderr)
61-
} else if len(stdout) < 2 {
62-
return "", util.NewInvalidArgumentErrorf("not enough output for calculating fingerprint: %s", stdout)
63-
}
64-
return strings.Split(stdout, " ")[1], nil
65-
}
66-
6730
func calcFingerprintNative(publicKeyContent string) (string, error) {
6831
// Calculate fingerprint.
6932
pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKeyContent))
@@ -75,15 +38,12 @@ func calcFingerprintNative(publicKeyContent string) (string, error) {
7538

7639
// CalcFingerprint calculate public key's fingerprint
7740
func CalcFingerprint(publicKeyContent string) (string, error) {
78-
// Call the method based on configuration
79-
useNative := setting.SSH.KeygenPath == ""
80-
calcFn := util.Iif(useNative, calcFingerprintNative, calcFingerprintSSHKeygen)
81-
fp, err := calcFn(publicKeyContent)
41+
fp, err := calcFingerprintNative(publicKeyContent)
8242
if err != nil {
8343
if IsErrKeyUnableVerify(err) {
8444
return "", err
8545
}
86-
return "", fmt.Errorf("CalcFingerprint(%s): %w", util.Iif(useNative, "native", "ssh-keygen"), err)
46+
return "", fmt.Errorf("CalcFingerprint: %w", err)
8747
}
8848
return fp, nil
8949
}

models/asymkey/ssh_key_parse.go

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ import (
1313
"errors"
1414
"fmt"
1515
"math/big"
16-
"os"
17-
"strconv"
1816
"strings"
1917

2018
"code.gitea.io/gitea/modules/log"
21-
"code.gitea.io/gitea/modules/process"
2219
"code.gitea.io/gitea/modules/setting"
2320
"code.gitea.io/gitea/modules/util"
2421

@@ -175,20 +172,9 @@ func CheckPublicKeyString(content string) (_ string, err error) {
175172
return content, nil
176173
}
177174

178-
var (
179-
fnName string
180-
keyType string
181-
length int
182-
)
183-
if len(setting.SSH.KeygenPath) == 0 {
184-
fnName = "SSHNativeParsePublicKey"
185-
keyType, length, err = SSHNativeParsePublicKey(content)
186-
} else {
187-
fnName = "SSHKeyGenParsePublicKey"
188-
keyType, length, err = SSHKeyGenParsePublicKey(content)
189-
}
175+
keyType, length, err := SSHNativeParsePublicKey(content)
190176
if err != nil {
191-
return "", fmt.Errorf("%s: %w", fnName, err)
177+
return "", fmt.Errorf("SSHNativeParsePublicKey: %w", err)
192178
}
193179
log.Trace("Key info [native: %v]: %s-%d", setting.SSH.StartBuiltinServer, keyType, length)
194180

@@ -258,56 +244,3 @@ func SSHNativeParsePublicKey(keyLine string) (string, int, error) {
258244
}
259245
return "", 0, fmt.Errorf("unsupported key length detection for type: %s", pkey.Type())
260246
}
261-
262-
// writeTmpKeyFile writes key content to a temporary file
263-
// and returns the name of that file, along with any possible errors.
264-
func writeTmpKeyFile(content string) (string, error) {
265-
tmpFile, err := os.CreateTemp(setting.GetSSHKeyTestPath(), "gitea_keytest")
266-
if err != nil {
267-
return "", fmt.Errorf("TempFile: %w", err)
268-
}
269-
defer tmpFile.Close()
270-
271-
if _, err = tmpFile.WriteString(content); err != nil {
272-
return "", fmt.Errorf("WriteString: %w", err)
273-
}
274-
return tmpFile.Name(), nil
275-
}
276-
277-
// SSHKeyGenParsePublicKey extracts key type and length using ssh-keygen.
278-
func SSHKeyGenParsePublicKey(key string) (string, int, error) {
279-
tmpName, err := writeTmpKeyFile(key)
280-
if err != nil {
281-
return "", 0, fmt.Errorf("writeTmpKeyFile: %w", err)
282-
}
283-
defer func() {
284-
if err := util.Remove(tmpName); err != nil {
285-
log.Warn("Unable to remove temporary key file: %s: Error: %v", tmpName, err)
286-
}
287-
}()
288-
289-
keygenPath := setting.SSH.KeygenPath
290-
if len(keygenPath) == 0 {
291-
keygenPath = "ssh-keygen"
292-
}
293-
294-
stdout, stderr, err := process.GetManager().Exec("SSHKeyGenParsePublicKey", keygenPath, "-lf", tmpName)
295-
if err != nil {
296-
return "", 0, fmt.Errorf("fail to parse public key: %s - %s", err, stderr)
297-
}
298-
if strings.Contains(stdout, "is not a public key file") {
299-
return "", 0, ErrKeyUnableVerify{stdout}
300-
}
301-
302-
fields := strings.Split(stdout, " ")
303-
if len(fields) < 4 {
304-
return "", 0, fmt.Errorf("invalid public key line: %s", stdout)
305-
}
306-
307-
keyType := strings.Trim(fields[len(fields)-1], "()\r\n")
308-
length, err := strconv.ParseInt(fields[0], 10, 32)
309-
if err != nil {
310-
return "", 0, err
311-
}
312-
return strings.ToLower(keyType), int(length), nil
313-
}

models/asymkey/ssh_key_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818

1919
"github.com/42wim/sshsig"
2020
"github.com/stretchr/testify/assert"
21-
"github.com/stretchr/testify/require"
2221
)
2322

2423
func Test_SSHParsePublicKey(t *testing.T) {
@@ -45,27 +44,6 @@ func Test_SSHParsePublicKey(t *testing.T) {
4544
assert.Equal(t, tc.keyType, keyTypeN)
4645
assert.Equal(t, tc.length, lengthN)
4746
})
48-
if tc.skipSSHKeygen {
49-
return
50-
}
51-
t.Run("SSHKeygen", func(t *testing.T) {
52-
keyTypeK, lengthK, err := SSHKeyGenParsePublicKey(tc.content)
53-
if err != nil {
54-
// Some servers do not support ecdsa format.
55-
if !strings.Contains(err.Error(), "line 1 too long:") {
56-
require.NoError(t, err)
57-
}
58-
}
59-
assert.Equal(t, tc.keyType, keyTypeK)
60-
assert.Equal(t, tc.length, lengthK)
61-
})
62-
t.Run("SSHParseKeyNative", func(t *testing.T) {
63-
keyTypeK, lengthK, err := SSHNativeParsePublicKey(tc.content)
64-
require.NoError(t, err)
65-
66-
assert.Equal(t, tc.keyType, keyTypeK)
67-
assert.Equal(t, tc.length, lengthK)
68-
})
6947
})
7048
}
7149
}
@@ -186,14 +164,6 @@ func Test_calcFingerprint(t *testing.T) {
186164
assert.NoError(t, err)
187165
assert.Equal(t, tc.fp, fpN)
188166
})
189-
if tc.skipSSHKeygen {
190-
return
191-
}
192-
t.Run("SSHKeygen", func(t *testing.T) {
193-
fpK, err := calcFingerprintSSHKeygen(tc.content)
194-
assert.NoError(t, err)
195-
assert.Equal(t, tc.fp, fpK)
196-
})
197167
})
198168
}
199169
}

modules/setting/ssh.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package setting
55

66
import (
7-
"os"
87
"path/filepath"
98
"strings"
109
"text/template"
@@ -31,7 +30,6 @@ var SSH = struct {
3130
ServerKeyExchanges []string `ini:"SSH_SERVER_KEY_EXCHANGES"`
3231
ServerMACs []string `ini:"SSH_SERVER_MACS"`
3332
ServerHostKeys []string `ini:"SSH_SERVER_HOST_KEYS"`
34-
KeygenPath string `ini:"SSH_KEYGEN_PATH"`
3533
AuthorizedKeysBackup bool `ini:"SSH_AUTHORIZED_KEYS_BACKUP"`
3634
AuthorizedPrincipalsBackup bool `ini:"SSH_AUTHORIZED_PRINCIPALS_BACKUP"`
3735
AuthorizedKeysCommandTemplate string `ini:"SSH_AUTHORIZED_KEYS_COMMAND_TEMPLATE"`
@@ -56,7 +54,6 @@ var SSH = struct {
5654
ServerCiphers: []string{"[email protected]", "aes128-ctr", "aes192-ctr", "aes256-ctr", "[email protected]", "[email protected]"},
5755
ServerKeyExchanges: []string{"curve25519-sha256", "ecdh-sha2-nistp256", "ecdh-sha2-nistp384", "ecdh-sha2-nistp521", "diffie-hellman-group14-sha256", "diffie-hellman-group14-sha1"},
5856
ServerMACs: []string{"[email protected]", "hmac-sha2-256", "hmac-sha1"},
59-
KeygenPath: "",
6057
MinimumKeySizeCheck: true,
6158
MinimumKeySizes: map[string]int{"ed25519": 256, "ed25519-sk": 256, "ecdsa": 256, "ecdsa-sk": 256, "rsa": 3071},
6259
ServerHostKeys: []string{"ssh/gitea.rsa", "ssh/gogs.rsa"},
@@ -97,10 +94,6 @@ func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) {
9794
return authorizedPrincipalsAllow, true
9895
}
9996

100-
func GetSSHKeyTestPath() string {
101-
return filepath.Join(TempPath, "ssh_key_test")
102-
}
103-
10497
func loadSSHFrom(rootCfg ConfigProvider) {
10598
sec := rootCfg.Section("server")
10699
if len(SSH.Domain) == 0 {
@@ -135,11 +128,6 @@ func loadSSHFrom(rootCfg ConfigProvider) {
135128
}
136129
}
137130

138-
if err := os.MkdirAll(GetSSHKeyTestPath(), os.ModePerm); err != nil {
139-
log.Fatal("failed to create directory %q for ssh key test: %w", GetSSHKeyTestPath(), err)
140-
}
141-
142-
SSH.KeygenPath = sec.Key("SSH_KEYGEN_PATH").String()
143131
SSH.Port = sec.Key("SSH_PORT").MustInt(22)
144132
SSH.ListenPort = sec.Key("SSH_LISTEN_PORT").MustInt(SSH.Port)
145133
SSH.UseProxyProtocol = sec.Key("SSH_SERVER_USE_PROXY_PROTOCOL").MustBool(false)

options/locale/locale_en-US.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3287,8 +3287,6 @@ config.ssh_domain = SSH Server Domain
32873287
config.ssh_port = Port
32883288
config.ssh_listen_port = Listen Port
32893289
config.ssh_root_path = Root Path
3290-
config.ssh_key_test_path = Key Test Path
3291-
config.ssh_keygen_path = Keygen ('ssh-keygen') Path
32923290
config.ssh_minimum_key_size_check = Minimum Key Size Check
32933291
config.ssh_minimum_key_sizes = Minimum Key Sizes
32943292

templates/admin/config.tmpl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@
6969
{{if not .SSH.StartBuiltinServer}}
7070
<dt>{{ctx.Locale.Tr "admin.config.ssh_root_path"}}</dt>
7171
<dd>{{.SSH.RootPath}}</dd>
72-
<dt>{{ctx.Locale.Tr "admin.config.ssh_key_test_path"}}</dt>
73-
<dd>{{.SSH.KeyTestPath}}</dd>
74-
<dt>{{ctx.Locale.Tr "admin.config.ssh_keygen_path"}}</dt>
75-
<dd>{{.SSH.KeygenPath}}</dd>
7672
<dt>{{ctx.Locale.Tr "admin.config.ssh_minimum_key_size_check"}}</dt>
7773
<dd>{{svg (Iif .SSH.MinimumKeySizeCheck "octicon-check" "octicon-x")}}</dd>
7874
{{if .SSH.MinimumKeySizeCheck}}

0 commit comments

Comments
 (0)