Skip to content
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

Refactor ID mapping #240

Merged
merged 3 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 5 additions & 44 deletions cmd/umoci/raw-runtime-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ import (
"github.com/openSUSE/umoci/oci/cas/dir"
"github.com/openSUSE/umoci/oci/casext"
"github.com/openSUSE/umoci/oci/layer"
"github.com/openSUSE/umoci/pkg/idtools"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/net/context"
)

var rawConfigCommand = cli.Command{
var rawConfigCommand = uxRemap(cli.Command{
Name: "runtime-config",
Aliases: []string{"config"},
Usage: "generates an OCI runtime configuration for an image",
Expand All @@ -51,18 +50,6 @@ Note that the results of this may not agree with umoci-unpack(1) because the
Category: "image",

Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "uid-map",
Usage: "specifies a uid mapping to use when generating config",
},
cli.StringSliceFlag{
Name: "gid-map",
Usage: "specifies a gid mapping to use when generating config",
},
cli.BoolFlag{
Name: "rootless",
Usage: "generate rootless configuration",
},
cli.StringFlag{
Name: "rootfs",
Usage: "path to secondary source of truth (root filesystem)",
Expand All @@ -81,7 +68,7 @@ Note that the results of this may not agree with umoci-unpack(1) because the
ctx.App.Metadata["config"] = ctx.Args().First()
return nil
},
}
})

