Skip to content

Commit

Permalink
tsh play --format=text (#47073)
Browse files Browse the repository at this point in the history
For sessions with print events, this command will write the text directly
to standard out. This makes it easier to grep for a particular pattern or
just dump a session recording to a file for manual analysis.

Closes #11694
  • Loading branch information
zmb3 authored Oct 1, 2024
1 parent a217ab3 commit 4f8eedc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
23 changes: 21 additions & 2 deletions tool/tsh/common/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ func exportSession(cf *CLIConf) error {
}

switch format {
case teleport.JSON, teleport.YAML:
case teleport.JSON, teleport.YAML, teleport.Text:
default:
return trace.Errorf("Invalid format %s, only json and yaml are supported", format)
// this should be unreachable since kingpin validates the format flag
return trace.BadParameter("Invalid format %s", format)
}

sid, err := session.ParseID(cf.SessionID)
Expand All @@ -142,6 +143,8 @@ func exportSession(cf *CLIConf) error {
exporter = jsonSessionExporter{}
case teleport.YAML:
exporter = yamlSessionExporter{}
case teleport.Text:
exporter = textSessionExporter{}
}

exporter.WriteStart()
Expand Down Expand Up @@ -231,6 +234,22 @@ func (yamlSessionExporter) WriteEvent(evt apievents.AuditEvent) error {
return err
}

type textSessionExporter struct{}

func (textSessionExporter) WriteStart() error { return nil }
func (textSessionExporter) WriteEnd() error { return nil }
func (textSessionExporter) WriteSeparator() error { return nil }

func (textSessionExporter) WriteEvent(evt apievents.AuditEvent) error {
printEvent, ok := evt.(*apievents.SessionPrint)
if !ok {
return nil
}

_, err := os.Stdout.Write(printEvent.Data)
return trace.Wrap(err)
}

// exportFile converts the binary protobuf events from the file
// identified by path to text (JSON/YAML) and writes the converted
// events to standard out.
Expand Down
4 changes: 2 additions & 2 deletions tool/tsh/common/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -983,8 +983,8 @@ func Run(ctx context.Context, args []string, opts ...CliOption) error {
play.Flag("speed", "Playback speed, applicable when streaming SSH or Kubernetes sessions.").Default("1x").EnumVar(&cf.PlaySpeed, "0.5x", "1x", "2x", "4x", "8x")
play.Flag("skip-idle-time", "Quickly skip over idle time, applicable when streaming SSH or Kubernetes sessions.").BoolVar(&cf.NoWait)
play.Flag("format", defaults.FormatFlagDescription(
teleport.PTY, teleport.JSON, teleport.YAML,
)).Short('f').Default(teleport.PTY).EnumVar(&cf.Format, teleport.PTY, teleport.JSON, teleport.YAML)
teleport.PTY, teleport.JSON, teleport.YAML, teleport.Text,
)).Short('f').Default(teleport.PTY).EnumVar(&cf.Format, teleport.PTY, teleport.JSON, teleport.YAML, teleport.Text)
play.Arg("session-id", "ID or path to session file to play").Required().StringVar(&cf.SessionID)

// scp
Expand Down

0 comments on commit 4f8eedc

Please sign in to comment.