Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion common/pkg/hooks/exec/runtimeconfigfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"time"

Expand All @@ -21,6 +23,11 @@ var spewConfig = spew.ConfigState{
SortKeys: true,
}

const (
AnnotationHookStdout = "run.oci.hooks.stdout"
AnnotationHookStderr = "run.oci.hooks.stderr"
)

type RuntimeConfigFilterOptions struct {
// The hooks to run
Hooks []spec.Hook
Expand Down Expand Up @@ -55,9 +62,41 @@ func RuntimeConfigFilterWithOptions(ctx context.Context, options RuntimeConfigFi
if err != nil {
return nil, err
}
var stdoutFile, stderrFile *os.File

if options.Config != nil && options.Config.Annotations != nil {
if stdoutPath, ok := options.Config.Annotations[AnnotationHookStdout]; ok {
f, openErr := os.OpenFile(stdoutPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o700)
if openErr != nil {
return nil, fmt.Errorf("opening stdout file for config-filter hook: %w", openErr)
}
stdoutFile = f
defer stdoutFile.Close()
}

if stderrPath, ok := options.Config.Annotations[AnnotationHookStderr]; ok {
f, openErr := os.OpenFile(stderrPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o700)
if openErr != nil {
return nil, fmt.Errorf("opening stderr file for config-filter hook: %w", openErr)
}
stderrFile = f
defer stderrFile.Close()
}
}
for i, hook := range options.Hooks {
var stdout bytes.Buffer
hookErr, err = RunWithOptions(ctx, RunOptions{Hook: &hook, Dir: options.Dir, State: data, Stdout: &stdout, PostKillTimeout: options.PostKillTimeout})
var runStdout io.Writer = &stdout
var runStderr io.Writer

if stdoutFile != nil {
runStdout = io.MultiWriter(&stdout, stdoutFile)
}

if stderrFile != nil {
runStderr = stderrFile
}

hookErr, err = RunWithOptions(ctx, RunOptions{Hook: &hook, Dir: options.Dir, State: data, Stdout: runStdout, Stderr: runStderr, PostKillTimeout: options.PostKillTimeout})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I am missing something Stderr: stderrFile should work here without having to decalre or do a nil check above. If stderrFile is nil then we can just pass it as nil

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried this and it being a nil os.File breaks it. passing it as a nil causes os/exec to close the stderr fd instead of redirecting to /dev/null. added a regression test and it fails without the check

if err != nil {
return hookErr, err
}
Expand Down
119 changes: 119 additions & 0 deletions common/pkg/hooks/exec/runtimeconfigfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"time"

spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRuntimeConfigFilter(t *testing.T) {
Expand Down Expand Up @@ -263,3 +265,120 @@ func TestRuntimeConfigFilter(t *testing.T) {
})
}
}

func TestRuntimeConfigFilterOutputRedirection(t *testing.T) {
for _, tt := range []struct {
name string
hookScript string
useStdoutAnnotation bool
stdoutPathOverride string
useStderrAnnotation bool
preExistingStdout string
checkStdoutMode bool
expectedStderr string
expectedErr string
}{
{
name: "no stderr annotation still allows hook to write to stderr",
hookScript: "cat; echo -n stderr-content 1>&2",
},
{
name: "stdout annotation redirects output and preserves round-trip",
hookScript: "cat",
useStdoutAnnotation: true,
},
{
name: "created stdout file uses 0700 permissions, matching crun",
hookScript: "cat",
useStdoutAnnotation: true,
checkStdoutMode: true,
},
{
name: "stderr annotation redirects stderr only",
hookScript: "echo -n stderr-content 1>&2; cat",
useStderrAnnotation: true,
expectedStderr: "stderr-content",
},
{
name: "both annotations set redirect independently",
hookScript: "echo -n stderr-content 1>&2; cat",
useStdoutAnnotation: true,
useStderrAnnotation: true,
expectedStderr: "stderr-content",
},
{
name: "existing file content is preserved in append mode",
hookScript: "cat",
useStdoutAnnotation: true,
preExistingStdout: "existing-log-line\n",
},
{
name: "invalid stdout path returns an error",
hookScript: "cat",
useStdoutAnnotation: true,
stdoutPathOverride: "/no/such/directory/stdout.log",
expectedErr: "opening stdout file",
},
} {
test := tt
t.Run(test.name, func(t *testing.T) {
dir := t.TempDir()
stdoutPath := filepath.Join(dir, "stdout.log")
if test.stdoutPathOverride != "" {
stdoutPath = test.stdoutPathOverride
}
stderrPath := filepath.Join(dir, "stderr.log")

if test.preExistingStdout != "" {
require.NoError(t, os.WriteFile(stdoutPath, []byte(test.preExistingStdout), 0o644))
}

annotations := map[string]string{}
if test.useStdoutAnnotation {
annotations[AnnotationHookStdout] = stdoutPath
}
if test.useStderrAnnotation {
annotations[AnnotationHookStderr] = stderrPath
}

input := &spec.Spec{
Version: "1.0.0",
Root: &spec.Root{Path: "rootfs"},
Annotations: annotations,
}

hooks := []spec.Hook{{Path: path, Args: []string{"sh", "-c", test.hookScript}}}

if test.expectedErr != "" {
_, err := RuntimeConfigFilterWithOptions(t.Context(), RuntimeConfigFilterOptions{Hooks: hooks, Config: input, PostKillTimeout: DefaultPostKillTimeout})
assert.ErrorContains(t, err, test.expectedErr)
return
}

expectedJSON, err := json.Marshal(input)
require.NoError(t, err)

hookErr, err := RuntimeConfigFilterWithOptions(t.Context(), RuntimeConfigFilterOptions{Hooks: hooks, Config: input, PostKillTimeout: DefaultPostKillTimeout})
require.NoError(t, err)
require.NoError(t, hookErr)

if test.useStdoutAnnotation {
contents, err := os.ReadFile(stdoutPath)
require.NoError(t, err)
assert.Equal(t, test.preExistingStdout+string(expectedJSON), string(contents))

if test.checkStdoutMode {
info, err := os.Stat(stdoutPath)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0o700), info.Mode().Perm())
}
}

if test.expectedStderr != "" {
contents, err := os.ReadFile(stderrPath)
require.NoError(t, err)
assert.Equal(t, test.expectedStderr, string(contents))
}
})
}
}
Loading