@@ -3,11 +3,11 @@ package agentssh
3
3
import (
4
4
"bufio"
5
5
"context"
6
- "crypto/rand"
7
6
"crypto/rsa"
8
7
"errors"
9
8
"fmt"
10
9
"io"
10
+ "math/rand"
11
11
"net"
12
12
"os"
13
13
"os/exec"
@@ -85,6 +85,10 @@ type Config struct {
85
85
X11DisplayOffset * int
86
86
// BlockFileTransfer restricts use of file transfer applications.
87
87
BlockFileTransfer bool
88
+
89
+ // RandomSeed is a random seed value exclusively used to generate a
90
+ // deterministic SSH host key.
91
+ RandomSeed int64
88
92
}
89
93
90
94
type Server struct {
@@ -112,20 +116,25 @@ type Server struct {
112
116
}
113
117
114
118
func NewServer (ctx context.Context ,logger slog.Logger ,prometheusRegistry * prometheus.Registry ,fs afero.Fs ,execer agentexec.Execer ,config * Config ) (* Server ,error ) {
119
+ if config == nil {
120
+ config = & Config {}
121
+ }
122
+
115
123
// Clients' should ignore the host key when connecting.
116
124
// The agent needs to authenticate with coderd to SSH,
117
125
// so SSH authentication doesn't improve security.
118
- randomHostKey ,err := rsa .GenerateKey (rand .Reader ,2048 )
126
+
127
+ // Create a deterministic random source
128
+ // nolint: gosec
129
+ deterministicRand := rand .New (rand .NewSource (config .RandomSeed ))
130
+ coderHostKey ,err := rsa .GenerateKey (deterministicRand ,2048 )
119
131
if err != nil {
120
132
return nil ,err
121
133
}
122
- randomSigner ,err := gossh .NewSignerFromKey (randomHostKey )
134
+ coderSigner ,err := gossh .NewSignerFromKey (coderHostKey )
123
135
if err != nil {
124
136
return nil ,err
125
137
}
126
- if config == nil {
127
- config = & Config {}
128
- }
129
138
if config .X11DisplayOffset == nil {
130
139
offset := X11DefaultDisplayOffset
131
140
config .X11DisplayOffset = & offset
@@ -190,7 +199,7 @@ func NewServer(ctx context.Context, logger slog.Logger, prometheusRegistry *prom
190
199
slog .Error (err ))
191
200
},
192
201
Handler :s .sessionHandler ,
193
- HostSigners : []ssh.Signer {randomSigner },
202
+ HostSigners : []ssh.Signer {coderSigner },
194
203
LocalPortForwardingCallback :func (ctx ssh.Context ,destinationHost string ,destinationPort uint32 )bool {
195
204
// Allow local port forwarding all!
196
205
s .logger .Debug (ctx ,"local port forward" ,