-
Notifications
You must be signed in to change notification settings - Fork 4
/
create-test-data.py
executable file
·824 lines (741 loc) · 28.2 KB
/
create-test-data.py
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#!/usr/bin/python3
#
# Copyright (C) 2021 FreeIPA Contributors see COPYING for license
#
import click
import math
import sys
from datetime import datetime, timedelta
from ipalib import api
class IPAData(object):
def __init__(
self, domain, basedn, realm,
users=50000,
hosts=40000,
host_prefix="client",
services=0,
number_of_subgroups=0,
outfile=None,
):
# TBD: compute everything else based on users and groups
"""
groups=1000,
groups_per_user=20,
nested_groups_max_level=2,
nested_groups_per_user=10,
hostgroups=1000,
hostgroups_per_host=10,
nested_hostgroups_max_level=2,
nested_hostgroups_per_host=5,
direct_sudorules=20, # users, hosts
indirect_sudorules=80, # groups, hostgroups
sudorules_per_user=5,
sudorules_per_group=2,
sudorules_per_host=5,
sudorules_per_hostgroup=5,
direct_hbac=20, # users, hosts
indirect_hbac=80, # groups, hostgroups
hbac_per_user=5,
hbac_per_group=2,
hbac_per_host=5,
hbac_per_hostgroup=5
"""
self.host_prefix = host_prefix
groups = 0
groups_per_user = 0
nested_groups_max_level = 0
nested_groups_per_user = 0
hostgroups = 0
hostgroups_per_host = 0
nested_hostgroups_max_level = 0
nested_hostgroups_per_host = 0
direct_sudorules = 0 # users, hosts
indirect_sudorules = 0 # groups, hostgroups
sudorules_per_user = 0
sudorules_per_group = 0
sudorules_per_host = 0
sudorules_per_hostgroup = 0
direct_hbac = 0 # users, hosts
indirect_hbac = 0 # groups, hostgroups
hbac_per_user = 0
hbac_per_group = 0
hbac_per_host = 0
hbac_per_hostgroup = 0
self.domain = domain
self.basedn = basedn
self.realm = realm
if outfile:
self.stream = open(outfile, "w")
else:
self.stream = sys.stdout
self.users = users
self.groups = groups
self.groups_per_user = groups_per_user
self.nested_groups_max_level = nested_groups_max_level
self.nested_groups_per_user = nested_groups_per_user
self.hosts = hosts
self.hostgroups = hostgroups
self.services = services
self.number_of_subgroups = number_of_subgroups
self.hostgroups_per_host = hostgroups_per_host
self.nested_hostgroups_max_level = nested_hostgroups_max_level
self.nested_hostgroups_per_host = nested_hostgroups_per_host
self.direct_sudorules = direct_sudorules
self.indirect_sudorules = indirect_sudorules
self.sudorules_per_user = sudorules_per_user
self.sudorules_per_group = sudorules_per_group
self.sudorules_per_host = sudorules_per_host
self.sudorules_per_hostgroup = sudorules_per_hostgroup
self.direct_hbac = direct_hbac
self.indirect_hbac = indirect_hbac
self.hbac_per_user = hbac_per_user
self.hbac_per_group = hbac_per_group
self.hbac_per_host = hbac_per_host
self.hbac_per_hostgroup = hbac_per_hostgroup
_sshpubkey = (
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGAX3xAeLeaJggwTqMjxNwa'
'6XHBUAikXPGMzEpVrlLDCZtv00djsFTBi38PkgxBJVkgRWMrcBsr/35lq7P6w'
'8KGIwA8GI48Z0qBS2NBMJ2u9WQ2hjLN6GdMlo77O0uJY3251p12pCVIS/bHRS'
'q8kHO2No8g7KA9fGGcagPfQH+ee3t7HUkpbQkFTmbPPN++r3V8oVUk5LxbryB'
'3UIIVzNmcSIn3JrXynlvui4MixvrtX6zx+O/bBo68o8/eZD26QrahVbA09fiv'
'rn/4h3TM019Eu/c2jOdckfU3cHUV/3Tno5d6JicibyaoDDK7S/yjdn5jhaz8M'
'SEayQvFkZkiF0L public key test'
)
# encrypted value of 'password'
_password = (
'{PBKDF2_SHA256}AAAIAG+ooTntmgayOuMZRJuCMFVykCVWcVF3tw8YTlvFp9'
'KF9UnFYhgpz8fC03o4Jv5KD4rQ4GsdXHIRlg/doyd1xOVFDYUZYAPHjnSpm9X'
'nzIcaS4ZCV/JeLqueyeZQWKOlCH+k9BCmO2HZC7AKXWGMG5bJ8GLALceyf0xi'
'oP447wehg8caWYY7f2VYBz6k233DUkgV5NqswzuEwtLdDDFjpro0Ac15ok6fS'
'0vqjDfiMs0ai5FCbivEKw4j6ZyuXHCpck6gcH8lh33rs779ZaLAgtIeeW+xde'
'a3BDCQXhuHwCqIxxifLn2gQy/lLxlikXb82gk2ClADhwoG5RIdJ3ABrPDB100'
'yw6QJ7NF14vV7DDzqxqylocWD/l1xw8HIYdkCpRG8PyfLz8TOR+Mgq0uB/mdd'
'rSkU6Hsmg9MkGpST69av'
)
utcnow = datetime.utcnow()
twoyears = timedelta(days=365*2)
expiration = (utcnow + twoyears).strftime("%Y%m%d%H%M%SZ")
self.user_defaults = {
'objectClass': [
'ipaobject',
'person',
'top',
'ipasshuser',
'inetorgperson',
'organizationalperson',
'krbticketpolicyaux',
'krbprincipalaux',
'inetuser',
'posixaccount',
'ipaSshGroupOfPubKeys',
'mepOriginEntry'],
'ipaUniqueID': ['autogenerate'],
'loginShell': ['/bin/zsh'],
'uidNumber': ['-1'],
'gidNumber': ['-1'],
'ipaSshPubKey': [_sshpubkey],
'krbExtraData': [
'this-will-not-work-this-is-a-placeholder-for-IPA-framework-'
'dont-try-to-kinit-with-this-because-it-will-blow-up'
],
'krbLastPwdChange': [utcnow.strftime("%Y%m%d%H%M%SZ")],
'krbPasswordExpiration': [expiration],
'userpassword': [_password],
}
self.group_defaults = {
'objectClass': [
'ipaobject',
'top',
'ipausergroup',
'posixgroup',
'groupofnames',
'nestedgroup', ],
'ipaUniqueID': ['autogenerate'],
'gidNumber': [-1],
}
self.host_defaults = {
'objectClass': [
'ipaobject',
'ieee802device',
'nshost',
'ipaservice',
'pkiuser',
'ipahost',
'krbprincipal',
'krbprincipalaux',
'ipasshhost',
'top',
'ipaSshGroupOfPubKeys',
],
'ipaUniqueID': ['autogenerate'],
'ipaSshPubKey': [_sshpubkey],
'krbExtraData': [
'this-will-not-work-this-is-a-placeholder-for-IPA-framework-'
'dont-try-to-kinit-with-this-because-it-will-blow-up'
],
'krbLastPwdChange': ['20160408125354Z'],
'krbPasswordExpiration': ['20160408125354Z'],
}
self.hostgroup_defaults = {
'objectClass': [
'ipahostgroup',
'ipaobject',
'nestedGroup',
'groupOfNames',
'top',
'mepOriginEntry',
],
'ipaUniqueID': ['autogenerate'],
}
self.service_defaults = {
'objectClass': [
'ipakrbprincipal',
'ipaobject',
'ipaservice',
'krbprincipal',
'krbprincipalaux',
'krbticketpolicyaux',
'pkiuser',
'top',
],
'ipaUniqueID': ['autogenerate'],
}
self.sudo_defaults = {
'objectClass': [
'ipasudorule',
'ipaassociation',
],
'ipaEnabledFlag': ['TRUE'],
'ipaUniqueID': ['autogenerate'],
}
self.hbac_defaults = {
'objectClass': [
'ipahbacrule',
'ipaassociation',
],
'ipaEnabledFlag': ['TRUE'],
'ipaUniqueID': ['autogenerate'],
'accessRuleType': ['allow'],
}
def put_entry(self, entry):
"""
Abstract method, implementation depends on if we want just print LDIF,
or update LDAP directly
"""
raise NotImplementedError()
def gen_user(self, uid):
user = dict(self.user_defaults)
user['dn'] = 'uid={uid},cn=users,cn=accounts,{suffix}'.format(
uid=uid,
suffix=self.basedn,
)
user['uid'] = [uid]
user['displayName'] = ['{} {}'.format(uid, uid)]
user['initials'] = ['{}{}'.format(uid[1], uid[-1])]
user['gecos'] = user['displayName']
user['sn'] = [uid]
user['homeDirectory'] = ['/other-home/{}'.format(uid)]
user['mail'] = ['{uid}@{domain}'.format(
uid=uid, domain=self.domain)]
user['krbPrincipalName'] = ['{uid}@{realm}'.format(
uid=uid, realm=self.realm)]
user['krbCanonicalName'] = ['{uid}@{realm}'.format(
uid=uid, realm=self.realm)]
user['givenName'] = [uid]
user['cn'] = ['{} {}'.format(uid, uid)]
return user
def username_generator(self, start, stop, step=1):
for i in range(start, stop, step):
yield 'user%d' % i
def gen_group(self, name, members=(), group_members=()):
group = dict(self.group_defaults)
group['dn'] = 'cn={name},cn=groups,cn=accounts,{suffix}'.format(
name=name,
suffix=self.basedn,
)
group['cn'] = [name]
group['member'] = ['uid={uid},cn=users,cn=accounts,{suffix}'.format(
uid=uid,
suffix=self.basedn,
) for uid in members]
group['member'].extend(
['cn={name},cn=groups,cn=accounts,{suffix}'.format(
name=name,
suffix=self.basedn,
) for name in group_members])
return group
def groupname_generator(self, start, stop, step=1):
for i in range(start, stop, step):
yield 'group%s' % i
def gen_host(self, hostname):
host = dict(self.host_defaults)
host['dn'] = 'fqdn={hostname},cn=computers,cn=accounts,{suffix}'.format(
hostname=hostname,
suffix=self.basedn
)
host['fqdn'] = [hostname]
host['cn'] = [hostname]
host['managedBy'] = [host['dn']]
host['krbPrincipalName'] = ['host/{hostname}@{realm}'.format(
hostname=hostname,
realm=self.realm
)]
host['serverHostName'] = [hostname.split('.')[1]]
return host
def hostname_generator(self, start, stop, step=1):
for i in range(start, stop, step):
yield 'host{}.{}'.format(
i, self.domain
)
def gen_hostgroup(self, name, members=(), group_members=()):
hostgroup = dict(self.hostgroup_defaults)
hostgroup['dn'] = 'cn={name},cn=hostgroups,cn=accounts,{suffix}'.format(
name=name,
suffix=self.basedn
)
hostgroup['cn'] = [name]
hostgroup['member'] = [
'fqdn={hostname},cn=computers,cn=accounts,{suffix}'.format(
hostname=hostname,
suffix=self.basedn
) for hostname in members
]
hostgroup['member'].extend([
'cn={name},cn=hostgroups,cn=accounts,{suffix}'.format(
name=group,
suffix=self.basedn
) for group in group_members
])
return hostgroup
def hostgroupname_generator(self, start, stop, step=1):
for i in range(start, stop, step):
yield 'hostgroup{}'.format(i)
def gen_service(self, servicename, hostname):
service = dict(self.service_defaults)
service['dn'] = 'krbprincipalname={servicename}/{hostname}@{realm},' \
'cn=services,cn=accounts,{suffix}'.format(
servicename=servicename,
hostname=hostname,
realm=self.realm,
suffix=self.basedn
)
service['krbPrincipalName'] = ['{servicename}/{hostname}@{realm}'.format(
servicename=servicename,
hostname=hostname,
realm=self.realm
)]
service['krbCanonicalName'] = service['krbPrincipalName']
service['ipaKrbPrincipalAlias'] = service['krbPrincipalName']
service['managedBy'] = [
'fqdn={hostname},cn=computers,cn=accounts,{suffix}'.format(
hostname=hostname,
suffix=self.basedn)]
return service
def generate_services(self):
for i in range(0, self.hosts, 1):
hostname = '{}{:03d}.{}'.format(
self.host_prefix, i, self.domain
)
for j in range(0, self.services, 1):
service = self.gen_service(f'service{j}', hostname)
self.put_entry(service)
def gen_sudorule(
self, name,
user_members=(), usergroup_members=(),
host_members=(), hostgroup_members=()
):
sudorule = dict(self.sudo_defaults)
sudorule['dn'] = 'ipaUniqueID=autogenerate,cn=sudorules,cn=sudo,{suffix}'.format(
suffix=self.basedn
)
sudorule['cn'] = [name]
sudorule['memberUser'] = [
'uid={username},cn=users,cn=accounts,{suffix}'.format(
username=user,
suffix=self.basedn
) for user in user_members
]
sudorule['memberUser'].extend([
'cn={groupname},cn=groups,cn=accounts,{suffix}'.format(
groupname=group,
suffix=self.basedn
) for group in usergroup_members
])
sudorule['memberHost'] = [
'fqdn={hostname},cn=computers,cn=accounts,{suffix}'.format(
hostname=host,
suffix=self.basedn
) for host in host_members
]
sudorule['memberHost'].extend([
'cn={groupname},cn=hostgroups,cn=accounts,{suffix}'.format(
groupname=group,
suffix=self.basedn
) for group in hostgroup_members
])
return sudorule
def sudoname_generator(self, start, stop, step=1):
for i in range(start, stop, step):
yield 'sudo{}'.format(i)
def gen_hbac(
self, name,
user_members=(), usergroup_members=(),
host_members=(), hostgroup_members=()
):
hbac = dict(self.hbac_defaults)
hbac['dn'] = 'ipaUniqueID=autogenerate,cn=hbac,{suffix}'.format(
suffix=self.basedn
)
hbac['cn'] = [name]
hbac['memberUser'] = [
'uid={username},cn=users,cn=accounts,{suffix}'.format(
username=user,
suffix=self.basedn
) for user in user_members
]
hbac['memberUser'].extend([
'cn={groupname},cn=groups,cn=accounts,{suffix}'.format(
groupname=group,
suffix=self.basedn
) for group in usergroup_members
])
hbac['memberHost'] = [
'fqdn={hostname},cn=computers,cn=accounts,{suffix}'.format(
hostname=host,
suffix=self.basedn
) for host in host_members
]
hbac['memberHost'].extend([
'cn={groupname},cn=hostgroups,cn=accounts,{suffix}'.format(
groupname=group,
suffix=self.basedn
) for group in hostgroup_members
])
return hbac
def hbacname_generator(self, start, stop, step=1):
for i in range(start, stop, step):
yield 'hbac{}'.format(i)
def gen_users_and_groups(self):
self.__gen_entries_with_groups(
self.users,
self.groups,
self.groups_per_user,
self.nested_groups_per_user,
self.nested_groups_max_level,
self.username_generator, self.gen_user,
self.groupname_generator, self.gen_group
)
def gen_hosts_and_hostgroups(self):
self.__gen_entries_with_groups(
self.hosts,
self.hostgroups,
self.hostgroups_per_host,
self.nested_hostgroups_per_host,
self.nested_hostgroups_max_level,
self.hostname_generator, self.gen_host,
self.hostgroupname_generator, self.gen_hostgroup
)
def __gen_entries_with_groups(
self,
num_of_entries,
num_of_groups,
groups_per_entry,
nested_groups_per_entry,
max_nesting_level,
gen_entry_name_f, gen_entry_f,
gen_group_name_f, gen_group_f
):
assert num_of_groups % groups_per_entry == 0
assert num_of_groups >= groups_per_entry
assert groups_per_entry > nested_groups_per_entry
assert max_nesting_level > 0
assert nested_groups_per_entry > 0
assert (
groups_per_entry - nested_groups_per_entry >
int(math.ceil(nested_groups_per_entry / float(max_nesting_level)))
), (
"At least {} groups is required to generate proper amount of "
"nested groups".format(
nested_groups_per_entry +
int(
math.ceil(
nested_groups_per_entry / float(max_nesting_level)
)
)
)
)
for uid in gen_entry_name_f(0, num_of_entries):
self.put_entry(gen_entry_f(uid))
# create N groups per entry, <num_of_nested_groups> of them are nested
# User/Host (max nesting level = 2)
# |
# +--- G1 --- G2 (nested) --- G3 (nested, max level)
# |
# +--- G5 --- G6 (nested)
# |
# ......
# |
# +--- GN
# how many members should be added to groups (set of groups_per_entry
# have the same members)
entries_per_group = num_of_entries // (num_of_groups // groups_per_entry)
# generate groups and put users there
for i in range(num_of_groups // groups_per_entry):
uids = list(gen_entry_name_f(
i * entries_per_group,
(i + 1) * entries_per_group
))
# per user
last_grp_name = None
nest_lvl = 0
nested_groups_added = 0
for group_name in gen_group_name_f(
i * groups_per_entry,
(i + 1) * groups_per_entry,
):
# create nested groups first
if nested_groups_added < nested_groups_per_entry:
if nest_lvl == 0:
# the top group
self.put_entry(
gen_group_f(
group_name,
members=uids
)
)
nest_lvl += 1
nested_groups_added += 1
elif nest_lvl == max_nesting_level:
# the last level group this group is not nested
self.put_entry(
gen_group_f(
group_name,
group_members=[last_grp_name],
)
)
nest_lvl = 0
else:
# mid level group
self.put_entry(
gen_group_f(
group_name,
group_members=[last_grp_name]
)
)
nested_groups_added += 1
nest_lvl += 1
last_grp_name = group_name
else:
# rest of groups have direct membership
if nest_lvl != 0:
# assign the last nested group if exists
self.put_entry(
gen_group_f(
group_name,
members=uids,
group_members=[last_grp_name],
)
)
nest_lvl = 0
else:
self.put_entry(
gen_group_f(
group_name,
members=uids
)
)
def generate_sudorules(self):
self.__generate_entries_with_users_hosts_groups(
self.direct_sudorules,
self.indirect_sudorules,
self.sudorules_per_user,
self.sudorules_per_group,
self.sudorules_per_host,
self.sudorules_per_hostgroup,
self.sudoname_generator, self.gen_sudorule
)
def generate_hbac(self):
self.__generate_entries_with_users_hosts_groups(
self.direct_hbac,
self.indirect_hbac,
self.hbac_per_user,
self.hbac_per_group,
self.hbac_per_host,
self.hbac_per_hostgroup,
self.hbacname_generator, self.gen_hbac
)
def __generate_entries_with_users_hosts_groups(
self,
num_of_entries_direct_members,
num_of_entries_indirect_members,
entries_per_user,
entries_per_group,
entries_per_host,
entries_per_hostgroup,
gen_entry_name_f, gen_entry_f,
):
assert num_of_entries_direct_members % entries_per_user == 0
assert num_of_entries_direct_members % entries_per_host == 0
assert num_of_entries_indirect_members % entries_per_group == 0
assert num_of_entries_indirect_members % entries_per_hostgroup == 0
num_of_entries = num_of_entries_direct_members + num_of_entries_indirect_members
# direct members
users_per_entry = self.users // (num_of_entries_direct_members // entries_per_user)
hosts_per_entry = self.hosts // (num_of_entries_direct_members // entries_per_host)
start_user = 0
stop_user = users_per_entry
start_host = 0
stop_host = hosts_per_entry
for name in gen_entry_name_f(0, num_of_entries_direct_members):
self.put_entry(
gen_entry_f(
name,
user_members=self.username_generator(start_user, stop_user),
host_members=self.hostname_generator(start_host, stop_host)
)
)
start_user = stop_user % self.users
stop_user = start_user + users_per_entry
stop_user = stop_user if stop_user < self.users else self.users
start_host = stop_host % self.hosts
stop_host = start_host + hosts_per_entry
stop_host = stop_host if stop_host < self.hosts else self.hosts
groups_per_entry = self.groups // (
num_of_entries_indirect_members // entries_per_group
)
hostgroups_per_entry = self.hostgroups // (
num_of_entries_indirect_members // entries_per_hostgroup
)
# indirect members
start_group = 0
stop_group = groups_per_entry
start_hostgroup = 0
stop_hostgroup = hostgroups_per_entry
for name in gen_entry_name_f(num_of_entries_direct_members, num_of_entries):
self.put_entry(
gen_entry_f(
name,
usergroup_members=self.groupname_generator(start_group, stop_group),
hostgroup_members=self.hostgroupname_generator(start_hostgroup, stop_hostgroup)
)
)
start_group = stop_group % self.groups
stop_group = start_group + groups_per_entry
stop_group = stop_group if stop_group < self.groups else self.groups
start_hostgroup = stop_hostgroup % self.hostgroups
stop_hostgroup = start_hostgroup + hostgroups_per_entry
stop_hostgroup = stop_hostgroup if stop_hostgroup < self.hostgroups else self.hostgroups
def do_magic(self):
self.gen_users_and_groups()
self.gen_hosts_and_hostgroups()
self.generate_sudorules()
self.generate_hbac()
self.stream.close()
class IPADataLDIF(IPAData):
def put_entry(self, entry):
print(file=self.stream)
print("dn: ", entry['dn'], file=self.stream)
for k, values in entry.items():
if k == 'dn':
continue
for v in values:
print("{}: {}".format(k, v), file=self.stream)
print(file=self.stream)
class IPATestDataLDIF(IPADataLDIF):
def do_magic(self):
self.gen_users_and_groups()
self.generate_services()
def username_generator(self, start, stop, step=1, hostname=None):
for i in range(start, stop, step):
yield 'user%d%s' % (i, hostname)
def hostname_generator(self, start, stop, step=1):
for i in range(start, stop, step):
yield '{}{:03d}.{}'.format(
self.host_prefix, i, self.domain
)
def gen_users_and_groups(self):
members = []
group_members = []
groupnum = 0
users_per_subgroup = 0
if self.number_of_subgroups > 0:
users_per_subgroup = (self.hosts * self.users) // self.number_of_subgroups
for host in self.hostname_generator(0, self.hosts):
for uid in self.username_generator(0, self.users,
hostname=host):
self.put_entry(self.gen_user(uid))
members.append(uid)
if (
self.number_of_subgroups
and len(members) == users_per_subgroup
):
group_name = "group{}".format(groupnum)
groupnum += 1
self.put_entry(self.gen_group(group_name, members))
members = []
group_members.append(group_name)
if self.number_of_subgroups > 0:
if len(members):
group_name = "group{}".format(groupnum)
self.put_entry(self.gen_group(group_name, members))
self.put_entry(self.gen_group("allusers", group_members=group_members))
else:
self.put_entry(self.gen_group('allusers', members))
def put_entry(self, entry):
print(file=self.stream)
print("dn: ", entry['dn'], file=self.stream)
for k, values in entry.items():
if k == 'dn':
continue
for v in values:
print("{}: {}".format(k, v), file=self.stream)
print(file=self.stream)
@click.command("cli", context_settings={"show_default": True})
@click.option("--users-per-host", default=10,
help="Number of users to create for each host.",
type=int)
@click.option("--hosts", default=500, help="Number of hosts to create.",
type=int)
@click.option("--host-prefix", default="client", help="hostname prefix")
@click.option("--services", default=0, help="Number of services per host to create.",
type=int)
@click.option("--outfile", default=None, help="LDIF output file")
@click.option("--with-groups", default=False, help="Create user groups.",
is_flag=True)
@click.option("--with-hostgroups", default=False, help="Create host groups.",
is_flag=True)
@click.option("--with-sudo", default=False, help="Create sudo rules.",
is_flag=True)
@click.option("--with-hbac", default=False, help="Create hbac rules.",
is_flag=True)
@click.option("--debug", default=False, help="Debug logging", is_flag=True)
@click.option("--number-of-subgroups", default=0, help="Number of subgroups to create.")
def main(
users_per_host,
hosts,
host_prefix,
services,
with_groups,
with_hostgroups,
with_sudo,
with_hbac,
debug,
outfile,
number_of_subgroups,
):
api.bootstrap(in_server=True, context='server', in_tree=False, debug=debug)
api.finalize()
data = IPATestDataLDIF(
api.env.domain,
api.env.basedn,
api.env.realm,
users=users_per_host,
hosts=hosts,
host_prefix=host_prefix,
services=services,
number_of_subgroups=number_of_subgroups,
outfile=outfile,
)
data.do_magic()
if __name__ == '__main__':
main()