Skip to content

Commit

Permalink
[v14] Include system annotations in audit event entries for access re…
Browse files Browse the repository at this point in the history
…quests (#40122)

* Include system annotations in audit event entries

* resolve comments, add test
  • Loading branch information
Alex McGrath authored Apr 5, 2024
1 parent 7dbd02a commit 8eda6c9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,16 @@ func (a *Server) CreateAccessRequestV2(ctx context.Context, req types.AccessRequ
if _, err := a.Services.CreateAccessRequestV2(ctx, req); err != nil {
return nil, trace.Wrap(err)
}

var annotations *apievents.Struct
if sa := req.GetSystemAnnotations(); len(sa) > 0 {
var err error
annotations, err = apievents.EncodeMapStrings(sa)
if err != nil {
log.WithError(err).Debug("Failed to encode access request annotations.")
}
}

err := a.emitter.EmitAuditEvent(a.closeCtx, &apievents.AccessRequestCreate{
Metadata: apievents.Metadata{
Type: events.AccessRequestCreateEvent,
Expand All @@ -4551,6 +4561,7 @@ func (a *Server) CreateAccessRequestV2(ctx context.Context, req types.AccessRequ
RequestState: req.GetState().String(),
Reason: req.GetRequestReason(),
MaxDuration: req.GetMaxDuration(),
Annotations: annotations,
})
if err != nil {
log.WithError(err).Warn("Failed to emit access request create event.")
Expand Down Expand Up @@ -4651,6 +4662,13 @@ func (a *Server) SetAccessRequestState(ctx context.Context, params types.AccessR
Roles: params.Roles,
AssumeStartTime: params.AssumeStartTime,
}
if sa := req.GetSystemAnnotations(); len(sa) > 0 {
var err error
event.Annotations, err = apievents.EncodeMapStrings(sa)
if err != nil {
log.WithError(err).Debug("Failed to encode access request annotations.")
}
}

if delegator := apiutils.GetDelegator(ctx); delegator != "" {
event.Delegator = delegator
Expand Down
58 changes: 58 additions & 0 deletions lib/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3252,3 +3252,61 @@ func TestGetTokens(t *testing.T) {
_, err = s.a.GetTokens(ctx)
require.NoError(t, err)
}

func TestAccessRequestAuditLog(t *testing.T) {
t.Parallel()
ctx := context.Background()

p, err := newTestPack(ctx, t.TempDir())
require.NoError(t, err)

requester, _, _ := createSessionTestUsers(t, p.a)

paymentsRole, err := types.NewRole("paymentsRole", types.RoleSpecV6{
Allow: types.RoleConditions{
Request: &types.AccessRequestConditions{
Roles: []string{"requestRole"},
Annotations: map[string][]string{
"pagerduty_services": {"payments"},
},
},
},
})
require.NoError(t, err)

requestRole, err := types.NewRole("requestRole", types.RoleSpecV6{})
require.NoError(t, err)

p.a.CreateRole(ctx, requestRole)
p.a.CreateRole(ctx, paymentsRole)

user, err := p.a.GetUser(requester, true)
require.NoError(t, err)
user.AddRole(paymentsRole.GetName())
err = p.a.UpsertUser(user)
require.NoError(t, err)

accessRequest, err := types.NewAccessRequest(uuid.NewString(), requester, "requestRole")
require.NoError(t, err)
req, err := p.a.CreateAccessRequestV2(ctx, accessRequest, TestUser(requester).I.GetIdentity())
require.NoError(t, err)

expectedAnnotations, err := apievents.EncodeMapStrings(paymentsRole.GetAccessRequestConditions(types.Allow).Annotations)
require.NoError(t, err)

arc, ok := p.mockEmitter.LastEvent().(*apievents.AccessRequestCreate)
require.True(t, ok)
require.Equal(t, expectedAnnotations, arc.Annotations)
require.Equal(t, "PENDING", arc.RequestState)

err = p.a.SetAccessRequestState(ctx, types.AccessRequestUpdate{
RequestID: req.GetName(),
State: types.RequestState_APPROVED,
})
require.NoError(t, err)

arc, ok = p.mockEmitter.LastEvent().(*apievents.AccessRequestCreate)
require.True(t, ok)
require.Equal(t, expectedAnnotations, arc.Annotations)
require.Equal(t, "APPROVED", arc.RequestState)
}

0 comments on commit 8eda6c9

Please sign in to comment.