Skip to content

Commit

Permalink
Specialize function to get user and group id
Browse files Browse the repository at this point in the history
Signed-off-by: Diogo Behrens <[email protected]>
  • Loading branch information
db7 committed Apr 19, 2024
1 parent 31974f1 commit d47f196
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
42 changes: 10 additions & 32 deletions tools/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"vsync/logger"
Expand Down Expand Up @@ -41,46 +40,25 @@ func DockerPull(ctx context.Context) error {

func DockerRun(ctx context.Context, args []string, volumes []string) error {
var (
cmd = []string{"run", "--rm"}
rootless = false
cmd = []string{"run", "--rm"}
)
// find current user
u, err := user.Current()

// find out current directory
cwd, err := os.Getwd()
if err != nil {
return err
}

// check docker installation
if err := exec.CommandContext(ctx, dockerCmd).Run(); err != nil {
return fmt.Errorf("could not run docker: %v", err)
}

// are we running outside docker?
if FileExists("/.dockerenv") == nil {
return fmt.Errorf("running inside docker. Set VSYNCER_DOCKER=false")
}

// is it rootless?
if output, err := exec.CommandContext(ctx, dockerCmd, "info", "-f",
"{{println .SecurityOptions}}").Output(); err != nil {
return fmt.Errorf("could not run docker: %v", err)
} else {
rootless = strings.Contains(string(output), "rootless")
// get user/group flags
if u, err := dockerUserGroup(ctx); err != nil {
return err
} else if len(u) > 0 {
cmd = append(cmd, u...)
}

// if not rooless do I have permission?
if !rootless && u.Uid != "0" {
// check if user in docker group, otherwise should we request sudo?
if output, err := exec.CommandContext(ctx, "id", "-Gn").Output(); err != nil {
return fmt.Errorf("could get user groups: %v", err)
} else if !strings.Contains(string(output), "docker") {
return fmt.Errorf("user is not in docker group")
}

cmd = append(cmd, "-u", fmt.Sprintf("%v:%v", u.Uid, u.Gid))
// find out current directory
cwd, err := os.Getwd()
if err != nil {
return err
}

// mount current directory
Expand Down
12 changes: 12 additions & 0 deletions tools/docker_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
package tools

import (
"context"
"fmt"
"os/exec"
"os/user"
)

func dockerUserGroup(ctx context.Context) ([]string, error) {
// find current user
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("could not find current user: %v", err)
}

return []string{"-u", fmt.Sprintf("%v:%v", u.Uid, u.Gid)}, nil
}

func dockerInteractive(_ *exec.Cmd) error {
return fmt.Errorf("functionality not supported")
}
41 changes: 41 additions & 0 deletions tools/docker_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,58 @@
package tools

import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"os/user"
"strings"
"syscall"

"github.com/creack/pty"
"golang.org/x/term"
)

func dockerUserGroup(ctx context.Context) ([]string, error) {
var rootless bool

// find current user
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("could not find current user: %v", err)
}

// check docker installation
if err := exec.CommandContext(ctx, dockerCmd).Run(); err != nil {
return nil, fmt.Errorf("could not run docker: %v", err)
}

// is it rootless?
if output, err := exec.CommandContext(ctx, dockerCmd, "info", "-f",
"{{println .SecurityOptions}}").Output(); err != nil {
return nil, fmt.Errorf("could not run docker: %v", err)
} else {
rootless = strings.Contains(string(output), "rootless")
}

// if not rooless do I have permission?
if rootless || u.Uid != "0" {
return nil, nil
}

// check if user in docker group, otherwise should we request sudo?
if output, err := exec.CommandContext(ctx, "id", "-Gn").Output(); err != nil {
return nil, fmt.Errorf("could get user groups: %v", err)
} else if !strings.Contains(string(output), "docker") {
return nil, fmt.Errorf("user is not in docker group")
}

return []string{"-u", fmt.Sprintf("%v:%v", u.Uid, u.Gid)}, nil
}

func dockerInteractive(c *exec.Cmd) error {
ptmx, err := pty.Start(c)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions tools/docker_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
package tools

import (
"context"
"fmt"
"os/exec"
)

func dockerUserGroup(ctx context.Context) ([]string, error) {
return fmt.Errorf("functionality not supported")
}

func dockerInteractive(_ *exec.Cmd) error {
return fmt.Errorf("functionality not supported")
}

0 comments on commit d47f196

Please sign in to comment.