<commit_before>#!/bin/bash

# About runc specs
# https://github.com/opencontainers/runtime-spec/blob/master/specs-go/config.go

DOCKER_SOCK=${DOCKER_SOCK:=/var/run/docker.sock}
DOCKER_API_VERSION=${DOCKER_API_VERSION:=v1.23}
DOCKER_URL=${DOCKER_URL:=http://localhost/${DOCKER_API_VERSION}}
CURL="curl -s --unix-socket ${DOCKER_SOCK}"

CONTAINER_ID="$1"
HOSTNAME="$2"
basedir="/tmp/pth"

response=$($CURL ${DOCKER_URL}/info)
if [[ -z "$response" ]]; then
	echo "no response"
	exit 1
fi
echo "$response" | jq . > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	echo "$response"
	exit 1
fi

response=$($CURL ${DOCKER_URL}/containers/${CONTAINER_ID}/json)
if [[ -z "$response" ]]; then
	echo "no response"
	exit 1
fi
echo "$response" | jq . > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	echo "$response"
	exit 1
fi

function get_container_rootfs {
	local json="$1"

	case "$(echo "$json" | jq -r .GraphDriver.Name)" in
	    overlay)
			mount_option=$(echo "$json" | jq -r '.GraphDriver.Data | "lowerdir=\(.LowerDir),upperdir=\(.UpperDir),workdir=\(.WorkDir)"')
			echo -n "overlay / overlay "
			echo "$json" | jq -r '.GraphDriver.Data | "lowerdir=\(.LowerDir),upperdir=\(.UpperDir),workdir=\(.WorkDir)"'
			;;
		devicemapper)
			devicename=$(echo "$json" | jq -r .GraphDriver.Data.DeviceName)
			while read line
			do
				set -- $line
				if [[ "$1" == "$devicename" ]]; then
					echo "${devicename} / bind bind"
					break
				fi
			done < /proc/mounts
			;;
		*)
			echo "rootfs type not suported"
			exit 1
			;;
	esac
}

function get_mountinfo {
	local json="$response"

	get_container_rootfs "$response"
	echo "$json" | jq -r '.Mounts[] | select(.Destination != "/dev/termination-log" and (.Type == "bind" or .Type == null )) | "\(.Source) \(.Destination) bind bind"'
	echo "$json" | jq -r '"\(.ResolvConfPath) /etc/resolv.conf bind bind\n\(.HostnamePath) /etc/hostname bind bind"'

	echo ${HOSTNAME} > ${basedir}/hostname
	echo "$json" | jq -r '"\(.ResolvConfPath) __POD__/etc/resolv.conf bind bind"'
	echo "${basedir}/hostname __POD__/etc/hostname bind bind"
}

function format_mountinfo {
	comma=""
	if [[ -d "${basedir}/node_root" ]]; then
		node_root="${basedir}/node_root/"
	fi

	echo '['
	while read line
	do
		set -- $line
		if [[ "$2" =~ ^/ ]]; then
			dest="/container${2}"
		elif [[ "$2" =~ ^__POD__(.*) ]]; then
			node_root=""
			dest=${BASH_REMATCH[1]}
		fi
		cat << __EOT__
	$comma
	{
		"destination": "$dest",
		"type": "$3",
		"source": "${node_root}$1",
		"options": ["$4"]
	}
__EOT__
		comma=","
	done
	echo ']'
}

function set_capabilities {
	local json="$1"

	capabilities="{}"
	cap=$(cat ${basedir}/config.json | jq '.process.capabilities.bounding + ["CAP_SYS_ADMIN", "CAP_NET_ADMIN", "CAP_NET_RAW", "CAP_SETGID", "CAP_SETUID", "CAP_SYS_PTRACE", "CAP_SYS_CHROOT", "CAP_MKNOD"]')

	for key in $(cat ${basedir}/config.json | jq -r '.process.capabilities | keys[]')
	do
		tmp=$(cat << __EOT__
	{
		"$key": ${cap}
	}
__EOT__
		)
		capabilities=$(echo ${capabilities} | jq ". + ${tmp}")
	done

	config=$(cat ${basedir}/config.json | jq ".process.capabilities = $capabilities")
	echo "$config" > ${basedir}/config.json
} 

