-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthcmd_test.go
More file actions
224 lines (222 loc) · 5.61 KB
/
authcmd_test.go
File metadata and controls
224 lines (222 loc) · 5.61 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"os"
"regexp"
"strings"
"testing"
)
func TestAuthCmd(t *testing.T) {
tt := []struct {
name string
command string
mainArgs []string
configFile string
want string
wantRegex string
exitCode int
}{
{
name: "empty command",
command: "",
mainArgs: []string{"test1"},
configFile: "tests/authcmd_test.yml",
want: "Denied : direct ssh not allowed, you must specify a command",
exitCode: 1,
},
{
name: "forbidden command",
command: "rm",
mainArgs: []string{"test1"},
configFile: "tests/authcmd_test.yml",
want: "Denied : command `rm` not allowed",
exitCode: 1,
},
{
name: "working command",
command: "/bin/echo test",
mainArgs: []string{"test1"},
configFile: "tests/authcmd_test.yml",
want: "test",
exitCode: 0,
},
{
name: "working command with some args",
command: "echo -e -n test",
mainArgs: []string{"test2"},
configFile: "tests/authcmd_test.yml",
want: "test",
exitCode: 0,
},
{
name: "just to be sure...",
command: "echo test ; rm mycriticalfile",
mainArgs: []string{"test2"},
configFile: "tests/authcmd_test.yml",
want: "test ; rm mycriticalfile",
exitCode: 0,
},
{
name: "terse output",
command: "chmod test",
mainArgs: []string{"test2"},
configFile: "tests/authcmd_test.yml",
want: "Denied",
exitCode: 1,
},
{
name: "allowed cmd output",
command: "echo test",
mainArgs: []string{"test3"},
configFile: "tests/authcmd_test.yml",
want: "Allowed : ls,id",
exitCode: 1,
},
{
name: "exit code",
command: "ls -l /doesnotexists",
configFile: "tests/authcmd_test.yml",
wantRegex: "ls: .*doesnotexists.*",
exitCode: 2,
},
{
name: "forbidden arg",
command: "/bin/echo iwant $MY_SECRET",
mainArgs: []string{"test1"},
configFile: "tests/authcmd_test.yml",
want: "Denied : command `/bin/echo` argument : `$MY_SECRET` forbidden : regex `\\$`",
exitCode: 1,
},
{
name: "allowed arg",
command: "ls -l authcmd.go",
mainArgs: []string{"test1"},
configFile: "tests/authcmd_test.yml",
wantRegex: ".*authcmd.go.*",
exitCode: 0,
},
{
name: "replace arg",
command: "/bin/echo I love pizza and pizza",
mainArgs: []string{"test1"},
configFile: "tests/authcmd_test.yml",
want: "We love pizza and pasta",
exitCode: 0,
},
{
name: "help text",
command: "notallowed",
mainArgs: []string{"test4"},
configFile: "tests/authcmd_test.yml",
want: "This is a helping text",
exitCode: 1,
},
{
name: "test expand",
command: "echo $PATH",
mainArgs: []string{"test5"},
configFile: "tests/authcmd_test.yml",
wantRegex: ".*/bin.*",
exitCode: 0,
},
{
name: "test no expand",
command: "echo $PATH",
mainArgs: []string{"test4"},
configFile: "tests/authcmd_test.yml",
want: "$PATH",
exitCode: 0,
},
{
name: "logging + double merging",
command: "echo test",
mainArgs: []string{"test5", "test6"},
configFile: "tests/authcmd_test.yml",
want: "test",
exitCode: 0,
},
{
name: "shell",
command: `echo "shell is : $0"`,
mainArgs: []string{"test5", "test7"},
configFile: "tests/authcmd_test.yml",
wantRegex: "shell is : .*/sh",
exitCode: 0,
},
{
name: "key_tag merging",
command: "id",
configFile: "tests/authcmd_test.yml",
mainArgs: []string{"test8"},
wantRegex: ".*uid=.*",
exitCode: 0,
},
{
name: "key_tag no merging",
command: "id",
configFile: "tests/authcmd_test.yml",
mainArgs: []string{"doesnotexists"},
want: "Denied : command `id` not allowed",
exitCode: 1,
},
{
name: "must match ko",
command: "cat /etc/passwd",
configFile: "tests/authcmd_test.yml",
mainArgs: []string{"test9"},
want: "Denied : command `cat` arguments : ` /etc/passwd` not matching regex `.*LICENSE$`",
exitCode: 1,
},
{
name: "must match ok",
command: "cat LICENSE",
configFile: "tests/authcmd_test.yml",
mainArgs: []string{"test9"},
wantRegex: "^MIT License",
exitCode: 0,
},
{
name: "no env var",
command: "echo x$MY_VAR",
mainArgs: []string{"test5"},
configFile: "tests/authcmd_test.yml",
want: "x",
exitCode: 0,
},
{
name: "command env var",
command: "echo $MY_VAR",
mainArgs: []string{"test10"},
configFile: "tests/authcmd_test.yml",
want: "test10 echo cmd",
exitCode: 0,
},
{
name: "global env var",
command: "echo $MY_VAR",
mainArgs: []string{"test11"},
configFile: "tests/authcmd_test.yml",
want: "test11 global",
exitCode: 0,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
os.Setenv("SSH_ORIGINAL_COMMAND", tc.command)
os.Setenv("AUTHCMD_CONFIG_FILE", tc.configFile)
os.Args = append(os.Args[:1], tc.mainArgs...)
exitCode, out := handle()
//fmt.Println("out:", string(out))
if exitCode != tc.exitCode {
t.Errorf("Want exit code '%d', got '%d' with command '%s'", tc.exitCode, exitCode, tc.command)
}
if tc.wantRegex != "" {
re, _ := regexp.Compile(tc.wantRegex)
if !re.MatchString(out) {
t.Errorf("Regex '%s' not matching, got '%s'", tc.wantRegex, out)
}
} else if strings.TrimSpace(string(out)) != tc.want {
t.Errorf("Want '%s', got '%s'", tc.want, out)
}
})
}
}