From 427315902b957d722dc40820d532d53572046593 Mon Sep 17 00:00:00 2001 From: Felix Abecassis Date: Wed, 25 Apr 2018 14:38:58 -0700 Subject: [PATCH] cmd: move handling of ID mapping to a new file Signed-off-by: Felix Abecassis --- cmd/umoci/raw-runtime-config.go | 49 ++----------------- cmd/umoci/unpack.go | 49 ++----------------- cmd/umoci/utils_idmap.go | 85 +++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 88 deletions(-) create mode 100644 cmd/umoci/utils_idmap.go diff --git a/cmd/umoci/raw-runtime-config.go b/cmd/umoci/raw-runtime-config.go index 6a9b9152d..c49881825 100644 --- a/cmd/umoci/raw-runtime-config.go +++ b/cmd/umoci/raw-runtime-config.go @@ -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 = remappingCommand(cli.Command{ Name: "runtime-config", Aliases: []string{"config"}, Usage: "generates an OCI runtime configuration for an image", @@ -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)", @@ -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) @@ -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("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 { diff --git a/cmd/umoci/unpack.go b/cmd/umoci/unpack.go index 03631cea5..1596d89f8 100644 --- a/cmd/umoci/unpack.go +++ b/cmd/umoci/unpack.go @@ -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 = remappingCommand(cli.Command{ Name: "unpack", Usage: "unpacks a reference into an OCI runtime bundle", ArgsUsage: `--image [:] @@ -51,18 +50,6 @@ creation with umoci-repack(1).`, 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", - }, }, Action: unpack, @@ -77,7 +64,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) @@ -87,38 +74,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 { diff --git a/cmd/umoci/utils_idmap.go b/cmd/umoci/utils_idmap.go new file mode 100644 index 000000000..b0c875848 --- /dev/null +++ b/cmd/umoci/utils_idmap.go @@ -0,0 +1,85 @@ +/* + * umoci: Umoci Modifies Open Containers' Images + * Copyright (C) 2018 SUSE LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fmt" + "os" + + "github.com/apex/log" + "github.com/openSUSE/umoci/pkg/idtools" + "github.com/pkg/errors" + "github.com/urfave/cli" +) + +func remappingCommand(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 +} + +// 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 +} +