join.bats 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env bats
  2. function is_podman_available()
  3. {
  4. if podman help >> /dev/null; then
  5. printf %s\\n 1
  6. return
  7. fi
  8. printf %s\\n 0
  9. }
  10. @test "Join namespace of a Docker container" {
  11. ID="$(docker run -d alpine sleep 100)"
  12. PID="$(docker inspect --format '{{.State.Pid}}' "${ID:?}")"
  13. printf %s\\n "(calling sudo)"
  14. run sudo ./bin/gfpsgo -pids "${PID:?}" -join
  15. [ "${status:-}" -eq 0 ]
  16. [[ ${lines[1]:?} =~ "sleep" ]]
  17. docker rm -f "${ID:?}"
  18. }
  19. @test "Join namespace of a Docker container and format" {
  20. ID="$(docker run -d alpine sleep 100)"
  21. PID="$(docker inspect --format '{{.State.Pid}}' "${ID}")"
  22. printf %s\\n "(calling sudo)"
  23. run sudo ./bin/gfpsgo -pids "${PID:?}" -join -format "pid, group, args"
  24. [ "${status:-}" -eq 0 ]
  25. [[ ${lines[0]:?} == "PID GROUP COMMAND" ]]
  26. [[ ${lines[1]:?} =~ "1 root sleep 100" ]]
  27. docker rm -f "${ID:?}"
  28. }
  29. @test "Join namespace of a Docker container and check capabilities" {
  30. ID="$(docker run --privileged -d alpine sleep 100)"
  31. PID="$(docker inspect --format '{{.State.Pid}}' "${ID:?}")"
  32. printf %s\\n "(calling sudo)"
  33. run sudo ./bin/gfpsgo -pids "${PID:?}" -join -format "pid, capeff"
  34. [ "${status:-}" -eq 0 ]
  35. [[ ${lines[0]:?} == "PID EFFECTIVE CAPS" ]]
  36. [[ ${lines[1]:?} =~ "1 full" ]]
  37. docker rm -f "${ID:?}"
  38. }
  39. @test "Join namespace of a Docker container and check seccomp mode" {
  40. # (Travis CI is broken, so run in a privileged container.)
  41. ID="$(docker run -d --privileged alpine sleep 100)"
  42. PID="$(docker inspect --format '{{.State.Pid}}' "${ID:?}")"
  43. printf %s\\n "(calling sudo)"
  44. run sudo ./bin/gfpsgo -pids "${PID:?}" --join -format "pid, seccomp"
  45. [ "${status:-}" -eq 0 ]
  46. [[ ${lines[0]:?} == "PID SECCOMP" ]]
  47. [[ ${lines[1]:?} =~ "1 disabled" ]]
  48. docker rm -f "${ID:?}"
  49. }
  50. @test "Join namespace of a Docker container and extract host PID" {
  51. ID="$(docker run -d alpine sleep 100)"
  52. PID="$(docker inspect --format '{{.State.Pid}}' "${ID:?}")"
  53. printf %s\\n "(calling sudo)"
  54. run sudo ./bin/gfpsgo -pids "${PID:?}" -join -format "pid, hpid"
  55. [ "${status:-}" -eq 0 ]
  56. [[ ${lines[0]:?} == "PID HPID" ]]
  57. [[ ${lines[1]:?} =~ ^1.*"${PID:?}" ]]
  58. docker rm -f "${ID:?}"
  59. }
  60. @test "Join namespace of a Docker container and extract effective host user ID" {
  61. ID="$(docker run -d alpine sleep 100)"
  62. PID="$(docker inspect --format '{{.State.Pid}}' "${ID:?}")"
  63. run sudo ./bin/gfpsgo -pids "${PID:?}" -join -format "pid, huser"
  64. [ "${status}" -eq 0 ]
  65. [[ ${lines[0]:?} == "PID HUSER" ]]
  66. [[ ${lines[1]:?} =~ "1 root" ]]
  67. docker rm -f "${ID:?}"
  68. }
  69. @test "Join namespace of a Podman container and extract pid, {host,}user and group with {g,u}idmap" {
  70. enabled=$(is_podman_available)
  71. if [[ "${enabled:-}" -eq 0 ]]; then
  72. skip "Podman is not available."
  73. fi
  74. printf %s\\n "(calling sudo)"
  75. ID="$(sudo podman run -d --uidmap=0:300000:70000 --gidmap=0:100000:70000 alpine sleep 100)"
  76. printf %s\\n "(calling sudo)"
  77. PID="$(sudo podman inspect --format '{{.State.Pid}}' "${ID:?}")"
  78. printf %s\\n "(calling sudo)"
  79. run sudo ./bin/gfpsgo -pids "${PID:?}" -join -format "pid, user, huser, group, hgroup"
  80. [ "${status:-}" -eq 0 ]
  81. [[ ${lines[0]:?} == "PID USER HUSER GROUP HGROUP" ]]
  82. [[ ${lines[1]:?} =~ "1 root 300000 root 100000" ]]
  83. printf %s\\n "(calling sudo)"
  84. sudo podman rm -f "${ID:?}"
  85. }
  86. @test "Join namespace of a Docker container and extract effective host group ID" {
  87. ID="$(docker run -d alpine sleep 100)"
  88. PID="$(docker inspect --format '{{.State.Pid}}' "${ID:?}")"
  89. run sudo ./bin/gfpsgo -pids "${PID:?}" -join -format "pid, hgroup"
  90. [ "${status:-}" -eq 0 ]
  91. [[ ${lines[0]:?} == "PID HGROUP" ]]
  92. [[ ${lines[1]:?} =~ "1 root" ]]
  93. docker rm -f "${ID:?}"
  94. }
  95. @test "Join namespace of a Docker container and check the process state" {
  96. ID="$(docker run -d alpine sleep 100)"
  97. PID="$(docker inspect --format '{{.State.Pid}}' "${ID}")"
  98. run sudo ./bin/gfpsgo -pids "${PID:?}" -join -format "pid, state"
  99. [ "${status:-}" -eq 0 ]
  100. [[ ${lines[0]:?} == "PID STATE" ]]
  101. [[ ${lines[1]:?} =~ "1 S" ]]
  102. docker rm -f "${ID:?}"
  103. }
  104. @test "Run Podman pod and check for redundant entries" {
  105. enabled=$(is_podman_available)
  106. if [[ "${enabled:-}" -eq 0 ]]; then
  107. skip "Podman is not available."
  108. fi
  109. printf %s\\n "(calling sudo)"
  110. POD_ID="$(sudo podman pod create)"
  111. printf %s\\n "(calling sudo)"
  112. ID_1="$(sudo podman run --pod "${POD_ID:?}" -d alpine sleep 111)"
  113. printf %s\\n "(calling sudo)"
  114. PID_1="$(sudo podman inspect --format '{{.State.Pid}}' "${ID_1:?}")"
  115. printf %s\\n "(calling sudo)"
  116. ID_2="$(sudo podman run --pod "${POD_ID:?}" -d alpine sleep 222)"
  117. printf %s\\n "(calling sudo)"
  118. PID_2="$(sudo podman inspect --format '{{.State.Pid}}' "${ID_2:?}")"
  119. # The underlying idea is that is that we had redundant entries if
  120. # the detection of PID namespaces wouldn't work correctly.
  121. # printf %s\\n "(calling sudo)"
  122. run sudo ./bin/gfpsgo -pids "${PID_1:?},${PID_2:?}" -join -format "pid, args"
  123. [ "${status:-}" -eq 0 ]
  124. [[ ${lines[0]:?} == "PID COMMAND" ]]
  125. [[ ${lines[1]:?} =~ "1 sleep 111" ]]
  126. [[ ${lines[2]:?} =~ "1 sleep 222" ]]
  127. [[ ${lines[3]:-} == "" ]]
  128. printf %s\\n "(calling sudo)"
  129. sudo podman rm -f "${ID_1:?}" "${ID_2:?}"
  130. printf %s\\n "(calling sudo)"
  131. sudo podman pod rm "${POD_ID:?}"
  132. }
  133. @test "Test fill-mappings" {
  134. if [[ -n ${TRAVIS:-} ]]; then
  135. skip "Travis CI is unsupported."
  136. fi
  137. run unshare -muinpfr --mount-proc true
  138. if [[ ${status:-} -ne 0 ]]; then
  139. skip "unshare unavailable or unsupported."
  140. fi
  141. unshare -muinpfr --mount-proc sleep 20 &
  142. PID=$(printf %s\\n $!)
  143. run nsenter --preserve-credentials -U -t "${PID:?}" ./bin/gfpsgo -pids "${PID:?}" -join -fill-mappings -format huser
  144. kill -9 "${PID:?}"
  145. [ "${status:-}" -eq 0 ]
  146. [[ ${lines[0]:?} != "root" ]]
  147. }