Skip to content
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

Add extra metadata to the join_token.create audit event #47737

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/auth/auth_with_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2224,21 +2224,22 @@ 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{
Metadata: apievents.Metadata{
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.")
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
20 changes: 20 additions & 0 deletions lib/auth/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading