-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_test.go
211 lines (194 loc) · 8.16 KB
/
root_test.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
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
package cmd
import (
"fmt"
"os"
osuser "os/user"
"path/filepath"
"runtime"
"testing"
"github.com/inpher/sb/internal/commands"
"github.com/inpher/sb/internal/helpers"
"github.com/inpher/sb/internal/models"
"github.com/inpher/sb/internal/types"
"github.com/stretchr/testify/require"
)
type commandTestStructure struct {
i commandTestStructureInputData
o error
}
type commandTestStructureInputData struct {
arguments []string
user *models.User
}
func TestIsATrustedCommand(t *testing.T) {
require.Equal(t, true, commands.IsAPublicCommand("self accesses list"), "The command self accesses list is a public command")
require.Equal(t, false, commands.IsAPublicCommand("INVALID_COMMAND"), "The command INVALID_COMMAND is not a public command")
require.Equal(t, false, commands.IsAPublicCommand("ttyrec"), "The command INVALID_COMMAND is not a public command")
}
func TestBuildSBCommand(t *testing.T) {
// Build a valid path for tests
_, filename, _, _ := runtime.Caller(0)
etcGroupPath := fmt.Sprintf("%s/../internal/models/test_assets/group/group", filepath.Dir(filename))
helpers.SetEtcGroupFilePath(etcGroupPath)
owner := &models.User{
User: &osuser.User{
Uid: "1000",
Gid: "1000",
Username: "testuser",
Name: "Test User",
HomeDir: "",
},
Groups: map[string]*models.Group{},
}
owner.BuildGroupsMembership([]string{"bg_owners-o"})
user := &models.User{
User: &osuser.User{
Uid: "1000",
Gid: "1000",
Username: "testuser",
Name: "Test User",
HomeDir: "",
},
Groups: map[string]*models.Group{},
}
user.BuildGroupsMembership([]string{"bg_developers-aclk", "bg_sysadmins-gk", "bg_everyone"})
tests := []commandTestStructure{
{
i: commandTestStructureInputData{user: user, arguments: []string{"help"}},
o: nil,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"INVALID_COMMAND"}},
o: fmt.Errorf("unknown command"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self access add", "help"}},
o: types.ErrMissingArguments,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self access add", "--host", "test"}},
o: types.ErrMissingArguments,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self egress-key generate", "--algo", "ed25519"}},
o: fmt.Errorf("please provide required argument --size"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self egress-key generate", "--algo", "ed25519", "--size", "INVALID"}},
o: fmt.Errorf("please provide a valid numeric size"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self egress-key generate", "--algo", "INVALID_ALGO", "--size", "256"}},
o: fmt.Errorf("argument --algo's value should be from the list: rsa, ecdsa, ed25519"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self egress-key generate", "--algo", "ed25519", "--size", "512"}},
o: fmt.Errorf("for ED25519, size is always 256"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self egress-key generate", "--algo", "rsa", "--size", "128"}},
o: fmt.Errorf("for RSA, choose a size between 2048 and 8192 (4096 is good)"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"self egress-key generate", "--algo", "ecdsa", "--size", "128"}},
o: fmt.Errorf("for ECDSA, choose either 256, 384 or 512"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"self egress-key generate", "--algo", "ed25519", "--size", "256"}},
o: nil,
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group egress-key generate", "--group", "owners", "--algo", "ed25519"}},
o: fmt.Errorf("please provide required argument --size"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group egress-key generate", "--algo", "ed25519", "--size", "INVALID"}},
o: fmt.Errorf("please provide a valid numeric size"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group egress-key generate", "--group", "owners", "--algo", "INVALID_ALGO", "--size", "256"}},
o: fmt.Errorf("argument --algo's value should be from the list: rsa, ecdsa, ed25519"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"groupGenerateEgressKey", "--group", "owners", "--algo", "ed25519", "--size", "512"}},
o: fmt.Errorf("for ED25519, size is always 256"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group egress-key generate", "--group", "owners", "--algo", "rsa", "--size", "128"}},
o: fmt.Errorf("for RSA, choose a size between 2048 and 8192 (4096 is good)"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group egress-key generate", "--group", "owners", "--algo", "ecdsa", "--size", "128"}},
o: fmt.Errorf("for ECDSA, choose either 256, 384 or 512"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group egress-key generate", "--group", "owners", "--algo", "ed25519", "--size", "256"}},
o: nil,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group access add", "--group", "developers", "--host", "test.com", "--user", "test"}},
o: nil,
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group access add", "--group", "developers", "--host", "test.com", "--user", "test"}},
o: fmt.Errorf("user is not an ACL keeper of the group"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group access add", "--group", "developers", "--host", "test.com", "--user", "test", "--alias", "account delete"}},
o: fmt.Errorf("the host or alias provided matches a sb command name, please provide a different value"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group member add", "--group", "sysadmins", "--account", os.Getenv("USER")}},
o: nil,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group member add", "--group", "developers", "--account", os.Getenv("USER")}},
o: fmt.Errorf("user is not a gate keeper of the group"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group accesses list", "--group", "everyone"}},
o: nil,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group accesses list", "--group", "developers"}},
o: fmt.Errorf("user is not a member of the group"),
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group accesses list", "--group", "everyone"}},
o: nil,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group owner add", "--group", "developers", "--account", os.Getenv("USER")}},
o: fmt.Errorf("user is not an owner of the group"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"group owner add", "--group", "owners", "--account", os.Getenv("USER")}},
o: nil,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"account create", "--username", "test", "--public-key", "test"}},
o: fmt.Errorf("user is not a sb owner"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"account create", "--username", "test", "--public-key", "test"}},
o: fmt.Errorf("ssh: no key found"),
},
{
i: commandTestStructureInputData{user: owner, arguments: []string{"account create", "--username", "test", "--public-key", "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFxu5J1fpfRBHe/2JKreeDGgJlMZji3n97fYm3KJt8Yv sb@localhost"}},
o: nil,
},
{
i: commandTestStructureInputData{user: user, arguments: []string{"group accesses list", "--group", "everyone"}},
o: nil,
},
}
for _, testData := range tests {
log := models.NewLog(testData.i.user.User.Username, []string{":memory:"}, testData.i.arguments)
_, _, err := commands.BuildSBCommand(log, testData.i.user, testData.i.arguments...)
if testData.o == nil {
require.NoError(t, err, "An unexpected error occurred while testing BuildSBCommand()")
} else {
require.Error(t, err, testData.o.Error(), "An error should have occurred while testing BuildSBCommand()")
}
}
}