Skip to content

Commit

Permalink
[v7] backport #9758 (access requests in audit log) (#9935)
Browse files Browse the repository at this point in the history
* Refactor most uses of `UserMetadata` into a handful of functions

* Add access requests to `UserMetadata`

* Explanation for the reserved field in SessionStart
  • Loading branch information
espadolini authored Jan 27, 2022
1 parent 4c2b6d2 commit 404d05b
Show file tree
Hide file tree
Showing 24 changed files with 469 additions and 559 deletions.
694 changes: 347 additions & 347 deletions api/types/events/events.pb.go

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions api/types/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ message UserMetadata {

// AWSRoleARN is AWS IAM role user assumes when accessing AWS console.
string AWSRoleARN = 4 [ (gogoproto.jsontag) = "aws_role_arn,omitempty" ];

// AccessRequests are the IDs of access requests created by the user
repeated string AccessRequests = 5 [ (gogoproto.jsontag) = "access_requests,omitempty" ];
}

// Server is a server metadata
Expand Down Expand Up @@ -155,8 +158,10 @@ message SessionStart {
// SessionRecording is the type of session recording.
string SessionRecording = 10 [ (gogoproto.jsontag) = "session_recording,omitempty" ];

// AccessRequests are the IDs of access requests created by the user
repeated string AccessRequests = 11 [ (gogoproto.jsontag) = "access_requests,omitempty" ];
// AccessRequests used to be here, it is now part of UserMetadata
reserved "AccessRequests";
reserved 11;
// reserved jsontag "access_requests"
}

// SessionJoin emitted when another user joins a session
Expand Down
19 changes: 4 additions & 15 deletions lib/auth/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func (a *Server) UpsertRole(ctx context.Context, role types.Role) error {
Type: events.RoleCreatedEvent,
Code: events.RoleCreatedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: role.GetName(),
},
Expand Down Expand Up @@ -92,10 +90,7 @@ func (a *Server) DeleteRole(ctx context.Context, name string) error {
Type: events.RoleDeletedEvent,
Code: events.RoleDeletedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: name,
},
Expand All @@ -116,10 +111,7 @@ func (a *Server) UpsertLock(ctx context.Context, lock types.Lock) error {
Type: events.LockCreatedEvent,
Code: events.LockCreatedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: lock.GetName(),
},
Expand All @@ -140,10 +132,7 @@ func (a *Server) DeleteLock(ctx context.Context, lockName string) error {
Type: events.LockDeletedEvent,
Code: events.LockDeletedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: lockName,
},
Expand Down
19 changes: 5 additions & 14 deletions lib/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,18 +1355,15 @@ func (a *Server) GenerateToken(ctx context.Context, req GenerateTokenRequest) (s
return "", trace.Wrap(err)
}

user := ClientUsername(ctx)
userMetadata := ClientUserMetadata(ctx)
for _, role := range req.Roles {
if role == types.RoleTrustedCluster {
if err := a.emitter.EmitAuditEvent(ctx, &apievents.TrustedClusterTokenCreate{
Metadata: apievents.Metadata{
Type: events.TrustedClusterTokenCreateEvent,
Code: events.TrustedClusterTokenCreateCode,
},
UserMetadata: apievents.UserMetadata{
User: user,
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: userMetadata,
}); err != nil {
log.WithError(err).Warn("Failed to emit trusted cluster token create event.")
}
Expand Down Expand Up @@ -1976,10 +1973,7 @@ func (a *Server) CreateAccessRequest(ctx context.Context, req types.AccessReques
Type: events.AccessRequestCreateEvent,
Code: events.AccessRequestCreateCode,
},
UserMetadata: apievents.UserMetadata{
User: req.GetUser(),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadataWithUser(ctx, req.GetUser()),
ResourceMetadata: apievents.ResourceMetadata{
Expires: req.GetAccessExpiry(),
},
Expand All @@ -2003,11 +1997,8 @@ func (a *Server) DeleteAccessRequest(ctx context.Context, name string) error {
Type: events.AccessRequestDeleteEvent,
Code: events.AccessRequestDeleteCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
RequestID: name,
UserMetadata: ClientUserMetadata(ctx),
RequestID: name,
}); err != nil {
log.WithError(err).Warn("Failed to emit access request delete event.")
}
Expand Down
10 changes: 2 additions & 8 deletions lib/auth/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ func (a *Server) upsertGithubConnector(ctx context.Context, connector types.Gith
Type: events.GithubConnectorCreatedEvent,
Code: events.GithubConnectorCreatedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connector.GetName(),
},
Expand All @@ -100,10 +97,7 @@ func (a *Server) deleteGithubConnector(ctx context.Context, connectorName string
Type: events.GithubConnectorDeletedEvent,
Code: events.GithubConnectorDeletedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connectorName,
},
Expand Down
8 changes: 2 additions & 6 deletions lib/auth/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,9 +1586,7 @@ func (g *GRPCServer) AddMFADevice(stream proto.AuthService_AddMFADeviceServer) e
Code: events.MFADeviceAddEventCode,
ClusterName: clusterName.GetClusterName(),
},
UserMetadata: apievents.UserMetadata{
User: actx.Identity.GetIdentity().Username,
},
UserMetadata: actx.Identity.GetIdentity().GetUserMetadata(),
MFADeviceMetadata: mfaDeviceEventMetadata(dev),
}); err != nil {
return trace.Wrap(err)
Expand Down Expand Up @@ -1871,9 +1869,7 @@ func (g *GRPCServer) DeleteMFADevice(stream proto.AuthService_DeleteMFADeviceSer
Code: events.MFADeviceDeleteEventCode,
ClusterName: clusterName.GetClusterName(),
},
UserMetadata: apievents.UserMetadata{
User: actx.Identity.GetIdentity().Username,
},
UserMetadata: actx.Identity.GetIdentity().GetUserMetadata(),
MFADeviceMetadata: mfaDeviceEventMetadata(d),
}); err != nil {
return trace.Wrap(err)
Expand Down
10 changes: 2 additions & 8 deletions lib/auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ func (a *Server) UpsertOIDCConnector(ctx context.Context, connector types.OIDCCo
Type: events.OIDCConnectorCreatedEvent,
Code: events.OIDCConnectorCreatedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connector.GetName(),
},
Expand All @@ -164,10 +161,7 @@ func (a *Server) DeleteOIDCConnector(ctx context.Context, connectorName string)
Type: events.OIDCConnectorDeletedEvent,
Code: events.OIDCConnectorDeletedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connectorName,
},
Expand Down
36 changes: 36 additions & 0 deletions lib/auth/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/api/types/wrappers"
"github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/lib/services"
Expand Down Expand Up @@ -714,6 +715,41 @@ func ClientImpersonator(ctx context.Context) string {
return identity.Impersonator
}

