forked from UPC/ravada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path65_user_ldap.t
385 lines (288 loc) · 11.6 KB
/
65_user_ldap.t
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use warnings;
use strict;
use Data::Dumper;
use Test::More;
use YAML qw(LoadFile DumpFile);
no warnings "experimental::signatures";
use feature qw(signatures);
use lib 't/lib';
use Test::Ravada;
use_ok('Ravada');
use_ok('Ravada::Auth::LDAP');
my $ADMIN_GROUP = "test.admin.group";
my $RAVADA_POSIX_GROUP = "rvd_posix_group";
my ($LDAP_USER , $LDAP_PASS) = ("cn=Directory Manager","saysomething");
init();
my @USERS;
sub test_user_fail {
my $user_fail;
eval { $user_fail = Ravada::Auth::LDAP->new(name => 'root',password => 'fail')};
ok(!$user_fail,"User should fail, got ".Dumper($user_fail));
}
sub test_user{
my $name = (shift or 'jimmy.mcnulty');
my $with_posix_group = ( shift or 0);
my $password = 'jameson';
if ( Ravada::Auth::LDAP::search_user($name) ) {
diag("Removing $name");
Ravada::Auth::LDAP::remove_user($name)
}
my $user = Ravada::Auth::LDAP::search_user($name);
ok(!$user,"I shouldn't find user $name in the LDAP server") or return;
my $user_db = Ravada::Auth::SQL->new( name => $name);
$user_db->remove();
# check for the user in the SQL db, he shouldn't be there
#
my $sth = connector->dbh->prepare("SELECT * FROM users WHERE name=?");
$sth->execute($name);
my $row = $sth->fetchrow_hashref;
$sth->finish;
ok(!$row->{name},"I shouldn't find $name in the SQL db ".Dumper($row));
eval { Ravada::Auth::LDAP::add_user($name,$password) };
push @USERS,($name);
ok(!$@,$@) or return;
_add_to_posix_group($name);
my $mcnulty;
eval { $mcnulty = Ravada::Auth::LDAP->new(name => $name,password => $password) };
is($@,'', Dumper($Ravada::CONFIG)) or exit;
ok($mcnulty,($@ or "ldap login failed for $name")) or return;
ok(ref($mcnulty) =~ /Ravada/i,"User must be Ravada::Auth::LDAP , it is '".ref($mcnulty));
ok(!$mcnulty->is_admin,"User ".$mcnulty->name." should not be admin "
.Dumper($mcnulty->{_data}));
ok($mcnulty->ldap_entry,"Expecting User LDAP entry");
# try to login
my $mcnulty_login = Ravada::Auth::login($name,$password);
ok($mcnulty_login,"No login");
ok(ref $mcnulty_login && ref($mcnulty_login) eq 'Ravada::Auth::LDAP',
"ref should be Ravada::Auth::LDAP , got ".ref($mcnulty_login));
ok($mcnulty_login->ldap_entry,"Expecting User LDAP entry");
# check for the user in the SQL db
#
$sth = connector->dbh->prepare("SELECT * FROM users WHERE name=?");
$sth->execute($name);
$row = $sth->fetchrow_hashref;
$sth->finish;
ok($row->{name} && $row->{name} eq $name
&& $row->{id},"I can't find $name in the users SQL table ".Dumper($row));
my $mcnulty_sql = Ravada::Auth::SQL->new(name => $name);
ok($mcnulty_sql,"I can't find mcnulty in the SQL db");
ok($mcnulty_sql->{name} eq $name, "Expecting '$name', got $mcnulty_sql->{name}");
# login again to check it doesn't get added twice
my $mcnulty2;
eval { $mcnulty2 = Ravada::Auth::LDAP->new(name => $name,password => $password) };
ok($mcnulty2,($@ or "ldap login failed for $name")) or return;
$sth = connector->dbh->prepare("SELECT count(*) FROM users WHERE name=?");
$sth->execute($name);
my ($count) = $sth->fetchrow;
$sth->finish;
ok($count == 1,"Found $count $name, expecting 1");
my $auth_ok;
eval { $auth_ok = Ravada::Auth::login($name, $password)};
is($@,'');
ok($auth_ok,"Expecting auth_ok: ".Dumper($auth_ok));
is($auth_ok->is_external,1);
my $user_sql = Ravada::Auth::SQL->new(name => $name);
ok($user_sql,"Expecting a SQL user for $name");
ok($user_sql->is_external,"Expecting is_external");
$auth_ok = undef;
eval { $auth_ok = Ravada::Auth::login($name, 'fail','quiet')};
ok($@,"Expecting fails, got : ".($@ or ''));
ok(!$auth_ok,"Expecting no auth_ok. got: ".Dumper($auth_ok));
return $mcnulty;
}
sub remove_users {
for my $name (@USERS) {
my $user = Ravada::Auth::LDAP::search_user($name);
next if !$user;
Ravada::Auth::LDAP::remove_user($name);
$user = Ravada::Auth::LDAP::search_user($name);
ok(!$user,"I shouldn't find user $name in the LDAP server") or return;
}
}
sub test_add_group {
my $name = "grup.test";
Ravada::Auth::LDAP::remove_group($name)
if Ravada::Auth::LDAP::search_group(name => $name);
my $group0 = Ravada::Auth::LDAP::search_group(name => $name);
ok(!$group0,"Group $name shouldn't exist") or return;
Ravada::Auth::LDAP::add_group($name);
my $group = Ravada::Auth::LDAP::search_group(name => $name);
ok($group,"Group $name not created");
Ravada::Auth::LDAP::remove_group($name) if $group;
my $group2 = Ravada::Auth::LDAP::search_group(name => $name);
ok(!$group2,"Group $name not removed");
}
sub test_manage_group {
my $with_admin = shift;
my $with_posix_group = shift;
my $name = $ADMIN_GROUP;
diag("Testing LDAP admin with group $ADMIN_GROUP enabled= $with_admin");
Ravada::Auth::LDAP::remove_group($name)
if Ravada::Auth::LDAP::search_group(name => $name);
my $group0 = Ravada::Auth::LDAP::search_group(name => $name);
ok(!$group0,"Group $name shouldn't exist") or return;
Ravada::Auth::LDAP::add_group($name);
my $group = Ravada::Auth::LDAP::search_group(name => $name);
ok($group,"Group $name not created") or return;
my $uid = 'ragnar.lothbrok';
my $user = test_user($uid, $with_posix_group);
my $is_admin;
eval { $is_admin = $user->is_admin };
ok(!$@,$@);
ok(!$is_admin,"User $uid should not be admin");
Ravada::Auth::LDAP::add_to_group($uid, $name);
if ($with_admin) {
my $admin_group = $$Ravada::Auth::LDAP::CONFIG->{ldap}->{admin_group};
SKIP: {
if (!$admin_group) {
diag("Missing admin_group in config file , got : \n"
.Dumper($$Ravada::Auth::LDAP::CONFIG->{ldap}));
skip("No admin group defined",1);
}
ok($user->is_admin,"User $uid (".(ref $user).") "
."should be admin, he was added to $name group") or exit;
};
}
Ravada::Auth::LDAP::remove_user($uid);
Ravada::Auth::LDAP::remove_group($name);
my $group2 = Ravada::Auth::LDAP::search_group(name => $name);
ok(!$group2,"Group $name not removed");
}
sub test_user_bind {
my $file_config = shift;
my $with_posix_group = shift;
my $config = LoadFile($file_config);
$config->{ldap}->{auth} = 'bind';
my $file_config_bind = "/var/tmp/ravada_test_ldap_bind_$$.conf";
DumpFile($file_config_bind, $config);
my $ravada = Ravada->new(config => $file_config_bind
, connector => connector);
Ravada::Auth::LDAP::init();
_add_to_posix_group('jimmy.mcnulty') if $with_posix_group;
my $mcnulty;
eval { $mcnulty = Ravada::Auth::LDAP->new(name => 'jimmy.mcnulty',password => 'jameson') };
is($@,'');
ok($mcnulty,($@ or "ldap login failed ")) or return;
is($mcnulty->{_auth}, 'bind');
unlink $file_config_bind;
$ravada = Ravada->new(config => $file_config
, connector => connector);
Ravada::Auth::LDAP::_init_ldap_admin();
}
sub _init_config($file_config, $with_admin, $with_posix_group) {
if ( ! -e $file_config) {
my $config = {
ldap => {
admin_user => { dn => $LDAP_USER , password => $LDAP_PASS }
,base => "dc=example,dc=com"
,admin_group => $ADMIN_GROUP
,auth => 'match'
,ravada_posix_group => $RAVADA_POSIX_GROUP
}
};
DumpFile($file_config,$config);
}
my $config = LoadFile($file_config);
delete $config->{ldap}->{admin_group} if !$with_admin;
if ($with_posix_group) {
if ( !exists $config->{ldap}->{ravada_posix_group}
|| !$config->{ldap}->{ravada_posix_group}) {
$config->{ldap}->{ravada_posix_group} = $RAVADA_POSIX_GROUP;
diag("Adding ravada_posix_group = $RAVADA_POSIX_GROUP in $file_config");
}
} else {
delete $config->{ldap}->{ravada_posix_group};
}
$config->{vm}=['KVM','Void'];
delete $config->{ldap}->{ravada_posix_group} if !$with_posix_group;
my $fly_config = "/var/tmp/$$.config";
DumpFile($fly_config, $config);
return $fly_config;
}
sub _add_posix_group {
my $ldap = Ravada::Auth::LDAP::_init_ldap_admin();
my $base = "ou=groups,".Ravada::Auth::LDAP::_dc_base();
my $mesg = $ldap->add(
cn => $RAVADA_POSIX_GROUP
,dn => "cn=$RAVADA_POSIX_GROUP,$base"
,attrs => [ cn => $RAVADA_POSIX_GROUP
,objectClass=> [ 'posixGroup' ]
,gidNumber => 999
]
);
die "Error ".$mesg->code." adding $RAVADA_POSIX_GROUP ".$mesg->error
if $mesg->code && $mesg->code != 68;
$mesg = $ldap->search( filter => "cn=$RAVADA_POSIX_GROUP",base => $base );
my @group = $mesg->entries;
ok($group[0],"Expecting group $RAVADA_POSIX_GROUP") or return;
return $group[0];
}
sub _add_to_posix_group {
my $user_name = shift;
my $group = _add_posix_group();
my $ldap = Ravada::Auth::LDAP::_init_ldap_admin();
$group->add(memberUid => $user_name);
my $mesg = $group->update($ldap);
die $mesg->code." ".$mesg->error if $mesg->code && $mesg->code != 20;
}
sub test_posix_group {
my $with_posix_group = shift;
my $group = _add_posix_group();
my ($user_name, $password) = ('mcnulty_'.new_domain_name(), 'jameson');
my $user = create_ldap_user($user_name, $password);
my $user_login;
eval { $user_login= Ravada::Auth::LDAP->new(name => $user_name,password => $password) };
my $error = $@;
if ($with_posix_group) {
ok(!$user_login,"Expecting no login $user_name with posix group ".Dumper($Ravada::CONFIG));
like($error,qr(Login failed));
} else {
ok($user_login);
}
_add_to_posix_group($user_name);
eval { $user_login= Ravada::Auth::LDAP->new(name => $user_name,password => $password) };
is($@,'');
ok($user_login);
my $ldap = Ravada::Auth::LDAP::_init_ldap_admin();
my $mesg = $ldap->delete($user);
die $mesg->code." ".$mesg->error if $mesg->code && $mesg->code;
$mesg = $ldap->delete($group);
die $mesg->code." ".$mesg->error if $mesg->code && $mesg->code;
}
SKIP: {
my $file_config = "t/etc/ravada_ldap.conf";
for my $with_posix_group (0,1) {
for my $with_admin (0,1) {
my $fly_config = _init_config($file_config, $with_admin, $with_posix_group);
my $ravada = Ravada->new(config => $fly_config
, connector => connector);
$ravada->_install();
Ravada::Auth::LDAP::init();
if ($with_posix_group) {
ok($Ravada::CONFIG->{ldap}->{ravada_posix_group},
"Expecting ravada_posix_group on $fly_config\n"
.Dumper($Ravada::CONFIG->{ldap}))
or exit;
}
my $ldap;
eval { $ldap = Ravada::Auth::LDAP::_init_ldap_admin() };
if ($@ =~ /Bad credentials/) {
diag("$@\nFix admin credentials in $file_config");
} else {
diag("Skipped LDAP tests ".($@ or '')) if !$ldap;
}
skip( ($@ or "No LDAP server found"),6) if !$ldap && $@ !~ /Bad credentials/;
ok($ldap) and do {
test_user_fail();
test_user( );
test_add_group();
test_manage_group($with_admin);
test_posix_group($with_posix_group);
test_user_bind($fly_config, $with_posix_group);
remove_users();
};
unlink($fly_config) if -e $fly_config;
}
}
};
done_testing();