blob: 809a870174c47b61ffb841ded34b6f517f46e4f8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package server
import (
"os"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/dlog"
"github.com/mimecast/dtail/internal/ssh"
)
const (
defaultHostKeyBits = 4096
defaultHostKeyFile = "./cache/ssh_host_key"
)
// PrivateHostKey retrieves the private server RSA host key.
func PrivateHostKey(hostKeyFile string, hostKeyBits int) []byte {
if hostKeyFile == "" {
hostKeyFile = defaultHostKeyFile
}
if hostKeyBits <= 0 {
hostKeyBits = defaultHostKeyBits
}
if config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
hostKeyFile = "./ssh_host_key"
}
_, err := os.Stat(hostKeyFile)
if os.IsNotExist(err) {
dlog.Server.Info("Generating private server RSA host key")
privateKey, err := ssh.GeneratePrivateRSAKey(hostKeyBits)
if err != nil {
dlog.Server.FatalPanic("Failed to generate private server RSA host key", err)
}
pem := ssh.EncodePrivateKeyToPEM(privateKey)
if err := os.WriteFile(hostKeyFile, pem, 0600); err != nil {
dlog.Server.Error("Unable to write private server RSA host key to file",
hostKeyFile, err)
}
return pem
}
dlog.Server.Info("Reading private server RSA host key from file", hostKeyFile)
pem, err := os.ReadFile(hostKeyFile)
if err != nil {
dlog.Server.FatalPanic("Failed to load private server RSA host key", err)
}
return pem
}
|