Skip to content
Merged
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
30 changes: 27 additions & 3 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,15 @@
GH_USERNAME: ${{ github.repository_owner }}
GH_ACCESS_TOKEN: ${{ github.token }}
GH_CREDENTIAL_USERNAME: x-access-token
run: '"$CI_TOOL" e2e run --label "${{ matrix.name }}" --runner "${{ matrix.runner }}" --e2e-dir ./e2e'
run: |
export DEVSY_E2E_SENTINEL_FILE="${RUNNER_TEMP}/e2e-sentinels.jsonl"
(umask 077; : > "$DEVSY_E2E_SENTINEL_FILE")
set +e
"$CI_TOOL" e2e run --label "${{ matrix.name }}" --runner "${{ matrix.runner }}" --e2e-dir ./e2e 2>&1 | tee "${RUNNER_TEMP}/e2e-output.log"

Check warning on line 781 in .github/workflows/pr-ci.yml

View check run for this annotation

codefactor.io / CodeFactor

template-injection

Code injection via template expansion: may expand into attacker-controllable code.

Check warning on line 781 in .github/workflows/pr-ci.yml

View check run for this annotation

codefactor.io / CodeFactor

template-injection

Code injection via template expansion: may expand into attacker-controllable code.
status=("${PIPESTATUS[@]}")
"$CI_TOOL" e2e sentinel-check "$DEVSY_E2E_SENTINEL_FILE" "${RUNNER_TEMP}/e2e-output.log"
check=$?
(( status[0] == 0 && status[1] == 0 && check == 0 ))

prepare-microsandbox-provider:
name: Prepare MicroSandbox provider assets
Expand Down Expand Up @@ -926,7 +934,15 @@
TEST_TIMEOUT: ${{ matrix.test_timeout }}
DEVSY_REQUIRE_MICROSANDBOX: "true"
DEVSY_E2E_PROVIDER_DOWNLOAD_CACHE: ${{ runner.temp }}/microsandbox-provider/cache
run: '"${RUNNER_TEMP}/devsy-ci-microsandbox" run --label "${{ matrix.name }}" --expected-count "${{ matrix.expected_spec_count }}" --provider-mode "${{ matrix.provider_mode }}"'
run: |
export DEVSY_E2E_SENTINEL_FILE="${RUNNER_TEMP}/e2e-sentinels.jsonl"
(umask 077; : > "$DEVSY_E2E_SENTINEL_FILE")
set +e
"${RUNNER_TEMP}/devsy-ci-microsandbox" run --label "${{ matrix.name }}" --expected-count "${{ matrix.expected_spec_count }}" --provider-mode "${{ matrix.provider_mode }}" 2>&1 | tee "${RUNNER_TEMP}/e2e-output.log"

Check warning on line 941 in .github/workflows/pr-ci.yml

View check run for this annotation

codefactor.io / CodeFactor

template-injection

Code injection via template expansion: may expand into attacker-controllable code.

Check warning on line 941 in .github/workflows/pr-ci.yml

View check run for this annotation

codefactor.io / CodeFactor

template-injection

Code injection via template expansion: may expand into attacker-controllable code.
status=("${PIPESTATUS[@]}")
"$CI_TOOL" e2e sentinel-check "$DEVSY_E2E_SENTINEL_FILE" "${RUNNER_TEMP}/e2e-output.log"
check=$?
(( status[0] == 0 && status[1] == 0 && check == 0 ))

- name: upload MicroSandbox test evidence
if: always()
Expand Down Expand Up @@ -1217,7 +1233,15 @@
GH_USERNAME: ${{ github.repository_owner }}
GH_ACCESS_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
GH_CREDENTIAL_USERNAME: x-access-token
run: '"$CI_TOOL" e2e run --label "${{ matrix.name }}" --runner "${{ matrix.runner }}" --e2e-dir ./e2e'
run: |
export DEVSY_E2E_SENTINEL_FILE="${RUNNER_TEMP}/e2e-sentinels.jsonl"
(umask 077; : > "$DEVSY_E2E_SENTINEL_FILE")
set +e
"$CI_TOOL" e2e run --label "${{ matrix.name }}" --runner "${{ matrix.runner }}" --e2e-dir ./e2e 2>&1 | tee "${RUNNER_TEMP}/e2e-output.log"

Check warning on line 1240 in .github/workflows/pr-ci.yml

View check run for this annotation

codefactor.io / CodeFactor

template-injection

Code injection via template expansion: may expand into attacker-controllable code.

Check warning on line 1240 in .github/workflows/pr-ci.yml

View check run for this annotation

codefactor.io / CodeFactor

template-injection

