mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
On a default install Pulse can see that a container failed and not why. The line that names the cause is in the log, and a default agent reads no logs. On the fault set, the restart loop from a bad DATABASE_URL stayed "Handed back" on a default agent through every replay for that reason. An operator can now turn on --enable-failure-logs (or PULSE_ENABLE_FAILURE_LOGS) on a host. The agent then reports the last lines of a container whose own state shows a failure: a non-zero exit, an out-of-memory kill, a restart after either, or a failing health check. It never reads a container that stopped cleanly, runs healthy or was never started, and with the switch off it never asks the runtime for logs at all. Nothing the server sends can turn the switch on: this is a local boundary like custom sensors, not a read-only command mode, and the server never chooses a container, a path or a line count. What leaves the host is at most 30 lines and 4 KB, read once per failure, with terminal codes and control characters removed and then credential-shaped values masked. Cleaning comes before masking because a secret split by a control character matches no rule until the character is gone. The server cleans, masks and bounds the lines again on receipt and accepts only the four known reasons, so it does not depend on the agent having done any of it. The masking rules moved from the AI safety package to pkg/secretredact, with no change to them, so the agent and the server apply one set. The lines show in the container's details as "Last log lines", in view on the overview, as plain text. Patrol and the Assistant get them on the single-container payload with a fixed note that they are untrusted text and never instructions. Masking is pattern-based and is not a guarantee. The lines are readable by anyone who can read the container, go to the AI provider when Patrol reads it, and can be quoted by Patrol in a finding. The docs say so. A read-only review before this was pushed found that masking ran before cleaning, that a restarted healthy container qualified for a read, that the stream reader trusted a frame's declared length and kept the oldest text, and that stale lines could be served for a new failure. All are fixed here with tests. It also found that the first docs promised the lines never reach a notification, which a quoted line in a finding can. The promise is corrected.
256 lines
9.9 KiB
Go
256 lines
9.9 KiB
Go
package dockeragent
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
|
|
containertypes "github.com/moby/moby/api/types/container"
|
|
"github.com/moby/moby/api/types/image"
|
|
"github.com/moby/moby/api/types/network"
|
|
swarmtypes "github.com/moby/moby/api/types/swarm"
|
|
systemtypes "github.com/moby/moby/api/types/system"
|
|
"github.com/moby/moby/api/types/volume"
|
|
"github.com/moby/moby/client"
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
|
)
|
|
|
|
type fakeDockerClient struct {
|
|
daemonHost string
|
|
infoFunc func(ctx context.Context) (systemtypes.Info, error)
|
|
containerListFunc func(ctx context.Context, opts dockerContainerListOptions) ([]containertypes.Summary, error)
|
|
containerInspectWithRawFn func(ctx context.Context, id string, size bool) (containertypes.InspectResponse, []byte, error)
|
|
containerStatsOneShotFn func(ctx context.Context, id string) (dockerStatsResponseReader, error)
|
|
containerInspectFn func(ctx context.Context, id string) (containertypes.InspectResponse, error)
|
|
containerLogTailFn func(ctx context.Context, id string, lines int) (io.ReadCloser, error)
|
|
imagePullFn func(ctx context.Context, ref string, opts dockerImagePullOptions) (io.ReadCloser, error)
|
|
containerStopFn func(ctx context.Context, id string, opts dockerContainerStopOptions) error
|
|
containerRestartFn func(ctx context.Context, id string, opts dockerContainerRestartOptions) error
|
|
containerRenameFn func(ctx context.Context, id, newName string) error
|
|
containerCreateFn func(ctx context.Context, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (containertypes.CreateResponse, error)
|
|
networkConnectFn func(ctx context.Context, netName, containerID string, endpoint *network.EndpointSettings) error
|
|
containerStartFn func(ctx context.Context, id string, opts dockerContainerStartOptions) error
|
|
containerRemoveFn func(ctx context.Context, id string, opts dockerContainerRemoveOptions) error
|
|
imageListFn func(ctx context.Context, opts dockerImageListOptions) ([]image.Summary, error)
|
|
volumeListFn func(ctx context.Context, opts dockerVolumeListOptions) ([]volume.Volume, error)
|
|
networkListFn func(ctx context.Context, opts dockerNetworkListOptions) ([]network.Summary, error)
|
|
diskUsageFn func(ctx context.Context, opts dockerDiskUsageOptions) (client.DiskUsageResult, error)
|
|
serviceListFn func(ctx context.Context, opts dockerServiceListOptions) ([]swarmtypes.Service, error)
|
|
taskListFn func(ctx context.Context, opts dockerTaskListOptions) ([]swarmtypes.Task, error)
|
|
nodeListFn func(ctx context.Context, opts dockerNodeListOptions) ([]swarmtypes.Node, error)
|
|
secretListFn func(ctx context.Context, opts dockerSecretListOptions) ([]swarmtypes.Secret, error)
|
|
configListFn func(ctx context.Context, opts dockerConfigListOptions) ([]swarmtypes.Config, error)
|
|
imageInspectWithRawFn func(ctx context.Context, imageID string) (image.InspectResponse, []byte, error)
|
|
closeFn func() error
|
|
}
|
|
|
|
func (f *fakeDockerClient) Info(ctx context.Context) (systemtypes.Info, error) {
|
|
if f.infoFunc == nil {
|
|
return systemtypes.Info{}, errors.New("unexpected Info call")
|
|
}
|
|
return f.infoFunc(ctx)
|
|
}
|
|
|
|
func (f *fakeDockerClient) DaemonHost() string {
|
|
return f.daemonHost
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerList(ctx context.Context, opts dockerContainerListOptions) ([]containertypes.Summary, error) {
|
|
if f.containerListFunc == nil {
|
|
return nil, errors.New("unexpected ContainerList call")
|
|
}
|
|
return f.containerListFunc(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerInspectWithRaw(ctx context.Context, id string, size bool) (containertypes.InspectResponse, []byte, error) {
|
|
if f.containerInspectWithRawFn == nil {
|
|
return containertypes.InspectResponse{}, nil, errors.New("unexpected ContainerInspectWithRaw call")
|
|
}
|
|
return f.containerInspectWithRawFn(ctx, id, size)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerStatsOneShot(ctx context.Context, id string) (dockerStatsResponseReader, error) {
|
|
if f.containerStatsOneShotFn == nil {
|
|
return dockerStatsResponseReader{}, errors.New("unexpected ContainerStatsOneShot call")
|
|
}
|
|
return f.containerStatsOneShotFn(ctx, id)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerLogTail(ctx context.Context, id string, lines int) (io.ReadCloser, error) {
|
|
if f.containerLogTailFn == nil {
|
|
return nil, errors.New("unexpected ContainerLogTail call")
|
|
}
|
|
return f.containerLogTailFn(ctx, id, lines)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerInspect(ctx context.Context, id string) (containertypes.InspectResponse, error) {
|
|
if f.containerInspectFn == nil {
|
|
return containertypes.InspectResponse{}, errors.New("unexpected ContainerInspect call")
|
|
}
|
|
return f.containerInspectFn(ctx, id)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ImagePull(ctx context.Context, ref string, opts dockerImagePullOptions) (io.ReadCloser, error) {
|
|
if f.imagePullFn == nil {
|
|
return nil, errors.New("unexpected ImagePull call")
|
|
}
|
|
return f.imagePullFn(ctx, ref, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerStop(ctx context.Context, id string, opts dockerContainerStopOptions) error {
|
|
if f.containerStopFn == nil {
|
|
return errors.New("unexpected ContainerStop call")
|
|
}
|
|
return f.containerStopFn(ctx, id, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerRestart(ctx context.Context, id string, opts dockerContainerRestartOptions) error {
|
|
if f.containerRestartFn == nil {
|
|
return errors.New("unexpected ContainerRestart call")
|
|
}
|
|
return f.containerRestartFn(ctx, id, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerRename(ctx context.Context, id, newName string) error {
|
|
if f.containerRenameFn == nil {
|
|
return errors.New("unexpected ContainerRename call")
|
|
}
|
|
return f.containerRenameFn(ctx, id, newName)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerCreate(ctx context.Context, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (containertypes.CreateResponse, error) {
|
|
if f.containerCreateFn == nil {
|
|
return containertypes.CreateResponse{}, errors.New("unexpected ContainerCreate call")
|
|
}
|
|
return f.containerCreateFn(ctx, config, hostConfig, networkingConfig, platform, containerName)
|
|
}
|
|
|
|
func (f *fakeDockerClient) NetworkConnect(ctx context.Context, netName, containerID string, endpoint *network.EndpointSettings) error {
|
|
if f.networkConnectFn == nil {
|
|
return errors.New("unexpected NetworkConnect call")
|
|
}
|
|
return f.networkConnectFn(ctx, netName, containerID, endpoint)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerStart(ctx context.Context, id string, opts dockerContainerStartOptions) error {
|
|
if f.containerStartFn == nil {
|
|
return errors.New("unexpected ContainerStart call")
|
|
}
|
|
return f.containerStartFn(ctx, id, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ContainerRemove(ctx context.Context, id string, opts dockerContainerRemoveOptions) error {
|
|
if f.containerRemoveFn == nil {
|
|
return errors.New("unexpected ContainerRemove call")
|
|
}
|
|
return f.containerRemoveFn(ctx, id, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ImageList(ctx context.Context, opts dockerImageListOptions) ([]image.Summary, error) {
|
|
if f.imageListFn == nil {
|
|
return nil, nil
|
|
}
|
|
return f.imageListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) VolumeList(ctx context.Context, opts dockerVolumeListOptions) ([]volume.Volume, error) {
|
|
if f.volumeListFn == nil {
|
|
return nil, nil
|
|
}
|
|
return f.volumeListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) NetworkList(ctx context.Context, opts dockerNetworkListOptions) ([]network.Summary, error) {
|
|
if f.networkListFn == nil {
|
|
return nil, nil
|
|
}
|
|
return f.networkListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) DiskUsage(ctx context.Context, opts dockerDiskUsageOptions) (client.DiskUsageResult, error) {
|
|
if f.diskUsageFn == nil {
|
|
return client.DiskUsageResult{}, nil
|
|
}
|
|
return f.diskUsageFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ServiceList(ctx context.Context, opts dockerServiceListOptions) ([]swarmtypes.Service, error) {
|
|
if f.serviceListFn == nil {
|
|
return nil, errors.New("unexpected ServiceList call")
|
|
}
|
|
return f.serviceListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) TaskList(ctx context.Context, opts dockerTaskListOptions) ([]swarmtypes.Task, error) {
|
|
if f.taskListFn == nil {
|
|
return nil, errors.New("unexpected TaskList call")
|
|
}
|
|
return f.taskListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) NodeList(ctx context.Context, opts dockerNodeListOptions) ([]swarmtypes.Node, error) {
|
|
if f.nodeListFn == nil {
|
|
return nil, nil
|
|
}
|
|
return f.nodeListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) SecretList(ctx context.Context, opts dockerSecretListOptions) ([]swarmtypes.Secret, error) {
|
|
if f.secretListFn == nil {
|
|
return nil, nil
|
|
}
|
|
return f.secretListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ConfigList(ctx context.Context, opts dockerConfigListOptions) ([]swarmtypes.Config, error) {
|
|
if f.configListFn == nil {
|
|
return nil, nil
|
|
}
|
|
return f.configListFn(ctx, opts)
|
|
}
|
|
|
|
func (f *fakeDockerClient) ImageInspectWithRaw(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
|
|
if f.imageInspectWithRawFn == nil {
|
|
// Return empty response with no RepoDigests by default (simulates locally built image)
|
|
return image.InspectResponse{}, nil, nil
|
|
}
|
|
return f.imageInspectWithRawFn(ctx, imageID)
|
|
}
|
|
|
|
func (f *fakeDockerClient) Close() error {
|
|
if f.closeFn == nil {
|
|
return nil
|
|
}
|
|
return f.closeFn()
|
|
}
|
|
|
|
func statsReader(t *testing.T, stats containertypes.StatsResponse) dockerStatsResponseReader {
|
|
t.Helper()
|
|
|
|
payload, err := json.Marshal(stats)
|
|
if err != nil {
|
|
t.Fatalf("marshal stats: %v", err)
|
|
}
|
|
|
|
return dockerStatsResponseReader{
|
|
Body: io.NopCloser(bytes.NewReader(payload)),
|
|
}
|
|
}
|
|
|
|
func immediateTimer(time.Duration) *time.Timer {
|
|
return time.NewTimer(0)
|
|
}
|
|
|
|
func swap[T any](t *testing.T, target *T, value T) {
|
|
t.Helper()
|
|
prev := *target
|
|
*target = value
|
|
t.Cleanup(func() {
|
|
*target = prev
|
|
})
|
|
}
|