From 916a919bf0d7d917e9bd82ac2b55608a26ae6cce Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Fri, 12 May 2023 17:57:29 +0000 Subject: [PATCH] feat(bsr): Remove ascii format of protocol This was originally introduced to ensure that the meta files only included ascii characters. However, this makes reading the protocol from meta file less reliable since it does not exactly match the protocol string. Instead protocol strings will be recorded in the meta files as is. Generally protocol strings should include readable characters. This also updates the ssh.Protocol to be 4 readable characters. --- internal/bsr/bsr_open_test.go | 6 +++--- internal/bsr/meta.go | 2 +- internal/bsr/protocol.go | 9 --------- internal/bsr/protocol_test.go | 36 ----------------------------------- internal/bsr/ssh/chunk.go | 2 +- 5 files changed, 5 insertions(+), 50 deletions(-) diff --git a/internal/bsr/bsr_open_test.go b/internal/bsr/bsr_open_test.go index 53baf244d..33e21e08f 100644 --- a/internal/bsr/bsr_open_test.go +++ b/internal/bsr/bsr_open_test.go @@ -21,10 +21,10 @@ func TestPopulateMeta(t *testing.T) { require.NoError(t, err) sessionId := "session" - sessionProtocol := "TEST" + sessionProtocol := Protocol("TEST") // Populate session meta - sessionMeta := TestSessionMeta(sessionId, Protocol((sessionProtocol))) + sessionMeta := TestSessionMeta(sessionId, sessionProtocol) s, err := NewSession(ctx, sessionMeta, &fstest.MemFS{}, keys, WithSupportsMultiplex(true)) require.NoError(t, err) require.NotNil(t, s) @@ -35,7 +35,7 @@ func TestPopulateMeta(t *testing.T) { require.NoError(t, err) s.Meta = sm require.Equal(t, s.Meta.Id, sessionId) - require.Equal(t, s.Meta.Protocol.ToText(), sessionProtocol) + require.Equal(t, s.Meta.Protocol, sessionProtocol) require.Equal(t, s.Meta.Target, sessionMeta.Target) require.Equal(t, s.Meta.User, sessionMeta.User) require.Equal(t, s.Meta.StaticHost, sessionMeta.StaticHost) diff --git a/internal/bsr/meta.go b/internal/bsr/meta.go index 9f77189ae..2c818033a 100644 --- a/internal/bsr/meta.go +++ b/internal/bsr/meta.go @@ -738,7 +738,7 @@ func (s SessionMeta) writeMeta(ctx context.Context, c *container) error { if err != nil { return err } - _, err = c.WriteMeta(ctx, "protocol", s.Protocol.ToText()) + _, err = c.WriteMeta(ctx, "protocol", string(s.Protocol)) if err != nil { return err } diff --git a/internal/bsr/protocol.go b/internal/bsr/protocol.go index 6711931fd..e4ad1d895 100644 --- a/internal/bsr/protocol.go +++ b/internal/bsr/protocol.go @@ -3,8 +3,6 @@ package bsr -import "regexp" - // Protocol identifies the protocol of the data captured in a chunk. type Protocol string @@ -12,10 +10,3 @@ type Protocol string func ValidProtocol(p Protocol) bool { return len(p) <= protocolSize } - -// ToText converts a protocol to a text-safe string -func (p Protocol) ToText() string { - re := regexp.MustCompile("[[:^ascii:]]") - t := re.ReplaceAllLiteralString(string(p), "") - return t -} diff --git a/internal/bsr/protocol_test.go b/internal/bsr/protocol_test.go index b1c18f6c8..e72886de1 100644 --- a/internal/bsr/protocol_test.go +++ b/internal/bsr/protocol_test.go @@ -35,39 +35,3 @@ func TestValidProtocol(t *testing.T) { }) } } - -func TestToText(t *testing.T) { - cases := []struct { - name string - in bsr.Protocol - want string - }{ - { - "all ascii", - bsr.Protocol("BOOM"), - "BOOM", - }, - { - "not all ascii", - bsr.Protocol("SSH\x87"), - "SSH", - }, - { - "mostly not ascii", - bsr.Protocol("S\x89\x99\x87"), - "S", - }, - { - "no ascii", - bsr.Protocol("\x95\x89\x99\x87"), - "", - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - got := tc.in.ToText() - assert.Equal(t, tc.want, got) - }) - } -} diff --git a/internal/bsr/ssh/chunk.go b/internal/bsr/ssh/chunk.go index a49a59bcb..cf8c2f595 100644 --- a/internal/bsr/ssh/chunk.go +++ b/internal/bsr/ssh/chunk.go @@ -44,7 +44,7 @@ func init() { const ( // Protocol is used to identify chunks that are recorded from SSH. - Protocol bsr.Protocol = "SSH\x87" + Protocol bsr.Protocol = "BSSH" // MaxPacketSize is used by the DataWriter to determine if SSH data should // be broken into multiple chunks.