Add a new /tcp endpoint on the agent that is used to pass along the client session ID (with the existing baggage mechanisms). The agent then can upgrade that into the desired connection type, based on the port. For now that is only the standard SSH port and reconnecting pty. The `ssh`, `ssh --stdio`, `ssh --stdio` over Coder Connect, `exp rpty`, and web terminal paths are all updated to use this new endpoint, and fall back to the old method when the endpoint is not available. That ID is added to the log context, and is also used to populate the session ID column in the connection logs table. There are some logs related to a connection that will not be tagged with the ID (like the "accepted connection" line) because they happen before the http handler gets to run.
150 lines
4.2 KiB
Go
150 lines
4.2 KiB
Go
package agentssh
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/gliderlabs/ssh"
|
|
"github.com/google/uuid"
|
|
gossh "golang.org/x/crypto/ssh"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/agent/proto"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
// localForwardChannelData is copied from the ssh package.
|
|
type localForwardChannelData struct {
|
|
DestAddr string
|
|
DestPort uint32
|
|
|
|
OriginAddr string
|
|
OriginPort uint32
|
|
}
|
|
|
|
// JetbrainsChannelWatcher is used to track JetBrains port forwarded (Gateway)
|
|
// channels. If the port forward is something other than JetBrains, this struct
|
|
// is a noop.
|
|
//
|
|
// Sessions are counted here, not in sessionHandler: JetBrains opens hundreds
|
|
// of ssh sessions but only one persistent forwarded channel.
|
|
type JetbrainsChannelWatcher struct {
|
|
gossh.NewChannel
|
|
startSession startSessionFunc
|
|
logger slog.Logger
|
|
originAddr string
|
|
clientSessionID string
|
|
connectionReporter proto.ConnectionReporter
|
|
}
|
|
|
|
func NewJetbrainsChannelWatcher(ctx ssh.Context, logger slog.Logger,
|
|
connectionReporter proto.ConnectionReporter,
|
|
newChannel gossh.NewChannel, startSession startSessionFunc,
|
|
clientSessionID string,
|
|
) gossh.NewChannel {
|
|
logger = logger.With(slog.F("client_session_id", clientSessionID))
|
|
|
|
d := localForwardChannelData{}
|
|
if err := gossh.Unmarshal(newChannel.ExtraData(), &d); err != nil {
|
|
// If the data fails to unmarshal, do nothing.
|
|
logger.Warn(ctx, "failed to unmarshal port forward data", slog.Error(err))
|
|
return newChannel
|
|
}
|
|
|
|
// If we do get a port, we should be able to get the matching PID and from
|
|
// there look up the invocation.
|
|
cmdline, err := getListeningPortProcessCmdline(d.DestPort)
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to inspect port",
|
|
slog.F("destination_port", d.DestPort),
|
|
slog.Error(err))
|
|
return newChannel
|
|
}
|
|
|
|
// If this is not JetBrains, then we do not need to do anything special. We
|
|
// attempt to match on something that appears unique to JetBrains software.
|
|
if !isJetbrainsProcess(cmdline) {
|
|
return newChannel
|
|
}
|
|
|
|
logger.Debug(ctx, "discovered forwarded JetBrains process",
|
|
slog.F("destination_port", d.DestPort))
|
|
|
|
return &JetbrainsChannelWatcher{
|
|
NewChannel: newChannel,
|
|
startSession: startSession,
|
|
logger: logger.With(slog.F("destination_port", d.DestPort)),
|
|
originAddr: d.OriginAddr,
|
|
connectionReporter: connectionReporter,
|
|
clientSessionID: clientSessionID,
|
|
}
|
|
}
|
|
|
|
func (w *JetbrainsChannelWatcher) Accept() (gossh.Channel, <-chan *gossh.Request, error) {
|
|
connReporter := w.connectionReporter.Connect(proto.ConnectEvent{
|
|
ID: uuid.New(),
|
|
Type: proto.Connection_JETBRAINS,
|
|
AppName: string(codersdk.AppFamilyJetBrains),
|
|
IP: w.originAddr,
|
|
ClientSessionID: w.clientSessionID,
|
|
})
|
|
|
|
c, r, err := w.NewChannel.Accept()
|
|
if err != nil {
|
|
connReporter.Disconnect(proto.DisconnectEvent{
|
|
Code: 1,
|
|
Reason: err.Error(),
|
|
})
|
|
return c, r, err
|
|
}
|
|
endSession := w.startSession(string(codersdk.AppFamilyJetBrains))
|
|
// nolint: gocritic // JetBrains is a proper noun and should be capitalized
|
|
w.logger.Debug(context.Background(), "JetBrains watcher accepted channel")
|
|
|
|
return &ChannelOnClose{
|
|
Channel: c,
|
|
done: func() {
|
|
endSession()
|
|
connReporter.Disconnect(proto.DisconnectEvent{
|
|
Code: 0,
|
|
Reason: "normal close",
|
|
})
|
|
// nolint: gocritic // JetBrains is a proper noun and should be capitalized
|
|
w.logger.Debug(context.Background(), "JetBrains channel closed",
|
|
codersdk.ConnectionDirectionAgentToClient.SlogField(),
|
|
codersdk.DisconnectReasonGraceful.SlogField(),
|
|
codersdk.DisconnectReasonGraceful.SlogExpectedField(),
|
|
)
|
|
},
|
|
}, r, err
|
|
}
|
|
|
|
type ChannelOnClose struct {
|
|
gossh.Channel
|
|
// once ensures close only decrements the counter once.
|
|
// Because close can be called multiple times.
|
|
once sync.Once
|
|
done func()
|
|
}
|
|
|
|
func (c *ChannelOnClose) Close() error {
|
|
c.once.Do(c.done)
|
|
return c.Channel.Close()
|
|
}
|
|
|
|
func isJetbrainsProcess(cmdline string) bool {
|
|
opts := []string{
|
|
MagicProcessCmdlineJetBrains,
|
|
MagicProcessCmdlineToolbox,
|
|
MagicProcessCmdlineGateway,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
if strings.Contains(strings.ToLower(cmdline), strings.ToLower(opt)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|