function set_namespace {
	local json="$1"
	pid=$(echo "$json" | jq -r .State.Pid)

    ns=$(cat <<__EOT__
[
	{
		"type": "pid",<commit_msg>Add CAP_DAC_READ_SEARCH capability to debug pod Close #1<commit_after>#!/bin/bash

# About runc specs
# https://github.com/opencontainers/runtime-spec/blob/master/specs-go/config.go

DOCKER_SOCK=${DOCKER_SOCK:=/var/run/docker.sock}
DOCKER_API_VERSION=${DOCKER_API_VERSION:=v1.23}
DOCKER_URL=${DOCKER_URL:=http://localhost/${DOCKER_API_VERSION}}
CURL="curl -s --unix-socket ${DOCKER_SOCK}"

CONTAINER_ID="$1"
HOSTNAME="$2"
basedir="/tmp/pth"

response=$($CURL ${DOCKER_URL}/info)
if [[ -z "$response" ]]; then
	echo "no response"
	exit 1
fi
echo "$response" | jq . > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	echo "$response"
	exit 1
fi

response=$($CURL ${DOCKER_URL}/containers/${CONTAINER_ID}/json)
if [[ -z "$response" ]]; then
	echo "no response"
	exit 1
fi
echo "$response" | jq . > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	echo "$response"
	exit 1
fi

function get_container_rootfs {
	local json="$1"

	case "$(echo "$json" | jq -r .GraphDriver.Name)" in
	    overlay)
			mount_option=$(echo "$json" | jq -r '.GraphDriver.Data | "lowerdir=\(.LowerDir),upperdir=\(.UpperDir),workdir=\(.WorkDir)"')
			echo -n "overlay / overlay "
			echo "$json" | jq -r '.GraphDriver.Data | "lowerdir=\(.LowerDir),upperdir=\(.UpperDir),workdir=\(.WorkDir)"'
			;;
		devicemapper)
			devicename=$(echo "$json" | jq -r .GraphDriver.Data.DeviceName)
			while read line
			do
				set -- $line
				if [[ "$1" == "$devicename" ]]; then
					echo "${devicename} / bind bind"
					break
				fi
			done < /proc/mounts
			;;
		*)
			echo "rootfs type not suported"
			exit 1
			;;
	esac
}

function get_mountinfo {
	local json="$response"

	get_container_rootfs "$response"
	echo "$json" | jq -r '.Mounts[] | select(.Destination != "/dev/termination-log" and (.Type == "bind" or .Type == null )) | "\(.Source) \(.Destination) bind bind"'
	echo "$json" | jq -r '"\(.ResolvConfPath) /etc/resolv.conf bind bind\n\(.HostnamePath) /etc/hostname bind bind"'

	echo ${HOSTNAME} > ${basedir}/hostname
	echo "$json" | jq -r '"\(.ResolvConfPath) __POD__/etc/resolv.conf bind bind"'
	echo "${basedir}/hostname __POD__/etc/hostname bind bind"
}

function format_mountinfo {
	comma=""
	if [[ -d "${basedir}/node_root" ]]; then
		node_root="${basedir}/node_root/"
	fi

	echo '['
	while read line
	do
		set -- $line
		if [[ "$2" =~ ^/ ]]; then
			dest="/container${2}"
		elif [[ "$2" =~ ^__POD__(.*) ]]; then
			node_root=""
			dest=${BASH_REMATCH[1]}
		fi
		cat << __EOT__
	$comma
	{
		"destination": "$dest",
		"type": "$3",
		"source": "${node_root}$1",
		"options": ["$4"]
	}
__EOT__
		comma=","
	done
	echo ']'
}

function set_capabilities {
	local json="$1"

	capabilities="{}"
	cap=$(cat ${basedir}/config.json | jq '.process.capabilities.bounding + ["CAP_SYS_ADMIN", "CAP_NET_ADMIN", "CAP_NET_RAW", "CAP_SETGID", "CAP_SETUID", "CAP_SYS_PTRACE", "CAP_SYS_CHROOT", "CAP_MKNOD", "CAP_DAC_READ_SEARCH"]')

	for key in $(cat ${basedir}/config.json | jq -r '.process.capabilities | keys[]')
	do
		tmp=$(cat << __EOT__
	{
		"$key": ${cap}
	}
__EOT__
		)
		capabilities=$(echo ${capabilities} | jq ". + ${tmp}")
	done

	config=$(cat ${basedir}/config.json | jq ".process.capabilities = $capabilities")
	echo "$config" > ${basedir}/config.json
} 

function set_namespace {
	local json="$1"
	pid=$(echo "$json" | jq -r .State.Pid)

    ns=$(cat <<__EOT__
[
	{
		"type": "pid",
{'fraction_non_alphanumeric': 0.20411871592973954, 'fraction_numerical': 0.008328285887341006, 'mean_word_length': 3.8817442719881745, 'pattern_counts': {'":': 12, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5949982', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2583, 'n_words': 561}