/
shell.go
151 lines (107 loc) · 3.91 KB
/
shell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package debug
import (
"strings"
//log "github.com/sirupsen/logrus"
)
const (
sidKey = "_SESSION_ID_"
shellKey = "_SHELL_NAME_"
)
const (
defaultShellName = "sh"
bashShellName = "bash"
)
// NOTES: Mitigating variable expansion done by kubernetes & shell/heredoc
var shellConfig = `
set -eu
cat << 'EOF' > /.mint_debugger_shell_config.sh
#!/bin/sh
if [ -d "/proc/$$$/root/usr/local/sbin/" ]; then
ln -s /proc/$$$/root/usr/local/sbin/ /proc/1/root/.mint_debugger_ulsbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_ulsbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/usr/local/bin/" ]; then
ln -s /proc/$$$/root/usr/local/bin/ /proc/1/root/.mint_debugger_ulbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_ulbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/usr/sbin/" ]; then
ln -s /proc/$$$/root/usr/sbin/ /proc/1/root/.mint_debugger_usbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_usbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/usr/bin/" ]; then
ln -s /proc/$$$/root/usr/bin/ /proc/1/root/.mint_debugger_ubin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_ubin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/sbin/" ]; then
ln -s /proc/$$$/root/sbin/ /proc/1/root/.mint_debugger_sbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_sbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/bin/" ]; then
ln -s /proc/$$$/root/bin/ /proc/1/root/.mint_debugger_bin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_bin__SESSION_ID_
fi
chroot /proc/1/root sh
EOF
sh /.mint_debugger_shell_config.sh
`
//exec sh...
func configShell(sessionID string, isK8s bool) string {
result := strings.ReplaceAll(shellConfig, sidKey, sessionID)
if isK8s {
return result
}
return strings.ReplaceAll(result, "/$$$/", "/$$/")
}
var shellConfigAlt = `
set -eu
cat << 'EOF' > /.mint_debugger_shell_config.sh
#!/bin/sh
if [ -d "/proc/$$$/root/usr/local/sbin/" ]; then
ln -s /proc/$$$/root/usr/local/sbin/ /proc/1/root/.mint_debugger_ulsbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_ulsbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/usr/local/bin/" ]; then
ln -s /proc/$$$/root/usr/local/bin/ /proc/1/root/.mint_debugger_ulbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_ulbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/usr/sbin/" ]; then
ln -s /proc/$$$/root/usr/sbin/ /proc/1/root/.mint_debugger_usbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_usbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/usr/bin/" ]; then
ln -s /proc/$$$/root/usr/bin/ /proc/1/root/.mint_debugger_ubin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_ubin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/sbin/" ]; then
ln -s /proc/$$$/root/sbin/ /proc/1/root/.mint_debugger_sbin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_sbin__SESSION_ID_
fi
if [ -d "/proc/$$$/root/bin/" ]; then
ln -s /proc/$$$/root/bin/ /proc/1/root/.mint_debugger_bin__SESSION_ID_
export PATH=$PATH:/.mint_debugger_bin__SESSION_ID_
fi
if [ -f "/proc/$$$/root/bin/busybox" ] && [ ! -f "/proc/1/root/bin/busybox" ]; then
ln -s /proc/$$$/root/bin/busybox /proc/1/root/bin/busybox
fi
ln -s /proc/$$$/root/lib/ /proc/1/root/.mint_debugger_lib__SESSION_ID_
ln -s /proc/$$$/root/usr/lib/ /proc/1/root/.mint_debugger_ulib__SESSION_ID_
ln -s /proc/$$$/root/usr/lib/libncursesw.so.6 /proc/1/root/usr/lib/libncursesw.so.6
ln -s /proc/$$$/root/usr/lib/libncursesw.so.6.4 /proc/1/root/usr/lib/libncursesw.so.6.4
#if [ -n "$LD_LIBRARY_PATH" ]; then
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/.mint_debugger_ulib__SESSION_ID_
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/.mint_debugger_lib__SESSION_ID_:/.mint_debugger_ulib__SESSION_ID_
#else
#export LD_LIBRARY_PATH=/.mint_debugger_ulib__SESSION_ID_
# export LD_LIBRARY_PATH=/.mint_debugger_lib__SESSION_ID_:/.mint_debugger_ulib__SESSION_ID_
#fi
chroot /proc/1/root bash
EOF
sh /.mint_debugger_shell_config.sh
`
func configShellAlt(sessionID string, isK8s bool) string {
result := strings.ReplaceAll(shellConfigAlt, sidKey, sessionID)
if isK8s {
return result
}
return strings.ReplaceAll(result, "/$$$/", "/$$/")
}