func rawConfig(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
Expand All @@ -91,38 +78,12 @@ func rawConfig(ctx *cli.Context) error {
var meta UmociMeta
meta.Version = UmociMetaVersion

// Parse map options.
// We need to set mappings if we're in rootless mode.
meta.MapOptions.Rootless = ctx.Bool("rootless")
if meta.MapOptions.Rootless {
if !ctx.IsSet("uid-map") {
ctx.Set("uid-map", fmt.Sprintf("%d:0:1", os.Geteuid()))
}
if !ctx.IsSet("gid-map") {
ctx.Set("gid-map", fmt.Sprintf("%d:0:1", os.Getegid()))
}
}
// Parse and set up the mapping options.
for _, uidmap := range ctx.StringSlice("uid-map") {
idMap, err := idtools.ParseMapping(uidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --uid-map %s: %s", uidmap)
}
meta.MapOptions.UIDMappings = append(meta.MapOptions.UIDMappings, idMap)
}
for _, gidmap := range ctx.StringSlice("gid-map") {
idMap, err := idtools.ParseMapping(gidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --gid-map %s: %s", gidmap)
}
meta.MapOptions.GIDMappings = append(meta.MapOptions.GIDMappings, idMap)
err := parseIdmapOptions(&meta, ctx)
if err != nil {
return err
}

log.WithFields(log.Fields{
"map.uid": meta.MapOptions.UIDMappings,
"map.gid": meta.MapOptions.GIDMappings,
}).Debugf("parsed mappings")

// Get a reference to the CAS.
engine, err := dir.Open(imagePath)
if err != nil {
Expand Down
52 changes: 6 additions & 46 deletions cmd/umoci/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ import (
"github.com/openSUSE/umoci/oci/casext"
"github.com/openSUSE/umoci/oci/layer"
"github.com/openSUSE/umoci/pkg/fseval"
"github.com/openSUSE/umoci/pkg/idtools"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/net/context"
)

var unpackCommand = cli.Command{
var unpackCommand = uxRemap(cli.Command{
Name: "unpack",
Usage: "unpacks a reference into an OCI runtime bundle",
ArgsUsage: `--image <image-path>[:<tag>] <bundle>
Expand All @@ -50,20 +49,7 @@ creation with umoci-repack(1).`,
// unpack reads manifest information.
Category: "image",

Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "uid-map",
Usage: "specifies a uid mapping to use when repacking (container:host:size)",
},
cli.StringSliceFlag{
Name: "gid-map",
Usage: "specifies a gid mapping to use when repacking (container:host:size)",
},
cli.BoolFlag{
Name: "rootless",
Usage: "enable rootless unpacking support",
},
},
Flags: []cli.Flag{},

Action: unpack,

Expand All @@ -77,7 +63,7 @@ creation with umoci-repack(1).`,
ctx.App.Metadata["bundle"] = ctx.Args().First()
return nil
},
}
})

func unpack(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
Expand All @@ -87,38 +73,12 @@ func unpack(ctx *cli.Context) error {
var meta UmociMeta
meta.Version = UmociMetaVersion

// Parse map options.
// We need to set mappings if we're in rootless mode.
meta.MapOptions.Rootless = ctx.Bool("rootless")
if meta.MapOptions.Rootless {
if !ctx.IsSet("uid-map") {
ctx.Set("uid-map", fmt.Sprintf("0:%d:1", os.Geteuid()))
}
if !ctx.IsSet("gid-map") {
ctx.Set("gid-map", fmt.Sprintf("0:%d:1", os.Getegid()))
}
}
// Parse and set up the mapping options.
for _, uidmap := range ctx.StringSlice("uid-map") {
idMap, err := idtools.ParseMapping(uidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --uid-map %s: %s", uidmap)
}
meta.MapOptions.UIDMappings = append(meta.MapOptions.UIDMappings, idMap)
}
for _, gidmap := range ctx.StringSlice("gid-map") {
idMap, err := idtools.ParseMapping(gidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --gid-map %s: %s", gidmap)
}
meta.MapOptions.GIDMappings = append(meta.MapOptions.GIDMappings, idMap)
err := parseIdmapOptions(&meta, ctx)
if err != nil {
return err
}

log.WithFields(log.Fields{
"map.uid": meta.MapOptions.UIDMappings,
"map.gid": meta.MapOptions.GIDMappings,
}).Debugf("parsed mappings")

// Get a reference to the CAS.
engine, err := dir.Open(imagePath)
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions cmd/umoci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import (
"github.com/openSUSE/umoci/oci/casext"
igen "github.com/openSUSE/umoci/oci/config/generate"
"github.com/openSUSE/umoci/oci/layer"
"github.com/openSUSE/umoci/pkg/idtools"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/vbatts/go-mtree"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -280,3 +282,40 @@ func generateBundleManifest(mtreeName string, bundlePath string, fsEval mtree.Fs

return nil
}

// parseIdmapOptions sets up the mapping options for UmociMeta, using
// the arguments specified on the command line
func parseIdmapOptions(meta *UmociMeta, ctx *cli.Context) error {
// We need to set mappings if we're in rootless mode.
meta.MapOptions.Rootless = ctx.Bool("rootless")
if meta.MapOptions.Rootless {
if !ctx.IsSet("uid-map") {
ctx.Set("uid-map", fmt.Sprintf("0:%d:1", os.Geteuid()))
}
if !ctx.IsSet("gid-map") {
ctx.Set("gid-map", fmt.Sprintf("0:%d:1", os.Getegid()))
}
}

for _, uidmap := range ctx.StringSlice("uid-map") {
idMap, err := idtools.ParseMapping(uidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --uid-map %s: %s", uidmap)
}
meta.MapOptions.UIDMappings = append(meta.MapOptions.UIDMappings, idMap)
}
for _, gidmap := range ctx.StringSlice("gid-map") {
idMap, err := idtools.ParseMapping(gidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --gid-map %s: %s", gidmap)
}
meta.MapOptions.GIDMappings = append(meta.MapOptions.GIDMappings, idMap)
}

log.WithFields(log.Fields{
"map.uid": meta.MapOptions.UIDMappings,
"map.gid": meta.MapOptions.GIDMappings,
}).Debugf("parsed mappings")

return nil
}
19 changes: 19 additions & 0 deletions cmd/umoci/utils_ux.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,22 @@ func uxLayout(cmd cli.Command) cli.Command {

return cmd
}

func uxRemap(cmd cli.Command) cli.Command {
cmd.Flags = append(cmd.Flags, []cli.Flag{
cli.StringSliceFlag{
Name: "uid-map",
Usage: "specifies a uid mapping to use (container:host:size)",
},
cli.StringSliceFlag{
Name: "gid-map",
Usage: "specifies a gid mapping to use (container:host:size)",
},
cli.BoolFlag{
Name: "rootless",
Usage: "enable rootless command support",
},
}...)

return cmd
}
2 changes: 1 addition & 1 deletion doc/man/umoci-raw-runtime-config.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ operations that necessitate it.
```
% skopeo copy docker://opensuse/amd64:42.2 oci:image:latest
# umoci unpack --image image bundle
% umoci raw runtime-generate --image image --rootfs bundle/rootfs config.json
% umoci raw runtime-config --image image --rootfs bundle/rootfs config.json
```

# SEE ALSO
Expand Down