- Notifications
You must be signed in to change notification settings - Fork914
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
c9a226f
feat: implement jetbrains agentssh tracking
Emyrk2447970
Add unit test to confirm tracking
Emyrk76d3a24
implement unit test to verify jetbrains functionality
Emyrkadf2fb3
Implement port process inspection
code-asherad034f2
Add JetBrains tracking to bottom bar
code-asher34b7c5e
Elaborate on process name check comment
code-asherdce56fd
Comment that localForwardChannelData is copied
code-asher7139448
Comment ChannelAccepterWatcher
code-asher6e8f235
Rename channel watcher to be specific to Jetbrains
code-asher4d65478
Log unmarshal failure
code-asher254a5b6
Add constant for JetBrains magic string
code-ashera75ed6c
Fix JetBrains tracking test
code-asherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
74 changes: 73 additions & 1 deletionagent/agent_test.go
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
16 changes: 12 additions & 4 deletionsagent/agentssh/agentssh.go
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
89 changes: 89 additions & 0 deletionsagent/agentssh/jetbrainstrack.go
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 { | ||
code-asher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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() | ||
} |
31 changes: 31 additions & 0 deletionsagent/agentssh/portinspection_supported.go
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 | ||
} |
9 changes: 9 additions & 0 deletionsagent/agentssh/portinspection_unsupported.go
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 | ||
} |
50 changes: 50 additions & 0 deletionsscripts/echoserver/main.go
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) | ||
} | ||
}() | ||
} | ||
} |
16 changes: 16 additions & 0 deletionssite/src/components/Dashboard/DeploymentBanner/DeploymentBannerView.tsx
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.
Uh oh!
There was an error while loading.Please reload this page.
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.