From c569a80449e44202dfbb9f05d62ebafa922467fb Mon Sep 17 00:00:00 2001 From: Zac Bergquist Date: Sun, 20 Oct 2024 17:52:18 -0600 Subject: [PATCH] Add extra metadata to the join_token.create audit event Include the [potentially redacted] token name, expiry, and the name of the user who performed the create/update operation. Closes #44017 --- lib/auth/auth_with_roles.go | 20 ++++++++++---------- lib/auth/tls_test.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/auth/auth_with_roles.go b/lib/auth/auth_with_roles.go index fb07cfd46981c..c3a7edc28af12 100644 --- a/lib/auth/auth_with_roles.go +++ b/lib/auth/auth_with_roles.go @@ -2224,11 +2224,7 @@ func enforceEnterpriseJoinMethodCreation(token types.ProvisionToken) error { // emitTokenEvent is called by Create/Upsert Token in order to emit any relevant // events. -func emitTokenEvent( - ctx context.Context, - e apievents.Emitter, - roles types.SystemRoles, - joinMethod types.JoinMethod, +func emitTokenEvent(ctx context.Context, e apievents.Emitter, token types.ProvisionToken, ) { userMetadata := authz.ClientUserMetadata(ctx) if err := e.EmitAuditEvent(ctx, &apievents.ProvisionTokenCreate{ @@ -2236,9 +2232,14 @@ func emitTokenEvent( Type: events.ProvisionTokenCreateEvent, Code: events.ProvisionTokenCreateCode, }, + ResourceMetadata: apievents.ResourceMetadata{ + Name: token.GetSafeName(), + Expires: token.Expiry(), + UpdatedBy: userMetadata.GetUser(), + }, UserMetadata: userMetadata, - Roles: roles, - JoinMethod: joinMethod, + Roles: token.GetRoles(), + JoinMethod: token.GetJoinMethod(), }); err != nil { log.WithError(err).Warn("Failed to emit join token create event.") } @@ -2262,12 +2263,11 @@ func (a *ServerWithRoles) UpsertToken(ctx context.Context, token types.Provision return trace.Wrap(err) } - emitTokenEvent(ctx, a.authServer.emitter, token.GetRoles(), token.GetJoinMethod()) + emitTokenEvent(ctx, a.authServer.emitter, token) return nil } func (a *ServerWithRoles) CreateToken(ctx context.Context, token types.ProvisionToken) error { - jm := token.GetJoinMethod() if err := a.action(apidefaults.Namespace, types.KindToken, types.VerbCreate); err != nil { return trace.Wrap(err) } @@ -2284,7 +2284,7 @@ func (a *ServerWithRoles) CreateToken(ctx context.Context, token types.Provision return trace.Wrap(err) } - emitTokenEvent(ctx, a.authServer.emitter, token.GetRoles(), jm) + emitTokenEvent(ctx, a.authServer.emitter, token) return nil } diff --git a/lib/auth/tls_test.go b/lib/auth/tls_test.go index cc85616a315ec..3282e52dafd65 100644 --- a/lib/auth/tls_test.go +++ b/lib/auth/tls_test.go @@ -4393,6 +4393,10 @@ func TestGRPCServer_CreateTokenV2(t *testing.T) { Type: events.ProvisionTokenCreateEvent, Code: events.ProvisionTokenCreateCode, }, + ResourceMetadata: eventtypes.ResourceMetadata{ + Name: "*******", + UpdatedBy: "token-creator", + }, UserMetadata: eventtypes.UserMetadata{ User: "token-creator", UserKind: eventtypes.UserKind_USER_KIND_HUMAN, @@ -4422,6 +4426,10 @@ func TestGRPCServer_CreateTokenV2(t *testing.T) { Type: events.ProvisionTokenCreateEvent, Code: events.ProvisionTokenCreateCode, }, + ResourceMetadata: eventtypes.ResourceMetadata{ + Name: "*****************luster", + UpdatedBy: "token-creator", + }, UserMetadata: eventtypes.UserMetadata{ User: "token-creator", UserKind: eventtypes.UserKind_USER_KIND_HUMAN, @@ -4544,6 +4552,10 @@ func TestGRPCServer_UpsertTokenV2(t *testing.T) { Type: events.ProvisionTokenCreateEvent, Code: events.ProvisionTokenCreateCode, }, + ResourceMetadata: eventtypes.ResourceMetadata{ + Name: "*******", + UpdatedBy: "token-upserter", + }, UserMetadata: eventtypes.UserMetadata{ User: "token-upserter", UserKind: eventtypes.UserKind_USER_KIND_HUMAN, @@ -4573,6 +4585,10 @@ func TestGRPCServer_UpsertTokenV2(t *testing.T) { Type: events.ProvisionTokenCreateEvent, Code: events.ProvisionTokenCreateCode, }, + ResourceMetadata: eventtypes.ResourceMetadata{ + Name: "*****************luster", + UpdatedBy: "token-upserter", + }, UserMetadata: eventtypes.UserMetadata{ User: "token-upserter", UserKind: eventtypes.UserKind_USER_KIND_HUMAN, @@ -4604,6 +4620,10 @@ func TestGRPCServer_UpsertTokenV2(t *testing.T) { Type: events.ProvisionTokenCreateEvent, Code: events.ProvisionTokenCreateCode, }, + ResourceMetadata: eventtypes.ResourceMetadata{ + Name: "**************", + UpdatedBy: "token-upserter", + }, UserMetadata: eventtypes.UserMetadata{ User: "token-upserter", UserKind: eventtypes.UserKind_USER_KIND_HUMAN,