mirror of
https://github.com/containers/podman.git
synced 2025-05-17 06:59:07 +08:00
uid/gid mapping flags
Motivation =========== This feature aims to make --uidmap and --gidmap easier to use, especially in rootless podman setups. (I will focus here on the --gidmap option, although the same applies for --uidmap.) In rootless podman, the user namespace mapping happens in two steps, through an intermediate mapping. See https://docs.podman.io/en/latest/markdown/podman-run.1.html#uidmap-container-uid-from-uid-amount for further detail, here is a summary: First the user GID is mapped to 0 (root), and all subordinate GIDs (defined at /etc/subgid, and usually >100000) are mapped starting at 1. One way to customize the mapping is through the `--gidmap` option, that maps that intermediate mapping to the final mapping that will be seen by the container. As an example, let's say we have as main GID the group 1000, and we also belong to the additional GID 2000, that we want to make accessible inside the container. We first ask the sysadmin to subordinate the group to us, by adding "$user:2000:1" to /etc/subgid. Then we need to use --gidmap to specify that we want to map GID 2000 into some GID inside the container. And here is the first trouble: Since the --gidmap option operates on the intermediate mapping, we first need to figure out where has podman placed our GID 2000 in that intermediate mapping using: podman unshare cat /proc/self/gid_map Then, we may see that GID 2000 was mapped to intermediate GID 5. So our --gidmap option should include: --gidmap 20000:5:1 This intermediate mapping may change in the future if further groups are subordinated to us (or we stop having its subordination), so we are forced to verify the mapping with `podman unshare cat /proc/self/gid_map` every time, and parse it if we want to script it. **The first usability improvement** we agreed on #18333 is to be able to use: --gidmap 20000:@2000:1 so podman does this lookup in the parent user namespace for us. But this is only part of the problem. We must specify a **full** gidmap and not only what we want: --gidmap 0:0:5 --gidmap 5:6:15000 --gidmap 20000:5:1 This is becoming complicated. We had to break the gidmap at 5, because the intermediate 5 had to be mapped to another value (20000), and then we had to keep mapping all other subordinate ids... up to close to the maximum number of subordinate ids that we have (or some reasonable value). This is hard to explain to someone who does not understand how the mappings work internally. To simplify this, **the second usability improvement** is to be able to use: --gidmap "+20000:@2000:1" where the plus flag (`+`) states that the given mapping should extend any previous/default mapping, overriding any previous conflicting assignment. Podman will set that mapping and fill the rest of mapped gids with all other subordinated gids, leading to the same (or an equivalent) full gidmap that we were specifying before. One final usability improvement related to this is the following: By default, when podman gets a --gidmap argument but not a --uidmap argument, it copies the mapping. This is convenient in many scenarios, since usually subordinated uids and gids are assigned in chunks simultaneously, and the subordinated IDs in /etc/subuid and /etc/subgid for a given user match. For scenarios with additional subordinated GIDs, this map copying is annoying, since it forces the user to provide a --uidmap, to prevent the copy from being made. This means, that when the user wants: --gidmap 0:0:5 --gidmap 5:6:15000 --gidmap 20000:5:1 The user has to include a uidmap as well: --gidmap 0:0:5 --gidmap 5:6:15000 --gidmap 20000:5:1 --uidmap 0:0:65000 making everything even harder to understand without proper context. For this reason, besides the "+" flag, we introduce the "u" and "g" flags. Those flags applied to a mapping tell podman that the mapping should only apply to users or groups, and ignored otherwise. Therefore we can use: --gidmap "+g20000:@2000:1" So the mapping only applies to groups and is ignored for uidmaps. If no "u" nor "g" flag is assigned podman assumes the mapping applies to both users and groups as before, so we preserve backwards compatibility. Co-authored-by: Tom Sweeney <tsweeney@redhat.com> Signed-off-by: Sergio Oller <sergioller@gmail.com>
This commit is contained in:
@ -2,11 +2,11 @@
|
||||
####> podman create, run
|
||||
####> If file is edited, make sure the changes
|
||||
####> are applicable to all of those.
|
||||
#### **--gidmap**=*container_gid:host_gid:amount*
|
||||
#### **--gidmap**=*[flags]container_uid:from_uid[:amount]*
|
||||
|
||||
Run the container in a new user namespace using the supplied GID mapping. This
|
||||
option conflicts with the **--userns** and **--subgidname** options. This
|
||||
option provides a way to map host GIDs to container GIDs in the same way as
|
||||
__--uidmap__ maps host UIDs to container UIDs. For details see __--uidmap__.
|
||||
|
||||
Note: the **--gidmap** flag cannot be called in conjunction with the **--pod** flag as a gidmap cannot be set on the container level when in a pod.
|
||||
Note: the **--gidmap** option cannot be called in conjunction with the **--pod** option as a gidmap cannot be set on the container level when in a pod.
|
||||
|
@ -2,16 +2,23 @@
|
||||
####> podman create, run
|
||||
####> If file is edited, make sure the changes
|
||||
####> are applicable to all of those.
|
||||
#### **--uidmap**=*container_uid:from_uid:amount*
|
||||
#### **--uidmap**=*[flags]container_uid:from_uid[:amount]*
|
||||
|
||||
Run the container in a new user namespace using the supplied UID mapping. This
|
||||
option conflicts with the **--userns** and **--subuidname** options. This
|
||||
option provides a way to map host UIDs to container UIDs. It can be passed
|
||||
several times to map different ranges.
|
||||
|
||||
The possible values of the optional *flags* are discussed further down on this page.
|
||||
The *amount* value is optional and assumed to be **1** if not given.
|
||||
|
||||
The *from_uid* value is based upon the user running the command, either rootful or rootless users.
|
||||
* rootful user: *container_uid*:*host_uid*:*amount*
|
||||
* rootless user: *container_uid*:*intermediate_uid*:*amount*
|
||||
|
||||
* rootful user: [*flags*]*container_uid*:*host_uid*[:*amount*]
|
||||
* rootless user: [*flags*]*container_uid*:*intermediate_uid*[:*amount*]
|
||||
|
||||
`Rootful mappings`
|
||||
|
||||
|
||||
When **podman <<subcommand>>** is called by a privileged user, the option **--uidmap**
|
||||
works as a direct mapping between host UIDs and container UIDs.
|
||||
@ -28,6 +35,8 @@ If for example _amount_ is **4** the mapping looks like:
|
||||
| *from_uid* + 2 | *container_uid* + 2 |
|
||||
| *from_uid* + 3 | *container_uid* + 3 |
|
||||
|
||||
`Rootless mappings`
|
||||
|
||||
When **podman <<subcommand>>** is called by an unprivileged user (i.e. running rootless),
|
||||
the value *from_uid* is interpreted as an "intermediate UID". In the rootless
|
||||
case, host UIDs are not mapped directly to container UIDs. Instead the mapping
|
||||
@ -76,13 +85,112 @@ Every additional range is added sequentially afterward:
|
||||
| 1 | $FIRST_RANGE_ID | $FIRST_RANGE_LENGTH |
|
||||
| 1+$FIRST_RANGE_LENGTH | $SECOND_RANGE_ID | $SECOND_RANGE_LENGTH|
|
||||
|
||||
By default, providing either **--uidmap** or **--gidmap** replaces the
|
||||
whole mapping. If only one of those two options is given, the other one is
|
||||
copied by default. If only one value of the two needs to be changed,
|
||||
both values should be provided.
|
||||
`Referencing a host ID from the parent namespace`
|
||||
|
||||
As a rootless user, the given host ID in **--uidmap** or **--gidmap**
|
||||
is mapped from the *intermediate namespace* generated by Podman. Sometimes
|
||||
it is desirable to refer directly at the *host namespace*. It is possible
|
||||
to manually do so, by running `podman unshare cat /proc/self/gid_map`,
|
||||
finding the desired host id at the second column of the output, and getting
|
||||
the corresponding intermediate id from the first column.
|
||||
|
||||
Podman can perform all that by preceding the host id in the mapping
|
||||
with the `@` symbol. For instance, by specifying `--gidmap 100000:@2000:1`,
|
||||
podman will look up the intermediate id corresponding to host id `2000` and
|
||||
it will map the found intermediate id to the container id `100000`. The
|
||||
given host id must have been subordinated (otherwise it would not be mapped
|
||||
into the intermediate space in the first place).
|
||||
|
||||
If the length is greater than one, for instance with `--gidmap 100000:@2000:2`,
|
||||
Podman will map host ids `2000` and `2001` to `100000` and `100001`, respectively,
|
||||
regardless of how the intermediate mapping is defined.
|
||||
|
||||
`Extending previous mappings`
|
||||
|
||||
Some mapping modifications may be cumbersome. For instance, a user
|
||||
starts with a mapping such as `--gidmap="0:0:65000"`, that needs to be
|
||||
changed such as the parent id `1000` is mapped to container id `100000`
|
||||
instead, leaving container id `1` unassigned. The corresponding `--gidmap`
|
||||
becomes `--gidmap="0:0:1" --gidmap="2:2:65534" --gidmap="100000:1:1"`.
|
||||
|
||||
This notation can be simplified using the `+` flag, that takes care of
|
||||
breaking previous mappings removing any conflicting assignment with
|
||||
the given mapping. The flag is given before the container id
|
||||
as follows: `--gidmap="0:0:65000" --gidmap="+100000:1:1"`
|
||||
|
||||
|
||||
Flag | Example | Description
|
||||
-----------|---------------|-------------
|
||||
`+` | `+100000:1:1` | Extend the previous mapping
|
||||
|
||||
This notation leads to gaps in the assignment, so it may be convenient to
|
||||
fill those gaps afterwards: `--gidmap="0:0:65000" --gidmap="+100000:1:1" --gidmap="1:65001:1"`
|
||||
|
||||
One specific use case for this flag is in the context of rootless
|
||||
users. A rootless user may specify mappings with the `+` flag as
|
||||
in `--gidmap="+100000:1:1"`. Podman will then "fill the gaps" starting
|
||||
from zero with all the remaining intermediate ids. This is convenient when
|
||||
a user wants to map a specific intermediate id to a container id, leaving
|
||||
the rest of subordinate ids to be mapped by Podman at will.
|
||||
|
||||
`Passing only one of --uidmap or --gidmap`
|
||||
|
||||
Usually, subordinated user and group ids are assigned simultaneously, and
|
||||
for any user the subordinated user ids match the subordinated group ids.
|
||||
For convenience, if only one of **--uidmap** or **--gidmap** is given,
|
||||
podman assumes the mapping refers to both UIDs and GIDs and applies the
|
||||
given mapping to both. If only one value of the two needs to be changed,
|
||||
the mappings should include the `u` or the `g` flags to specify that
|
||||
they only apply to UIDs or GIDs and should not be copied over.
|
||||
|
||||
flag | Example | Description
|
||||
---------|-----------------|-----------------
|
||||
`u` | `u20000:2000:1` |The mapping only applies to UIDs
|
||||
`g` | `g10000:1000:1` |The mapping only applies to GIDs
|
||||
|
||||
For instance given the command
|
||||
|
||||
podman <<subcommand>> --gidmap "0:0:1000" --gidmap "g2000:2000:1"
|
||||
|
||||
Since no **--uidmap** is given, the **--gidmap** is copied to **--uidmap**,
|
||||
giving a command equivalent to
|
||||
|
||||
podman <<subcommand>> --gidmap "0:0:1000" --gidmap "2000:2000:1" --uidmap "0:0:1000"
|
||||
|
||||
The `--gidmap "g2000:2000:1"` used the `g` flag and therefore it was
|
||||
not copied to **--uidmap**.
|
||||
|
||||
`Rootless mapping of additional host GIDs`
|
||||
|
||||
A rootless user may desire to map a specific host group that has already been
|
||||
subordinated within _/etc/subgid_ without specifying the rest of the mapping.
|
||||
|
||||
This can be done with **--gidmap "+g*container_gid*:@*host_gid*"**
|
||||
|
||||
Where:
|
||||
|
||||
- The host GID is given through the `@` symbol
|
||||
- The mapping of this GID is not copied over to **--usermap** thanks to the `g` flag.
|
||||
- The rest of the container IDs will be mapped starting from 0 to n,
|
||||
with all the remaining subordinated GIDs, thanks to the `+` flag.
|
||||
|
||||
For instance, if a user belongs to the group `2000` and that group is
|
||||
subordinated to that user (with `usermod --add-subgids 2000-2000 $USER`),
|
||||
the user can map the group into the container with: **--gidmap=+g100000:@2000**.
|
||||
|
||||
If this mapping is combined with the option, **--group-add=keep-groups**, the
|
||||
process in the container will belong to group `100000`, and files belonging
|
||||
to group `2000` in the host will appear as being owned by group `100000`
|
||||
inside the container.
|
||||
|
||||
podman run --group-add=keep-groups --gidmap="+100000:@2000" ...
|
||||
|
||||
`No subordinate UIDs`
|
||||
|
||||
Even if a user does not have any subordinate UIDs in _/etc/subuid_,
|
||||
**--uidmap** can be used to map the normal UID of the user to a
|
||||
container UID by running `podman <<subcommand>> --uidmap $container_uid:0:1 --user $container_uid ...`.
|
||||
|
||||
Note: the **--uidmap** flag cannot be called in conjunction with the **--pod** flag as a uidmap cannot be set on the container level when in a pod.
|
||||
`Pods`
|
||||
|
||||
The **--uidmap** option cannot be called in conjunction with the **--pod** option as a uidmap cannot be set on the container level when in a pod.
|
||||
|
@ -87,6 +87,34 @@ grep johndoe /etc/subuid /etc/subgid
|
||||
/etc/subgid:johndoe:100000:65536
|
||||
```
|
||||
|
||||
#### Giving access to additional groups
|
||||
|
||||
Users can fully map additional groups to a container namespace if
|
||||
those groups subordinated to the user:
|
||||
|
||||
```
|
||||
usermod --add-subgids 2000-2000 johndoe
|
||||
grep johndoe /etc/subgid
|
||||
```
|
||||
|
||||
This means the user `johndoe` can "impersonate" the group `2000` inside the
|
||||
container. Note that it is usually not a good idea to subordinate active
|
||||
user ids to other users, because it would allow user impersonation.
|
||||
|
||||
`johndoe` can use `--group-add keep-groups` to preserve the additional
|
||||
groups, and `--gidmap="+g102000:@2000"` to map the group `2000` in the host
|
||||
to the group `102000` in the container:
|
||||
|
||||
```
|
||||
podman run \
|
||||
--rm \
|
||||
--group-add keep-groups \
|
||||
--gidmap="+g102000:@2000" \
|
||||
--volume "$PWD:/data:ro" \
|
||||
--workdir /data \
|
||||
alpine ls -lisa
|
||||
```
|
||||
|
||||
### Enable unprivileged `ping`
|
||||
|
||||
Users running in a non-privileged container may not be able to use the `ping` utility from that container.
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
@ -17,6 +18,7 @@ import (
|
||||
"github.com/containers/podman/v4/pkg/domain/entities"
|
||||
"github.com/containers/podman/v4/pkg/namespaces"
|
||||
"github.com/containers/podman/v4/pkg/rootless"
|
||||
"github.com/containers/podman/v4/pkg/util"
|
||||
"github.com/containers/storage/pkg/idtools"
|
||||
"github.com/containers/storage/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -333,11 +335,22 @@ func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []strin
|
||||
options.UIDMap = mappings.UIDs()
|
||||
options.GIDMap = mappings.GIDs()
|
||||
}
|
||||
parsedUIDMap, err := idtools.ParseIDMap(uidMapSlice, "UID")
|
||||
|
||||
parentUIDMap, parentGIDMap, err := rootless.GetAvailableIDMaps()
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
// The kernel-provided files only exist if user namespaces are supported
|
||||
logrus.Debugf("User or group ID mappings not available: %s", err)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
parsedUIDMap, err := util.ParseIDMap(uidMapSlice, "UID", parentUIDMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parsedGIDMap, err := idtools.ParseIDMap(gidMapSlice, "GID")
|
||||
parsedGIDMap, err := util.ParseIDMap(gidMapSlice, "GID", parentGIDMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3,11 +3,14 @@ package util
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"math"
|
||||
"math/bits"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -28,11 +31,19 @@ import (
|
||||
"github.com/containers/storage/pkg/idtools"
|
||||
stypes "github.com/containers/storage/types"
|
||||
securejoin "github.com/cyphar/filepath-securejoin"
|
||||
ruser "github.com/opencontainers/runc/libcontainer/user"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// The flags that an [ug]id mapping can have
|
||||
type idMapFlags struct {
|
||||
Extends bool // The "+" flag
|
||||
UserMap bool // The "u" flag
|
||||
GroupMap bool // The "g" flag
|
||||
}
|
||||
|
||||
var containerConfig *config.Config
|
||||
|
||||
func init() {
|
||||
@ -224,13 +235,6 @@ func GetKeepIDMapping(opts *namespaces.KeepIDUserNsOptions) (*stypes.IDMappingOp
|
||||
return &options, uid, gid, nil
|
||||
}
|
||||
|
||||
min := func(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
uid := rootless.GetRootlessUID()
|
||||
gid := rootless.GetRootlessGID()
|
||||
if opts.UID != nil {
|
||||
@ -303,6 +307,530 @@ func GetNoMapMapping() (*stypes.IDMappingOptions, int, int, error) {
|
||||
return &options, 0, 0, nil
|
||||
}
|
||||
|
||||
// Map a given ID to the Parent/Host ID of a given mapping, and return
|
||||
// its corresponding ID/ContainerID.
|
||||
// Returns an error if the given ID is not found on the mapping parents
|
||||
func mapIDwithMapping(id uint64, mapping []ruser.IDMap, mapSetting string) (mappedid uint64, err error) {
|
||||
for _, v := range mapping {
|
||||
if v.Count == 0 {
|
||||
continue
|
||||
}
|
||||
if id >= uint64(v.ParentID) && id < uint64(v.ParentID+v.Count) {
|
||||
offset := id - uint64(v.ParentID)
|
||||
return uint64(v.ID) + offset, nil
|
||||
}
|
||||
}
|
||||
return uint64(0), fmt.Errorf("parent ID %s %d is not mapped/delegated", mapSetting, id)
|
||||
}
|
||||
|
||||
// Parse flags from spec
|
||||
// The `u` and `g` flags can be used to enforce that the mapping applies
|
||||
// exclusively to UIDs or GIDs.
|
||||
//
|
||||
// The `+` flag is interpreted as if the mapping replaces previous mappings
|
||||
// removing any conflicting mapping from those before adding this one.
|
||||
func parseFlags(spec []string) (flags idMapFlags, read int, err error) {
|
||||
flags.Extends = false
|
||||
flags.UserMap = false
|
||||
flags.GroupMap = false
|
||||
for read, char := range spec[0] {
|
||||
switch {
|
||||
case '0' <= char && char <= '9':
|
||||
return flags, read, nil
|
||||
case char == '+':
|
||||
flags.Extends = true
|
||||
case char == 'u':
|
||||
flags.UserMap = true
|
||||
case char == 'g':
|
||||
flags.GroupMap = true
|
||||
case true:
|
||||
return flags, 0, fmt.Errorf("invalid mapping: %v. Unknown flag %v", spec, char)
|
||||
}
|
||||
}
|
||||
return flags, read, fmt.Errorf("invalid mapping: %v, parsing flags", spec)
|
||||
}
|
||||
|
||||
// Extension of idTools.parseTriple that parses idmap triples.
|
||||
// The triple should be a length 3 string array, containing:
|
||||
// - Flags and ContainerID
|
||||
// - HostID
|
||||
// - Size
|
||||
//
|
||||
// parseTriple returns the parsed mapping, the mapping flags and
|
||||
// any possible error. If the error is not-nil, the mapping and flags
|
||||
// are not well-defined.
|
||||
//
|
||||
// idTools.parseTriple is extended here with the following enhancements:
|
||||
//
|
||||
// HostID @ syntax:
|
||||
// =================
|
||||
// HostID may use the "@" syntax: The "101001:@1001:1" mapping
|
||||
// means "take the 1001 id from the parent namespace and map it to 101001"
|
||||
//
|
||||
// Flags:
|
||||
// ======
|
||||
// Flags can be used to tell the caller how should the mapping be interpreted
|
||||
func parseTriple(spec []string, parentMapping []ruser.IDMap, mapSetting string) (mappings []idtools.IDMap, flags idMapFlags, err error) {
|
||||
if len(spec[0]) == 0 {
|
||||
return mappings, flags, fmt.Errorf("invalid empty container id at %s map: %v", mapSetting, spec)
|
||||
}
|
||||
var cids, hids, sizes []uint64
|
||||
var cid, hid uint64
|
||||
var hidIsParent bool
|
||||
flags, i, err := parseFlags(spec)
|
||||
if err != nil {
|
||||
return mappings, flags, err
|
||||
}
|
||||
// If no "u" nor "g" flag is given, assume the mapping applies to both
|
||||
if !flags.UserMap && !flags.GroupMap {
|
||||
flags.UserMap = true
|
||||
flags.GroupMap = true
|
||||
}
|
||||
// Parse the container ID, which must be an integer:
|
||||
cid, err = strconv.ParseUint(spec[0][i:], 10, 32)
|
||||
if err != nil {
|
||||
return mappings, flags, fmt.Errorf("parsing id map value %q: %w", spec[0], err)
|
||||
}
|
||||
// Parse the host id, which may be integer or @<integer>
|
||||
if len(spec[1]) == 0 {
|
||||
return mappings, flags, fmt.Errorf("invalid empty host id at %s map: %v", mapSetting, spec)
|
||||
}
|
||||
if spec[1][0] != '@' {
|
||||
hidIsParent = false
|
||||
hid, err = strconv.ParseUint(spec[1], 10, 32)
|
||||
} else {
|
||||
// Parse @<id>, where <id> is an integer corresponding to the parent mapping
|
||||
hidIsParent = true
|
||||
hid, err = strconv.ParseUint(spec[1][1:], 10, 32)
|
||||
}
|
||||
if err != nil {
|
||||
return mappings, flags, fmt.Errorf("parsing id map value %q: %w", spec[1], err)
|
||||
}
|
||||
// Parse the size of the mapping, which must be an integer
|
||||
sz, err := strconv.ParseUint(spec[2], 10, 32)
|
||||
if err != nil {
|
||||
return mappings, flags, fmt.Errorf("parsing id map value %q: %w", spec[2], err)
|
||||
}
|
||||
|
||||
if hidIsParent {
|
||||
if (mapSetting == "UID" && flags.UserMap) || (mapSetting == "GID" && flags.GroupMap) {
|
||||
for i := uint64(0); i < sz; i++ {
|
||||
cids = append(cids, cid+i)
|
||||
mappedID, err := mapIDwithMapping(hid+i, parentMapping, mapSetting)
|
||||
if err != nil {
|
||||
return mappings, flags, err
|
||||
}
|
||||
hids = append(hids, mappedID)
|
||||
sizes = append(sizes, 1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cids = []uint64{cid}
|
||||
hids = []uint64{hid}
|
||||
sizes = []uint64{sz}
|
||||
}
|
||||
|
||||
// Avoid possible integer overflow on 32bit builds
|
||||
if bits.UintSize == 32 {
|
||||
for i := range cids {
|
||||
if cids[i] > math.MaxInt32 || hids[i] > math.MaxInt32 || sizes[i] > math.MaxInt32 {
|
||||
return mappings, flags, fmt.Errorf("initializing ID mappings: %s setting is malformed expected [\"[+ug]uint32:[@]uint32[:uint32]\"] : %q", mapSetting, spec)
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range cids {
|
||||
mappings = append(mappings, idtools.IDMap{
|
||||
ContainerID: int(cids[i]),
|
||||
HostID: int(hids[i]),
|
||||
Size: int(sizes[i]),
|
||||
})
|
||||
}
|
||||
return mappings, flags, nil
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Remove any conflicting mapping from mapping present in extension, so
|
||||
// extension can be appended to mapping without conflicts.
|
||||
// Returns the resulting mapping, with extension appended to it.
|
||||
func breakInsert(mapping []idtools.IDMap, extension idtools.IDMap) (result []idtools.IDMap) {
|
||||
// Two steps:
|
||||
// 1. Remove extension regions from mapping
|
||||
// For each element in mapping, remove those parts of the mapping
|
||||
// that overlap with the extension, both in the container range
|
||||
// or in the host range.
|
||||
// 2. Add extension to mapping
|
||||
// Step 1: Remove extension regions from mapping
|
||||
for _, mapPiece := range mapping {
|
||||
// Make container and host ranges comparable, by computing their
|
||||
// extension relative to the start of the mapPiece:
|
||||
range1Start := extension.ContainerID - mapPiece.ContainerID
|
||||
range2Start := extension.HostID - mapPiece.HostID
|
||||
|
||||
// Range end relative to mapPiece range
|
||||
range1End := range1Start + extension.Size
|
||||
range2End := range2Start + extension.Size
|
||||
|
||||
// mapPiece range:
|
||||
mapPieceStart := 0
|
||||
mapPieceEnd := mapPiece.Size
|
||||
|
||||
if range1End < mapPieceStart || range1Start >= mapPieceEnd {
|
||||
// out of range, forget about it
|
||||
range1End = -1
|
||||
range1Start = -1
|
||||
} else {
|
||||
// clip limits removal to mapPiece
|
||||
range1End = min(range1End, mapPieceEnd)
|
||||
range1Start = max(range1Start, mapPieceStart)
|
||||
}
|
||||
|
||||
if range2End < mapPieceStart || range2Start >= mapPieceEnd {
|
||||
// out of range, forget about it
|
||||
range2End = -1
|
||||
range2Start = -1
|
||||
} else {
|
||||
// clip limits removal to mapPiece
|
||||
range2End = min(range2End, mapPieceEnd)
|
||||
range2Start = max(range2Start, mapPieceStart)
|
||||
}
|
||||
|
||||
// If there is nothing to remove, append the original and continue:
|
||||
if range1Start == -1 && range2Start == -1 {
|
||||
result = append(result, mapPiece)
|
||||
continue
|
||||
}
|
||||
|
||||
// If there is one range to remove, save it at range1:
|
||||
if range1Start == -1 && range2Start != -1 {
|
||||
range1Start = range2Start
|
||||
range1End = range2End
|
||||
range2Start = -1
|
||||
range2End = -1
|
||||
}
|
||||
|
||||
// If we have two valid ranges, merge them into range1 if possible
|
||||
if range2Start != -1 {
|
||||
// Swap ranges so always range1Start is <= range2Start
|
||||
if range2Start < range1Start {
|
||||
range1Start, range2Start = range2Start, range1Start
|
||||
range1End, range2End = range2End, range1End
|
||||
}
|
||||
// If there is overlap, merge them:
|
||||
if range1End >= range2Start {
|
||||
range1End = max(range1End, range2End)
|
||||
range2Start = -1
|
||||
range2End = -1
|
||||
}
|
||||
}
|
||||
|
||||
if range1Start > 0 {
|
||||
// Append everything before range1Start
|
||||
result = append(result, idtools.IDMap{
|
||||
ContainerID: mapPiece.ContainerID,
|
||||
HostID: mapPiece.HostID,
|
||||
Size: range1Start,
|
||||
})
|
||||
}
|
||||
if range2Start == -1 {
|
||||
// Append everything after range1
|
||||
if mapPiece.Size-range1End > 0 {
|
||||
result = append(result, idtools.IDMap{
|
||||
ContainerID: mapPiece.ContainerID + range1End,
|
||||
HostID: mapPiece.HostID + range1End,
|
||||
Size: mapPiece.Size - range1End,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// Append everything between range1 and range2
|
||||
result = append(result, idtools.IDMap{
|
||||
ContainerID: mapPiece.ContainerID + range1End,
|
||||
HostID: mapPiece.HostID + range1End,
|
||||
Size: range2Start - range1End,
|
||||
})
|
||||
// Append everything after range2
|
||||
if mapPiece.Size-range2End > 0 {
|
||||
result = append(result, idtools.IDMap{
|
||||
ContainerID: mapPiece.ContainerID + range2End,
|
||||
HostID: mapPiece.HostID + range2End,
|
||||
Size: mapPiece.Size - range2End,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
// Step 2. Add extension to mapping
|
||||
result = append(result, extension)
|
||||
return result
|
||||
}
|
||||
|
||||
// A multirange is a list of [start,end) ranges and is expressed as
|
||||
// an array of length-2 integers.
|
||||
//
|
||||
// This function computes availableRanges = fullRanges - usedRanges,
|
||||
// where all variables are multiranges.
|
||||
// The subtraction operation is defined as "return the multirange
|
||||
// containing all integers found in fullRanges and not found in usedRanges.
|
||||
func getAvailableIDRanges(fullRanges, usedRanges [][2]int) (availableRanges [][2]int) {
|
||||
// Sort them
|
||||
sort.Slice(fullRanges, func(i, j int) bool {
|
||||
return fullRanges[i][0] < fullRanges[j][0]
|
||||
})
|
||||
|
||||
if len(usedRanges) == 0 {
|
||||
return fullRanges
|
||||
}
|
||||
|
||||
sort.Slice(usedRanges, func(i, j int) bool {
|
||||
return usedRanges[i][0] < usedRanges[j][0]
|
||||
})
|
||||
|
||||
// To traverse usedRanges
|
||||
i := 0
|
||||
nextUsedID := usedRanges[i][0]
|
||||
nextUsedIDEnd := usedRanges[i][1]
|
||||
|
||||
for _, fullRange := range fullRanges {
|
||||
currentIDToProcess := fullRange[0]
|
||||
for currentIDToProcess < fullRange[1] {
|
||||
switch {
|
||||
case nextUsedID == -1:
|
||||
// No further used ids, append all the remaining ranges
|
||||
availableRanges = append(availableRanges, [2]int{currentIDToProcess, fullRange[1]})
|
||||
currentIDToProcess = fullRange[1]
|
||||
case currentIDToProcess < nextUsedID:
|
||||
// currentIDToProcess is not used, append:
|
||||
if fullRange[1] <= nextUsedID {
|
||||
availableRanges = append(availableRanges, [2]int{currentIDToProcess, fullRange[1]})
|
||||
currentIDToProcess = fullRange[1]
|
||||
} else {
|
||||
availableRanges = append(availableRanges, [2]int{currentIDToProcess, nextUsedID})
|
||||
currentIDToProcess = nextUsedID
|
||||
}
|
||||
case currentIDToProcess == nextUsedID:
|
||||
// currentIDToProcess and all ids until nextUsedIDEnd are used
|
||||
// Advance currentIDToProcess
|
||||
currentIDToProcess = min(fullRange[1], nextUsedIDEnd)
|
||||
default: // currentIDToProcess > nextUsedID
|
||||
// Increment nextUsedID so it is >= currentIDToProcess
|
||||
// Go to next used block if this one is all behind:
|
||||
if currentIDToProcess >= nextUsedIDEnd {
|
||||
i += 1
|
||||
if i == len(usedRanges) {
|
||||
// No more used ranges
|
||||
nextUsedID = -1
|
||||
} else {
|
||||
nextUsedID = usedRanges[i][0]
|
||||
nextUsedIDEnd = usedRanges[i][1]
|
||||
}
|
||||
continue
|
||||
} else { // currentIDToProcess < nextUsedIDEnd
|
||||
currentIDToProcess = min(fullRange[1], nextUsedIDEnd)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return availableRanges
|
||||
}
|
||||
|
||||
// Gets the multirange of subordinated ids from parentMapping and the
|
||||
// multirange of already assigned ids from idmap, and returns the
|
||||
// multirange of unassigned subordinated ids.
|
||||
func getAvailableIDRangesFromMappings(idmap []idtools.IDMap, parentMapping []ruser.IDMap) (availableRanges [][2]int) {
|
||||
// Get all subordinated ids from parentMapping:
|
||||
fullRanges := [][2]int{} // {Multirange: [start, end), [start, end), ...}
|
||||
for _, mapPiece := range parentMapping {
|
||||
fullRanges = append(fullRanges, [2]int{int(mapPiece.ID), int(mapPiece.ID + mapPiece.Count)})
|
||||
}
|
||||
|
||||
// Get the ids already mapped:
|
||||
usedRanges := [][2]int{}
|
||||
for _, mapPiece := range idmap {
|
||||
usedRanges = append(usedRanges, [2]int{mapPiece.HostID, mapPiece.HostID + mapPiece.Size})
|
||||
}
|
||||
|
||||
// availableRanges = fullRanges - usedRanges
|
||||
availableRanges = getAvailableIDRanges(fullRanges, usedRanges)
|
||||
return availableRanges
|
||||
}
|
||||
|
||||
// Fills unassigned idmap ContainerIDs, starting from zero with all
|
||||
// the available ids given by availableRanges.
|
||||
// Returns the filled idmap.
|
||||
func fillIDMap(idmap []idtools.IDMap, availableRanges [][2]int) (output []idtools.IDMap) {
|
||||
idmapByCid := append([]idtools.IDMap{}, idmap...)
|
||||
sort.Slice(idmapByCid, func(i, j int) bool {
|
||||
return idmapByCid[i].ContainerID < idmapByCid[j].ContainerID
|
||||
})
|
||||
|
||||
if len(availableRanges) == 0 {
|
||||
return idmapByCid
|
||||
}
|
||||
|
||||
i := 0 // to iterate through availableRanges
|
||||
nextCid := 0
|
||||
nextAvailHid := availableRanges[i][0]
|
||||
|
||||
for _, mapPiece := range idmapByCid {
|
||||
// While there are available IDs to map and unassigned
|
||||
// container ids, map the available ids:
|
||||
for nextCid < mapPiece.ContainerID && nextAvailHid != -1 {
|
||||
size := min(mapPiece.ContainerID-nextCid, availableRanges[i][1]-nextAvailHid)
|
||||
output = append(output, idtools.IDMap{
|
||||
ContainerID: nextCid,
|
||||
HostID: nextAvailHid,
|
||||
Size: size,
|
||||
})
|
||||
nextCid += size
|
||||
if nextAvailHid+size < availableRanges[i][1] {
|
||||
nextAvailHid += size
|
||||
} else {
|
||||
i += 1
|
||||
if i == len(availableRanges) {
|
||||
nextAvailHid = -1
|
||||
continue
|
||||
}
|
||||
nextAvailHid = availableRanges[i][0]
|
||||
}
|
||||
}
|
||||
// The given mapping does not change
|
||||
output = append(output, mapPiece)
|
||||
nextCid += mapPiece.Size
|
||||
}
|
||||
// After the last given mapping is mapped, we use all the remaining
|
||||
// ids to map the rest of the space
|
||||
for nextAvailHid != -1 {
|
||||
size := availableRanges[i][1] - nextAvailHid
|
||||
output = append(output, idtools.IDMap{
|
||||
ContainerID: nextCid,
|
||||
HostID: nextAvailHid,
|
||||
Size: size,
|
||||
})
|
||||
nextCid += size
|
||||
i += 1
|
||||
if i == len(availableRanges) {
|
||||
nextAvailHid = -1
|
||||
continue
|
||||
}
|
||||
nextAvailHid = availableRanges[i][0]
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func addOneMapping(idmap []idtools.IDMap, fillMap bool, mapping idtools.IDMap, flags idMapFlags, mapSetting string) ([]idtools.IDMap, bool) {
|
||||
// If we are mapping uids and the spec doesn't have the usermap flag, ignore it
|
||||
if mapSetting == "UID" && !flags.UserMap {
|
||||
return idmap, fillMap
|
||||
}
|
||||
// If we are mapping gids and the spec doesn't have the groupmap flag, ignore it
|
||||
if mapSetting == "GID" && !flags.GroupMap {
|
||||
return idmap, fillMap
|
||||
}
|
||||
|
||||
// Zero-size mapping is ignored
|
||||
if mapping.Size == 0 {
|
||||
return idmap, fillMap
|
||||
}
|
||||
|
||||
// Not extending, just append:
|
||||
if !flags.Extends {
|
||||
idmap = append(idmap, mapping)
|
||||
return idmap, fillMap
|
||||
}
|
||||
// Break and extend the last mapping:
|
||||
|
||||
// Extending without any mapping, if rootless, we will fill
|
||||
// the space with the remaining IDs:
|
||||
if len(idmap) == 0 && rootless.IsRootless() {
|
||||
fillMap = true
|
||||
}
|
||||
|
||||
idmap = breakInsert(idmap, mapping)
|
||||
return idmap, fillMap
|
||||
}
|
||||
|
||||
// Extension of idTools.ParseIDMap that parses idmap triples from string.
|
||||
// This extension accepts additional flags that control how the mapping is done
|
||||
func ParseIDMap(mapSpec []string, mapSetting string, parentMapping []ruser.IDMap) (idmap []idtools.IDMap, err error) {
|
||||
stdErr := fmt.Errorf("initializing ID mappings: %s setting is malformed expected [\"[+ug]uint32:[@]uint32[:uint32]\"] : %q", mapSetting, mapSpec)
|
||||
// When fillMap is true, the given mapping will be filled with the remaining subordinate available ids
|
||||
fillMap := false
|
||||
for _, idMapSpec := range mapSpec {
|
||||
if idMapSpec == "" {
|
||||
continue
|
||||
}
|
||||
idSpec := strings.Split(idMapSpec, ":")
|
||||
// if it's a length-2 list assume the size is 1:
|
||||
if len(idSpec) == 2 {
|
||||
idSpec = append(idSpec, "1")
|
||||
}
|
||||
if len(idSpec)%3 != 0 {
|
||||
return nil, stdErr
|
||||
}
|
||||
for i := range idSpec {
|
||||
if i%3 != 0 {
|
||||
continue
|
||||
}
|
||||
if len(idSpec[i]) == 0 {
|
||||
return nil, stdErr
|
||||
}
|
||||
// Parse this mapping:
|
||||
mappings, flags, err := parseTriple(idSpec[i:i+3], parentMapping, mapSetting)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, mapping := range mappings {
|
||||
idmap, fillMap = addOneMapping(idmap, fillMap, mapping, flags, mapSetting)
|
||||
}
|
||||
}
|
||||
}
|
||||
if fillMap {
|
||||
availableRanges := getAvailableIDRangesFromMappings(idmap, parentMapping)
|
||||
idmap = fillIDMap(idmap, availableRanges)
|
||||
}
|
||||
|
||||
if len(idmap) == 0 {
|
||||
return idmap, nil
|
||||
}
|
||||
idmap = sortAndMergeConsecutiveMappings(idmap)
|
||||
return idmap, nil
|
||||
}
|
||||
|
||||
// Given a mapping, sort all entries by their ContainerID then and merge
|
||||
// entries that are consecutive.
|
||||
func sortAndMergeConsecutiveMappings(idmap []idtools.IDMap) (finalIDMap []idtools.IDMap) {
|
||||
idmapByCid := append([]idtools.IDMap{}, idmap...)
|
||||
sort.Slice(idmapByCid, func(i, j int) bool {
|
||||
return idmapByCid[i].ContainerID < idmapByCid[j].ContainerID
|
||||
})
|
||||
for i, mapPiece := range idmapByCid {
|
||||
if i == 0 {
|
||||
finalIDMap = append(finalIDMap, mapPiece)
|
||||
continue
|
||||
}
|
||||
lastMap := finalIDMap[len(finalIDMap)-1]
|
||||
containersMatch := lastMap.ContainerID+lastMap.Size == mapPiece.ContainerID
|
||||
hostsMatch := lastMap.HostID+lastMap.Size == mapPiece.HostID
|
||||
if containersMatch && hostsMatch {
|
||||
finalIDMap[len(finalIDMap)-1].Size += mapPiece.Size
|
||||
} else {
|
||||
finalIDMap = append(finalIDMap, mapPiece)
|
||||
}
|
||||
}
|
||||
return finalIDMap
|
||||
}
|
||||
|
||||
// ParseIDMapping takes idmappings and subuid and subgid maps and returns a storage mapping
|
||||
func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []string, subUIDMap, subGIDMap string) (*stypes.IDMappingOptions, error) {
|
||||
options := stypes.IDMappingOptions{
|
||||
@ -349,14 +877,38 @@ func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []strin
|
||||
options.UIDMap = mappings.UIDs()
|
||||
options.GIDMap = mappings.GIDs()
|
||||
}
|
||||
parsedUIDMap, err := idtools.ParseIDMap(uidMapSlice, "UID")
|
||||
|
||||
parentUIDMap, parentGIDMap, err := rootless.GetAvailableIDMaps()
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
// The kernel-provided files only exist if user namespaces are supported
|
||||
logrus.Debugf("User or group ID mappings not available: %s", err)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
parsedUIDMap, err := ParseIDMap(uidMapSlice, "UID", parentUIDMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parsedGIDMap, err := idtools.ParseIDMap(gidMapSlice, "GID")
|
||||
parsedGIDMap, err := ParseIDMap(gidMapSlice, "GID", parentGIDMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// When running rootless, if one of UID/GID mappings is provided, fill the other one:
|
||||
if rootless.IsRootless() {
|
||||
switch {
|
||||
case len(parsedUIDMap) != 0 && len(parsedGIDMap) == 0:
|
||||
availableRanges := getAvailableIDRangesFromMappings(parsedGIDMap, parentGIDMap)
|
||||
parsedGIDMap = fillIDMap(parsedGIDMap, availableRanges)
|
||||
case len(parsedUIDMap) == 0 && len(parsedGIDMap) != 0:
|
||||
availableRanges := getAvailableIDRangesFromMappings(parsedUIDMap, parentUIDMap)
|
||||
parsedUIDMap = fillIDMap(parsedUIDMap, availableRanges)
|
||||
}
|
||||
}
|
||||
|
||||
options.UIDMap = append(options.UIDMap, parsedUIDMap...)
|
||||
options.GIDMap = append(options.GIDMap, parsedGIDMap...)
|
||||
if len(options.UIDMap) > 0 {
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/containers/storage/pkg/homedir"
|
||||
"github.com/containers/storage/pkg/idtools"
|
||||
ruser "github.com/opencontainers/runc/libcontainer/user"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -15,6 +17,438 @@ var (
|
||||
sliceData = []string{"one", "two", "three", "four"}
|
||||
)
|
||||
|
||||
func BreakInsert(mapping []idtools.IDMap, extension idtools.IDMap) (result []idtools.IDMap) {
|
||||
result = breakInsert(mapping, extension)
|
||||
result = sortAndMergeConsecutiveMappings(result)
|
||||
return result
|
||||
}
|
||||
|
||||
//#####################
|
||||
|
||||
func TestBreakInsert1(t *testing.T) {
|
||||
// extension below mapping
|
||||
mapping := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 1000,
|
||||
HostID: 1000,
|
||||
Size: 1,
|
||||
},
|
||||
}
|
||||
extension := idtools.IDMap{
|
||||
ContainerID: 2,
|
||||
HostID: 2,
|
||||
Size: 1,
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 2,
|
||||
HostID: 2,
|
||||
Size: 1,
|
||||
},
|
||||
{
|
||||
ContainerID: 1000,
|
||||
HostID: 1000,
|
||||
Size: 1,
|
||||
},
|
||||
}
|
||||
result := BreakInsert(mapping, extension)
|
||||
assert.Equal(t, result, expectedResult)
|
||||
}
|
||||
|
||||
func TestBreakInsert2(t *testing.T) {
|
||||
// extension below mapping
|
||||
mapping := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 1000,
|
||||
HostID: 1001,
|
||||
Size: 2,
|
||||
},
|
||||
}
|
||||
extension := idtools.IDMap{
|
||||
ContainerID: 2,
|
||||
HostID: 3,
|
||||
Size: 2,
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 2,
|
||||
HostID: 3,
|
||||
Size: 2,
|
||||
},
|
||||
{
|
||||
ContainerID: 1000,
|
||||
HostID: 1001,
|
||||
Size: 2,
|
||||
},
|
||||
}
|
||||
result := BreakInsert(mapping, extension)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestBreakInsert3(t *testing.T) {
|
||||
// extension above mapping
|
||||
mapping := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 2,
|
||||
HostID: 3,
|
||||
Size: 2,
|
||||
},
|
||||
}
|
||||
extension := idtools.IDMap{
|
||||
ContainerID: 1000,
|
||||
HostID: 1001,
|
||||
Size: 2,
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 2,
|
||||
HostID: 3,
|
||||
Size: 2,
|
||||
},
|
||||
{
|
||||
ContainerID: 1000,
|
||||
HostID: 1001,
|
||||
Size: 2,
|
||||
},
|
||||
}
|
||||
result := BreakInsert(mapping, extension)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestBreakInsert4(t *testing.T) {
|
||||
// extension right below mapping
|
||||
mapping := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 4,
|
||||
HostID: 5,
|
||||
Size: 4,
|
||||
},
|
||||
}
|
||||
extension := idtools.IDMap{
|
||||
ContainerID: 1,
|
||||
HostID: 1,
|
||||
Size: 3,
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 1,
|
||||
HostID: 1,
|
||||
Size: 3,
|
||||
},
|
||||
{
|
||||
ContainerID: 4,
|
||||
HostID: 5,
|
||||
Size: 4,
|
||||
},
|
||||
}
|
||||
result := BreakInsert(mapping, extension)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestSortAndMergeConsecutiveMappings(t *testing.T) {
|
||||
// Extension and mapping are consecutive
|
||||
mapping := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 1,
|
||||
HostID: 1,
|
||||
Size: 3,
|
||||
},
|
||||
{
|
||||
ContainerID: 4,
|
||||
HostID: 4,
|
||||
Size: 4,
|
||||
},
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 1,
|
||||
HostID: 1,
|
||||
Size: 7,
|
||||
},
|
||||
}
|
||||
result := sortAndMergeConsecutiveMappings(mapping)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
//#####################
|
||||
|
||||
func TestParseIDMap(t *testing.T) {
|
||||
mapSpec := []string{"+100000:@1002:1"}
|
||||
|
||||
parentMapping := []ruser.IDMap{
|
||||
{
|
||||
ID: int64(20),
|
||||
ParentID: int64(1002),
|
||||
Count: 1,
|
||||
},
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 100000,
|
||||
HostID: 20,
|
||||
Size: 1,
|
||||
},
|
||||
}
|
||||
result, err := ParseIDMap(
|
||||
mapSpec,
|
||||
"UID",
|
||||
parentMapping,
|
||||
)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestParseIDMapSizeMissing(t *testing.T) {
|
||||
// Size is 1 if not provided
|
||||
mapSpec := []string{"+100000:@1002"}
|
||||
|
||||
parentMapping := []ruser.IDMap{
|
||||
{
|
||||
ID: int64(20),
|
||||
ParentID: int64(1002),
|
||||
Count: 1,
|
||||
},
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 100000,
|
||||
HostID: 20,
|
||||
Size: 1,
|
||||
},
|
||||
}
|
||||
result, err := ParseIDMap(
|
||||
mapSpec,
|
||||
"UID",
|
||||
parentMapping,
|
||||
)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestParseIDMap2(t *testing.T) {
|
||||
mapSpec := []string{"0:2000:100000", "+1:100:1"}
|
||||
parentMapping := []ruser.IDMap(nil)
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 0,
|
||||
HostID: 2000,
|
||||
Size: 1,
|
||||
},
|
||||
{
|
||||
ContainerID: 1,
|
||||
HostID: 100,
|
||||
Size: 1,
|
||||
},
|
||||
{
|
||||
ContainerID: 2,
|
||||
HostID: 2002,
|
||||
Size: 99998,
|
||||
},
|
||||
}
|
||||
result, err := ParseIDMap(
|
||||
mapSpec,
|
||||
"UID",
|
||||
parentMapping,
|
||||
)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestParseIDMap3(t *testing.T) {
|
||||
mapSpec := []string{"0:0:20", "24:24:6", "+7:1000:2", "+12:2000:3", "+18:3000:7"}
|
||||
parentMapping := []ruser.IDMap(nil)
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 0,
|
||||
HostID: 0,
|
||||
Size: 7,
|
||||
},
|
||||
{
|
||||
ContainerID: 7,
|
||||
HostID: 1000,
|
||||
Size: 2,
|
||||
},
|
||||
{
|
||||
ContainerID: 9,
|
||||
HostID: 9,
|
||||
Size: 3,
|
||||
},
|
||||
{
|
||||
ContainerID: 12,
|
||||
HostID: 2000,
|
||||
Size: 3,
|
||||
},
|
||||
{
|
||||
ContainerID: 15,
|
||||
HostID: 15,
|
||||
Size: 3,
|
||||
},
|
||||
{
|
||||
ContainerID: 18,
|
||||
HostID: 3000,
|
||||
Size: 7,
|
||||
},
|
||||
{
|
||||
ContainerID: 25,
|
||||
HostID: 25,
|
||||
Size: 5,
|
||||
},
|
||||
}
|
||||
result, err := ParseIDMap(
|
||||
mapSpec,
|
||||
"UID",
|
||||
parentMapping,
|
||||
)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestParseIDMap4(t *testing.T) {
|
||||
mapSpec := []string{"0:0:20", "+10:1:1"}
|
||||
parentMapping := []ruser.IDMap(nil)
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 0,
|
||||
HostID: 0,
|
||||
Size: 1,
|
||||
},
|
||||
{
|
||||
ContainerID: 2,
|
||||
HostID: 2,
|
||||
Size: 8,
|
||||
},
|
||||
{
|
||||
ContainerID: 10,
|
||||
HostID: 1,
|
||||
Size: 1,
|
||||
},
|
||||
{
|
||||
ContainerID: 11,
|
||||
HostID: 11,
|
||||
Size: 9,
|
||||
},
|
||||
}
|
||||
result, err := ParseIDMap(
|
||||
mapSpec,
|
||||
"UID",
|
||||
parentMapping,
|
||||
)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestParseIDMap5(t *testing.T) {
|
||||
mapSpec := []string{"0:20:10", "15:35:10", "+8:23:16"}
|
||||
parentMapping := []ruser.IDMap(nil)
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 0,
|
||||
HostID: 20,
|
||||
Size: 3,
|
||||
},
|
||||
{
|
||||
ContainerID: 8,
|
||||
HostID: 23,
|
||||
Size: 16,
|
||||
},
|
||||
{
|
||||
ContainerID: 24,
|
||||
HostID: 44,
|
||||
Size: 1,
|
||||
},
|
||||
}
|
||||
result, err := ParseIDMap(
|
||||
mapSpec,
|
||||
"UID",
|
||||
parentMapping,
|
||||
)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestParseIDMapUserGroupFlags(t *testing.T) {
|
||||
mapSpec := []string{"u1:3:1", "g2:4:2"}
|
||||
parentMapping := []ruser.IDMap(nil)
|
||||
expectedResultUser := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 1,
|
||||
HostID: 3,
|
||||
Size: 1,
|
||||
},
|
||||
}
|
||||
expectedResultGroup := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 2,
|
||||
HostID: 4,
|
||||
Size: 2,
|
||||
},
|
||||
}
|
||||
result, err := ParseIDMap(mapSpec, "UID", parentMapping)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, expectedResultUser, result)
|
||||
result, err = ParseIDMap(mapSpec, "GID", parentMapping)
|
||||
assert.Equal(t, err, nil)
|
||||
assert.Equal(t, expectedResultGroup, result)
|
||||
}
|
||||
|
||||
func TestFillIDMap(t *testing.T) {
|
||||
availableRanges := [][2]int{{0, 10}, {10000, 20000}}
|
||||
idmap := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 1,
|
||||
HostID: 1000,
|
||||
Size: 10,
|
||||
},
|
||||
{
|
||||
ContainerID: 30,
|
||||
HostID: 2000,
|
||||
Size: 20,
|
||||
},
|
||||
}
|
||||
expectedResult := []idtools.IDMap{
|
||||
{
|
||||
ContainerID: 0,
|
||||
HostID: 0,
|
||||
Size: 1,
|
||||
},
|
||||
{
|
||||
ContainerID: 1,
|
||||
HostID: 1000,
|
||||
Size: 10,
|
||||
},
|
||||
{
|
||||
ContainerID: 11,
|
||||
HostID: 1,
|
||||
Size: 9,
|
||||
},
|
||||
{
|
||||
ContainerID: 20,
|
||||
HostID: 10000,
|
||||
Size: 10,
|
||||
},
|
||||
{
|
||||
ContainerID: 30,
|
||||
HostID: 2000,
|
||||
Size: 20,
|
||||
},
|
||||
{
|
||||
ContainerID: 50,
|
||||
HostID: 10010,
|
||||
Size: 9990,
|
||||
},
|
||||
}
|
||||
result := fillIDMap(idmap, availableRanges)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestGetAvailableIDRanges(t *testing.T) {
|
||||
all := [][2]int{{0, 30}, {50, 70}}
|
||||
used := [][2]int{{2, 4}, {25, 55}}
|
||||
expectedResult := [][2]int{{0, 2}, {4, 25}, {55, 70}}
|
||||
result := getAvailableIDRanges(all, used)
|
||||
assert.Equal(t, expectedResult, result)
|
||||
}
|
||||
|
||||
func TestStringInSlice(t *testing.T) {
|
||||
// string is in the slice
|
||||
assert.True(t, StringInSlice("one", sliceData))
|
||||
|
@ -304,7 +304,7 @@ var _ = Describe("Podman UserNS support", func() {
|
||||
|
||||
It("podman --userns=container:CTR", func() {
|
||||
ctrName := "userns-ctr"
|
||||
session := podmanTest.Podman([]string{"run", "-d", "--uidmap=0:0:1", "--uidmap=1:1:4998", "--name", ctrName, "alpine", "top"})
|
||||
session := podmanTest.Podman([]string{"run", "-d", "--uidmap=0:0:1", "--uidmap=1:2:4998", "--name", ctrName, "alpine", "top"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(Exit(0))
|
||||
|
||||
|
Reference in New Issue
Block a user