Skip to content

Refactor desktop audit event emission#33189

Merged
zmb3 merged 1 commit intomasterfrom
zmb3/desktop-events
Oct 11, 2023
Merged

Refactor desktop audit event emission#33189
zmb3 merged 1 commit intomasterfrom
zmb3/desktop-events

Conversation

@zmb3
Copy link
Copy Markdown
Collaborator

@zmb3 zmb3 commented Oct 10, 2023

Our methods for emitting audit events take 10 arguments already, and we need to add more as part of the work in #30417. To make this more manageable, create an auditor struct that will hold on to state that is shared for all audit events in a session (ID, user identity, the desktop we're connecting to, etc.)

As a result, the "audit cache" for directory sharing events is also simplified - we now create one of these per-session rather than maintaining one large cache for all sessions.

Comment thread lib/srv/desktop/audit.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Convention: methods starting with make are just constructors - they always return an event.

Methods starting with on may or may not return an event (some of these just update the internal state for future events).

Comment thread lib/srv/desktop/audit.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Note to self to inline this.

Comment thread lib/srv/desktop/audit.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is the new type which holds on to a lot of common state (so that we don't have to pass these values around every time we want to emit an event).

The other change worth noting is this thing only build events - it's up to the caller to decide whether the event gets emitted to the audit log, recorded to the session recording, or both.

Comment thread lib/srv/desktop/audit_cache.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We've removed one layer of indirection here. An audit cache is associated to a single session, so the top level map of session ID to entry no longer needs to exist.

(And all the method signatures have been updated to remove the session ID argument)

Comment thread lib/srv/desktop/windows_server.go Outdated
Copy link
Copy Markdown
Collaborator Author

@zmb3 zmb3 Oct 10, 2023

Choose a reason for hiding this comment

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

This callback is now purely authorization and no longer grabs the windowsUser variable - we get that from the client instead.

(There was actually a subtle bug here in that sometimes we would try to emit audit events before we had a Windows username.)

Comment thread lib/srv/desktop/windows_server.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Closing the connection on error was moved from audit.go (which is just about building audit events) to windows_server.go (where the rest of the connection management logic is.

Comment thread lib/srv/desktop/windows_server_test.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This test was moved to audit_test.go.

Comment thread lib/srv/desktop/audit_test.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Most of these tests got a bit simpler, as we no longer need to emit the event and then ask a mock emitter what was emitted, we just build the event and it's already the type we expect.

Comment thread lib/srv/desktop/audit_test.go Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The changes in this file look scary, but it's very mechanical.

  • moved a bunch of constants to the top rather than repeat them dozens of times
  • no longer need to build send/receive handlers and encode TDP messages - instead we operate on the new audit event builder in a strongly-typed manner

@zmb3 zmb3 requested review from ibeckermayer and probakowski and removed request for r0mant October 10, 2023 02:50
Copy link
Copy Markdown
Contributor

@ibeckermayer ibeckermayer left a comment

Choose a reason for hiding this comment

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

Logs are working for me

Comment thread lib/srv/desktop/windows_server.go Outdated
Comment thread lib/srv/desktop/audit_cache.go Outdated
@public-teleport-github-review-bot public-teleport-github-review-bot Bot removed the request for review from xacrimon October 10, 2023 22:16
Comment thread lib/srv/desktop/audit_cache.go Outdated
Comment thread lib/srv/desktop/rdp/rdpclient/client.go Outdated
Comment on lines 195 to 197
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not super familiar with this code but isn't there potential for the username to empty if this is called before the username has been read and assigned in readClientUsername below? What are the implications if that happens?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yep, there is the potential. The implications are that the username would be missing from the audit event. This would only be the case if we failed to initialize a client in this branch:

https://github.com/gravitational/teleport/pull/33189/files#diff-d0d78875d7396d7dad9cc4ab10f6da26554c8e8ff30f2b661c1377d9b959bd85R864-R867

@zmb3 zmb3 force-pushed the zmb3/desktop-events branch 2 times, most recently from 2f4baae to ae98b45 Compare October 11, 2023 17:07
Comment thread lib/srv/desktop/windows_server.go Outdated
Comment thread lib/srv/desktop/windows_server.go Outdated
Our methods for emitting audit events take 10 arguments already,
and we need to add more as part of the work in #30417. To make
this more manageable, create an auditor struct that will hold on
to state that is shared for all audit events in a session (ID,
user identity, the desktop we're connecting to, etc.)

As a result, the "audit cache" for directory sharing events is
also simplified - we now create one of these per-session rather
than maintaining one large cache for all sessions.
@zmb3 zmb3 enabled auto-merge October 11, 2023 18:10
@zmb3 zmb3 force-pushed the zmb3/desktop-events branch from ae98b45 to 42c1cfc Compare October 11, 2023 18:11
@zmb3 zmb3 added this pull request to the merge queue Oct 11, 2023
Merged via the queue into master with commit f6e8c65 Oct 11, 2023
@zmb3 zmb3 deleted the zmb3/desktop-events branch October 11, 2023 18:46
@public-teleport-github-review-bot
Copy link
Copy Markdown

@zmb3 See the table below for backport results.

Branch Result
branch/v12 Failed
branch/v13 Failed
branch/v14 Create PR

@zmb3
Copy link
Copy Markdown
Collaborator Author

zmb3 commented Oct 11, 2023

Going to abandon the v13/v12 backports. I was only backporting this to those versions to try to avoid future conflicts, but there's conflicts here already.

Edit: I want to see Telemetry for v13 agents, which will still be around for a bit, so I opened a manual v13 backport.

zmb3 added a commit that referenced this pull request Oct 11, 2023
github-merge-queue Bot pushed a commit that referenced this pull request Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants