Skip to content

Commit

Permalink
Propagate user from image configuration
Browse files Browse the repository at this point in the history
This works for both build and test, but will probably be harder to make
work with builds because of file ownership shenanigans. For tests, this
makes it possible to test non-root users for packages that get mad if
you run them as root.

Signed-off-by: Jon Johnson <jon.johnson@chainguard.dev>
  • Loading branch information
jonjohnsonjr committed Mar 19, 2024
1 parent dc4c8ee commit a62f4ca
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ func (b *Build) buildWorkspaceConfig(ctx context.Context) *container.Config {
},
WorkspaceDir: b.WorkspaceDir,
Timeout: b.Configuration.Package.Timeout,
RunAs: b.Configuration.Environment.Accounts.RunAs,
}

if b.Configuration.Package.Resources != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/build/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (t *Test) TestPackage(ctx context.Context) error {
}
}

cfg, err := t.buildWorkspaceConfig(ctx, imgRef, pkg.Name, t.Configuration.Test.Environment.Environment)
cfg, err := t.buildWorkspaceConfig(ctx, imgRef, pkg.Name, t.Configuration.Test.Environment)
if err != nil {
return fmt.Errorf("unable to build workspace config: %w", err)
}
Expand Down Expand Up @@ -485,7 +485,7 @@ func (t *Test) TestPackage(ctx context.Context) error {
if err := t.OverlayBinSh(sp.Name); err != nil {
return fmt.Errorf("unable to install overlay /bin/sh: %w", err)
}
subCfg, err := t.buildWorkspaceConfig(ctx, spImgRef, sp.Name, sp.Test.Environment.Environment)
subCfg, err := t.buildWorkspaceConfig(ctx, spImgRef, sp.Name, sp.Test.Environment)
if err != nil {
return fmt.Errorf("unable to build workspace config: %w", err)
}
Expand Down Expand Up @@ -547,7 +547,7 @@ func (t *Test) Summarize(ctx context.Context) {
t.SummarizePaths(ctx)
}

func (t *Test) buildWorkspaceConfig(ctx context.Context, imgRef, pkgName string, env map[string]string) (*container.Config, error) {
func (t *Test) buildWorkspaceConfig(ctx context.Context, imgRef, pkgName string, imgcfg apko_types.ImageConfiguration) (*container.Config, error) {
log := clog.FromContext(ctx)
mounts := []container.BindMount{
{Source: t.WorkspaceDir, Destination: container.DefaultWorkspaceDir},
Expand Down Expand Up @@ -577,9 +577,10 @@ func (t *Test) buildWorkspaceConfig(ctx context.Context, imgRef, pkgName string,
Mounts: mounts,
Capabilities: caps,
Environment: map[string]string{},
RunAs: imgcfg.Accounts.RunAs,
}

for k, v := range env {
for k, v := range imgcfg.Environment {
cfg.Environment[k] = v
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/container/bubblewrap_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func (bw *bubblewrap) cmd(ctx context.Context, cfg *Config, debug bool, args ...
"--chdir", runnerWorkdir,
"--clearenv")

if cfg.RunAs != "" {
baseargs = append(baseargs, "--unshare-user")
baseargs = append(baseargs, "--uid", cfg.RunAs)
}

if !debug {
// This flag breaks job control, which we only care about for --interactive debugging.
// So we usually include it, but if we're about to debug, don't set it.
Expand Down
1 change: 1 addition & 0 deletions pkg/container/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
ImgRef string
PodID string
Arch apko_types.Architecture
RunAs string
WorkspaceDir string
CPU, Memory string
Timeout time.Duration
Expand Down
5 changes: 1 addition & 4 deletions pkg/container/docker/docker_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,8 @@ func (dk *docker) Run(ctx context.Context, cfg *mcontainer.Config, args ...strin
environ = append(environ, fmt.Sprintf("%s=%s", k, v))
}

// TODO(kaniini): We want to use the build user here, but for now lets keep it simple.
// TODO(epsilon-phase): building as the user "build" was removed from docker runner
// for consistency with other runners and to ensure that packages can be generated with files
// that have owners other than root. We should explore using fakeroot or similar tricks for these use-cases.
taskIDResp, err := dk.cli.ContainerExecCreate(ctx, cfg.PodID, types.ExecConfig{
User: cfg.RunAs,
Cmd: args,
WorkingDir: runnerWorkdir,
Env: environ,
Expand Down

0 comments on commit a62f4ca

Please sign in to comment.