269 lines
7.1 KiB
Go
269 lines
7.1 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package bsr
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/hashicorp/boundary/internal/bsr/internal/checksum"
|
|
"github.com/hashicorp/boundary/internal/bsr/internal/is"
|
|
"github.com/hashicorp/boundary/internal/bsr/kms"
|
|
"github.com/hashicorp/boundary/internal/storage"
|
|
)
|
|
|
|
const (
|
|
bsrFile = "%s.bsr"
|
|
connectionFile = "%s.connection"
|
|
channelFile = "%s.channel"
|
|
messagesFile = "messages-%s.data"
|
|
requestsFile = "requests-%s.data"
|
|
)
|
|
|
|
// SessionMeta contains metadata about a session in a BSR.
|
|
type SessionMeta struct {
|
|
Id string
|
|
Protocol Protocol
|
|
}
|
|
|
|
// Session is the top level container in a bsr that contains the files for
|
|
// a recorded session.
|
|
type Session struct {
|
|
*container
|
|
multiplexed bool
|
|
|
|
Meta *SessionMeta
|
|
}
|
|
|
|
// NewSession creates a Session container for a given session id.
|
|
func NewSession(ctx context.Context, meta *SessionMeta, f storage.FS, keys *kms.Keys, options ...Option) (*Session, error) {
|
|
const op = "bsr.NewSession"
|
|
|
|
switch {
|
|
case is.Nil(meta):
|
|
return nil, fmt.Errorf("%s: missing session meta: %w", op, ErrInvalidParameter)
|
|
case meta.Id == "":
|
|
return nil, fmt.Errorf("%s: missing session id: %w", op, ErrInvalidParameter)
|
|
case is.Nil(f):
|
|
return nil, fmt.Errorf("%s: missing storage fs: %w", op, ErrInvalidParameter)
|
|
case is.Nil(keys):
|
|
return nil, fmt.Errorf("%s: missing kms keys: %w", op, ErrInvalidParameter)
|
|
}
|
|
|
|
opts := getOpts(options...)
|
|
|
|
c, err := f.New(ctx, fmt.Sprintf(bsrFile, meta.Id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nc, err := newContainer(ctx, sessionContainer, c, keys)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = nc.WriteMeta(ctx, "id", meta.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = nc.WriteMeta(ctx, "protocol", meta.Protocol.ToText())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Session{
|
|
container: nc,
|
|
multiplexed: opts.withSupportsMultiplex,
|
|
Meta: meta,
|
|
}, nil
|
|
}
|
|
|
|
// NewConnection creates a Connection container for a given connection id.
|
|
func (s *Session) NewConnection(ctx context.Context, meta *ConnectionMeta) (*Connection, error) {
|
|
const op = "bsr.(Session).NewConnection"
|
|
|
|
switch {
|
|
case is.Nil(meta):
|
|
return nil, fmt.Errorf("%s: missing connection meta: %w", op, ErrInvalidParameter)
|
|
case meta.Id == "":
|
|
return nil, fmt.Errorf("%s: missing connection id: %w", op, ErrInvalidParameter)
|
|
}
|
|
|
|
name := fmt.Sprintf(connectionFile, meta.Id)
|
|
sc, err := s.container.container.SubContainer(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := s.WriteMeta(ctx, "connection", name); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nc, err := newContainer(ctx, connectionContainer, sc, s.keys)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := nc.WriteMeta(ctx, "id", meta.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
return &Connection{
|
|
container: nc,
|
|
multiplexed: s.multiplexed,
|
|
Meta: meta,
|
|
}, nil
|
|
}
|
|
|
|
// Close closes the Session container.
|
|
func (s *Session) Close(ctx context.Context) error {
|
|
return s.container.close(ctx)
|
|
}
|
|
|
|
// ConnectionMeta contains metadata about a connection in a BSR.
|
|
type ConnectionMeta struct {
|
|
Id string
|
|
}
|
|
|
|
// Connection is a container in a bsr for a specific connection in a session
|
|
// container. It contains the files for the recorded connection.
|
|
type Connection struct {
|
|
*container
|
|
multiplexed bool
|
|
|
|
Meta *ConnectionMeta
|
|
}
|
|
|
|
// NewChannel creates a Channel container for a given channel id.
|
|
func (c *Connection) NewChannel(ctx context.Context, meta *ChannelMeta) (*Channel, error) {
|
|
const op = "bsr.(Connection).NewChannel"
|
|
|
|
if !c.multiplexed {
|
|
return nil, fmt.Errorf("%s: connection cannot make channels: %w", op, ErrNotSupported)
|
|
}
|
|
|
|
switch {
|
|
case is.Nil(meta):
|
|
return nil, fmt.Errorf("%s: missing channel meta: %w", op, ErrInvalidParameter)
|
|
case meta.Id == "":
|
|
return nil, fmt.Errorf("%s: missing channel id: %w", op, ErrInvalidParameter)
|
|
}
|
|
|
|
name := fmt.Sprintf(channelFile, meta.Id)
|
|
sc, err := c.container.container.SubContainer(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := c.WriteMeta(ctx, "channel", name); err != nil {
|
|
return nil, err
|
|
}
|
|
nc, err := newContainer(ctx, channelContainer, sc, c.keys)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := nc.WriteMeta(ctx, "id", meta.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
return &Channel{
|
|
container: nc,
|
|
Meta: meta,
|
|
}, nil
|
|
}
|
|
|
|
// NewMessagesWriter creates a writer for recording channel messages.
|
|
func (c *Connection) NewMessagesWriter(ctx context.Context, dir Direction) (io.Writer, error) {
|
|
const op = "bsr.(Connection).NewMessagesWriter"
|
|
|
|
switch {
|
|
case !ValidDirection(dir):
|
|
return nil, fmt.Errorf("%s: invalid direction: %w", op, ErrInvalidParameter)
|
|
}
|
|
|
|
messagesName := fmt.Sprintf(messagesFile, dir.String())
|
|
c.container.WriteMeta(ctx, "messages", dir.String())
|
|
m, err := c.container.create(ctx, messagesName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return checksum.NewFile(ctx, m, c.checksums)
|
|
}
|
|
|
|
// NewRequestsWriter creates a writer for recording connection requests.
|
|
func (c *Connection) NewRequestsWriter(ctx context.Context, dir Direction) (io.Writer, error) {
|
|
const op = "bsr.(Connection).NewRequestsWriter"
|
|
|
|
switch {
|
|
case !ValidDirection(dir):
|
|
return nil, fmt.Errorf("%s: invalid direction: %w", op, ErrInvalidParameter)
|
|
}
|
|
|
|
requestName := fmt.Sprintf(requestsFile, dir.String())
|
|
c.container.WriteMeta(ctx, "requests", dir.String())
|
|
m, err := c.container.create(ctx, requestName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return checksum.NewFile(ctx, m, c.checksums)
|
|
}
|
|
|
|
// Close closes the Connection container.
|
|
func (c *Connection) Close(ctx context.Context) error {
|
|
return c.container.close(ctx)
|
|
}
|
|
|
|
// ChannelMeta contains metadata about a channel in a BSR.
|
|
type ChannelMeta struct {
|
|
Id string
|
|
Type string
|
|
}
|
|
|
|
// Channel is a container in a bsr for a specific channel in a session
|
|
// container. It contains the files for the recorded channel.
|
|
type Channel struct {
|
|
*container
|
|
|
|
Meta *ChannelMeta
|
|
}
|
|
|
|
// Close closes the Channel container.
|
|
func (c *Channel) Close(ctx context.Context) error {
|
|
return c.container.close(ctx)
|
|
}
|
|
|
|
// NewMessagesWriter creates a writer for recording channel messages.
|
|
func (c *Channel) NewMessagesWriter(ctx context.Context, dir Direction) (io.Writer, error) {
|
|
const op = "bsr.(Channel).NewMessagesWriter"
|
|
|
|
switch {
|
|
case !ValidDirection(dir):
|
|
return nil, fmt.Errorf("%s: invalid direction: %w", op, ErrInvalidParameter)
|
|
}
|
|
|
|
messagesName := fmt.Sprintf(messagesFile, dir.String())
|
|
c.container.WriteMeta(ctx, "messages", dir.String())
|
|
m, err := c.container.create(ctx, messagesName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return checksum.NewFile(ctx, m, c.checksums)
|
|
}
|
|
|
|
// NewRequestsWriter creates a writer for recording channel requests.
|
|
func (c *Channel) NewRequestsWriter(ctx context.Context, dir Direction) (io.Writer, error) {
|
|
const op = "bsr.(Channel).NewRequestsWriter"
|
|
|
|
switch {
|
|
case !ValidDirection(dir):
|
|
return nil, fmt.Errorf("%s: invalid direction: %w", op, ErrInvalidParameter)
|
|
}
|
|
|
|
requestName := fmt.Sprintf(requestsFile, dir.String())
|
|
c.container.WriteMeta(ctx, "requests", dir.String())
|
|
m, err := c.container.create(ctx, requestName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return checksum.NewFile(ctx, m, c.checksums)
|
|
}
|