Code injection via template expansion: may expand into attacker-controllable code.
status=("${PIPESTATUS[@]}")
"$CI_TOOL" e2e sentinel-check "$DEVSY_E2E_SENTINEL_FILE" "${RUNNER_TEMP}/e2e-output.log"
check=$?
(( status[0] == 0 && status[1] == 0 && check == 0 ))

- name: tear down Docker WSL SSH fixture
if: always() && matrix.setup_docker_wsl
Expand Down
90 changes: 33 additions & 57 deletions e2e/framework/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,30 @@ import (
// block Wait forever, letting a hung command outlive its spec timeout.
const execWaitDelay = 30 * time.Second

// ExecCommand executes the command string with the devsy test binary.
func (f *Framework) ExecCommandOutput(ctx context.Context, args []string) (string, error) {
var execOut bytes.Buffer
type commandCapture int

//nolint:gosec // e2e harness runs its own test commands by design
cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...)
docker.PrepareForGroupCancellation(cmd)
cmd.WaitDelay = execWaitDelay
outTee := livetee.New(&execOut, os.Stdout)
cmd.Stdout = outTee
cmd.Stderr = os.Stderr
const (
captureNone commandCapture = iota
captureStdout
captureBoth
)

err := cmd.Run()
f.liveStdoutDropped.Add(outTee.Close())
// ExecCommand executes the command string with the devsy test binary.
func (f *Framework) ExecCommandOutput(ctx context.Context, args []string) (string, error) {
out, _, err := f.execCommandCapture(ctx, args, captureStdout)
if err != nil {
if ctx.Err() != nil {
return execOut.String(), ctx.Err()
return out, ctx.Err()
}
return "", err
}

return execOut.String(), nil
return out, nil
}

// ExecCommandStdout executes the command string with the devsy test binary.
func (f *Framework) ExecCommandStdout(ctx context.Context, args []string) error {
//nolint:gosec // e2e harness runs its own test commands by design
cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...)
docker.PrepareForGroupCancellation(cmd)
cmd.WaitDelay = execWaitDelay
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}

return nil
_, _, err := f.execCommandCapture(ctx, args, captureNone)
return err
}

// ExecCommandContaining executes the command string with the devsy test binary and
Expand All @@ -69,26 +56,11 @@ func (f *Framework) ExecCommandContaining(
searchString string,
args []string,
) error {
var execOut bytes.Buffer

//nolint:gosec // e2e harness runs its own test commands by design
cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...)
docker.PrepareForGroupCancellation(cmd)
cmd.WaitDelay = execWaitDelay
outTee := livetee.New(&execOut, os.Stdout)
cmd.Stdout = outTee
cmd.Stderr = os.Stderr

err := cmd.Run()
f.liveStdoutDropped.Add(outTee.Close())
out, _, err := f.execCommandCapture(ctx, args, captureStdout)
if err != nil {
if ctx.Err() != nil {
return ctx.Err()
}
return err
}

