-
Notifications
You must be signed in to change notification settings - Fork 928
fix: track JetBrains connections #10968
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c9a226f
feat: implement jetbrains agentssh tracking
Emyrk 2447970
Add unit test to confirm tracking
Emyrk 76d3a24
implement unit test to verify jetbrains functionality
Emyrk adf2fb3
Implement port process inspection
code-asher ad034f2
Add JetBrains tracking to bottom bar
code-asher 34b7c5e
Elaborate on process name check comment
code-asher dce56fd
Comment that localForwardChannelData is copied
code-asher 7139448
Comment ChannelAccepterWatcher
code-asher 6e8f235
Rename channel watcher to be specific to Jetbrains
code-asher 4d65478
Log unmarshal failure
code-asher 254a5b6
Add constant for JetBrains magic string
code-asher a75ed6c
Fix JetBrains tracking test
code-asher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package agentssh | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
|
||
"cdr.dev/slog" | ||
"github.com/gliderlabs/ssh" | ||
"go.uber.org/atomic" | ||
gossh "golang.org/x/crypto/ssh" | ||
) | ||
|
||
// 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. | ||
type JetbrainsChannelWatcher struct { | ||
gossh.NewChannel | ||
jetbrainsCounter *atomic.Int64 | ||
} | ||
|
||
func NewJetbrainsChannelWatcher(ctx ssh.Context, logger slog.Logger, newChannel gossh.NewChannel, counter *atomic.Int64) gossh.NewChannel { | ||
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 !strings.Contains(strings.ToLower(cmdline), strings.ToLower(MagicProcessCmdlineJetBrains)) { | ||
return newChannel | ||
} | ||
|
||
logger.Debug(ctx, "discovered forwarded JetBrains process", | ||
slog.F("destination_port", d.DestPort)) | ||
|
||
return &JetbrainsChannelWatcher{ | ||
NewChannel: newChannel, | ||
jetbrainsCounter: counter, | ||
} | ||
} | ||
|
||
func (w *JetbrainsChannelWatcher) Accept() (gossh.Channel, <-chan *gossh.Request, error) { | ||
c, r, err := w.NewChannel.Accept() | ||
if err != nil { | ||
return c, r, err | ||
} | ||
w.jetbrainsCounter.Add(1) | ||
|
||
return &ChannelOnClose{ | ||
Channel: c, | ||
done: func() { | ||
w.jetbrainsCounter.Add(-1) | ||
}, | ||
}, 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() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build linux | ||
|
||
package agentssh | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cakturk/go-netstat/netstat" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func getListeningPortProcessCmdline(port uint32) (string, error) { | ||
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool { | ||
return s.LocalAddr != nil && uint32(s.LocalAddr.Port) == port | ||
}) | ||
if err != nil { | ||
return "", xerrors.Errorf("inspect port %d: %w", port, err) | ||
} | ||
if len(tabs) == 0 { | ||
return "", nil | ||
} | ||
// The process name provided by go-netstat does not include the full command | ||
// line so grab that instead. | ||
pid := tabs[0].Process.Pid | ||
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid)) | ||
if err != nil { | ||
return "", xerrors.Errorf("read /proc/%d/cmdline: %w", pid, err) | ||
} | ||
return string(data), nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !linux | ||
|
||
package agentssh | ||
|
||
func getListeningPortProcessCmdline(port uint32) (string, error) { | ||
// We are not worrying about other platforms at the moment because Gateway | ||
// only supports Linux anyway. | ||
return "", nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
// A simple echo server. It listens on a random port, prints that port, then | ||
// echos back anything sent to it. | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
) | ||
|
||
func main() { | ||
l, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
log.Fatalf("listen error: err=%s", err) | ||
} | ||
|
||
defer l.Close() | ||
tcpAddr, valid := l.Addr().(*net.TCPAddr) | ||
if !valid { | ||
log.Fatal("address is not valid") | ||
} | ||
|
||
remotePort := tcpAddr.Port | ||
_, err = fmt.Println(remotePort) | ||
if err != nil { | ||
log.Fatalf("print error: err=%s", err) | ||
} | ||
|
||
for { | ||
conn, err := l.Accept() | ||
if err != nil { | ||
log.Fatalf("accept error, err=%s", err) | ||
return | ||
} | ||
|
||
go func() { | ||
defer conn.Close() | ||
_, err := io.Copy(conn, conn) | ||
|
||
if errors.Is(err, io.EOF) { | ||
return | ||
} else if err != nil { | ||
log.Fatalf("copy error, err=%s", err) | ||
} | ||
}() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.