Skip to content

Commit

Permalink
cmd: move handling of ID mapping to a new file
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Abecassis <fabecassis@nvidia.com>
  • Loading branch information
flx42 committed Apr 25, 2018
1 parent 16e1bcb commit 4273159
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 88 deletions.
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 = remappingCommand(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("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
49 changes: 5 additions & 44 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 = remappingCommand(cli.Command{
Name: "unpack",
Usage: "unpacks a reference into an OCI runtime bundle",
ArgsUsage: `--image <image-path>[:<tag>] <bundle>
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
85 changes: 85 additions & 0 deletions cmd/umoci/utils_idmap.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4273159

Please sign in to comment.