- Notifications
You must be signed in to change notification settings - Fork924
refactor(agent/agentssh): move parsing of magic session and create type#16630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -26,6 +26,7 @@ import ( | ||
"github.com/spf13/afero" | ||
"go.uber.org/atomic" | ||
gossh "golang.org/x/crypto/ssh" | ||
"golang.org/x/exp/slices" | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
@@ -42,14 +43,6 @@ const ( | ||
// unlikely to shadow other exit codes, which are typically 1, 2, 3, etc. | ||
MagicSessionErrorCode = 229 | ||
// MagicProcessCmdlineJetBrains is a string in a process's command line that | ||
// uniquely identifies it as JetBrains software. | ||
MagicProcessCmdlineJetBrains = "idea.vendor.name=JetBrains" | ||
@@ -60,6 +53,29 @@ const ( | ||
BlockedFileTransferErrorMessage = "File transfer has been disabled." | ||
) | ||
// MagicSessionType is a type that represents the type of session that is being | ||
// established. | ||
type MagicSessionType string | ||
const ( | ||
// MagicSessionTypeEnvironmentVariable is used to track the purpose behind an SSH connection. | ||
// This is stripped from any commands being executed, and is counted towards connection stats. | ||
MagicSessionTypeEnvironmentVariable = "CODER_SSH_SESSION_TYPE" | ||
) | ||
// MagicSessionType enums. | ||
const ( | ||
// MagicSessionTypeUnknown means the session type could not be determined. | ||
MagicSessionTypeUnknown MagicSessionType = "unknown" | ||
// MagicSessionTypeSSH is the default session type. | ||
MagicSessionTypeSSH MagicSessionType = "ssh" | ||
// MagicSessionTypeVSCode is set in the SSH config by the VS Code extension to identify itself. | ||
MagicSessionTypeVSCode MagicSessionType = "vscode" | ||
// MagicSessionTypeJetBrains is set in the SSH config by the JetBrains | ||
// extension to identify itself. | ||
MagicSessionTypeJetBrains MagicSessionType = "jetbrains" | ||
) | ||
// BlockedFileTransferCommands contains a list of restricted file transfer commands. | ||
var BlockedFileTransferCommands = []string{"nc", "rsync", "scp", "sftp"} | ||
@@ -255,14 +271,42 @@ func (s *Server) ConnStats() ConnStats { | ||
} | ||
} | ||
func extractMagicSessionType(env []string) (magicType MagicSessionType, rawType string, filteredEnv []string) { | ||
for _, kv := range env { | ||
if !strings.HasPrefix(kv, MagicSessionTypeEnvironmentVariable) { | ||
continue | ||
} | ||
rawType = strings.TrimPrefix(kv, MagicSessionTypeEnvironmentVariable+"=") | ||
// Keep going, we'll use the last instance of the env. | ||
} | ||
// Always force lowercase checking to be case-insensitive. | ||
switch MagicSessionType(strings.ToLower(rawType)) { | ||
case MagicSessionTypeVSCode: | ||
magicType = MagicSessionTypeVSCode | ||
case MagicSessionTypeJetBrains: | ||
magicType = MagicSessionTypeJetBrains | ||
case "", MagicSessionTypeSSH: | ||
magicType = MagicSessionTypeSSH | ||
default: | ||
magicType = MagicSessionTypeUnknown | ||
} | ||
return magicType, rawType, slices.DeleteFunc(env, func(kv string) bool { | ||
return strings.HasPrefix(kv, MagicSessionTypeEnvironmentVariable+"=") | ||
}) | ||
} | ||
Comment on lines +274 to +300 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. suggestion: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I kind of like the suggestion, but I don't like returning valid data from a function that returns an error. Essentially we'd still want to filter the env in the case of an error parsing the magic session type. (We only warn log currently if it's invalid.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Fair! | ||
func (s *Server) sessionHandler(session ssh.Session) { | ||
ctx := session.Context() | ||
id := uuid.New() | ||
logger := s.logger.With( | ||
slog.F("remote_addr", session.RemoteAddr()), | ||
slog.F("local_addr", session.LocalAddr()), | ||
// Assigning a random uuid for each session is useful for tracking | ||
// logs for the same ssh session. | ||
slog.F("id",id.String()), | ||
) | ||
logger.Info(ctx, "handling ssh session") | ||
@@ -274,16 +318,21 @@ func (s *Server) sessionHandler(session ssh.Session) { | ||
} | ||
defer s.trackSession(session, false) | ||
env := session.Environ() | ||
magicType, magicTypeRaw, env := extractMagicSessionType(env) | ||
switch magicType { | ||
case MagicSessionTypeVSCode: | ||
s.connCountVSCode.Add(1) | ||
defer s.connCountVSCode.Add(-1) | ||
case MagicSessionTypeJetBrains: | ||
// Do nothing here because JetBrains launches hundreds of ssh sessions. | ||
// We instead track JetBrains in the single persistent tcp forwarding channel. | ||
case MagicSessionTypeSSH: | ||
s.connCountSSHSession.Add(1) | ||
defer s.connCountSSHSession.Add(-1) | ||
case MagicSessionTypeUnknown: | ||
logger.Warn(ctx, "invalid magic ssh session type specified", slog.F("raw_type", magicTypeRaw)) | ||
} | ||
if s.fileTransferBlocked(session) { | ||
@@ -309,7 +358,18 @@ func (s *Server) sessionHandler(session ssh.Session) { | ||
return | ||
} | ||
x11, hasX11 := session.X11() | ||
if hasX11 { | ||
display, handled := s.x11Handler(session.Context(), x11) | ||
if !handled { | ||
_ = session.Exit(1) | ||
logger.Error(ctx, "x11 handler failed") | ||
return | ||
} | ||
env = append(env, fmt.Sprintf("DISPLAY=localhost:%d.%d", display, x11.ScreenNumber)) | ||
} | ||
err := s.sessionStart(logger, session, env, magicType) | ||
var exitError *exec.ExitError | ||
if xerrors.As(err, &exitError) { | ||
code := exitError.ExitCode() | ||
@@ -379,32 +439,8 @@ func (s *Server) fileTransferBlocked(session ssh.Session) bool { | ||
return false | ||
} | ||
func (s *Server) sessionStart(logger slog.Logger, session ssh.Session,env []string, magicType MagicSessionType) (retErr error) { | ||
ctx := session.Context() | ||
magicTypeLabel := magicTypeMetricLabel(magicType) | ||
sshPty, windowSize, isPty := session.Pty() | ||
@@ -473,7 +509,7 @@ func (s *Server) startNonPTYSession(logger slog.Logger, session ssh.Session, mag | ||
}() | ||
go func() { | ||
for sig := range sigs { | ||
handleSignal(logger, sig, cmd.Process, s.metrics, magicTypeLabel) | ||
} | ||
}() | ||
return cmd.Wait() | ||
@@ -558,7 +594,7 @@ func (s *Server) startPTYSession(logger slog.Logger, session ptySession, magicTy | ||
sigs = nil | ||
continue | ||
} | ||
handleSignal(logger, sig, process, s.metrics, magicTypeLabel) | ||
case win, ok := <-windowSize: | ||
if !ok { | ||
windowSize = nil | ||
@@ -612,15 +648,15 @@ func (s *Server) startPTYSession(logger slog.Logger, session ptySession, magicTy | ||
return nil | ||
} | ||
func handleSignal(logger slog.Logger, ssig ssh.Signal, signaler interface{ Signal(os.Signal) error }, metrics *sshServerMetrics, magicTypeLabel string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 👍 | ||
ctx := context.Background() | ||
sig := osSignalFrom(ssig) | ||
logger = logger.With(slog.F("ssh_signal", ssig), slog.F("signal", sig.String())) | ||
logger.Info(ctx, "received signal from client") | ||
err := signaler.Signal(sig) | ||
if err != nil { | ||
logger.Warn(ctx, "signaling the process failed", slog.Error(err)) | ||
metrics.sessionErrors.WithLabelValues(magicTypeLabel, "yes", "signal").Add(1) | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -71,15 +71,15 @@ func newSSHServerMetrics(registerer prometheus.Registerer) *sshServerMetrics { | ||
} | ||
} | ||
func magicTypeMetricLabel(magicTypeMagicSessionType) string { | ||
switch magicType { | ||
case MagicSessionTypeVSCode: | ||
case MagicSessionTypeJetBrains: | ||
Comment on lines -78 to -79 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This smells like some cutover compatibility logic here. | ||
caseMagicSessionTypeSSH: | ||
case MagicSessionTypeUnknown: | ||
default: | ||
magicType =MagicSessionTypeUnknown | ||
} | ||
// Always be case insensitive | ||
return strings.ToLower(string(magicType)) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.