Skip to content

Commit b07b33e

Browse files
authored
feat: add agentapi endpoint to report connections for audit (coder#16507)
This change adds a new `ReportConnection` endpoint to the `agentapi`. The protocol version was bumped previously, so it has been omitted here. This allows the agent to report connection events, for example when the user connects to the workspace via SSH or VS Code. Updates coder#15139
1 parent dedc32f commit b07b33e

File tree

16 files changed

+1484
-705
lines changed

16 files changed

+1484
-705
lines changed

agent/agent.go

-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentM
372372
// Important: if the command times out, we may see a misleading error like
373373
// "exit status 1", so it's important to include the context error.
374374
err = errors.Join(err, ctx.Err())
375-
376375
if err != nil {
377376
result.Error = fmt.Sprintf("run cmd: %+v", err)
378377
}

agent/agenttest/client.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"golang.org/x/exp/slices"
1616
"golang.org/x/xerrors"
1717
"google.golang.org/protobuf/types/known/durationpb"
18+
"google.golang.org/protobuf/types/known/emptypb"
1819
"storj.io/drpc/drpcmux"
1920
"storj.io/drpc/drpcserver"
2021
"tailscale.com/tailcfg"
@@ -170,6 +171,7 @@ type FakeAgentAPI struct {
170171
lifecycleStates []codersdk.WorkspaceAgentLifecycle
171172
metadata map[string]agentsdk.Metadata
172173
timings []*agentproto.Timing
174+
connections []*agentproto.Connection
173175

174176
getAnnouncementBannersFunc func() ([]codersdk.BannerConfig, error)
175177
getResourcesMonitoringConfigurationFunc func() (*agentproto.GetResourcesMonitoringConfigurationResponse, error)
@@ -338,12 +340,20 @@ func (f *FakeAgentAPI) BatchCreateLogs(ctx context.Context, req *agentproto.Batc
338340

339341
func (f *FakeAgentAPI) ScriptCompleted(_ context.Context, req *agentproto.WorkspaceAgentScriptCompletedRequest) (*agentproto.WorkspaceAgentScriptCompletedResponse, error) {
340342
f.Lock()
341-
f.timings = append(f.timings, req.Timing)
343+
f.timings = append(f.timings, req.GetTiming())
342344
f.Unlock()
343345

344346
return &agentproto.WorkspaceAgentScriptCompletedResponse{}, nil
345347
}
346348

349+
func (f *FakeAgentAPI) ReportConnection(_ context.Context, req *agentproto.ReportConnectionRequest) (*emptypb.Empty, error) {
350+
f.Lock()
351+
f.connections = append(f.connections, req.GetConnection())
352+
f.Unlock()
353+
354+
return &emptypb.Empty{}, nil
355+
}
356+
347357
func NewFakeAgentAPI(t testing.TB, logger slog.Logger, manifest *agentproto.Manifest, statsCh chan *agentproto.Stats) *FakeAgentAPI {
348358
return &FakeAgentAPI{
349359
t: t,

0 commit comments

Comments
 (0)