Skip to content

Commit

Permalink
Add support for a custom gid.
Browse files Browse the repository at this point in the history
Permit the package author to specify a custom gid by providing a
io.whalebrew.config.customgid label.
  • Loading branch information
rjkroege committed Dec 13, 2019
1 parent e3c8d32 commit c5ee57e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Package struct {
MountMissingVolumes bool `yaml:"mount_missing_volumes,omitempty"`
RequiredVersion string `yaml:"required_version,omitempty"`
PathArguments []string `yaml:"path_arguments,omitempty"`
CustomGid string `yaml:"customgid,omitempty"`
}

// NewPackageFromImage creates a package from a given image name,
Expand Down Expand Up @@ -127,6 +128,12 @@ func NewPackageFromImage(image string, imageInspect types.ImageInspect) (*Packag
return pkg, fmt.Errorf("unexpected io.whalebrew.config.missing_volumes value: %s expecting error, skip or mount", missingVolumes)
}
}

if customgid, ok := labels["io.whalebrew.config.customgid"]; ok {
if err := yaml.Unmarshal([]byte(customgid), &pkg.CustomGid); err != nil {
return pkg, err
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestNewPackageFromImage(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, pkg.Name, "foo")
assert.Equal(t, pkg.Image, "whalebrew/foo:bar")
assert.Equal(t, pkg.CustomGid, "")

assert.Equal(t, "ws", mustNewTestPkg(t, "io.whalebrew.name", "ws").Name)
assert.Equal(t, "whalebrew/whalesay", mustNewTestPkg(t, "io.whalebrew.name", "ws").Image)
Expand All @@ -53,6 +54,7 @@ func TestNewPackageFromImage(t *testing.T) {

assert.False(t, mustNewTestPkg(t, "io.whalebrew.config.missing_volumes", "error").MountMissingVolumes)
assert.False(t, mustNewTestPkg(t, "io.whalebrew.config.missing_volumes", "error").SkipMissingVolumes)
assert.Equal(t, "42", mustNewTestPkg(t, "io.whalebrew.config.customgid", "42").CustomGid)

assert.False(t, mustNewTestPkg(t, "any", "ws").MountMissingVolumes)
assert.False(t, mustNewTestPkg(t, "any", "other").SkipMissingVolumes)
Expand Down
9 changes: 8 additions & 1 deletion run/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,17 @@ func (d *Docker) Run(p *packages.Package, e *Execution) error {
dockerArgs = append(dockerArgs, "-v")
dockerArgs = append(dockerArgs, volume)
}

if !p.KeepContainerUser {
if e.User != nil {
dockerArgs = append(dockerArgs, "-u")
dockerArgs = append(dockerArgs, e.User.Uid+":"+e.User.Gid)
if p.CustomGid != "" {
dockerArgs = append(dockerArgs, e.User.Uid+":"+p.CustomGid)
} else {
// does this have test..
dockerArgs = append(dockerArgs, e.User.Uid+":"+e.User.Gid)
}

}
}
dockerArgs = append(dockerArgs, p.Image)
Expand Down
29 changes: 29 additions & 0 deletions run/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,33 @@ func TestDockerRun(t *testing.T) {
Gid: "4086",
},
}))

// Test customgid functionality.
d.Exec = func(argv0 string, argv []string, envv []string) (err error) {
assert.Equal(t, "docker", argv0)
assert.Equal(
t,
[]string{
"docker", "run", "--interactive", "--rm",
"--workdir", "/workdir", "--init",
"-u", "2048:1234",
"alpine",
"-h", "hello", "world",
},
argv,
)
assert.Equal(t, os.Environ(), envv)
return nil
}
assert.NoError(t, d.Run(&packages.Package{
Image: "alpine",
CustomGid: "1234",
}, &run.Execution{
WorkingDir: "/workdir",
User: &user.User{
Uid: "2048",
Gid: "4086",
},
Args: []string{"-h", "hello", "world"},
}))
}

0 comments on commit c5ee57e

Please sign in to comment.