mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
feat: add boundary log forwarding from agent to coderd (#21345)
Add agent forwarding of boundary audit logs from workspaces to coderd via agent API, and re-emission of boundary logs to coderd stderr. This change adds a server to the workspace agent that always listens on a unix socket for boundary to connect and send audit logs. coderd log format example: ``` [API] 2025-12-23 18:31:46.755 [info] coderd.agentrpc: boundary_request owner=.. workspace_name=.. agent_name=.. decision=.. workspace_id=.. http_method=.. http_url=.. event_time=.. request_id=.. ``` Corresponding boundary PR: https://github.com/coder/boundary/pull/124 RFC: https://www.notion.so/coderhq/Agent-Boundary-Logs-2afd579be59280f29629fc9823ac41ba https://github.com/coder/coder/issues/21280
This commit is contained in:
+42
-4
@@ -43,6 +43,7 @@ import (
|
||||
"github.com/coder/coder/v2/agent/agentscripts"
|
||||
"github.com/coder/coder/v2/agent/agentsocket"
|
||||
"github.com/coder/coder/v2/agent/agentssh"
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy"
|
||||
"github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/agent/proto/resourcesmonitor"
|
||||
"github.com/coder/coder/v2/agent/reconnectingpty"
|
||||
@@ -102,6 +103,7 @@ type Options struct {
|
||||
Clock quartz.Clock
|
||||
SocketServerEnabled bool
|
||||
SocketPath string // Path for the agent socket server socket
|
||||
BoundaryLogProxySocketPath string
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
@@ -205,10 +207,11 @@ func New(options Options) Agent {
|
||||
metrics: newAgentMetrics(prometheusRegistry),
|
||||
execer: options.Execer,
|
||||
|
||||
devcontainers: options.Devcontainers,
|
||||
containerAPIOptions: options.DevcontainerAPIOptions,
|
||||
socketPath: options.SocketPath,
|
||||
socketServerEnabled: options.SocketServerEnabled,
|
||||
devcontainers: options.Devcontainers,
|
||||
containerAPIOptions: options.DevcontainerAPIOptions,
|
||||
socketPath: options.SocketPath,
|
||||
socketServerEnabled: options.SocketServerEnabled,
|
||||
boundaryLogProxySocketPath: options.BoundaryLogProxySocketPath,
|
||||
}
|
||||
// Initially, we have a closed channel, reflecting the fact that we are not initially connected.
|
||||
// Each time we connect we replace the channel (while holding the closeMutex) with a new one
|
||||
@@ -277,6 +280,11 @@ type agent struct {
|
||||
|
||||
logSender *agentsdk.LogSender
|
||||
|
||||
// boundaryLogProxy is a socket server that forwards boundary audit logs to coderd.
|
||||
// It may be nil if there is a problem starting the server.
|
||||
boundaryLogProxy *boundarylogproxy.Server
|
||||
boundaryLogProxySocketPath string
|
||||
|
||||
prometheusRegistry *prometheus.Registry
|
||||
// metrics are prometheus registered metrics that will be collected and
|
||||
// labeled in Coder with the agent + workspace.
|
||||
@@ -371,6 +379,7 @@ func (a *agent) init() {
|
||||
)
|
||||
|
||||
a.initSocketServer()
|
||||
a.startBoundaryLogProxyServer()
|
||||
|
||||
go a.runLoop()
|
||||
}
|
||||
@@ -395,6 +404,19 @@ func (a *agent) initSocketServer() {
|
||||
a.logger.Debug(a.hardCtx, "socket server started", slog.F("path", a.socketPath))
|
||||
}
|
||||
|
||||
// startBoundaryLogProxyServer starts the boundary log proxy socket server.
|
||||
func (a *agent) startBoundaryLogProxyServer() {
|
||||
proxy := boundarylogproxy.NewServer(a.logger, a.boundaryLogProxySocketPath)
|
||||
if err := proxy.Start(); err != nil {
|
||||
a.logger.Warn(a.hardCtx, "failed to start boundary log proxy", slog.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
a.boundaryLogProxy = proxy
|
||||
a.logger.Info(a.hardCtx, "boundary log proxy server started",
|
||||
slog.F("socket_path", a.boundaryLogProxySocketPath))
|
||||
}
|
||||
|
||||
// runLoop attempts to start the agent in a retry loop.
|
||||
// Coder may be offline temporarily, a connection issue
|
||||
// may be happening, but regardless after the intermittent
|
||||
@@ -1012,6 +1034,15 @@ func (a *agent) run() (retErr error) {
|
||||
return err
|
||||
})
|
||||
|
||||
// Forward boundary audit logs to coderd if boundary log forwarding is enabled.
|
||||
// These are audit logs so they should continue during graceful shutdown.
|
||||
if a.boundaryLogProxy != nil {
|
||||
proxyFunc := func(ctx context.Context, aAPI proto.DRPCAgentClient27) error {
|
||||
return a.boundaryLogProxy.RunForwarder(ctx, aAPI)
|
||||
}
|
||||
connMan.startAgentAPI("boundary log proxy", gracefulShutdownBehaviorRemain, proxyFunc)
|
||||
}
|
||||
|
||||
// part of graceful shut down is reporting the final lifecycle states, e.g "ShuttingDown" so the
|
||||
// lifecycle reporting has to be via gracefulShutdownBehaviorRemain
|
||||
connMan.startAgentAPI("report lifecycle", gracefulShutdownBehaviorRemain, a.reportLifecycle)
|
||||
@@ -1982,6 +2013,13 @@ func (a *agent) Close() error {
|
||||
a.logger.Error(a.hardCtx, "container API close", slog.Error(err))
|
||||
}
|
||||
|
||||
if a.boundaryLogProxy != nil {
|
||||
err = a.boundaryLogProxy.Close()
|
||||
if err != nil {
|
||||
a.logger.Warn(context.Background(), "close boundary log proxy", slog.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the graceful shutdown to complete, but don't wait forever so
|
||||
// that we don't break user expectations.
|
||||
go func() {
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package agent_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"cdr.dev/slog"
|
||||
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy"
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy/codec"
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/coderd/agentapi"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
// logSink captures structured log entries for testing.
|
||||
type logSink struct {
|
||||
mu sync.Mutex
|
||||
entries []slog.SinkEntry
|
||||
}
|
||||
|
||||
func (s *logSink) LogEntry(_ context.Context, e slog.SinkEntry) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.entries = append(s.entries, e)
|
||||
}
|
||||
|
||||
func (*logSink) Sync() {}
|
||||
|
||||
func (s *logSink) getEntries() []slog.SinkEntry {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return append([]slog.SinkEntry{}, s.entries...)
|
||||
}
|
||||
|
||||
// getField returns the value of a field by name from a slog.Map.
|
||||
func getField(fields slog.Map, name string) interface{} {
|
||||
for _, f := range fields {
|
||||
if f.Name == name {
|
||||
return f.Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendBoundaryLogsRequest(t *testing.T, conn net.Conn, req *agentproto.ReportBoundaryLogsRequest) {
|
||||
t.Helper()
|
||||
|
||||
data, err := proto.Marshal(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = codec.WriteFrame(conn, codec.TagV1, data)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// TestBoundaryLogs_EndToEnd is an end-to-end test that sends a protobuf
|
||||
// message over the agent's unix socket (as boundary would) and verifies
|
||||
// it is ultimately logged by coderd with the correct structured fields.
|
||||
func TestBoundaryLogs_EndToEnd(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
sink := &logSink{}
|
||||
logger := slog.Make(sink)
|
||||
workspaceID := uuid.New()
|
||||
reporter := &agentapi.BoundaryLogsAPI{
|
||||
Log: logger,
|
||||
WorkspaceID: workspaceID,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
// Allowed HTTP request.
|
||||
req := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: true,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "GET",
|
||||
Url: "https://example.com/allowed",
|
||||
MatchedRule: "*.example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendBoundaryLogsRequest(t, conn, req)
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
return len(sink.getEntries()) >= 1
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
entries := sink.getEntries()
|
||||
require.Len(t, entries, 1)
|
||||
entry := entries[0]
|
||||
require.Equal(t, slog.LevelInfo, entry.Level)
|
||||
require.Equal(t, "boundary_request", entry.Message)
|
||||
require.Equal(t, "allow", getField(entry.Fields, "decision"))
|
||||
require.Equal(t, workspaceID.String(), getField(entry.Fields, "workspace_id"))
|
||||
require.Equal(t, "GET", getField(entry.Fields, "http_method"))
|
||||
require.Equal(t, "https://example.com/allowed", getField(entry.Fields, "http_url"))
|
||||
require.Equal(t, "*.example.com", getField(entry.Fields, "matched_rule"))
|
||||
|
||||
// Denied HTTP request.
|
||||
req2 := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: false,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "POST",
|
||||
Url: "https://blocked.com/denied",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendBoundaryLogsRequest(t, conn, req2)
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
return len(sink.getEntries()) >= 2
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
entries = sink.getEntries()
|
||||
entry = entries[1]
|
||||
require.Len(t, entries, 2)
|
||||
require.Equal(t, slog.LevelInfo, entry.Level)
|
||||
require.Equal(t, "boundary_request", entry.Message)
|
||||
require.Equal(t, "deny", getField(entry.Fields, "decision"))
|
||||
require.Equal(t, workspaceID.String(), getField(entry.Fields, "workspace_id"))
|
||||
require.Equal(t, "POST", getField(entry.Fields, "http_method"))
|
||||
require.Equal(t, "https://blocked.com/denied", getField(entry.Fields, "http_url"))
|
||||
require.Equal(t, nil, getField(entry.Fields, "matched_rule"))
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
// Package codec implements the wire format for agent <-> boundary communication.
|
||||
//
|
||||
// Wire Format:
|
||||
// - 8 bits: big-endian tag
|
||||
// - 24 bits: big-endian length of the protobuf data (bit usage depends on tag)
|
||||
// - length bytes: encoded protobuf data
|
||||
//
|
||||
// Note that while there are 24 bits available for the length, the actual maximum
|
||||
// length depends on the tag. For TagV1, only 15 bits are used (MaxMessageSizeV1).
|
||||
package codec
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
type Tag uint8
|
||||
|
||||
const (
|
||||
// TagV1 identifies the first revision of the protocol. This version has a maximum
|
||||
// data length of MaxMessageSizeV1.
|
||||
TagV1 Tag = 1
|
||||
)
|
||||
|
||||
const (
|
||||
// DataLength is the number of bits used for the length of encoded protobuf data.
|
||||
DataLength = 24
|
||||
|
||||
// tagLength is the number of bits used for the tag.
|
||||
tagLength = 8
|
||||
|
||||
// MaxMessageSizeV1 is the maximum size of the encoded protobuf messages sent
|
||||
// over the wire for the TagV1 tag. While the wire format allows 24 bits for
|
||||
// length, TagV1 only uses 15 bits.
|
||||
MaxMessageSizeV1 uint32 = 1 << 15
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrMessageTooLarge is returned when the message exceeds the maximum size
|
||||
// allowed for the tag.
|
||||
ErrMessageTooLarge = xerrors.New("message too large")
|
||||
// ErrUnsupportedTag is returned when an unrecognized tag is encountered.
|
||||
ErrUnsupportedTag = xerrors.New("unsupported tag")
|
||||
)
|
||||
|
||||
// WriteFrame writes a framed message with the given tag and data. The data
|
||||
// must not exceed 2^DataLength in length.
|
||||
func WriteFrame(w io.Writer, tag Tag, data []byte) error {
|
||||
var maxSize uint32
|
||||
switch tag {
|
||||
case TagV1:
|
||||
maxSize = MaxMessageSizeV1
|
||||
default:
|
||||
return xerrors.Errorf("%w: %d", ErrUnsupportedTag, tag)
|
||||
}
|
||||
|
||||
if len(data) > int(maxSize) {
|
||||
return xerrors.Errorf("%w for tag %d: %d > %d", ErrMessageTooLarge, tag, len(data), maxSize)
|
||||
}
|
||||
|
||||
var header uint32
|
||||
//nolint:gosec // The length check above ensures there's no overflow.
|
||||
header |= uint32(len(data))
|
||||
header |= uint32(tag) << DataLength
|
||||
|
||||
if err := binary.Write(w, binary.BigEndian, header); err != nil {
|
||||
return xerrors.Errorf("write header error: %w", err)
|
||||
}
|
||||
if _, err := w.Write(data); err != nil {
|
||||
return xerrors.Errorf("write data error: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadFrame reads a framed message, returning the decoded tag and data. If the
|
||||
// message size exceeds MaxMessageSizeV1, ErrMessageTooLarge is returned. The
|
||||
// provided buf is used if it has sufficient capacity; otherwise a new buffer is
|
||||
// allocated. To reuse the buffer across calls, pass in the returned data slice:
|
||||
//
|
||||
// buf := make([]byte, initialSize)
|
||||
// for {
|
||||
// _, buf, _ = ReadFrame(r, buf)
|
||||
// }
|
||||
func ReadFrame(r io.Reader, buf []byte) (Tag, []byte, error) {
|
||||
var header uint32
|
||||
if err := binary.Read(r, binary.BigEndian, &header); err != nil {
|
||||
return 0, nil, xerrors.Errorf("read header error: %w", err)
|
||||
}
|
||||
|
||||
const lengthMask = (1 << DataLength) - 1
|
||||
length := header & lengthMask
|
||||
const tagMask = (1 << tagLength) - 1 // 0xFF
|
||||
shifted := (header >> DataLength) & tagMask
|
||||
if shifted > tagMask {
|
||||
// This is really only here to satisfy the gosec linter. We know from above that
|
||||
// shifted <= tagMask.
|
||||
return 0, nil, xerrors.Errorf("invalid tag: %d", shifted)
|
||||
}
|
||||
tag := Tag(shifted)
|
||||
|
||||
var maxSize uint32
|
||||
switch tag {
|
||||
case TagV1:
|
||||
maxSize = MaxMessageSizeV1
|
||||
default:
|
||||
return 0, nil, xerrors.Errorf("%w: %d", ErrUnsupportedTag, tag)
|
||||
}
|
||||
|
||||
if length > maxSize {
|
||||
return 0, nil, ErrMessageTooLarge
|
||||
}
|
||||
|
||||
if cap(buf) < int(length) {
|
||||
buf = make([]byte, length)
|
||||
} else {
|
||||
buf = buf[:length:cap(buf)]
|
||||
}
|
||||
|
||||
if _, err := io.ReadFull(r, buf[:length]); err != nil {
|
||||
return 0, nil, xerrors.Errorf("read full error: %w", err)
|
||||
}
|
||||
|
||||
return tag, buf[:length], nil
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package codec_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy/codec"
|
||||
)
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
tag codec.Tag
|
||||
data []byte
|
||||
}{
|
||||
{
|
||||
name: "empty data",
|
||||
tag: codec.TagV1,
|
||||
data: []byte{},
|
||||
},
|
||||
{
|
||||
name: "simple data",
|
||||
tag: codec.TagV1,
|
||||
data: []byte("hello world"),
|
||||
},
|
||||
{
|
||||
name: "binary data",
|
||||
tag: codec.TagV1,
|
||||
data: []byte{0x00, 0x01, 0x02, 0xff, 0xfe},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
err := codec.WriteFrame(&buf, tt.tag, tt.data)
|
||||
require.NoError(t, err)
|
||||
|
||||
readBuf := make([]byte, codec.MaxMessageSizeV1)
|
||||
tag, data, err := codec.ReadFrame(&buf, readBuf)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.tag, tag)
|
||||
require.Equal(t, tt.data, data)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFrameTooLarge(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Hand construct a header that indicates the message size exceeds the maximum
|
||||
// message size for codec.TagV1 by one. We just write the header to buf because
|
||||
// we expect codec.ReadFrame to bail out when reading the invalid length.
|
||||
header := uint32(codec.TagV1)<<codec.DataLength | (codec.MaxMessageSizeV1 + 1)
|
||||
data := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(data, header)
|
||||
|
||||
var buf bytes.Buffer
|
||||
_, err := buf.Write(data)
|
||||
require.NoError(t, err)
|
||||
|
||||
readBuf := make([]byte, 1)
|
||||
_, _, err = codec.ReadFrame(&buf, readBuf)
|
||||
require.ErrorIs(t, err, codec.ErrMessageTooLarge)
|
||||
}
|
||||
|
||||
func TestReadFrameEmptyReader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
readBuf := make([]byte, codec.MaxMessageSizeV1)
|
||||
_, _, err := codec.ReadFrame(&buf, readBuf)
|
||||
require.ErrorIs(t, err, io.EOF)
|
||||
}
|
||||
|
||||
func TestReadFrameInvalidTag(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Hand construct a header that indicates a tag we don't know about. We just
|
||||
// write the header to buf because we expect codec.ReadFrame to bail out when
|
||||
// reading the invalid tag.
|
||||
const (
|
||||
dataLength uint32 = 10
|
||||
bogusTag uint32 = 2
|
||||
)
|
||||
header := bogusTag<<codec.DataLength | dataLength
|
||||
data := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(data, header)
|
||||
|
||||
var buf bytes.Buffer
|
||||
_, err := buf.Write(data)
|
||||
require.NoError(t, err)
|
||||
|
||||
readBuf := make([]byte, 1)
|
||||
_, _, err = codec.ReadFrame(&buf, readBuf)
|
||||
require.ErrorIs(t, err, codec.ErrUnsupportedTag)
|
||||
}
|
||||
|
||||
func TestReadFrameAllocatesWhenNeeded(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
data := []byte("this message is longer than the buffer")
|
||||
err := codec.WriteFrame(&buf, codec.TagV1, data)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Buffer with insufficient capacity triggers allocation.
|
||||
readBuf := make([]byte, 4)
|
||||
tag, got, err := codec.ReadFrame(&buf, readBuf)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, codec.TagV1, tag)
|
||||
require.Equal(t, data, got)
|
||||
}
|
||||
|
||||
func TestWriteFrameDataSize(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
data := make([]byte, codec.MaxMessageSizeV1)
|
||||
err := codec.WriteFrame(&buf, codec.TagV1, data)
|
||||
require.NoError(t, err)
|
||||
|
||||
//nolint: makezero // This intentionally increases the slice length.
|
||||
data = append(data, 0) // One byte over the maximum
|
||||
err = codec.WriteFrame(&buf, codec.TagV1, data)
|
||||
require.ErrorIs(t, err, codec.ErrMessageTooLarge)
|
||||
}
|
||||
|
||||
func TestWriteFrameInvalidTag(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var buf bytes.Buffer
|
||||
data := make([]byte, 1)
|
||||
const bogusTag = 2
|
||||
err := codec.WriteFrame(&buf, codec.Tag(bogusTag), data)
|
||||
require.ErrorIs(t, err, codec.ErrUnsupportedTag)
|
||||
}
|
||||
@@ -2,6 +2,204 @@
|
||||
// audit logs and forwards them to coderd via the agent API.
|
||||
package boundarylogproxy
|
||||
|
||||
// Server a placeholder for the server that will listen on a Unix socket for
|
||||
// boundary logs to be forwarded.
|
||||
type Server struct{}
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy/codec"
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
// logBufferSize is the size of the channel buffer for incoming log requests
|
||||
// from workspaces. This buffer size is intended to handle short bursts of workspaces
|
||||
// forwarding batches of logs in parallel.
|
||||
logBufferSize = 100
|
||||
)
|
||||
|
||||
// DefaultSocketPath returns the default path for the boundary audit log socket.
|
||||
func DefaultSocketPath() string {
|
||||
return filepath.Join(os.TempDir(), "boundary-audit.sock")
|
||||
}
|
||||
|
||||
// Reporter reports boundary logs from workspaces.
|
||||
type Reporter interface {
|
||||
ReportBoundaryLogs(ctx context.Context, req *agentproto.ReportBoundaryLogsRequest) (*agentproto.ReportBoundaryLogsResponse, error)
|
||||
}
|
||||
|
||||
// Server listens on a Unix socket for boundary log messages and buffers them
|
||||
// for forwarding to coderd. The socket server and the forwarder are decoupled:
|
||||
// - Start() creates the socket and accepts a connection from boundary
|
||||
// - RunForwarder() drains the buffer and sends logs to coderd via AgentAPI
|
||||
type Server struct {
|
||||
logger slog.Logger
|
||||
socketPath string
|
||||
|
||||
listener net.Listener
|
||||
cancel context.CancelFunc
|
||||
wg sync.WaitGroup
|
||||
|
||||
// logs buffers incoming log requests for the forwarder to drain.
|
||||
logs chan *agentproto.ReportBoundaryLogsRequest
|
||||
}
|
||||
|
||||
// NewServer creates a new boundary log proxy server.
|
||||
func NewServer(logger slog.Logger, socketPath string) *Server {
|
||||
return &Server{
|
||||
logger: logger.Named("boundary-log-proxy"),
|
||||
socketPath: socketPath,
|
||||
logs: make(chan *agentproto.ReportBoundaryLogsRequest, logBufferSize),
|
||||
}
|
||||
}
|
||||
|
||||
// Start begins listening for connections on the Unix socket, and handles new
|
||||
// connections in a separate goroutine. Incoming logs from connections are
|
||||
// buffered until RunForwarder drains them.
|
||||
func (s *Server) Start() error {
|
||||
if err := os.Remove(s.socketPath); err != nil && !os.IsNotExist(err) {
|
||||
return xerrors.Errorf("remove existing socket: %w", err)
|
||||
}
|
||||
|
||||
listener, err := net.Listen("unix", s.socketPath)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("listen on socket: %w", err)
|
||||
}
|
||||
|
||||
s.listener = listener
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
s.cancel = cancel
|
||||
|
||||
s.wg.Add(1)
|
||||
go s.acceptLoop(ctx)
|
||||
|
||||
s.logger.Info(ctx, "boundary log proxy started", slog.F("socket_path", s.socketPath))
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunForwarder drains the log buffer and forwards logs to coderd.
|
||||
// It blocks until ctx is canceled.
|
||||
func (s *Server) RunForwarder(ctx context.Context, sender Reporter) error {
|
||||
s.logger.Debug(ctx, "boundary log forwarder started")
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case req := <-s.logs:
|
||||
_, err := sender.ReportBoundaryLogs(ctx, req)
|
||||
if err != nil {
|
||||
s.logger.Warn(ctx, "failed to forward boundary logs",
|
||||
slog.Error(err),
|
||||
slog.F("log_count", len(req.Logs)))
|
||||
// Continue forwarding other logs. The current batch is lost,
|
||||
// but the socket stays alive.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) acceptLoop(ctx context.Context) {
|
||||
defer s.wg.Done()
|
||||
|
||||
for {
|
||||
conn, err := s.listener.Accept()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
s.logger.Warn(ctx, "accept loop terminated", slog.Error(ctx.Err()))
|
||||
return
|
||||
}
|
||||
s.logger.Warn(ctx, "socket accept error", slog.Error(err))
|
||||
continue
|
||||
}
|
||||
|
||||
s.wg.Add(1)
|
||||
go s.handleConnection(ctx, conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleConnection(ctx context.Context, conn net.Conn) {
|
||||
defer s.wg.Done()
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
s.wg.Add(1)
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
<-ctx.Done()
|
||||
_ = conn.Close()
|
||||
}()
|
||||
|
||||
// This is intended to be a sane starting point for the read buffer size. It may be
|
||||
// grown by codec.ReadFrame if necessary.
|
||||
const initBufSize = 1 << 10
|
||||
buf := make([]byte, initBufSize)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
var (
|
||||
tag codec.Tag
|
||||
err error
|
||||
)
|
||||
tag, buf, err = codec.ReadFrame(conn, buf)
|
||||
switch {
|
||||
case errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed):
|
||||
return
|
||||
case err != nil:
|
||||
s.logger.Warn(ctx, "read frame error", slog.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
if tag != codec.TagV1 {
|
||||
s.logger.Warn(ctx, "invalid tag value", slog.F("tag", tag))
|
||||
return
|
||||
}
|
||||
|
||||
var req agentproto.ReportBoundaryLogsRequest
|
||||
if err := proto.Unmarshal(buf, &req); err != nil {
|
||||
s.logger.Warn(ctx, "proto unmarshal error", slog.Error(err))
|
||||
continue
|
||||
}
|
||||
|
||||
select {
|
||||
case s.logs <- &req:
|
||||
default:
|
||||
s.logger.Warn(ctx, "dropping boundary logs, buffer full",
|
||||
slog.F("log_count", len(req.Logs)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close stops the server and blocks until resources have been cleaned up.
|
||||
// It must be called after Start.
|
||||
func (s *Server) Close() error {
|
||||
if s.cancel != nil {
|
||||
s.cancel()
|
||||
}
|
||||
|
||||
if s.listener != nil {
|
||||
_ = s.listener.Close()
|
||||
}
|
||||
|
||||
s.wg.Wait()
|
||||
|
||||
err := os.Remove(s.socketPath)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,578 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package boundarylogproxy_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy"
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy/codec"
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
// sendMessage writes a framed protobuf message to the connection.
|
||||
func sendMessage(t *testing.T, conn net.Conn, req *agentproto.ReportBoundaryLogsRequest) {
|
||||
t.Helper()
|
||||
|
||||
data, err := proto.Marshal(req)
|
||||
if err != nil {
|
||||
//nolint:gocritic // In tests we're not worried about conn being nil.
|
||||
t.Errorf("%s marshal req: %s", conn.LocalAddr().String(), err)
|
||||
}
|
||||
|
||||
err = codec.WriteFrame(conn, codec.TagV1, data)
|
||||
if err != nil {
|
||||
//nolint:gocritic // In tests we're not worried about conn being nil.
|
||||
t.Errorf("%s write frame: %s", conn.LocalAddr().String(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeReporter implements boundarylogproxy.Reporter for testing.
|
||||
type fakeReporter struct {
|
||||
mu sync.Mutex
|
||||
logs []*agentproto.ReportBoundaryLogsRequest
|
||||
err error
|
||||
errOnce bool // only error once, then succeed
|
||||
|
||||
// reportCb is called when a ReportBoundaryLogsRequest is processed. It must not
|
||||
// block.
|
||||
reportCb func()
|
||||
}
|
||||
|
||||
func (f *fakeReporter) ReportBoundaryLogs(_ context.Context, req *agentproto.ReportBoundaryLogsRequest) (*agentproto.ReportBoundaryLogsResponse, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
if f.reportCb != nil {
|
||||
f.reportCb()
|
||||
}
|
||||
|
||||
if f.err != nil {
|
||||
if f.errOnce {
|
||||
err := f.err
|
||||
f.err = nil
|
||||
return nil, err
|
||||
}
|
||||
return nil, f.err
|
||||
}
|
||||
f.logs = append(f.logs, req)
|
||||
return &agentproto.ReportBoundaryLogsResponse{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeReporter) getLogs() []*agentproto.ReportBoundaryLogsRequest {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return append([]*agentproto.ReportBoundaryLogsRequest{}, f.logs...)
|
||||
}
|
||||
|
||||
func TestServer_StartAndClose(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify socket exists and is connectable.
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
err = conn.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = srv.Close()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestServer_ReceiveAndForwardLogs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
reporter := &fakeReporter{}
|
||||
|
||||
// Start forwarder in background.
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
// Connect and send a log message.
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
req := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: true,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "GET",
|
||||
Url: "https://example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
sendMessage(t, conn, req)
|
||||
|
||||
// Wait for the reporter to receive the log.
|
||||
require.Eventually(t, func() bool {
|
||||
logs := reporter.getLogs()
|
||||
return len(logs) == 1
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
logs := reporter.getLogs()
|
||||
require.Len(t, logs, 1)
|
||||
require.Len(t, logs[0].Logs, 1)
|
||||
require.True(t, logs[0].Logs[0].Allowed)
|
||||
require.Equal(t, "GET", logs[0].Logs[0].GetHttpRequest().Method)
|
||||
require.Equal(t, "https://example.com", logs[0].Logs[0].GetHttpRequest().Url)
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
|
||||
func TestServer_MultipleMessages(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
defer srv.Close()
|
||||
|
||||
reporter := &fakeReporter{}
|
||||
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
// Send multiple messages and verify they are all received.
|
||||
for range 5 {
|
||||
req := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: true,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "POST",
|
||||
Url: "https://example.com/api",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendMessage(t, conn, req)
|
||||
}
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
logs := reporter.getLogs()
|
||||
return len(logs) == 5
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
|
||||
func TestServer_MultipleConnections(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
reporter := &fakeReporter{}
|
||||
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
// Create multiple connections and send from each.
|
||||
const numConns = 3
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(numConns)
|
||||
for i := range numConns {
|
||||
go func(connID int) {
|
||||
defer wg.Done()
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
if err != nil {
|
||||
t.Errorf("conn %d dial: %s", connID, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
req := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: true,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "GET",
|
||||
Url: "https://example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendMessage(t, conn, req)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
logs := reporter.getLogs()
|
||||
return len(logs) == numConns
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
|
||||
func TestServer_MessageTooLarge(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
// Send a message claiming to be larger than the max message size.
|
||||
var length uint32 = codec.MaxMessageSizeV1 + 1
|
||||
err = binary.Write(conn, binary.BigEndian, length)
|
||||
require.NoError(t, err)
|
||||
|
||||
// The server should close the connection after receiving an oversized
|
||||
// message length.
|
||||
buf := make([]byte, 1)
|
||||
err = conn.SetReadDeadline(time.Now().Add(time.Second))
|
||||
require.NoError(t, err)
|
||||
_, err = conn.Read(buf)
|
||||
require.Error(t, err) // Should get EOF or closed connection.
|
||||
}
|
||||
|
||||
func TestServer_ForwarderContinuesAfterError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
reportNotify := make(chan struct{}, 1)
|
||||
reporter := &fakeReporter{
|
||||
// Simulate an error on the first call.
|
||||
err: context.DeadlineExceeded,
|
||||
errOnce: true,
|
||||
reportCb: func() {
|
||||
reportNotify <- struct{}{}
|
||||
},
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
// Send the first message to be processed and wait for failure.
|
||||
req1 := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: true,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "GET",
|
||||
Url: "https://example.com/first",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendMessage(t, conn, req1)
|
||||
|
||||
select {
|
||||
case <-reportNotify:
|
||||
case <-time.After(testutil.WaitShort):
|
||||
t.Fatal("timed out waiting for first message to be processed")
|
||||
}
|
||||
|
||||
// Send the second message, which should succeed.
|
||||
req2 := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: false,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "POST",
|
||||
Url: "https://example.com/second",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendMessage(t, conn, req2)
|
||||
|
||||
// Only the second message should be recorded.
|
||||
require.Eventually(t, func() bool {
|
||||
logs := reporter.getLogs()
|
||||
return len(logs) == 1
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
logs := reporter.getLogs()
|
||||
require.Len(t, logs, 1)
|
||||
require.Equal(t, "https://example.com/second", logs[0].Logs[0].GetHttpRequest().Url)
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
|
||||
func TestServer_CloseStopsForwarder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
reporter := &fakeReporter{}
|
||||
|
||||
forwarderCtx, forwarderCancel := context.WithCancel(context.Background())
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(forwarderCtx, reporter)
|
||||
}()
|
||||
|
||||
// Cancel the forwarder context and verify it stops.
|
||||
forwarderCancel()
|
||||
|
||||
select {
|
||||
case err := <-forwarderDone:
|
||||
require.ErrorIs(t, err, context.Canceled)
|
||||
case <-time.After(testutil.WaitShort):
|
||||
t.Fatal("forwarder did not stop")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_InvalidProtobuf(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
reporter := &fakeReporter{}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
// Send a valid header with garbage protobuf data.
|
||||
// The server should log an unmarshal error but continue processing.
|
||||
invalidProto := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
|
||||
//nolint: gosec // codec.DataLength is always less than the size of the header.
|
||||
header := (uint32(codec.TagV1) << codec.DataLength) | uint32(len(invalidProto))
|
||||
err = binary.Write(conn, binary.BigEndian, header)
|
||||
require.NoError(t, err)
|
||||
_, err = conn.Write(invalidProto)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Now send a valid message. The server should continue processing.
|
||||
req := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: true,
|
||||
Time: timestamppb.Now(),
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "GET",
|
||||
Url: "https://example.com/valid",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendMessage(t, conn, req)
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
logs := reporter.getLogs()
|
||||
return len(logs) == 1
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
|
||||
func TestServer_InvalidHeader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
reporter := &fakeReporter{}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
// sendInvalidHeader sends a header and verifies the server closes the
|
||||
// connection.
|
||||
sendInvalidHeader := func(t *testing.T, name string, header uint32) {
|
||||
t.Helper()
|
||||
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
err = binary.Write(conn, binary.BigEndian, header)
|
||||
require.NoError(t, err, name)
|
||||
|
||||
// The server closes the connection on invalid header, so the next
|
||||
// write should fail with a broken pipe error.
|
||||
require.Eventually(t, func() bool {
|
||||
_, err := conn.Write([]byte{0x00})
|
||||
return err != nil
|
||||
}, testutil.WaitShort, testutil.IntervalFast, name)
|
||||
}
|
||||
|
||||
// TagV1 with length exceeding MaxMessageSizeV1.
|
||||
sendInvalidHeader(t, "v1 too large", (uint32(codec.TagV1)<<codec.DataLength)|(codec.MaxMessageSizeV1+1))
|
||||
|
||||
// Unknown tag.
|
||||
const bogusTag = 0xFF
|
||||
sendInvalidHeader(t, "unknown tag too large", (bogusTag<<codec.DataLength)|(codec.MaxMessageSizeV1+1))
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
|
||||
func TestServer_AllowRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
socketPath := filepath.Join(testutil.TempDirUnixSocket(t), "boundary.sock")
|
||||
srv := boundarylogproxy.NewServer(testutil.Logger(t), socketPath)
|
||||
|
||||
err := srv.Start()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, srv.Close()) })
|
||||
|
||||
reporter := &fakeReporter{}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
forwarderDone := make(chan error, 1)
|
||||
go func() {
|
||||
forwarderDone <- srv.RunForwarder(ctx, reporter)
|
||||
}()
|
||||
|
||||
conn, err := net.Dial("unix", socketPath)
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
||||
// Send an allowed request with a matched rule.
|
||||
logTime := timestamppb.Now()
|
||||
req := &agentproto.ReportBoundaryLogsRequest{
|
||||
Logs: []*agentproto.BoundaryLog{
|
||||
{
|
||||
Allowed: true,
|
||||
Time: logTime,
|
||||
Resource: &agentproto.BoundaryLog_HttpRequest_{
|
||||
HttpRequest: &agentproto.BoundaryLog_HttpRequest{
|
||||
Method: "GET",
|
||||
Url: "https://malicious.com/attack",
|
||||
MatchedRule: "*.malicious.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sendMessage(t, conn, req)
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
logs := reporter.getLogs()
|
||||
return len(logs) == 1
|
||||
}, testutil.WaitShort, testutil.IntervalFast)
|
||||
|
||||
logs := reporter.getLogs()
|
||||
require.Len(t, logs, 1)
|
||||
require.True(t, logs[0].Logs[0].Allowed)
|
||||
require.Equal(t, logTime.Seconds, logs[0].Logs[0].Time.Seconds)
|
||||
require.Equal(t, logTime.Nanos, logs[0].Logs[0].Time.Nanos)
|
||||
require.Equal(t, "*.malicious.com", logs[0].Logs[0].GetHttpRequest().MatchedRule)
|
||||
|
||||
cancel()
|
||||
<-forwarderDone
|
||||
}
|
||||
+12
-2
@@ -31,6 +31,7 @@ import (
|
||||
"github.com/coder/coder/v2/agent/agentcontainers"
|
||||
"github.com/coder/coder/v2/agent/agentexec"
|
||||
"github.com/coder/coder/v2/agent/agentssh"
|
||||
"github.com/coder/coder/v2/agent/boundarylogproxy"
|
||||
"github.com/coder/coder/v2/agent/reaper"
|
||||
"github.com/coder/coder/v2/buildinfo"
|
||||
"github.com/coder/coder/v2/cli/clilog"
|
||||
@@ -59,6 +60,7 @@ func workspaceAgent() *serpent.Command {
|
||||
devcontainerDiscoveryAutostart bool
|
||||
socketServerEnabled bool
|
||||
socketPath string
|
||||
boundaryLogProxySocketPath string
|
||||
)
|
||||
agentAuth := &AgentAuth{}
|
||||
cmd := &serpent.Command{
|
||||
@@ -319,8 +321,9 @@ func workspaceAgent() *serpent.Command {
|
||||
agentcontainers.WithProjectDiscovery(devcontainerProjectDiscovery),
|
||||
agentcontainers.WithDiscoveryAutostart(devcontainerDiscoveryAutostart),
|
||||
},
|
||||
SocketPath: socketPath,
|
||||
SocketServerEnabled: socketServerEnabled,
|
||||
SocketPath: socketPath,
|
||||
SocketServerEnabled: socketServerEnabled,
|
||||
BoundaryLogProxySocketPath: boundaryLogProxySocketPath,
|
||||
})
|
||||
|
||||
if debugAddress != "" {
|
||||
@@ -494,6 +497,13 @@ func workspaceAgent() *serpent.Command {
|
||||
Description: "Specify the path for the agent socket.",
|
||||
Value: serpent.StringOf(&socketPath),
|
||||
},
|
||||
{
|
||||
Flag: "boundary-log-proxy-socket-path",
|
||||
Default: boundarylogproxy.DefaultSocketPath(),
|
||||
Env: "CODER_AGENT_BOUNDARY_LOG_PROXY_SOCKET_PATH",
|
||||
Description: "The path for the boundary log proxy server Unix socket. Boundary should write audit logs to this socket.",
|
||||
Value: serpent.StringOf(&boundaryLogProxySocketPath),
|
||||
},
|
||||
}
|
||||
agentAuth.AttachOptions(cmd, false)
|
||||
return cmd
|
||||
|
||||
+11
-1
@@ -138,6 +138,17 @@ func normalizeGoldenFile(t *testing.T, byt []byte) []byte {
|
||||
|
||||
// The home directory changes depending on the test environment.
|
||||
byt = bytes.ReplaceAll(byt, []byte(homeDir), []byte("~"))
|
||||
|
||||
// Normalize the temp directory. os.TempDir() may include a trailing slash
|
||||
// (macOS) or not (Linux/Windows), and the temp directory may be followed by
|
||||
// more filepath elements with an OS-specific separator. We handle all cases
|
||||
// by replacing tempdir+separator first, then tempdir alone.
|
||||
tempDir := filepath.Clean(os.TempDir())
|
||||
byt = bytes.ReplaceAll(byt, []byte(tempDir+string(filepath.Separator)), []byte("/tmp/"))
|
||||
byt = bytes.ReplaceAll(byt, []byte(tempDir), []byte("/tmp"))
|
||||
// Clean up trailing slash when temp dir is used standalone (e.g., "/tmp/)" -> "/tmp)").
|
||||
byt = bytes.ReplaceAll(byt, []byte("/tmp/)"), []byte("/tmp)"))
|
||||
|
||||
for _, r := range []struct {
|
||||
old string
|
||||
new string
|
||||
@@ -145,7 +156,6 @@ func normalizeGoldenFile(t *testing.T, byt []byte) []byte {
|
||||
{"\r\n", "\n"},
|
||||
{`~\.cache\coder`, "~/.cache/coder"},
|
||||
{`C:\Users\RUNNER~1\AppData\Local\Temp`, "/tmp"},
|
||||
{os.TempDir(), "/tmp"},
|
||||
} {
|
||||
byt = bytes.ReplaceAll(byt, []byte(r.old), []byte(r.new))
|
||||
}
|
||||
|
||||
+4
@@ -39,6 +39,10 @@ OPTIONS:
|
||||
--block-file-transfer bool, $CODER_AGENT_BLOCK_FILE_TRANSFER (default: false)
|
||||
Block file transfer using known applications: nc,rsync,scp,sftp.
|
||||
|
||||
--boundary-log-proxy-socket-path string, $CODER_AGENT_BOUNDARY_LOG_PROXY_SOCKET_PATH (default: /tmp/boundary-audit.sock)
|
||||
The path for the boundary log proxy server Unix socket. Boundary
|
||||
should write audit logs to this socket.
|
||||
|
||||
--debug-address string, $CODER_AGENT_DEBUG_ADDRESS (default: 127.0.0.1:2113)
|
||||
The bind address to serve a debug HTTP server.
|
||||
|
||||
|
||||
@@ -220,7 +220,10 @@ func New(opts Options, workspace database.Workspace) *API {
|
||||
Database: opts.Database,
|
||||
}
|
||||
|
||||
api.BoundaryLogsAPI = &BoundaryLogsAPI{}
|
||||
api.BoundaryLogsAPI = &BoundaryLogsAPI{
|
||||
Log: opts.Log,
|
||||
WorkspaceID: opts.WorkspaceID,
|
||||
}
|
||||
|
||||
// Start background cache refresh loop to handle workspace changes
|
||||
// like prebuild claims where owner_id and other fields may be modified in the DB.
|
||||
|
||||
@@ -2,14 +2,60 @@ package agentapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"cdr.dev/slog"
|
||||
|
||||
agentproto "github.com/coder/coder/v2/agent/proto"
|
||||
)
|
||||
|
||||
type BoundaryLogsAPI struct{}
|
||||
type BoundaryLogsAPI struct {
|
||||
Log slog.Logger
|
||||
WorkspaceID uuid.UUID
|
||||
}
|
||||
|
||||
func (*BoundaryLogsAPI) ReportBoundaryLogs(context.Context, *agentproto.ReportBoundaryLogsRequest) (*agentproto.ReportBoundaryLogsResponse, error) {
|
||||
return nil, xerrors.New("not implemented")
|
||||
func (a *BoundaryLogsAPI) ReportBoundaryLogs(ctx context.Context, req *agentproto.ReportBoundaryLogsRequest) (*agentproto.ReportBoundaryLogsResponse, error) {
|
||||
for _, l := range req.Logs {
|
||||
var logTime time.Time
|
||||
if l.Time != nil {
|
||||
logTime = l.Time.AsTime()
|
||||
}
|
||||
|
||||
switch r := l.Resource.(type) {
|
||||
case *agentproto.BoundaryLog_HttpRequest_:
|
||||
if r.HttpRequest == nil {
|
||||
a.Log.Warn(ctx, "empty http request resource",
|
||||
slog.F("workspace_id", a.WorkspaceID.String()))
|
||||
continue
|
||||
}
|
||||
|
||||
fields := []slog.Field{
|
||||
slog.F("decision", allowBoolToString(l.Allowed)),
|
||||
slog.F("workspace_id", a.WorkspaceID.String()),
|
||||
slog.F("http_method", r.HttpRequest.Method),
|
||||
slog.F("http_url", r.HttpRequest.Url),
|
||||
slog.F("event_time", logTime.Format(time.RFC3339Nano)),
|
||||
}
|
||||
if l.Allowed {
|
||||
fields = append(fields, slog.F("matched_rule", r.HttpRequest.MatchedRule))
|
||||
}
|
||||
|
||||
a.Log.With(fields...).Info(ctx, "boundary_request")
|
||||
default:
|
||||
a.Log.Warn(ctx, "unknown resource type",
|
||||
slog.F("workspace_id", a.WorkspaceID.String()))
|
||||
}
|
||||
}
|
||||
|
||||
return &agentproto.ReportBoundaryLogsResponse{}, nil
|
||||
}
|
||||
|
||||
//nolint:revive // This stringifies the boolean argument.
|
||||
func allowBoolToString(b bool) string {
|
||||
if b {
|
||||
return "allow"
|
||||
}
|
||||
return "deny"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user