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 Session tracker to DB, App, and Windows Desktop Sessions #12304

Merged
merged 44 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
d96cb6c
Add UpsertSessionTracker grpc method.
Joerger Apr 27, 2022
72a1fe3
Add session tracker to app sessions.
Joerger Apr 29, 2022
255f938
Add session tracker to windows desktop sessions.
Joerger Apr 29, 2022
43e06ee
Resolve Comments.
Joerger Apr 29, 2022
d01ad4a
Fixes and cleanup.
Joerger Apr 29, 2022
6efd481
Revert from UpsertSessionTracker to CreateSessionTracker.
Joerger Apr 30, 2022
2961ba3
Fix tests
Joerger Apr 30, 2022
d161527
Merge branch 'master' into joerger/track-all-sessions
Joerger May 2, 2022
4020281
Cleanup.
Joerger May 2, 2022
3818b32
Merge branch 'master' into joerger/track-all-sessions
Joerger May 2, 2022
9d4b670
Merge branch 'master' into joerger/track-all-sessions
Joerger May 2, 2022
9a2a239
Fix tests.
Joerger May 2, 2022
4259a31
Merge branch 'joerger/track-all-sessions' of github.com:gravitational…
Joerger May 2, 2022
c3a826f
Resolve comments.
Joerger May 2, 2022
f41112f
Require built in role for GetActiveSessiontrackers.
Joerger May 2, 2022
224044c
Minor changes.
Joerger May 2, 2022
7b45d4a
Merge branch 'master' into joerger/track-all-sessions
Joerger May 2, 2022
0c0ed67
Fix typo.
Joerger May 2, 2022
8ce6a0a
Resolve comments; Replace SessionTrackerWithCond and Service helper
Joerger May 3, 2022
5da4303
Merge branch 'master' into joerger/track-all-sessions
Joerger May 4, 2022
db21036
Fix race condition.
Joerger May 4, 2022
9fcec31
Close session tracker in kube session.
Joerger May 4, 2022
78e73ef
Fix race condition.
Joerger May 4, 2022
be6cb02
Cleanup session tracker synchronization, fix tests.
Joerger May 4, 2022
ac1ddba
Merge branch 'master' into joerger/track-all-sessions
Joerger May 4, 2022
59bfe8e
Add WaitForStateChange test and fix.
Joerger May 4, 2022
69ebae7
Fix serialization test.
Joerger May 4, 2022
80942c9
Fix log spam on session leave.
Joerger May 4, 2022
dc804fc
Fix party add/close logic with terminated session.
Joerger May 4, 2022
be4654e
Merge branch 'master' into joerger/track-all-sessions
Joerger May 5, 2022
2158bfa
Merge branch 'master' into joerger/track-all-sessions
Joerger May 5, 2022
c8ceb49
Use pre-existing IsServer role check.
Joerger May 5, 2022
01210e1
Merge branch 'joerger/track-all-sessions' of github.com:gravitational…
Joerger May 5, 2022
aea3f40
Merge branch 'master' into joerger/track-all-sessions
Joerger May 5, 2022
539f6c9
Merge branch 'master' into joerger/track-all-sessions
Joerger May 9, 2022
f34aca4
Replace clock.After with ticker.
Joerger May 10, 2022
d6b65b9
Merge branch 'master' into joerger/track-all-sessions
Joerger May 10, 2022
d9a3974
Update app session tracker to track individual chunks; Improve naming…
Joerger May 10, 2022
d865ed6
Merge branch 'master' into joerger/track-all-sessions
Joerger May 10, 2022
cdc4369
Update newStreamWriter comment.
Joerger May 11, 2022
dac419f
Merge branch 'master' into joerger/track-all-sessions
Joerger May 11, 2022
198094a
Fix app session chunk ttl.
Joerger May 11, 2022
349f11c
Merge branch 'master' into joerger/track-all-sessions
Joerger May 12, 2022
7e61b39
Merge branch 'master' into joerger/track-all-sessions
Joerger May 12, 2022
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
45 changes: 36 additions & 9 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,7 @@ func (c *Client) CreateRegisterChallenge(ctx context.Context, in *proto.CreateRe

// GenerateCertAuthorityCRL generates an empty CRL for a CA.
func (c *Client) GenerateCertAuthorityCRL(ctx context.Context, req *proto.CertAuthorityRequest) (*proto.CRL, error) {
resp, err := c.grpc.GenerateCertAuthorityCRL(ctx, req)
resp, err := c.grpc.GenerateCertAuthorityCRL(ctx, req, c.callOpts...)
return resp, trail.FromGRPC(err)
}

Expand Down Expand Up @@ -2525,21 +2525,48 @@ func GetResourcesWithFilters(ctx context.Context, clt ListResourcesClient, req p
}

// CreateSessionTracker creates a tracker resource for an active session.
func (c *Client) CreateSessionTracker(ctx context.Context, req *proto.CreateSessionTrackerRequest) (types.SessionTracker, error) {
resp, err := c.grpc.CreateSessionTracker(ctx, req)
return resp, trail.FromGRPC(err)
func (c *Client) CreateSessionTracker(ctx context.Context, st types.SessionTracker) (types.SessionTracker, error) {
v1, ok := st.(*types.SessionTrackerV1)
if !ok {
return nil, trace.BadParameter("invalid type %T, expected *types.SessionTrackerV1", st)
}

req := &proto.CreateSessionTrackerRequest{SessionTracker: v1}

// DELETE IN 11.0.0
// Early v9 versions use a flattened out types.SessionTrackerV1
req.ID = v1.Spec.SessionID
req.Type = v1.Spec.Kind
req.Reason = v1.Spec.Reason
req.Invited = v1.Spec.Invited
req.Hostname = v1.Spec.Hostname
req.Address = v1.Spec.Address
req.ClusterName = v1.Spec.ClusterName
req.Login = v1.Spec.Login
req.Expires = v1.Spec.Expires
req.KubernetesCluster = v1.Spec.KubernetesCluster
req.HostUser = v1.Spec.HostUser
if len(v1.Spec.Participants) > 0 {
req.Initiator = &v1.Spec.Participants[0]
}

tracker, err := c.grpc.CreateSessionTracker(ctx, req, c.callOpts...)
if err != nil {
return nil, trail.FromGRPC(err)
}
return tracker, nil
}

// GetSessionTracker returns the current state of a session tracker for an active session.
func (c *Client) GetSessionTracker(ctx context.Context, sessionID string) (types.SessionTracker, error) {
req := &proto.GetSessionTrackerRequest{SessionID: sessionID}
resp, err := c.grpc.GetSessionTracker(ctx, req)
resp, err := c.grpc.GetSessionTracker(ctx, req, c.callOpts...)
return resp, trail.FromGRPC(err)
}

// GetActiveSessionTrackers returns a list of active session trackers.
func (c *Client) GetActiveSessionTrackers(ctx context.Context) ([]types.SessionTracker, error) {
stream, err := c.grpc.GetActiveSessionTrackers(ctx, &empty.Empty{})
stream, err := c.grpc.GetActiveSessionTrackers(ctx, &empty.Empty{}, c.callOpts...)
if err != nil {
return nil, trail.FromGRPC(err)
}
Expand All @@ -2563,18 +2590,18 @@ func (c *Client) GetActiveSessionTrackers(ctx context.Context) ([]types.SessionT

// RemoveSessionTracker removes a tracker resource for an active session.
func (c *Client) RemoveSessionTracker(ctx context.Context, sessionID string) error {
_, err := c.grpc.RemoveSessionTracker(ctx, &proto.RemoveSessionTrackerRequest{SessionID: sessionID})
_, err := c.grpc.RemoveSessionTracker(ctx, &proto.RemoveSessionTrackerRequest{SessionID: sessionID}, c.callOpts...)
return trail.FromGRPC(err)
}

// UpdateSessionTracker updates a tracker resource for an active session.
func (c *Client) UpdateSessionTracker(ctx context.Context, req *proto.UpdateSessionTrackerRequest) error {
_, err := c.grpc.UpdateSessionTracker(ctx, req)
_, err := c.grpc.UpdateSessionTracker(ctx, req, c.callOpts...)
return trail.FromGRPC(err)
}

// MaintainSessionPresence establishes a channel used to continuously verify the presence for a session.
func (c *Client) MaintainSessionPresence(ctx context.Context) (proto.AuthService_MaintainSessionPresenceClient, error) {
stream, err := c.grpc.MaintainSessionPresence(ctx)
stream, err := c.grpc.MaintainSessionPresence(ctx, c.callOpts...)
Joerger marked this conversation as resolved.
Show resolved Hide resolved
return stream, trail.FromGRPC(err)
}
1,120 changes: 598 additions & 522 deletions api/client/proto/authservice.pb.go

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions api/client/proto/authservice.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1538,55 +1538,73 @@ message ListResourcesResponse {
// This is not specific to any session type. Relevant fields should be set for a given session type.
message CreateSessionTrackerRequest {
// Namespace is a session namespace, separating sessions from each other.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string Namespace = 1 [ (gogoproto.jsontag) = "namespace,omitempty" ];

// Type describes what type of session this is.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string Type = 2 [ (gogoproto.jsontag) = "type,omitempty" ];

// Reason is an arbitrary string that may be used to describe the session and/or it's
// purpose.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string Reason = 3 [ (gogoproto.jsontag) = "reason,omitempty" ];

// Invited is a list of invited users, this field is interpreted by different
// clients on a best-effort basis and used for delivering notifications to invited users.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
repeated string Invited = 4 [ (gogoproto.jsontag) = "invited,omitempty" ];

// Hostname is the address of the target this session is connected to.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string Hostname = 5 [ (gogoproto.jsontag) = "target_hostname,omitempty" ];

// Address is the address of the target this session is connected to.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string Address = 6 [ (gogoproto.jsontag) = "target_address,omitempty" ];

// ClusterName is the name of cluster that this session belongs to.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string ClusterName = 7 [ (gogoproto.jsontag) = "cluster_name,omitempty" ];

// Login is the local login/user on the target used by the session.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string Login = 8 [ (gogoproto.jsontag) = "login,omitempty" ];

// Initiator is the participant that initiated the session.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
types.Participant Initiator = 9 [ (gogoproto.jsontag) = "initiator,omitempty" ];

// Expires encodes the time at which this session expires and becomes invalid.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
google.protobuf.Timestamp Expires = 10 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "expires,omitempty"
];

// The Kubernetes cluster this session belongs to.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string KubernetesCluster = 11 [ (gogoproto.jsontag) = "kubernetes_cluster,omitempty" ];

// HostUser is the user regarded as the owner of this session, RBAC checks are performed
// against the require policies of this user.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string HostUser = 12 [ (gogoproto.jsontag) = "host_user,omitempty" ];

// ID is the ID of the session.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
string ID = 13 [ (gogoproto.jsontag) = "id,omitempty" ];

// HostPolicies is a list of RBAC policy sets held by the host user at the time of session
// creation.
// DELETE IN V11 - deprecated/reserve in favor of SessionTracker field.
repeated types.SessionTrackerPolicySet HostPolicies = 14
[ (gogoproto.jsontag) = "host_policies,omitempty" ];

// SessionTracker is the session tracker to be created.
types.SessionTrackerV1 SessionTracker = 15
[ (gogoproto.jsontag) = "session_tracker,omitempty" ];
}

// GetSessionTrackerRequest is a request to fetch a session resource.
Expand Down
3 changes: 3 additions & 0 deletions api/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const (
// deviation added to this time to avoid lots of simultaneous
// heartbeats coming to auth server
ServerAnnounceTTL = 600 * time.Second

// SessionTrackerTTL defines the default base ttl of a session tracker.
SessionTrackerTTL = time.Hour
)

var (
Expand Down
36 changes: 31 additions & 5 deletions api/types/session_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ package types
import (
"time"

"github.com/gravitational/teleport/api/defaults"

"github.com/gravitational/trace"
)

const (
SSHSessionKind SessionKind = "ssh"
KubernetesSessionKind SessionKind = "k8s"
SessionObserverMode SessionParticipantMode = "observer"
SessionModeratorMode SessionParticipantMode = "moderator"
SessionPeerMode SessionParticipantMode = "peer"
SSHSessionKind SessionKind = "ssh"
KubernetesSessionKind SessionKind = "k8s"
DatabaseSessionKind SessionKind = "db"
AppSessionKind SessionKind = "app"
WindowsDesktopSessionKind SessionKind = "desktop"
SessionObserverMode SessionParticipantMode = "observer"
SessionModeratorMode SessionParticipantMode = "moderator"
SessionPeerMode SessionParticipantMode = "peer"
)

// SessionKind is a type of session.
Expand All @@ -52,6 +57,9 @@ type SessionTracker interface {
// SetState sets the state of the session.
SetState(SessionState) error

// SetCreated sets the time at which the session was created.
SetCreated(time.Time)

// GetCreated returns the time at which the session was created.
GetCreated() time.Time

Expand Down Expand Up @@ -186,6 +194,19 @@ func (s *SessionTrackerV1) CheckAndSetDefaults() error {
return trace.Wrap(err)
}

if s.GetCreated().IsZero() {
s.SetCreated(time.Now())
}

if s.Expiry().IsZero() {
// By default, resource expiration should match session expiration.
expiry := s.GetExpires()
if expiry.IsZero() {
expiry = s.GetCreated().Add(defaults.SessionTrackerTTL)
}
s.SetExpiry(expiry)
}

return nil
}

Expand Down Expand Up @@ -220,6 +241,11 @@ func (s *SessionTrackerV1) GetCreated() time.Time {
return s.Spec.Created
}

// SetCreated returns the time at which the session was created.
func (s *SessionTrackerV1) SetCreated(created time.Time) {
s.Spec.Created = created
}

// GetExpires return the time at which the session expires.
func (s *SessionTrackerV1) GetExpires() time.Time {
return s.Spec.Expires
Expand Down
Loading