if !strings.Contains(execOut.String(), searchString) {
if !strings.Contains(out, searchString) {
return fmt.Errorf("expected to find string %s in output", searchString)
}

Expand All @@ -98,20 +70,20 @@ func (f *Framework) ExecCommandContaining(
// ExecCommandCapture executes the command string with the devsy test binary, and returns
// stdout, stderr, and any error that occurred.
func (f *Framework) ExecCommandCapture(ctx context.Context, args []string) (string, string, error) {
return f.execCommandCapture(ctx, args, false)
return f.execCommandCapture(ctx, args, captureBoth)
}

func (f *Framework) ExecCommandDiagnosticCapture(
ctx context.Context,
args []string,
) (string, string, error) {
return f.execCommandCapture(ctx, args, true)
return f.execCommandCapture(ctx, args, captureBoth)
}

func (f *Framework) execCommandCapture(
ctx context.Context,
args []string,
diagnostic bool,
capture commandCapture,
) (string, string, error) {
var execOut bytes.Buffer
var execErr bytes.Buffer
Expand All @@ -120,19 +92,25 @@ func (f *Framework) execCommandCapture(
cmd := exec.CommandContext(ctx, filepath.Join(f.DevsyBinDir, f.DevsyBinName), args...)
docker.PrepareForGroupCancellation(cmd)
cmd.WaitDelay = execWaitDelay
var redactor *redact.Redactor
if diagnostic {
redactor = redact.New(redact.Options{Environment: cmd.Environ()})
redactor := redact.New(
redact.Options{Environment: cmd.Environ(), Values: registeredSecretValues()},
)
var outWriter, errWriter io.Writer = &execOut, &execErr
switch capture {
case captureNone:
outWriter, errWriter = io.Discard, io.Discard
case captureStdout:
errWriter = io.Discard
}
stdout, closeStdout := captureCommandOutput(&execOut, os.Stdout, redactor)
stderr, closeStderr := captureCommandOutput(&execErr, os.Stderr, redactor)
stdout, closeStdout := captureCommandOutput(outWriter, os.Stdout, redactor)
stderr, closeStderr := captureCommandOutput(errWriter, os.Stderr, redactor)
cmd.Stdout = stdout
cmd.Stderr = stderr

err := cmd.Run()
f.liveStdoutDropped.Add(closeStdout())
f.liveStderrDropped.Add(closeStderr())
if err != nil && ctx.Err() != nil {
if capture != captureNone && err != nil && ctx.Err() != nil {
err = ctx.Err()
}
return execOut.String(), execErr.String(), err
Expand All @@ -142,17 +120,15 @@ func captureCommandOutput(
capture, live io.Writer,
redactor *redact.Redactor,
) (io.Writer, func() int64) {
if redactor == nil {
tee := livetee.New(capture, live)
return tee, tee.Close
}
tee := livetee.New(io.Discard, live)
diagnostic := newLiveDiagnosticCapture(tee, redactor)
return io.MultiWriter(capture, diagnostic), diagnostic.close
}

func externalCommandDiagnostic(value string) string {
redactor := redact.New(redact.Options{Environment: os.Environ()})
redactor := redact.New(
redact.Options{Environment: os.Environ(), Values: registeredSecretValues()},
)
return ReportText(redactor, value)
}

Expand Down
55 changes: 49 additions & 6 deletions e2e/framework/exec_capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ const (
)

func TestCaptureChild(t *testing.T) {
registeredCaptureChild()
switch os.Getenv("DEVSY_CAPTURE_CHILD") {
case "default-mask":
const record = "{\"Cookie\":\"CANARY\"}\n{\"message\":\"public detail\"}\n" +
"[INFO] start\n{\n \"Cookie\": \"CANARY\",\n \"message\": \"public detail\"\n}\n" +
"public detail "
_, _ = fmt.Fprint(os.Stdout, record+os.Getenv("DEVSY_FIXTURE_TOKEN"))
_, _ = fmt.Fprint(os.Stderr, record+os.Getenv("DEVSY_FIXTURE_TOKEN"))
os.Exit(0)
case "json-mask":
_, _ = fmt.Fprint(os.Stdout, `{"Cookie":"CANARY","message":"public detail"}`)
_, _ = fmt.Fprint(os.Stderr, `{"Cookie":"CANARY","message":"public detail"}`)
os.Exit(0)
case "exit":
_, _ = fmt.Fprint(os.Stdout, "stdout")
_, _ = fmt.Fprint(os.Stderr, "stderr")
Expand All @@ -44,13 +56,33 @@ func TestCaptureChild(t *testing.T) {
_, _ = fmt.Fprint(os.Stderr, record)
os.Exit(0)
case "blocked":
_, _ = fmt.Fprint(os.Stdout, strings.Repeat("x", 2<<20))
_, _ = fmt.Fprint(os.Stderr, "ready")
_, _ = fmt.Fprint(os.Stdout, blockedCaptureOutput())
_, _ = fmt.Fprint(os.Stderr, "ready\n")
time.Sleep(time.Minute)
os.Exit(0)
}
}

func blockedCaptureOutput() string {
return strings.Repeat(strings.Repeat("x", 4095)+"\n", 512)
}

func registeredCaptureChild() {
switch os.Getenv("DEVSY_CAPTURE_CHILD") {
case "registered":
for _, output := range []*os.File{os.Stdout, os.Stderr} {
_, _ = fmt.Fprint(output, "registered-fixture-")
_, _ = fmt.Fprint(output, "value\npublic detail\n")
}
os.Exit(0)
case "registered-multiline":
_, _ = fmt.Fprint(os.Stdout, "multi-fixture\n")
_, _ = fmt.Fprint(os.Stdout, "second-part\npublic detail\n")
_, _ = fmt.Fprint(os.Stderr, "multi-fixture\nsecond-part\npublic detail\n")
os.Exit(0)
}
}

type captureSuite struct{ suite.Suite }

func TestCaptureSuite(t *testing.T) { suite.Run(t, new(captureSuite)) }
Expand All @@ -65,6 +97,15 @@ func (s *captureSuite) TestExit() {
s.Equal("stderr", stderr)
}

func (s *captureSuite) TestStdoutCaptureDiscardsRawStderr() {
s.T().Setenv("DEVSY_CAPTURE_CHILD", "exit")
out, stderr, err := s.framework().
execCommandCapture(context.Background(), []string{captureChildArg}, captureStdout)
s.Require().Error(err)
s.Equal("stdout", out)
s.Empty(stderr)
}

func (s *captureSuite) TestCaptureMasksOnlyLiveDiagnosticCopies() {
s.T().Setenv("DEVSY_CAPTURE_CHILD", "folded")
outRead, outWrite, err := os.Pipe()
Expand Down Expand Up @@ -120,6 +161,8 @@ func (s *captureSuite) TestLiveDiagnosticsMaskUnterminatedJSONRecord() {
}

func (s *captureSuite) TestBlockedLiveOutput() {
restoreRegisteredSecrets(s.T())
RegisterSecret("blocked-fixture-first\nblocked-fixture-second")
s.T().Setenv("DEVSY_CAPTURE_CHILD", "blocked")
outRead, outWrite, err := os.Pipe()
s.Require().NoError(err)
Expand Down Expand Up @@ -148,9 +191,9 @@ func (s *captureSuite) TestBlockedLiveOutput() {
}()
ready := make(chan error, 1)
go func() {
b := make([]byte, 5)
b := make([]byte, 6)
_, err := io.ReadFull(errRead, b)
if err == nil && string(b) != "ready" {
if err == nil && string(b) != "ready\n" {
err = fmt.Errorf("unexpected stderr %q", b)
}
ready <- err
Expand All @@ -165,8 +208,8 @@ func (s *captureSuite) TestBlockedLiveOutput() {
select {
case got := <-done:
s.ErrorIs(got.err, context.Canceled)
s.Equal(strings.Repeat("x", 2<<20), got.out)
s.Equal("ready", got.stderr)
s.Equal(blockedCaptureOutput(), got.out)
s.Equal("ready\n", got.stderr)
dropped, _ := f.LiveOutputDropped()
s.Positive(dropped)
case <-time.After(40 * time.Second):
Expand Down
81 changes: 81 additions & 0 deletions e2e/framework/exec_default_mask_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package framework

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

const (
defaultCapture = "capture"
defaultOutput = "output"
)

func TestDefaultExecMasksLiveOutput(t *testing.T) {
t.Setenv("DEVSY_FIXTURE_TOKEN", "synthetic-token-value-12345")
for _, name := range []string{defaultCapture, defaultOutput, "containing", "stdout", formatJSON} {
t.Run(name, func(t *testing.T) {
mode := "default-mask"
if name == formatJSON {
mode = "json-mask"
}
t.Setenv("DEVSY_CAPTURE_CHILD", mode)
executable, err := os.Executable()
require.NoError(t, err)
f := &Framework{
DevsyBinDir: filepath.Dir(executable),
DevsyBinName: filepath.Base(executable),
}
outFile, err := os.CreateTemp(t.TempDir(), "stdout")
require.NoError(t, err)
defer func() { _ = outFile.Close() }()
errFile, err := os.CreateTemp(t.TempDir(), "stderr")
require.NoError(t, err)
defer func() { _ = errFile.Close() }()
oldOut, oldErr := os.Stdout, os.Stderr
os.Stdout, os.Stderr = outFile, errFile
defer func() { os.Stdout, os.Stderr = oldOut, oldErr }()
raw := runDefaultMaskHelper(t, f, name)
for _, file := range []*os.File{outFile, errFile} {
data, err := os.ReadFile(file.Name())
require.NoError(t, err)
require.Contains(t, string(data), "public detail")
require.NotContains(t, string(data), "CANARY")
require.NotContains(t, string(data), "synthetic-token-value-12345")
}
if name == defaultCapture || name == defaultOutput {
require.Contains(t, raw, "CANARY")
require.Contains(t, raw, "synthetic-token-value-12345")
}
})
}
}

func runDefaultMaskHelper(t *testing.T, f *Framework, name string) string {
t.Helper()
ctx := context.Background()
args := []string{captureChildArg}
switch name {
case defaultCapture:
out, stderr, err := f.ExecCommandCapture(ctx, args)
require.NoError(t, err)
require.Equal(t, out, stderr)
return out
case defaultOutput:
out, err := f.ExecCommandOutput(ctx, args)
require.NoError(t, err)
return out
case "containing":
require.NoError(t, f.ExecCommandContaining(ctx, "CANARY", args))
case "stdout":
require.NoError(t, f.ExecCommandStdout(ctx, args))
case formatJSON:
result, err := f.ExecCommandJSON(ctx, args)
require.NoError(t, err)
require.Equal(t, "CANARY", result["Cookie"])
}
return ""
}
Loading
Loading