Skip to content

feat: add agentapi endpoint to report connections for audit #16507

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 4 commits into from
Feb 20, 2025

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Feb 10, 2025

This change adds a new ReportConnection endpoint to the agentapi and
bumps the protocol version.

This allows the agent to report connection events, for example when the
user connects to the workspace via SSH or VS Code.

Updates #15139
Depends on #16493

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this makes sense to me.

To me it just comes down to using workspace_id or the agent_id as the audit log's resource id.

I really think using workspace_id makes the most sense, as if you search for a workspace, we should show all audit logs related to said workspace.

Saying that though, the correct thing is the agent id....


Random thought. We have AdditionalFields. Could we just add a custom query option to GetAuditLogsOffset that searches audit logs by their json body payload for the workspace id? Would that be too slow? Should we just add another column and allow a workspace_id for audit logs?

@mafredri mafredri force-pushed the mafredri/feat-add-audit-types-for-connect-open-agent-app branch from 8128b7e to de017dc Compare February 11, 2025 12:03
@mafredri mafredri force-pushed the mafredri/feat-add-agentapi-for-connect-audit branch from 841b6ca to 724efff Compare February 11, 2025 12:04
@mafredri mafredri force-pushed the mafredri/feat-add-audit-types-for-connect-open-agent-app branch from de017dc to 640a0c3 Compare February 11, 2025 12:10
@mafredri mafredri force-pushed the mafredri/feat-add-agentapi-for-connect-audit branch 2 times, most recently from ac60d34 to 761ba0a Compare February 11, 2025 12:13
@mafredri mafredri force-pushed the mafredri/feat-add-agentapi-for-connect-audit branch 4 times, most recently from dcdf3bd to 88c0862 Compare February 12, 2025 14:21
@mafredri mafredri force-pushed the mafredri/feat-add-audit-types-for-connect-open-agent-app branch from debcf9c to 58f1d2a Compare February 12, 2025 14:22
@mafredri mafredri force-pushed the mafredri/feat-add-agentapi-for-connect-audit branch 2 times, most recently from cc57f22 to 3edbd1a Compare February 12, 2025 14:28
@mafredri mafredri force-pushed the mafredri/feat-add-audit-types-for-connect-open-agent-app branch from a9b4adf to 88f22b7 Compare February 12, 2025 15:04
@mafredri mafredri force-pushed the mafredri/feat-add-agentapi-for-connect-audit branch from 3edbd1a to ab51fea Compare February 12, 2025 15:04
@mafredri mafredri marked this pull request as ready for review February 12, 2025 17:11
@mafredri mafredri force-pushed the mafredri/feat-add-audit-types-for-connect-open-agent-app branch from 88f22b7 to 469e2cd Compare February 12, 2025 18:28
@mafredri mafredri force-pushed the mafredri/feat-add-agentapi-for-connect-audit branch from ccfd326 to f9c6b31 Compare February 12, 2025 18:31
Copy link
Contributor

#16438 also changes the Agent API by adding some new RPCs. If these both land prior to the next release, then we only need to bump the minor version once to 2.4. If one lands but not the other, the latter will have to bump to 2.5. cc @defelmnq

@mafredri
Copy link
Member Author

mafredri commented Feb 13, 2025

#16438 also changes the Agent API by adding some new RPCs. If these both land prior to the next release, then we only need to bump the minor version once to 2.4. If one lands but not the other, the latter will have to bump to 2.5. cc @defelmnq

@spikecurtis thanks for the shoutout. I thought about this yesterday and I think there is value in bumping the version on main for unreleased changes as well. It would make release patching/cherry-picking safer. (In this case I think it's unlikely we'll run into a conflict due to that, but sticking to a system helps avoids unforeseen situations down the line.)

Edit: Well as I posted that, I just realized that if they're cherry-picked out of order that's no good 😅

Copy link
Contributor

We should bump on main for unreleased changes! Just no need to bump more than once per release.

google.protobuf.Timestamp timestamp = 4;
string ip = 5;
int32 status_code = 6;
optional string reason = 7;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unclear on what goes in this field in practice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What change would you like to see? It's a free-text reason as to why this event happened. Typically it will be used on disconnect to report why the connection ended, if we know. It could also be reported on connect if e.g. file transfer is blocked (i.e. failed connect, although it could be argued that it should be a connect+immediate disconnect). I can rename it to disconnect reason if you prefer it more explicit, should be fine even if more restrictive.

Base automatically changed from mafredri/feat-add-audit-types-for-connect-open-agent-app to main February 17, 2025 13:02
This change adds a new `ReportConnection` endpoint to the `agentapi` and
bumps the protocol version.

This allows the agent to report connection events, for example when the
user connects to the workspace via SSH or VS Code.

Updates #15139
@mafredri mafredri force-pushed the mafredri/feat-add-agentapi-for-connect-audit branch from f9c6b31 to 632bf98 Compare February 17, 2025 13:32
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM as these are going in with OOM/OOD resource monitors stuff.

}

// Fetch contextual data for this audit event.
workspaceAgent, err := a.AgentFn(ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: check that AgentFn is not nil; this could panic otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point, but one that should be solved for the entire package. Each sub-API on agentapi.API has this same potential edge-case. The assumption seems to be that New will correctly assign it, otherwise it's a developer error.

Testing for that in a future proof way is of course an issue. As is allowing Coder to start at all if it's missing.

Comment on lines +51 to +58
workspace, err := a.Database.GetWorkspaceByAgentID(ctx, workspaceAgent.ID)
if err != nil {
return nil, xerrors.Errorf("get workspace by agent id: %w", err)
}
build, err := a.Database.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
if err != nil {
return nil, xerrors.Errorf("get latest workspace build by workspace id: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking: It's unfortunate that GetWorkspaceByAgentID ends up doing a join on workspace_builds but disregarding the related row! I had a quick check and didn't see any other places that would benefit from returning this additional information though.

@@ -43,6 +43,8 @@ import (
// - Shipped in Coder v2.{{placeholder}} // TODO Vincent: Replace with the correct version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mafredri mafredri merged commit b07b33e into main Feb 20, 2025
32 checks passed
@mafredri mafredri deleted the mafredri/feat-add-agentapi-for-connect-audit branch February 20, 2025 12:52
@github-actions github-actions bot locked and limited conversation to collaborators Feb 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants