-
Notifications
You must be signed in to change notification settings - Fork 2
feat(docker): linux privilege elevation for Docker and Podman providers #737
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
Open
skevetter
wants to merge
3
commits into
main
Choose a base branch
from
feat/docker-podman-privilege-elevation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/log" | ||
| ) | ||
|
|
||
| // elevationAuthTimeout is generous enough for interactive credential entry, | ||
| // unlike the short per-command probe timeouts. | ||
| const elevationAuthTimeout = 2 * time.Minute | ||
|
|
||
| // Supported privilege-elevation helpers. | ||
| const ( | ||
| elevationPkexec = "pkexec" | ||
| elevationSudo = "sudo" | ||
| elevationDoas = "doas" | ||
| ) | ||
|
|
||
| // Elevator runs docker commands through a privilege-elevation helper (pkexec, | ||
| // sudo, doas). It authenticates once, up front, so an operation's many commands | ||
| // share a single prompt via the warmed OS credential cache. | ||
| type Elevator struct { | ||
| prefix []string // elevation command and leading args; always non-empty | ||
|
|
||
| once sync.Once | ||
| err error | ||
| } | ||
|
|
||
| // ElevatorFromName maps a helper name to an Elevator; "" and "none" return | ||
| // (nil, nil), unknown names error. | ||
| func ElevatorFromName(name string) (*Elevator, error) { | ||
| switch strings.ToLower(strings.TrimSpace(name)) { | ||
| case "", "none": | ||
| return nil, nil | ||
| case elevationPkexec: | ||
| return &Elevator{prefix: []string{elevationPkexec}}, nil | ||
| case elevationSudo: | ||
| return &Elevator{prefix: []string{elevationSudo}}, nil | ||
| case elevationDoas: | ||
| return &Elevator{prefix: []string{elevationDoas}}, nil | ||
| default: | ||
| return nil, fmt.Errorf( | ||
| "unknown privilege elevation %q (want pkexec, sudo, doas, or none)", | ||
| name, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // wrap builds the elevated invocation of dockerCommand. env (KEY=VAL entries, | ||
| // e.g. DOCKER_HOST) is forwarded through env(1) because sudo/pkexec/doas reset | ||
| // the child environment and would otherwise drop provider configuration. | ||
| func (e *Elevator) wrap(dockerCommand string, env, args []string) (string, []string) { | ||
| full := make([]string, 0, len(e.prefix)+len(env)+len(args)+1) | ||
| full = append(full, e.prefix[1:]...) | ||
| if len(env) > 0 { | ||
| full = append(full, "env") | ||
| full = append(full, env...) | ||
| } | ||
| full = append(full, dockerCommand) | ||
| full = append(full, args...) | ||
| return e.prefix[0], full | ||
| } | ||
|
|
||
| // ensureAuthenticated warms the credential cache once. It elevates the | ||
| // client-only "--version" (no daemon needed) on its own timeout, so a short | ||
| // caller deadline cannot kill the prompt. | ||
| func (e *Elevator) ensureAuthenticated(dockerCommand string, env []string) error { | ||
| e.once.Do(func() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), elevationAuthTimeout) | ||
| defer cancel() | ||
|
|
||
| name, args := e.wrap(dockerCommand, env, []string{"--version"}) | ||
| //nolint:gosec // command and args come from trusted provider config | ||
| cmd := exec.CommandContext(ctx, name, args...) | ||
| if env != nil { | ||
| cmd.Env = append(os.Environ(), env...) | ||
| } | ||
| cmd.Stdin = os.Stdin // attach terminal for the credential prompt | ||
| cmd.Stdout = io.Discard | ||
| cmd.Stderr = os.Stderr | ||
|
|
||
| log.Debugf("authenticating privilege elevation via %s", e.prefix[0]) | ||
| if err := cmd.Run(); err != nil { | ||
| e.err = fmt.Errorf("privilege elevation via %s failed: %w", e.prefix[0], err) | ||
| } | ||
| }) | ||
| return e.err | ||
| } | ||
|
|
||
| // EnsureElevated authenticates the configured elevator once; concurrent callers | ||
| // block on the single prompt. No-op and safe when no elevator is set. | ||
| func (r *DockerHelper) EnsureElevated() error { | ||
| if r.Elevator == nil { | ||
| return nil | ||
| } | ||
| return r.Elevator.ensureAuthenticated(r.DockerCommand, r.Environment) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const ( | ||
| dockerCmd = string(RuntimeDocker) | ||
| dockerHost = "DOCKER_HOST=tcp://host:2375" | ||
| ) | ||
|
|
||
| func TestElevatorFromName(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| wantNil bool | ||
| wantPrefix []string | ||
| wantErr bool | ||
| }{ | ||
| {name: "", wantNil: true}, | ||
| {name: "none", wantNil: true}, | ||
| {name: " None ", wantNil: true}, | ||
| {name: elevationPkexec, wantPrefix: []string{elevationPkexec}}, | ||
| {name: "SUDO", wantPrefix: []string{elevationSudo}}, | ||
| {name: elevationDoas, wantPrefix: []string{elevationDoas}}, | ||
| {name: "gksu", wantErr: true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| e, err := ElevatorFromName(tt.name) | ||
| if tt.wantErr { | ||
| require.Error(t, err) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| if tt.wantNil { | ||
| assert.Nil(t, e) | ||
| return | ||
| } | ||
| require.NotNil(t, e) | ||
| assert.Equal(t, tt.wantPrefix, e.prefix) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestElevatorWrap(t *testing.T) { | ||
| e, err := ElevatorFromName(elevationPkexec) | ||
| require.NoError(t, err) | ||
|
|
||
| name, args := e.wrap(dockerCmd, nil, []string{"ps", "-q"}) | ||
| assert.Equal(t, elevationPkexec, name) | ||
| assert.Equal(t, []string{dockerCmd, "ps", "-q"}, args) | ||
|
|
||
| // No docker args. | ||
| name, args = e.wrap("/usr/bin/docker", nil, nil) | ||
| assert.Equal(t, elevationPkexec, name) | ||
| assert.Equal(t, []string{"/usr/bin/docker"}, args) | ||
|
|
||
| // Environment is forwarded through env(1). | ||
| name, args = e.wrap(dockerCmd, []string{dockerHost}, []string{"ps"}) | ||
| assert.Equal(t, elevationPkexec, name) | ||
| assert.Equal(t, []string{"env", dockerHost, dockerCmd, "ps"}, args) | ||
| } | ||
|
|
||
| func TestEnsureElevatedNoOpWithoutElevator(t *testing.T) { | ||
| r := &DockerHelper{DockerCommand: dockerCmd} | ||
| assert.NoError(t, r.EnsureElevated()) | ||
| } | ||
|
|
||
| func TestBuildCmdWithoutElevator(t *testing.T) { | ||
| r := &DockerHelper{DockerCommand: dockerCmd} | ||
| cmd := r.buildCmd(t.Context(), "ps", "-q") | ||
| assert.Equal(t, []string{dockerCmd, "ps", "-q"}, cmd.Args) | ||
| } | ||
|
|
||
| func TestBuildCmdWithElevator(t *testing.T) { | ||
| e, err := ElevatorFromName(elevationSudo) | ||
| require.NoError(t, err) | ||
| // Mark authentication as already done so buildCmd does not attempt an | ||
| // interactive prompt during the test. | ||
| e.once.Do(func() {}) | ||
|
|
||
| r := &DockerHelper{DockerCommand: dockerCmd, Elevator: e} | ||
| cmd := r.buildCmd(t.Context(), "ps", "-q") | ||
| assert.Equal(t, []string{elevationSudo, dockerCmd, "ps", "-q"}, cmd.Args) | ||
| } | ||
|
|
||
| func TestBuildCmdWithElevatorForwardsEnv(t *testing.T) { | ||
| e, err := ElevatorFromName(elevationSudo) | ||
| require.NoError(t, err) | ||
| e.once.Do(func() {}) | ||
|
|
||
| r := &DockerHelper{ | ||
| DockerCommand: dockerCmd, | ||
| Environment: []string{dockerHost}, | ||
| Elevator: e, | ||
| } | ||
| cmd := r.buildCmd(t.Context(), "ps") | ||
| assert.Equal(t, | ||
| []string{elevationSudo, "env", dockerHost, dockerCmd, "ps"}, | ||
| cmd.Args, | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.