-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support using /usr/bin/getsubids (
--subid-source=dynamic
)
* `--subid-source=auto` (default): Try dynamic, then fall back to static * `--subid-source=dynamic`: Execute `/usr/bin/getsubids` * `--subid-source=static`: Read `/etc/{subuid,subgid}` Fix issue 254 Signed-off-by: Akihiro Suda <[email protected]>
- Loading branch information
1 parent
1206c9e
commit b39d64e
Showing
9 changed files
with
289 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# subid sources | ||
|
||
The subid sources can be specified via the `--subid-source=(auto|dynamic|static)` flag. | ||
|
||
The `auto` source is the default since RootlessKit v1.1.0. | ||
Prior to v1.1.0, only the `static` source was supported. | ||
|
||
## Auto | ||
The `auto` source (`--subid-source=auto`) tries the `dynamic` source and fall backs to the `static` source on an error. | ||
|
||
## Dynamic | ||
The `dynamic` source (`--subid-source=dynamic`) executes the `/usr/bin/getsubids` binary to get the subids. | ||
|
||
The `getsubuids` binary is known to be available for the following distributions: | ||
- Fedora, since 35 (`dnf install shadow-utils-subid`) | ||
- Alpine, since 3.16 (`apkg install shadow-subids`) | ||
- Ubuntu, since 22.10 (`apt-get install uidmap`) | ||
|
||
The `getsubids` binary typically reads subids from `/etc/subuid` and `/etc/subgid` as in the static mode, | ||
but it can be also configured to use SSSD by specifying `subid: sss` in `/etc/nsswitch.conf`. | ||
|
||
See also https://manpages.debian.org/testing/uidmap/getsubids.1.en.html . | ||
|
||
## Static | ||
The `static` source (`--subid-source=static`) reads subids from `/etc/subuid` and `/etc/subgid`. | ||
|
||
`/etc/subuid` and `/etc/subgid` should contain more than 65536 sub-IDs. e.g. `penguin:231072:65536`. These files are automatically configured on most distributions. | ||
|
||
```console | ||
$ id -u | ||
1001 | ||
$ whoami | ||
penguin | ||
$ grep "^$(whoami):" /etc/subuid | ||
penguin:231072:65536 | ||
$ grep "^$(whoami):" /etc/subgid | ||
penguin:231072:65536 | ||
``` | ||
|
||
See also https://rootlesscontaine.rs/getting-started/common/subuid/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package dynidtools | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/rootless-containers/rootlesskit/pkg/parent/idtools" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func GetSubIDRanges(uid int, username string) ([]idtools.SubIDRange, []idtools.SubIDRange, error) { | ||
getsubidsExeName := "getsubids" | ||
if v := os.Getenv("GETSUBIDS"); v != "" { | ||
getsubidsExeName = v | ||
} | ||
getsubidsExe, err := exec.LookPath(getsubidsExeName) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("subid-source:dynamic: %w", err) | ||
} | ||
|
||
uByUsername, uByUsernameErr := execGetsubids(getsubidsExe, false, username) | ||
uByUID, uByUIDErr := execGetsubids(getsubidsExe, false, strconv.Itoa(uid)) | ||
// Typically, uByUsernameErr == nil, uByUIDErr == "Error fetching ranges" (exit code 1) | ||
if uByUsernameErr != nil { | ||
logrus.WithError(uByUsernameErr).Debugf("subid-source:dynamic: failed to get subuids by the username %q", username) | ||
} | ||
if uByUIDErr != nil { | ||
logrus.WithError(uByUIDErr).Debugf("subid-source:dynamic: failed to get subuids by the UID %d", uid) | ||
if uByUsernameErr != nil { | ||
return nil, nil, fmt.Errorf("subid-source:dynamic: failed to get subuids by the username %q: %w; also failed to get subuids by the UID %d: %v", | ||
username, uByUsernameErr, uid, uByUIDErr) | ||
} | ||
} | ||
|
||
gByUsername, gByUsernameErr := execGetsubids(getsubidsExe, true, username) | ||
gByUID, gByUIDErr := execGetsubids(getsubidsExe, true, strconv.Itoa(uid)) | ||
// Typically, gByUsernameErr == nil, gByUIDErr == "Error fetching ranges" (exit code 1) | ||
if gByUsernameErr != nil { | ||
logrus.WithError(gByUsernameErr).Debugf("subid-source:dynamic: failed to get subgids by the username %q", username) | ||
} | ||
if gByUIDErr != nil { | ||
logrus.WithError(gByUIDErr).Debugf("subid-source:dynamic: failed to get subgids by the UID %d", uid) | ||
if gByUsernameErr != nil { | ||
return nil, nil, fmt.Errorf("subid-source:dynamic: failed to get subgids by the username %q: %w; also failed to get subuids by the UID %d: %v", | ||
username, gByUsernameErr, uid, gByUIDErr) | ||
} | ||
} | ||
|
||
u := append(uByUsername, uByUID...) | ||
g := append(gByUsername, gByUID...) | ||
return u, g, nil | ||
} | ||
|
||
// execGetsubids executes `getsubids [-g] user` | ||
func execGetsubids(exe string, g bool, s string) ([]idtools.SubIDRange, error) { | ||
var args []string | ||
if g { | ||
args = append(args, "-g") | ||
} | ||
var stderr bytes.Buffer | ||
args = append(args, s) | ||
cmd := exec.Command(exe, args...) | ||
cmd.Stderr = &stderr | ||
logrus.Debugf("Executing %v", cmd.Args) | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to exec %v: %w (stdout=%q, stderr=%q)", cmd.Args, err, string(out), stderr.String()) | ||
} | ||
r := bytes.NewReader(out) | ||
ranges, warns, err := parseGetsubidsOutput(r) | ||
for _, warn := range warns { | ||
logrus.Warnf("Error while parsing the result of %v: %s (stdout=%q, stderr=%q)", cmd.Args, warn, string(out), stderr.String()) | ||
} | ||
return ranges, err | ||
} | ||
|
||
func parseGetsubidsOutput(r io.Reader) (res []idtools.SubIDRange, warns []string, err error) { | ||
sc := bufio.NewScanner(r) | ||
for i := 0; sc.Scan(); i++ { | ||
line := strings.TrimSpace(sc.Text()) | ||
// line is like "0: foo 100000 655360" | ||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
splitByColon := strings.Split(line, ":") | ||
switch len(splitByColon) { | ||
case 0, 1: | ||
return res, warns, fmt.Errorf("line %d: unparsable line %q", i+1, line) | ||
case 2: | ||
// NOP | ||
default: | ||
warns = append(warns, fmt.Sprintf("line %d: line %q contains unknown fields", i+1, line)) | ||
} | ||
triplet := strings.Fields(strings.TrimSpace(splitByColon[1])) | ||
switch len(triplet) { | ||
case 0, 1, 2: | ||
return res, warns, fmt.Errorf("line %d: unparsable line %q", i+1, line) | ||
case 3: | ||
// NOP | ||
default: | ||
warns = append(warns, fmt.Sprintf("line %d: line %q contains unknown fields", i+1, line)) | ||
} | ||
var entry idtools.SubIDRange | ||
entry.Start, err = strconv.Atoi(triplet[1]) | ||
if err != nil { | ||
return res, warns, fmt.Errorf("line %d: unparsable line %q: failed to Atoi(%q): %w", i+1, line, triplet[1], err) | ||
} | ||
entry.Length, err = strconv.Atoi(triplet[2]) | ||
if err != nil { | ||
return res, warns, fmt.Errorf("line %d: unparsable line %q: failed to Atoi(%q): %w", i+1, line, triplet[2], err) | ||
} | ||
res = append(res, entry) | ||
} | ||
err = sc.Err() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dynidtools | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/rootless-containers/rootlesskit/pkg/parent/idtools" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestParseGetsubidsOutput(t *testing.T) { | ||
const s = `# foo | ||
0: foo 100000 655360 | ||
` | ||
expected := []idtools.SubIDRange{ | ||
{ | ||
Start: 100000, | ||
Length: 655360, | ||
}, | ||
} | ||
got, warn, err := parseGetsubidsOutput(strings.NewReader(s)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, 0, len(warn)) | ||
assert.DeepEqual(t, expected, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.