2018-04-25 06:17:46 +08:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load helpers
|
|
|
|
|
2019-08-09 21:22:38 +08:00
|
|
|
@test "already-in-userns" {
|
|
|
|
if test "$BUILDAH_ISOLATION" != "rootless" -o $UID == 0 ; then
|
|
|
|
skip "BUILDAH_ISOLATION = $BUILDAH_ISOLATION"
|
|
|
|
fi
|
|
|
|
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch alpine
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from $WITH_POLICY_JSON --quiet alpine
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "alpine-working-container"
|
2019-08-09 21:22:38 +08:00
|
|
|
ctr="$output"
|
|
|
|
|
|
|
|
run_buildah unshare buildah run --isolation=oci "$ctr" echo hello
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "hello"
|
2019-08-09 21:22:38 +08:00
|
|
|
}
|
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
@test "user-and-network-namespace" {
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
|
|
|
|
2022-04-26 23:09:11 +08:00
|
|
|
mkdir -p $TEST_SCRATCH_DIR/no-cni-configs
|
|
|
|
RUNOPTS="--cni-config-dir=${TEST_SCRATCH_DIR}/no-cni-configs ${RUNC_BINARY:+--runtime $RUNC_BINARY}"
|
2018-04-25 06:17:46 +08:00
|
|
|
# Check if we're running in an environment that can even test this.
|
|
|
|
run readlink /proc/self/ns/user
|
2019-12-12 07:21:51 +08:00
|
|
|
echo "readlink /proc/self/ns/user -> $output"
|
2018-04-25 06:17:46 +08:00
|
|
|
[ $status -eq 0 ] || skip "user namespaces not supported"
|
|
|
|
run readlink /proc/self/ns/net
|
2019-12-12 07:21:51 +08:00
|
|
|
echo "readlink /proc/self/ns/net -> $output"
|
2018-04-25 06:17:46 +08:00
|
|
|
[ $status -eq 0 ] || skip "network namespaces not supported"
|
|
|
|
mynetns="$output"
|
|
|
|
|
|
|
|
# Generate the mappings to use for using-a-user-namespace cases.
|
|
|
|
uidbase=$((${RANDOM}+1024))
|
|
|
|
gidbase=$((${RANDOM}+1024))
|
|
|
|
uidsize=$((${RANDOM}+1024))
|
|
|
|
gidsize=$((${RANDOM}+1024))
|
|
|
|
|
|
|
|
# Create a container that uses that mapping.
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch alpine
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from $WITH_POLICY_JSON --quiet --userns-uid-map 0:$uidbase:$uidsize --userns-gid-map 0:$gidbase:$gidsize alpine
|
2018-04-25 06:17:46 +08:00
|
|
|
ctr="$output"
|
|
|
|
|
|
|
|
# Check that with settings that require a user namespace, we also get a new network namespace by default.
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" readlink /proc/self/ns/net
|
2021-05-14 05:08:35 +08:00
|
|
|
assert "$output" != "$mynetns" "we should get a new network namespace"
|
2018-04-25 06:17:46 +08:00
|
|
|
|
|
|
|
# Check that with settings that require a user namespace, we can still try to use the host's network namespace.
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS --net=host "$ctr" readlink /proc/self/ns/net
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "$mynetns"
|
2018-04-25 06:17:46 +08:00
|
|
|
|
2021-11-04 20:45:40 +08:00
|
|
|
# Check that we are not bind mounting /sys from the host with --net=container
|
|
|
|
host_sys=$(grep "/sys " /proc/self/mountinfo | cut -d ' ' -f 3)
|
|
|
|
run_buildah run $RUNOPTS --net=container "$ctr" sh -c 'grep "/sys " /proc/self/mountinfo | cut -d " " -f 3'
|
|
|
|
assert "$output" != "$host_sys"
|
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
# Create a container that doesn't use that mapping.
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from $WITH_POLICY_JSON --quiet alpine
|
2018-04-25 06:17:46 +08:00
|
|
|
ctr="$output"
|
|
|
|
|
2021-11-19 03:09:29 +08:00
|
|
|
run_buildah run $RUNOPTS --net=host "$ctr" readlink /proc/self/ns/net
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "$mynetns"
|
2018-04-25 06:17:46 +08:00
|
|
|
|
|
|
|
# Check that with settings that don't require a user namespace, we can request to use a per-container network namespace.
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS --net=container "$ctr" readlink /proc/self/ns/net
|
2021-05-14 05:08:35 +08:00
|
|
|
assert "$output" != "$mynetns" \
|
|
|
|
"[/proc/self/ns/net (--net=container) should not be '$mynetns']"
|
2020-08-04 22:13:17 +08:00
|
|
|
|
|
|
|
run_buildah run $RUNOPTS --net=private "$ctr" readlink /proc/self/ns/net
|
2021-05-14 05:08:35 +08:00
|
|
|
assert "$output" != "$mynetns" \
|
|
|
|
"[/proc/self/ns/net (--net=private) should not be '$mynetns']"
|
2021-11-19 03:09:29 +08:00
|
|
|
|
|
|
|
run_buildah run $RUNOPTS "$ctr" readlink /proc/self/ns/net
|
|
|
|
assert "$output" != "$mynetns" \
|
|
|
|
"[/proc/self/ns/net (--net="") should not be '$mynetns']"
|
2018-04-25 06:17:46 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 00:51:47 +08:00
|
|
|
# Helper for idmapping test: check UID or GID mapping
|
|
|
|
# NOTE SIDE EFFECT: sets $rootxid for possible use by caller
|
|
|
|
idmapping_check_map() {
|
|
|
|
local _output_idmap=$1
|
|
|
|
local _expect_idmap=$2
|
|
|
|
local _testname=$3
|
2021-04-27 09:25:01 +08:00
|
|
|
|
2021-04-28 00:51:47 +08:00
|
|
|
[ -n "$_output_idmap" ]
|
|
|
|
local _idmap=$(sed -E -e 's, +, ,g' -e 's,^ +,,g' <<< "${_output_idmap}")
|
|
|
|
expect_output --from="$_idmap" "${_expect_idmap}" "$_testname"
|
2021-04-27 09:25:01 +08:00
|
|
|
|
2021-04-28 00:51:47 +08:00
|
|
|
# SIDE EFFECT: Global: our caller may want this
|
|
|
|
rootxid=$(sed -E -e 's,^([^ ]*) (.*) ([^ ]*),\2,' <<< "$_idmap")
|
2021-04-27 09:25:01 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 00:51:47 +08:00
|
|
|
# Helper for idmapping test: check file permissions
|
2021-04-27 09:25:01 +08:00
|
|
|
idmapping_check_permission() {
|
|
|
|
local _output_file_stat=$1
|
|
|
|
local _output_dir_stat=$2
|
|
|
|
|
|
|
|
expect_output --from="${_output_file_stat}" "1:1" "Check if a copied file gets the right permissions"
|
|
|
|
expect_output --from="${_output_dir_stat}" "0:0" "Check if a copied directory gets the right permissions"
|
|
|
|
}
|
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
@test "idmapping" {
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2022-04-26 23:09:11 +08:00
|
|
|
mkdir -p $TEST_SCRATCH_DIR/no-cni-configs
|
|
|
|
RUNOPTS="--cni-config-dir=${TEST_SCRATCH_DIR}/no-cni-configs ${RUNC_BINARY:+--runtime $RUNC_BINARY}"
|
2018-04-25 06:17:46 +08:00
|
|
|
|
|
|
|
# Check if we're running in an environment that can even test this.
|
|
|
|
run readlink /proc/self/ns/user
|
2019-12-12 07:21:51 +08:00
|
|
|
echo "readlink /proc/self/ns/user -> $output"
|
2018-04-25 06:17:46 +08:00
|
|
|
[ $status -eq 0 ] || skip "user namespaces not supported"
|
|
|
|
mynamespace="$output"
|
|
|
|
|
|
|
|
# Generate the mappings to use.
|
|
|
|
uidbase=$((${RANDOM}+1024))
|
|
|
|
gidbase=$((${RANDOM}+1024))
|
|
|
|
uidsize=$((${RANDOM}+1024))
|
|
|
|
gidsize=$((${RANDOM}+1024))
|
|
|
|
# Test with no mappings.
|
2018-10-05 01:46:34 +08:00
|
|
|
uidmapargs[0]=
|
|
|
|
gidmapargs[0]=
|
2018-04-25 06:17:46 +08:00
|
|
|
uidmaps[0]="0 0 4294967295"
|
|
|
|
gidmaps[0]="0 0 4294967295"
|
|
|
|
# Test with both UID and GID maps specified.
|
2018-10-05 01:46:34 +08:00
|
|
|
uidmapargs[1]="--userns-uid-map=0:$uidbase:$uidsize"
|
|
|
|
gidmapargs[1]="--userns-gid-map=0:$gidbase:$gidsize"
|
2018-04-25 06:17:46 +08:00
|
|
|
uidmaps[1]="0 $uidbase $uidsize"
|
|
|
|
gidmaps[1]="0 $gidbase $gidsize"
|
|
|
|
# Conditionalize some tests on the subuid and subgid files being present.
|
|
|
|
if test -s /etc/subuid ; then
|
|
|
|
if test -s /etc/subgid ; then
|
|
|
|
# Look for a name that's in both the subuid and subgid files.
|
|
|
|
for candidate in $(sed -e 's,:.*,,g' /etc/subuid); do
|
|
|
|
if test $(sed -e 's,:.*,,g' -e "/$candidate/!d" /etc/subgid) == "$candidate"; then
|
|
|
|
# Read the start of the subuid/subgid ranges. Assume length=65536.
|
|
|
|
userbase=$(sed -e "/^${candidate}:/!d" -e 's,^[^:]*:,,g' -e 's,:[^:]*,,g' /etc/subuid)
|
|
|
|
groupbase=$(sed -e "/^${candidate}:/!d" -e 's,^[^:]*:,,g' -e 's,:[^:]*,,g' /etc/subgid)
|
|
|
|
# Test specifying both the user and group names.
|
2018-10-05 01:46:34 +08:00
|
|
|
uidmapargs[${#uidmaps[*]}]=--userns-uid-map-user=$candidate
|
|
|
|
gidmapargs[${#gidmaps[*]}]=--userns-gid-map-group=$candidate
|
2018-04-25 06:17:46 +08:00
|
|
|
uidmaps[${#uidmaps[*]}]="0 $userbase 65536"
|
|
|
|
gidmaps[${#gidmaps[*]}]="0 $groupbase 65536"
|
|
|
|
# Test specifying just the user name.
|
2018-10-05 01:46:34 +08:00
|
|
|
uidmapargs[${#uidmaps[*]}]=--userns-uid-map-user=$candidate
|
2018-04-25 06:17:46 +08:00
|
|
|
uidmaps[${#uidmaps[*]}]="0 $userbase 65536"
|
|
|
|
gidmaps[${#gidmaps[*]}]="0 $groupbase 65536"
|
|
|
|
# Test specifying just the group name.
|
2018-10-05 01:46:34 +08:00
|
|
|
gidmapargs[${#gidmaps[*]}]=--userns-gid-map-group=$candidate
|
2018-04-25 06:17:46 +08:00
|
|
|
uidmaps[${#uidmaps[*]}]="0 $userbase 65536"
|
|
|
|
gidmaps[${#gidmaps[*]}]="0 $groupbase 65536"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# Choose different names from the files.
|
|
|
|
for candidateuser in $(sed -e 's,:.*,,g' /etc/subuid); do
|
|
|
|
for candidategroup in $(sed -e 's,:.*,,g' /etc/subgid); do
|
|
|
|
if test "$candidateuser" == "$candidate" ; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if test "$candidategroup" == "$candidate" ; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if test "$candidateuser" == "$candidategroup" ; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
# Read the start of the ranges. Assume length=65536.
|
|
|
|
userbase=$(sed -e "/^${candidateuser}:/!d" -e 's,^[^:]*:,,g' -e 's,:[^:]*,,g' /etc/subuid)
|
|
|
|
groupbase=$(sed -e "/^${candidategroup}:/!d" -e 's,^[^:]*:,,g' -e 's,:[^:]*,,g' /etc/subgid)
|
|
|
|
# Test specifying both the user and group names.
|
2018-10-05 01:46:34 +08:00
|
|
|
uidmapargs[${#uidmaps[*]}]=--userns-uid-map-user=$candidateuser
|
|
|
|
gidmapargs[${#gidmaps[*]}]=--userns-gid-map-group=$candidategroup
|
2018-04-25 06:17:46 +08:00
|
|
|
uidmaps[${#uidmaps[*]}]="0 $userbase 65536"
|
|
|
|
gidmaps[${#gidmaps[*]}]="0 $groupbase 65536"
|
|
|
|
break
|
|
|
|
done
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-04-26 23:09:11 +08:00
|
|
|
touch ${TEST_SCRATCH_DIR}/somefile
|
|
|
|
mkdir ${TEST_SCRATCH_DIR}/somedir
|
|
|
|
touch ${TEST_SCRATCH_DIR}/somedir/someotherfile
|
|
|
|
chmod 700 ${TEST_SCRATCH_DIR}/somedir/someotherfile
|
|
|
|
chmod u+s ${TEST_SCRATCH_DIR}/somedir/someotherfile
|
2018-04-25 06:17:46 +08:00
|
|
|
|
2020-10-06 13:13:09 +08:00
|
|
|
for i in $(seq 0 "$((${#uidmaps[*]}-1))") ; do
|
2021-04-28 00:51:47 +08:00
|
|
|
# local helper function for checking /proc/self/ns/user
|
|
|
|
function idmapping_check_namespace() {
|
|
|
|
local _output=$1
|
|
|
|
local _testname=$2
|
|
|
|
|
|
|
|
[ "$_output" != "" ]
|
|
|
|
if [ -z "${uidmapargs[$i]}${gidmapargs[$i]}" ]; then
|
|
|
|
if test "$BUILDAH_ISOLATION" != "chroot" -a "$BUILDAH_ISOLATION" != "rootless" ; then
|
|
|
|
expect_output --from="$_output" "$mynamespace" "/proc/self/ns/user ($_testname)"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
[ "$_output" != "$mynamespace" ]
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
# Create a container using these mappings.
|
2022-04-26 21:47:03 +08:00
|
|
|
echo "Building container with $WITH_POLICY_JSON --quiet ${uidmapargs[$i]} ${gidmapargs[$i]} alpine"
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch alpine
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from $WITH_POLICY_JSON --quiet ${uidmapargs[$i]} ${gidmapargs[$i]} alpine
|
2018-04-25 06:17:46 +08:00
|
|
|
ctr="$output"
|
|
|
|
|
|
|
|
# If we specified mappings, expect to be in a different namespace by default.
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" readlink /proc/self/ns/user
|
2021-04-28 00:51:47 +08:00
|
|
|
idmapping_check_namespace "$output" "container"
|
|
|
|
# Check that we got the UID and GID mappings that we expected.
|
|
|
|
# rootuid/rootgid are obtained (side effect) from helper function
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" cat /proc/self/uid_map
|
2021-04-28 00:51:47 +08:00
|
|
|
idmapping_check_map "$output" "${uidmaps[$i]}" "uid_map"
|
|
|
|
rootuid=$rootxid
|
|
|
|
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" cat /proc/self/gid_map
|
2021-04-28 00:51:47 +08:00
|
|
|
idmapping_check_map "$output" "${gidmaps[$i]}" "gid_map"
|
|
|
|
rootgid=$rootxid
|
2018-04-25 06:17:46 +08:00
|
|
|
|
|
|
|
# Check that if we copy a file into the container, it gets the right permissions.
|
2022-04-26 23:09:11 +08:00
|
|
|
run_buildah copy --chown 1:1 "$ctr" ${TEST_SCRATCH_DIR}/somefile /
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" stat -c '%u:%g' /somefile
|
2021-04-27 09:25:01 +08:00
|
|
|
output_file_stat="$output"
|
2018-04-25 06:17:46 +08:00
|
|
|
# Check that if we copy a directory into the container, its contents get the right permissions.
|
2022-04-26 23:09:11 +08:00
|
|
|
run_buildah copy "$ctr" ${TEST_SCRATCH_DIR}/somedir /somedir
|
2019-12-12 04:03:37 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" stat -c '%u:%g' /somedir
|
2021-04-27 09:25:01 +08:00
|
|
|
output_dir_stat="$output"
|
2021-04-28 00:51:47 +08:00
|
|
|
idmapping_check_permission "$output_file_stat" "$output_dir_stat"
|
|
|
|
|
2021-04-27 09:25:01 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" stat -c '%u:%g %a' /somedir/someotherfile
|
2021-04-28 00:51:47 +08:00
|
|
|
expect_output "0:0 4700" "stat(someotherfile), in container test"
|
2021-04-27 09:25:01 +08:00
|
|
|
|
|
|
|
# Check that the copied file has the right permissions on host.
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah mount "$ctr"
|
2018-04-25 06:17:46 +08:00
|
|
|
mnt="$output"
|
|
|
|
run stat -c '%u:%g %a' "$mnt"/somedir/someotherfile
|
|
|
|
[ $status -eq 0 ]
|
2019-04-05 23:59:54 +08:00
|
|
|
expect_output "$rootuid:$rootgid 4700"
|
2020-11-12 15:18:57 +08:00
|
|
|
|
|
|
|
# Check that a container with mapped-layer can be committed.
|
|
|
|
run_buildah commit "$ctr" localhost/alpine-working:$i
|
2021-04-22 05:19:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Also test bud command
|
|
|
|
# Build an image using these mappings.
|
|
|
|
echo "Building image with ${uidmapargs[$i]} ${gidmapargs[$i]}"
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah bud ${uidmapargs[$i]} ${gidmapargs[$i]} $RUNOPTS $WITH_POLICY_JSON \
|
2022-04-26 23:09:11 +08:00
|
|
|
-t localhost/alpine-bud:$i -f $BUDFILES/namespaces/Containerfile $TEST_SCRATCH_DIR
|
2021-04-22 05:19:49 +08:00
|
|
|
# If we specified mappings, expect to be in a different namespace by default.
|
2021-04-27 09:25:01 +08:00
|
|
|
output_namespace="$(grep -A1 'ReadlinkResult' <<< "$output" | tail -n1)"
|
2021-04-28 00:51:47 +08:00
|
|
|
idmapping_check_namespace "${output_namespace}" "bud"
|
2021-04-22 05:19:49 +08:00
|
|
|
# Check that we got the mappings that we expected.
|
2021-04-27 09:25:01 +08:00
|
|
|
output_uidmap="$(grep -A1 'UidMapResult' <<< "$output" | tail -n1)"
|
|
|
|
output_gidmap="$(grep -A1 'GidMapResult' <<< "$output" | tail -n1)"
|
2021-04-28 00:51:47 +08:00
|
|
|
idmapping_check_map "$output_uidmap" "${uidmaps[$i]}" "UidMapResult"
|
|
|
|
idmapping_check_map "$output_gidmap" "${gidmaps[$i]}" "GidMapResult"
|
2021-04-27 09:25:01 +08:00
|
|
|
|
2021-04-22 05:19:49 +08:00
|
|
|
# Check that if we copy a file into the container, it gets the right permissions.
|
2021-04-27 09:25:01 +08:00
|
|
|
output_file_stat="$(grep -A1 'StatSomefileResult' <<< "$output" | tail -n1)"
|
2021-04-22 05:19:49 +08:00
|
|
|
# Check that if we copy a directory into the container, its contents get the right permissions.
|
2021-04-27 09:25:01 +08:00
|
|
|
output_dir_stat="$(grep -A1 'StatSomedirResult' <<< "$output" | tail -n1)"
|
|
|
|
output_otherfile_stat="$(grep -A1 'StatSomeotherfileResult' <<< "$output" | tail -n1)"
|
2022-03-23 18:37:57 +08:00
|
|
|
output_workdir_stat="$(grep -A1 'StatNewWorkdir' <<< "$output" | tail -n1)"
|
2021-04-22 05:19:49 +08:00
|
|
|
# bud strips suid.
|
2021-04-28 00:51:47 +08:00
|
|
|
idmapping_check_permission "$output_file_stat" "$output_dir_stat"
|
|
|
|
expect_output --from="${output_otherfile_stat}" "0:0 700" "stat(someotherfile), in bud test"
|
2022-03-23 18:37:57 +08:00
|
|
|
expect_output --from="${output_workdir_stat}" "guest:users" "stat(new-workdir), in bud test"
|
2018-04-25 06:17:46 +08:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
general_namespace() {
|
2022-04-26 23:09:11 +08:00
|
|
|
mkdir -p $TEST_SCRATCH_DIR/no-cni-configs
|
|
|
|
RUNOPTS="--cni-config-dir=${TEST_SCRATCH_DIR}/no-cni-configs ${RUNC_BINARY:+--runtime $RUNC_BINARY}"
|
|
|
|
mytmpdir=$TEST_SCRATCH_DIR/my-dir
|
2021-04-22 05:19:49 +08:00
|
|
|
mkdir -p ${mytmpdir}
|
2018-04-25 06:17:46 +08:00
|
|
|
|
|
|
|
# The name of the /proc/self/ns/$link.
|
|
|
|
nstype="$1"
|
|
|
|
# The flag to use, if it's not the same as the namespace name.
|
|
|
|
nsflag="${2:-$1}"
|
|
|
|
|
|
|
|
# Check if we're running in an environment that can even test this.
|
|
|
|
run readlink /proc/self/ns/"$nstype"
|
2019-12-12 07:21:51 +08:00
|
|
|
echo "readlink /proc/self/ns/$nstype -> $output"
|
2018-04-25 06:17:46 +08:00
|
|
|
[ $status -eq 0 ] || skip "$nstype namespaces not supported"
|
|
|
|
mynamespace="$output"
|
|
|
|
|
|
|
|
# Settings to test.
|
|
|
|
types[0]=
|
|
|
|
types[1]=container
|
|
|
|
types[2]=host
|
|
|
|
types[3]=/proc/$$/ns/$nstype
|
2020-08-04 22:13:17 +08:00
|
|
|
types[4]=private
|
|
|
|
types[5]=ns:/proc/$$/ns/$nstype
|
2018-04-25 06:17:46 +08:00
|
|
|
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch alpine
|
2018-04-25 06:17:46 +08:00
|
|
|
for namespace in "${types[@]}" ; do
|
|
|
|
# Specify the setting for this namespace for this container.
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from $WITH_POLICY_JSON --quiet --"$nsflag"=$namespace alpine
|
2018-04-25 06:17:46 +08:00
|
|
|
[ "$output" != "" ]
|
|
|
|
ctr="$output"
|
|
|
|
|
|
|
|
# Check that, unless we override it, we get that setting in "run".
|
2019-12-12 02:28:27 +08:00
|
|
|
run_buildah run $RUNOPTS "$ctr" readlink /proc/self/ns/"$nstype"
|
2018-04-25 06:17:46 +08:00
|
|
|
[ "$output" != "" ]
|
|
|
|
case "$namespace" in
|
2020-08-04 22:13:17 +08:00
|
|
|
""|container|private)
|
2018-04-25 06:17:46 +08:00
|
|
|
[ "$output" != "$mynamespace" ]
|
|
|
|
;;
|
|
|
|
host)
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "$mynamespace"
|
2018-04-25 06:17:46 +08:00
|
|
|
;;
|
|
|
|
/*)
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "$(readlink $namespace)"
|
2018-04-25 06:17:46 +08:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-04-22 05:19:49 +08:00
|
|
|
# "run" doesn't have --userns option.
|
|
|
|
if [ "$nsflag" != "userns" ]; then
|
|
|
|
for different in ${types[@]} ; do
|
|
|
|
# Check that, if we override it, we get what we specify for "run".
|
|
|
|
run_buildah run $RUNOPTS --"$nsflag"=$different "$ctr" readlink /proc/self/ns/"$nstype"
|
|
|
|
[ "$output" != "" ]
|
|
|
|
case "$different" in
|
|
|
|
""|container|private)
|
|
|
|
[ "$output" != "$mynamespace" ]
|
|
|
|
;;
|
|
|
|
host)
|
|
|
|
expect_output "$mynamespace"
|
|
|
|
;;
|
|
|
|
/*)
|
|
|
|
expect_output "$(readlink $different)"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2021-04-02 05:16:05 +08:00
|
|
|
fi
|
|
|
|
|
2021-04-22 05:19:49 +08:00
|
|
|
# Also check "from" command
|
|
|
|
cat > $mytmpdir/Containerfile << _EOF
|
|
|
|
FROM alpine
|
|
|
|
RUN echo "TargetOutput" && readlink /proc/self/ns/$nstype
|
|
|
|
_EOF
|
2022-07-07 13:43:56 +08:00
|
|
|
run_buildah bud --"$nsflag"=$namespace $RUNOPTS $WITH_POLICY_JSON --file ${mytmpdir}/Containerfile .
|
2021-04-22 05:19:49 +08:00
|
|
|
result=$(grep -A1 "TargetOutput" <<< "$output" | tail -n1)
|
|
|
|
case "$namespace" in
|
|
|
|
""|container|private)
|
|
|
|
[ "$result" != "$mynamespace" ]
|
|
|
|
;;
|
|
|
|
host)
|
|
|
|
expect_output --from="$result" "$mynamespace"
|
|
|
|
;;
|
|
|
|
/*)
|
|
|
|
expect_output --from="$result" "$(readlink $namespace)"
|
|
|
|
;;
|
|
|
|
esac
|
2018-04-25 06:17:46 +08:00
|
|
|
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "ipc-namespace" {
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2019-11-06 02:22:07 +08:00
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
general_namespace ipc
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "net-namespace" {
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2019-11-06 02:22:07 +08:00
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
general_namespace net
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "network-namespace" {
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2019-11-06 02:22:07 +08:00
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
general_namespace net network
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "pid-namespace" {
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2019-11-06 02:22:07 +08:00
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
general_namespace pid
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "user-namespace" {
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2019-11-06 02:22:07 +08:00
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
general_namespace user userns
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "uts-namespace" {
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2019-11-06 02:22:07 +08:00
|
|
|
|
2018-04-25 06:17:46 +08:00
|
|
|
general_namespace uts
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "combination-namespaces" {
|
2019-11-06 02:22:07 +08:00
|
|
|
skip_if_chroot
|
|
|
|
skip_if_rootless
|
|
|
|
|
2019-12-09 21:45:52 +08:00
|
|
|
_prefetch alpine
|
2020-04-28 02:12:30 +08:00
|
|
|
# mnt is always per-container, cgroup isn't a thing OCI runtime lets us configure
|
2022-02-21 21:20:59 +08:00
|
|
|
for ipc in host private; do
|
|
|
|
for net in host private; do
|
|
|
|
for pid in host private; do
|
|
|
|
for userns in host private; do
|
|
|
|
for uts in host private; do
|
|
|
|
for cgroupns in host private; do
|
|
|
|
|
2022-04-26 21:47:03 +08:00
|
|
|
echo "buildah from $WITH_POLICY_JSON --ipc=$ipc --net=$net --pid=$pid --userns=$userns --uts=$uts --cgroupns=$cgroupns alpine"
|
|
|
|
run_buildah from $WITH_POLICY_JSON --quiet --ipc=$ipc --net=$net --pid=$pid --userns=$userns --uts=$uts --cgroupns=$cgroupns alpine
|
2021-11-19 04:20:50 +08:00
|
|
|
[ "$output" != "" ]
|
|
|
|
ctr="$output"
|
|
|
|
run_buildah run $ctr pwd
|
|
|
|
[ "$output" != "" ]
|
|
|
|
run_buildah run --tty=true $ctr pwd
|
|
|
|
[ "$output" != "" ]
|
|
|
|
run_buildah run --terminal=false $ctr pwd
|
|
|
|
[ "$output" != "" ]
|
2022-02-21 21:20:59 +08:00
|
|
|
done
|
2018-04-25 06:17:46 +08:00
|
|
|
done
|
|
|
|
done
|
|
|
|
done
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "idmapping-and-squash" {
|
2022-03-04 18:38:38 +08:00
|
|
|
skip_if_rootless_environment
|
2022-04-26 23:09:11 +08:00
|
|
|
createrandom ${TEST_SCRATCH_DIR}/randomfile
|
2019-12-12 03:11:08 +08:00
|
|
|
run_buildah from --userns-uid-map 0:32:16 --userns-gid-map 0:48:16 scratch
|
|
|
|
cid=$output
|
2022-04-26 23:09:11 +08:00
|
|
|
run_buildah copy "$cid" ${TEST_SCRATCH_DIR}/randomfile /
|
|
|
|
run_buildah copy --chown 1:1 "$cid" ${TEST_SCRATCH_DIR}/randomfile /randomfile2
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah commit --squash $WITH_POLICY_JSON --rm "$cid" squashed
|
2019-12-12 03:11:08 +08:00
|
|
|
run_buildah from --quiet squashed
|
|
|
|
cid=$output
|
|
|
|
run_buildah mount $cid
|
|
|
|
mountpoint=$output
|
2018-04-25 06:17:46 +08:00
|
|
|
run stat -c %u:%g $mountpoint/randomfile
|
|
|
|
[ "$status" -eq 0 ]
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "0:0"
|
|
|
|
|
2019-05-30 12:22:53 +08:00
|
|
|
run stat -c %u:%g $mountpoint/randomfile2
|
|
|
|
[ "$status" -eq 0 ]
|
2019-12-12 07:21:51 +08:00
|
|
|
expect_output "1:1"
|
2018-04-25 06:17:46 +08:00
|
|
|
}
|
2020-10-08 08:28:34 +08:00
|
|
|
|
|
|
|
@test "invalid userns-uid-map userns-gid-map" {
|
|
|
|
run_buildah 125 from --userns-uid-map 16 --userns-gid-map 0:48:16 scratch
|
2022-09-18 18:36:08 +08:00
|
|
|
expect_output 'Error: initializing ID mappings: userns-uid-map setting is malformed expected ["uint32:uint32:uint32"]: ["16"]'
|
2020-10-08 08:28:34 +08:00
|
|
|
|
|
|
|
run_buildah 125 from --userns-uid-map 0:32:16 --userns-gid-map 16 scratch
|
2022-09-18 18:36:08 +08:00
|
|
|
expect_output 'Error: initializing ID mappings: userns-gid-map setting is malformed expected ["uint32:uint32:uint32"]: ["16"]'
|
2020-10-08 08:28:34 +08:00
|
|
|
|
|
|
|
run_buildah 125 bud --userns-uid-map a --userns-gid-map bogus bud/from-scratch
|
2022-09-18 18:36:08 +08:00
|
|
|
expect_output 'Error: initializing ID mappings: userns-uid-map setting is malformed expected ["uint32:uint32:uint32"]: ["a"]'
|
2020-10-08 08:28:34 +08:00
|
|
|
|
|
|
|
run_buildah 125 bud --userns-uid-map 0:32:16 --userns-gid-map bogus bud/from-scratch
|
2022-09-18 18:36:08 +08:00
|
|
|
expect_output 'Error: initializing ID mappings: userns-gid-map setting is malformed expected ["uint32:uint32:uint32"]: ["bogus"]'
|
2020-10-08 08:28:34 +08:00
|
|
|
|
|
|
|
run_buildah from --userns-uid-map 0:32:16 scratch
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "idmapping-syntax" {
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah from $WITH_POLICY_JSON --quiet --userns-uid-map=0:10000:65536 alpine
|
2020-10-08 08:28:34 +08:00
|
|
|
|
2022-04-26 21:47:03 +08:00
|
|
|
run_buildah 125 from $WITH_POLICY_JSON --quiet --userns-gid-map=0:10000:65536 alpine
|
2020-10-08 08:28:34 +08:00
|
|
|
expect_output --substring "userns-gid-map can not be used without --userns-uid-map"
|
|
|
|
}
|
2022-03-23 02:14:06 +08:00
|
|
|
|
|
|
|
@test "use containers.conf namespace settings" {
|
|
|
|
skip_if_chroot
|
|
|
|
|
|
|
|
_prefetch alpine
|
2022-04-26 23:09:11 +08:00
|
|
|
containers_conf_file="$TEST_SCRATCH_DIR/containers-namespaces.conf"
|
2022-03-23 02:14:06 +08:00
|
|
|
|
|
|
|
for mode in host private; do
|
|
|
|
cat > "$containers_conf_file" << EOF
|
|
|
|
[containers]
|
|
|
|
|
|
|
|
cgroupns = "$mode"
|
|
|
|
netns = "$mode"
|
|
|
|
pidns = "$mode"
|
|
|
|
ipcns = "$mode"
|
|
|
|
utsns = "$mode"
|
|
|
|
EOF
|
|
|
|
|
2022-04-26 21:47:03 +08:00
|
|
|
CONTAINERS_CONF="$containers_conf_file" run_buildah from $WITH_POLICY_JSON --quiet alpine
|
2022-03-23 02:14:06 +08:00
|
|
|
[ "$output" != "" ]
|
|
|
|
ctr="$output"
|
|
|
|
|
|
|
|
local op="=="
|
|
|
|
if [[ "$mode" == "private" ]]; then
|
|
|
|
op="!="
|
|
|
|
fi
|
|
|
|
|
|
|
|
for nstype in cgroup ipc net pid uts; do
|
|
|
|
run readlink /proc/self/ns/"$nstype"
|
|
|
|
ns="$output"
|
|
|
|
run_buildah run $ctr readlink /proc/self/ns/"$nstype"
|
|
|
|
assert "$output" $op "$ns" "namespace matches expected ($mode)"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
rm "$containers_conf_file"
|
|
|
|
}
|