Repository navigation
fix(runtime): reject reserved core labels set through runArgs #1829
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/log" | ||
| "github.com/stretchr/testify/suite" | ||
| "go.uber.org/zap/zapcore" | ||
| ) | ||
|
|
||
| type StartRedactionSuite struct{ suite.Suite } | ||
|
|
||
| func TestStartRedactionSuite(t *testing.T) { suite.Run(t, new(StartRedactionSuite)) } | ||
|
|
||
| func (s *StartRedactionSuite) TestStartFailure() { | ||
| observed := log.InitTestObserved(s.T(), zapcore.ErrorLevel) | ||
| path := writeScript(s.T(), s.T().TempDir(), "engine", `#!/bin/sh | ||
| printf '%s\n' "$FIXTURE_TOKEN" "$ORDINARY" | ||
| if [ "$1" = start ]; then exit 1; fi | ||
| `) | ||
| h := &DockerHelper{DockerCommand: path, Environment: []string{ | ||
| "FIXTURE_TOKEN=fixture-private-value", "ORDINARY=container-private-value", | ||
| }} | ||
| err := h.StartContainerWithEnvironment( | ||
| context.Background(), | ||
| "container", | ||
| []string{"ORDINARY=container-private-value"}, | ||
| ) | ||
| s.Require().Error(err) | ||
| s.NotContains(err.Error(), "fixture-private-value") | ||
| s.Require().NotEmpty(observed.All()) | ||
| s.NotContains(observed.All()[0].Message, "fixture-private-value") | ||
| s.NotContains(err.Error(), "container-private-value") | ||
| s.NotContains(observed.All()[0].Message, "container-private-value") | ||
| s.Contains(err.Error(), "***") | ||
| s.Contains(observed.All()[0].Message, "***") | ||
| } | ||
|
|
||
| func (s *StartRedactionSuite) TestInspectCreatedContainer() { | ||
| path := writeScript(s.T(), s.T().TempDir(), "engine", `#!/bin/sh | ||
| printf '%s\n' '[{"Id":"created-id","Config":{"Labels":{"team":"a"},"Env":["TOKEN=fixture-private-value"]}}]' | ||
| `) | ||
| h := &DockerHelper{DockerCommand: path} | ||
| labels, environment, err := h.InspectCreatedContainer(context.Background(), "created-id") | ||
| s.Require().NoError(err) | ||
| s.Equal(map[string]string{"team": "a"}, labels) | ||
| s.Equal([]string{"TOKEN=fixture-private-value"}, environment) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,16 @@ | ||
| package apple | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/apple" | ||
| pkgconfig "github.com/devsy-org/devsy/pkg/config" | ||
| "github.com/devsy-org/devsy/pkg/devcontainer/config" | ||
| "github.com/devsy-org/devsy/pkg/driver" | ||
| "github.com/devsy-org/devsy/pkg/log" | ||
|
|
@@ -163,9 +168,39 @@ func (d *appleDriver) RunImageDevContainer( | |
| "running apple container command: command=%s, args=%s", | ||
| d.command, redactArgs(args), | ||
| ) | ||
| var output bytes.Buffer | ||
| err := d.Apple.RunWithDir(ctx, params.LocalWorkspaceFolder, args, | ||
| apple.Streams{Stdout: writer, Stderr: writer}) | ||
| apple.Streams{Stdout: io.MultiWriter(writer, &output), Stderr: writer}) | ||
| if err != nil { | ||
| return fmt.Errorf( | ||
| "failed to start dev container: %w; %s", | ||
| err, | ||
| "runArgs must be valid for container create; options that only apply to run are not supported", | ||
| ) | ||
| } | ||
| ids := strings.Fields(output.String()) | ||
| if len(ids) != 1 { | ||
| return fmt.Errorf( | ||
| "failed to identify created container; inspect the runtime for an unstarted container", | ||
| ) | ||
| } | ||
| id := ids[0] | ||
| details, err := d.Apple.FindContainerByID(ctx, []string{id}) | ||
| if err == nil && (details == nil || details.ID != id) { | ||
| err = fmt.Errorf("created container inspection returned an unexpected result") | ||
| } | ||
| if err == nil { | ||
| labels := append( | ||
| config.GetIDLabels(params.WorkspaceID, d.IDLabels), | ||
| params.Options.Labels...) | ||
| err = pkgconfig.ValidateCreatedLabels(labels, details.Config.Labels) | ||
| } | ||
| if err != nil { | ||
| cleanupCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
| return errors.Join(err, d.Apple.Remove(cleanupCtx, id)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate retained containers before a later start can run them. When label validation fails and Check the container's labels before any driver start path runs. Alternatively, prevent discovery from selecting an unvalidated container.
|
||
| } | ||
| if err := d.Apple.StartContainer(ctx, id); err != nil { | ||
| return fmt.Errorf("failed to start dev container: %w", err) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package apple | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/devcontainer/config" | ||
| "github.com/devsy-org/devsy/pkg/driver" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type CreatedLabelsSuite struct{ suite.Suite } | ||
|
|
||
| const ( | ||
| testUserLabel = "devsy.user" | ||
| testPrivateLabelValue = "private-value" | ||
| testRemoveCreated = "remove:created-id" | ||
| ) | ||
|
|
||
| func TestCreatedLabelsSuite(t *testing.T) { suite.Run(t, new(CreatedLabelsSuite)) } | ||
|
|
||
| func (s *CreatedLabelsSuite) TestAmbiguousCreateOutputNeverStartsOrDeletes() { | ||
| m := &mockClient{createOutput: "private-value\ncreated-id\n"} | ||
| d := &appleDriver{Apple: m} | ||
| err := d.RunImageDevContainer(context.Background(), &driver.RunImageDevContainerParams{ | ||
| WorkspaceID: "ws", ParsedConfig: &config.DevContainerConfig{}, | ||
| Options: &driver.RunOptions{Image: testImageRef}, | ||
| }) | ||
| s.Require().ErrorContains(err, "inspect the runtime for an unstarted container") | ||
| s.NotContains(err.Error(), testPrivateLabelValue) | ||
| s.Equal([]string{testRunCall}, m.calls) | ||
| } | ||
|
|
||
| func (s *CreatedLabelsSuite) TestCreateInspectsBeforeStarting() { | ||
| for _, tc := range []struct { | ||
| name string | ||
| labels map[string]string | ||
| wantCalls []string | ||
| wantKey string | ||
| }{ | ||
| {"match", map[string]string{testUserLabel: execUser, "team": "a"}, []string{testRunCall, "start:created-id"}, ""}, | ||
| { | ||
| "override", | ||
| map[string]string{testUserLabel: testPrivateLabelValue}, | ||
| []string{testRunCall, testRemoveCreated}, | ||
| testUserLabel, | ||
| }, | ||
| {"missing", nil, []string{testRunCall, testRemoveCreated}, testUserLabel}, | ||
| { | ||
| "extra", | ||
| map[string]string{testUserLabel: execUser, "sh.devsy.recovery": testPrivateLabelValue}, | ||
| []string{testRunCall, testRemoveCreated}, | ||
| "sh.devsy.recovery", | ||
| }, | ||
| } { | ||
| s.Run(tc.name, func() { | ||
| m := &mockClient{found: &config.ContainerDetails{ | ||
| ID: "created-id", Config: config.ContainerDetailsConfig{Labels: tc.labels}, | ||
| }} | ||
| d := &appleDriver{Apple: m} | ||
| err := d.RunImageDevContainer(context.Background(), &driver.RunImageDevContainerParams{ | ||
| WorkspaceID: "ws", | ||
| ParsedConfig: &config.DevContainerConfig{}, | ||
| Options: &driver.RunOptions{ | ||
| Image: testImageRef, | ||
| Labels: []string{"devsy.user=root"}, | ||
| }, | ||
| }) | ||
| if tc.wantKey != "" { | ||
| s.Require().ErrorContains(err, tc.wantKey) | ||
| s.NotContains(err.Error(), testPrivateLabelValue) | ||
| } else { | ||
| s.NoError(err) | ||
| } | ||
| s.Equal(tc.wantCalls, m.calls) | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ import ( | |
| // buildRunArgs assembles `container run` args, emitting only flags Apple supports | ||
| // (Docker/Podman-only flags like --security-opt/--gpus/--userns are omitted). | ||
| func (d *appleDriver) buildRunArgs(params *driver.RunImageDevContainerParams) []string { | ||
| args := []string{"run", "-d"} | ||
| args := []string{"create"} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing Update the test to start the created container, or run it through the new launch flow.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed. A one-line fix to start the created probe before waiting is prepared locally, but is not pushed while the security finding remains unresolved. The Apple integration test is gated and skipped on this host, so the runtime behavior has not been verified. |
||
|
|
||
| if d.Rosetta { | ||
| args = append(args, "--rosetta") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.