// ClientUserMetadata returns a UserMetadata suitable for events caused by a
// remote client making a call. If ctx didn't pass through auth middleware or
// did not come from an HTTP request, metadata for teleport.UserSystem is
// returned.
func ClientUserMetadata(ctx context.Context) apievents.UserMetadata {
userI := ctx.Value(ContextUser)
userWithIdentity, ok := userI.(IdentityGetter)
if !ok {
return apievents.UserMetadata{
User: teleport.UserSystem,
}
}
meta := userWithIdentity.GetIdentity().GetUserMetadata()
if meta.User == "" {
meta.User = teleport.UserSystem
}
return meta
}

// ClientUserMetadataWithUser returns a UserMetadata suitable for events caused
// by a remote client making a call, with the specified username overriding the one
// from the remote client.
func ClientUserMetadataWithUser(ctx context.Context, user string) apievents.UserMetadata {
userI := ctx.Value(ContextUser)
userWithIdentity, ok := userI.(IdentityGetter)
if !ok {
return apievents.UserMetadata{
User: user,
}
}
meta := userWithIdentity.GetIdentity().GetUserMetadata()
meta.User = user
return meta
}

// LocalUser is a local user
type LocalUser struct {
// Username is local username
Expand Down
5 changes: 1 addition & 4 deletions lib/auth/resetpasswordtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ func (s *Server) CreateResetPasswordToken(ctx context.Context, req CreateResetPa
Type: events.ResetPasswordTokenCreateEvent,
Code: events.ResetPasswordTokenCreateCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: req.Name,
TTL: req.TTL.String(),
Expand Down
10 changes: 2 additions & 8 deletions lib/auth/saml.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ func (a *Server) UpsertSAMLConnector(ctx context.Context, connector types.SAMLCo
Type: events.SAMLConnectorCreatedEvent,
Code: events.SAMLConnectorCreatedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connector.GetName(),
},
Expand All @@ -75,10 +72,7 @@ func (a *Server) DeleteSAMLConnector(ctx context.Context, connectorName string)
Type: events.SAMLConnectorDeletedEvent,
Code: events.SAMLConnectorDeletedCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: connectorName,
},
Expand Down
10 changes: 2 additions & 8 deletions lib/auth/trustedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ func (a *Server) UpsertTrustedCluster(ctx context.Context, trustedCluster types.
Type: events.TrustedClusterCreateEvent,
Code: events.TrustedClusterCreateCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: trustedCluster.GetName(),
},
Expand Down Expand Up @@ -223,10 +220,7 @@ func (a *Server) DeleteTrustedCluster(ctx context.Context, name string) error {
Type: events.TrustedClusterDeleteEvent,
Code: events.TrustedClusterDeleteCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: name,
},
Expand Down
15 changes: 3 additions & 12 deletions lib/auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ func (s *Server) CreateUser(ctx context.Context, user types.User) error {
Type: events.UserCreateEvent,
Code: events.UserCreateCode,
},
UserMetadata: apievents.UserMetadata{
User: user.GetCreatedBy().User.Name,
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadataWithUser(ctx, user.GetCreatedBy().User.Name),
ResourceMetadata: apievents.ResourceMetadata{
Name: user.GetName(),
Expires: user.Expiry(),
Expand Down Expand Up @@ -98,10 +95,7 @@ func (s *Server) UpdateUser(ctx context.Context, user types.User) error {
Type: events.UserUpdatedEvent,
Code: events.UserUpdateCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: user.GetName(),
Expires: user.Expiry(),
Expand Down Expand Up @@ -176,10 +170,7 @@ func (s *Server) DeleteUser(ctx context.Context, user string) error {
Type: events.UserDeleteEvent,
Code: events.UserDeleteCode,
},
UserMetadata: apievents.UserMetadata{
User: ClientUsername(ctx),
Impersonator: ClientImpersonator(ctx),
},
UserMetadata: ClientUserMetadata(ctx),
ResourceMetadata: apievents.ResourceMetadata{
Name: user,
},
Expand Down
Loading

0 comments on commit 404d05b

Please sign in to comment.