Skip to content

Commit

Permalink
tool/tsh: support ID for tsh play -f json
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex McGrath authored and lxea committed Jan 5, 2022
1 parent e6d3f52 commit 672425f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
26 changes: 26 additions & 0 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,32 @@ func (tc *TeleportClient) Play(ctx context.Context, namespace, sessionID string)
return playSession(sessionEvents, stream)
}

func (tc *TeleportClient) GetSessionEvents(ctx context.Context, namespace, sessionID string) ([]events.EventFields, error) {
if namespace == "" {
return nil, trace.BadParameter(auth.MissingNamespaceError)
}
sid, err := session.ParseID(sessionID)
if err != nil {
return nil, fmt.Errorf("'%v' is not a valid session ID (must be GUID)", sid)
}
// connect to the auth server (site) who made the recording
proxyClient, err := tc.ConnectToProxy(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
defer proxyClient.Close()

site, err := proxyClient.ConnectToCurrentCluster(ctx, false)
if err != nil {
return nil, trace.Wrap(err)
}
events, err := site.GetSessionEvents(namespace, *sid, 0, true)
if err != nil {
return nil, trace.Wrap(err)
}
return events, nil
}

// PlayFile plays the recorded session from a tar file
func PlayFile(ctx context.Context, tarFile io.Reader, sid string) error {
var sessionEvents []events.EventFields
Expand Down
28 changes: 25 additions & 3 deletions tool/tsh/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,31 @@ func onPlay(cf *CLIConf) error {
}
}
default:
err := exportFile(cf.SessionID, cf.Format)
if err != nil {
return trace.Wrap(err)
switch {
case path.Ext(cf.SessionID) == ".tar":
err := exportFile(cf.SessionID, cf.Format)
if err != nil {
return trace.Wrap(err)
}
default:
tc, err := makeClient(cf, true)
if err != nil {
return trace.Wrap(err)
}
events, err := tc.GetSessionEvents(context.TODO(), cf.Namespace, cf.SessionID)
if err != nil {
return trace.Wrap(err)
}
for _, event := range events {
// when playing from a file, id is not included, this
// makes the outputs otherwise identical
delete(event, "id")
e, err := utils.FastMarshal(event)
if err != nil {
return trace.Wrap(err)
}
fmt.Println(string(e))
}
}
}
return nil
Expand Down

0 comments on commit 672425f

Please sign in to comment.