Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pkg/driverbuilder): fixed docker container output when multiplexed. #305

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 47 additions & 12 deletions pkg/driverbuilder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"log"
"log/slog"
"os"
"runtime"
"strconv"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
Expand All @@ -37,7 +30,14 @@ import (
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"github.com/falcosecurity/driverkit/pkg/signals"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"io"
"io/ioutil"
"k8s.io/apimachinery/pkg/util/uuid"
"log"
"log/slog"
"os"
"runtime"
"strconv"
)

// DockerBuildProcessorName is a constant containing the docker name.
Expand Down Expand Up @@ -276,13 +276,21 @@ func (bp *DockerBuildProcessor) Start(b *builder.Build) error {
return err
}

hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{})
hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{Tty: false})
if err != nil {
return err
}
defer hr.Close()

forwardLogs(hr.Reader)
isMultiplexed := false
if val, ok := hr.MediaType(); ok {
isMultiplexed = val == "application/vnd.docker.multiplexed-stream"
}
if isMultiplexed {
multiplexedForwardLogs(hr.Reader)
} else {
forwardLogs(hr.Reader)
}

if len(b.ModuleFilePath) > 0 {
if err := copyFromContainer(ctx, cli, cdata.ID, builder.ModuleFullPath, b.ModuleFilePath); err != nil {
Expand Down Expand Up @@ -360,11 +368,38 @@ func forwardLogs(logPipe io.Reader) {
slog.Debug(string(line))
}
if err == io.EOF {
slog.With("err", err.Error()).Debug("log pipe close")
return
break
}
if err != nil {
slog.With("err", err.Error()).Error("log pipe error")
}
}
slog.Debug("log pipe close")
}

// When docker container attach is called on a non-tty terminal,
// docker SDK uses a custom multiplexing protocol allowing STDOUT and STDERR string to be sent to a single stream.
// Protocol:
// > The format of the multiplexed stream is as follows:
// > [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
// see cli.ContainerAttach() method for more info.
func multiplexedForwardLogs(logPipe io.Reader) {
hdr := make([]byte, 8)
for {
_, err := logPipe.Read(hdr)
if err == io.EOF {
break
}
if err != nil {
slog.With("err", err.Error()).Error("log pipe error")
}
count := binary.BigEndian.Uint32(hdr[4:])
dat := make([]byte, count)
_, err = logPipe.Read(dat)
if err != nil {
slog.With("err", err.Error()).Error("log pipe error")
}
slog.Debug(string(dat))
}
slog.Debug("log pipe close")
}