-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreport-modules-plugins.py
1357 lines (1220 loc) · 47.3 KB
/
report-modules-plugins.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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import os
import sys
import logging
import re
from pathlib import Path
import jinja2
import yaml
import json
import argparse
from ansible.errors import AnsibleParserError
from ansible.parsing.dataloader import DataLoader
from ansible.parsing.mod_args import ModuleArgsParser
from ansible.parsing.yaml.objects import AnsibleSequence, AnsibleMapping
from ansible.template import Templar
from ansible.plugins.loader import (
filter_loader,
lookup_loader,
module_loader,
test_loader,
)
if os.environ.get("LSR_DEBUG") == "true":
logging.getLogger().setLevel(logging.DEBUG)
COLLECTION_DIRS = ["playbooks", "plugins", "roles", "tests"]
ANSIBLE_BUILTIN = "ansible.builtin"
JINJA2_BUILTIN = "jinja2"
LOCAL = "local"
COLLECTION_BUILTINS = {ANSIBLE_BUILTIN, JINJA2_BUILTIN}
ROLE_DIRS = [
"meta",
"defaults",
"examples",
"files",
"filter_plugins",
"handlers",
"library",
"module_utils",
"playbooks",
"roles",
"tasks",
"templates",
"tests",
"vars",
]
PLAY_KEYS = {
"gather_facts",
"handlers",
"hosts",
"import_playbook",
"post_tasks",
"pre_tasks",
"roles",
"tasks",
}
TASK_LIST_KWS = [
"always",
"block",
"handlers",
"post_tasks",
"pre_tasks",
"rescue",
"tasks",
]
# these are collection tests/ sub-directories that we know we do not
# need to process
SKIP_COLLECTION_TEST_DIRS = ["unit", "pytests", "files"]
def get_role_name(role_path):
dir_pth = Path(role_path)
if dir_pth.parts[-2] == "roles":
return dir_pth.parts[-3] + "." + dir_pth.parts[-1]
else:
return dir_pth.parts[-1]
def get_file_type(item):
if isinstance(item, AnsibleMapping):
if "galaxy_info" in item or "dependencies" in item:
return "meta"
return "vars"
elif isinstance(item, AnsibleSequence):
return "tasks"
else:
return "unknown"
def get_item_type(item):
if isinstance(item, AnsibleMapping):
for key in PLAY_KEYS:
if key in item:
return "play"
if "block" in item:
return "block"
return "task"
else:
raise Exception(f"Error: unknown type of item: {item}")
def handle_other(item, filectx):
"""handle properties of Ansible item other than vars and tasks"""
if "when" in item:
find_plugins_for_when_that(item["when"], filectx)
def __do_handle_vars(vars, filectx):
if vars is None or not vars:
return
elif isinstance(vars, (int, bool, float)):
return
elif isinstance(vars, list):
for item in vars:
__do_handle_vars(item, filectx)
elif isinstance(vars, dict):
for key, item in vars.items():
if filectx.filename.endswith("defaults/main.yml"):
filectx.rolevars.add(key)
__do_handle_vars(item, filectx)
elif filectx.templar.is_template(vars):
find_plugins(vars, filectx)
def handle_vars(item, filectx):
"""handle vars of Ansible item"""
__do_handle_vars(item.get("vars"), filectx)
def handle_meta(item, filectx):
"""handle meta/main.yml file"""
pass
PLUGIN_BUILTINS = set(["lookup", "q", "query"])
def is_builtin(plugin):
return plugin in PLUGIN_BUILTINS
jinja2_macros = set()
class PluginItem(object):
def __init__(
self,
collection,
name,
type,
used_in_collection,
used_in_role,
relpth,
lineno,
is_test,
):
"""Name might be plain or fqcn."""
ary = self.split_fqcn(name)
self.name = ary[-1]
self.collection = collection
if collection != LOCAL and len(ary) > 1 and not ary[0] == collection:
raise Exception(
f"Given collection name {collection} does not match the plugin FQCN {name}"
)
self.type = type
self.relpth = relpth
self.lineno = lineno
self.is_test = is_test
self.used_in_collection = used_in_collection
self.used_in_role = used_in_role
self.orig_name = name
def split_fqcn(self, name):
ary = name.rsplit(".", 1)
if len(ary) > 1:
if ary[0] == "ansible.posix.system":
# special case for some ansible.posix tests that use
# ansible.posix.system.selinux:
ary[0] = "ansible.posix"
elif ary[0] == "community.general.system":
ary[0] = "community.general"
return ary
def has_correct_fqcn(self):
"""The local plugin has correct FQCN if it matches the collection it was used in."""
ary = self.split_fqcn(self.orig_name)
if len(ary) < 2:
return False # not FQCN
if self.collection == LOCAL:
expected = self.used_in_collection + "." + self.name
return expected == self.orig_name
return True
class NumericOpItem(object):
def __init__(
self,
collection_name,
role_name,
varname,
opname,
value,
relpth,
lineno,
is_test,
):
self.collection_name = collection_name
self.role_name = role_name
self.varname = varname
self.opname = opname
self.value = value
self.relpth = relpth
self.lineno = lineno
self.is_test = is_test
def __str__(self):
if self.collection_name:
where = self.collection_name + "."
else:
where = ""
where = where + self.role_name
return f"[{self.varname}] [{self.opname}] [{self.value}] at {where} {self.relpth}:{self.lineno}"
def node2plugin_type(nodetype):
if nodetype == jinja2.nodes.Filter:
return "filter"
elif nodetype == jinja2.nodes.Test:
return "test"
elif nodetype == jinja2.nodes.Macro:
return "macro"
else:
return "module"
MATH_OPS = (
jinja2.nodes.Add,
jinja2.nodes.Div,
jinja2.nodes.Mod,
jinja2.nodes.Mul,
jinja2.nodes.Neg,
jinja2.nodes.Pos,
jinja2.nodes.Pow,
jinja2.nodes.Sub,
)
# Look for places where a public variable is used in some
# sort of numeric operation that might require casting
# the variable to some numeric type. e.g.
# {{ if my_float_var < 1.0 }}
# should be
# {{ if my_float_var | float < 1.0 }}
# so comparisons, arithmetic, unary
# {{ 2 + my_int_var }}
# {{ -my_int_var }}
# These should be cast to int
# The return value is the variable name, the operation,
# and the value (or 0 for unary ops)
# The report at the end will also specify the location where
# the usage occurred
def get_bare_numeric_op(jinja_node, filectx):
var_name, op_name, value = None, None, None
if isinstance(jinja_node, jinja2.nodes.Compare):
if (
isinstance(jinja_node.expr, jinja2.nodes.Name)
and jinja_node.expr.name in filectx.rolevars
and isinstance(jinja_node.ops[0], jinja2.nodes.Operand)
and isinstance(jinja_node.ops[0].expr, jinja2.nodes.Const)
and isinstance(jinja_node.ops[0].expr.value, (int, float))
):
var_name = jinja_node.expr.name
op_name = jinja_node.ops[0].op
value = jinja_node.ops[0].expr.value
elif (
isinstance(jinja_node.expr, jinja2.nodes.Const)
and isinstance(jinja_node.expr.value, (int, float))
and isinstance(jinja_node.ops[0], jinja2.nodes.Operand)
and isinstance(jinja_node.ops[0].expr, jinja2.nodes.Name)
and jinja_node.ops[0].expr.name in filectx.rolevars
):
var_name = jinja_node.ops[0].expr.name
op_name = jinja_node.ops[0].op
value = jinja_node.expr.value
elif isinstance(jinja_node, (jinja2.nodes.Neg, jinja2.nodes.Pos)):
if (
isinstance(jinja_node.node, jinja2.nodes.Name)
and jinja_node.node.name in filectx.rolevars
):
var_name = jinja_node.node.name
op_name = jinja_node.operator
value = 0 # unary operator, no other value
else:
if isinstance(jinja_node.left, jinja2.nodes.Name):
var_name = jinja_node.left.name
other = jinja_node.right
elif isinstance(jinja_node.right, jinja2.nodes.Name):
var_name = jinja_node.right.name
other = jinja_node.left
if (
var_name in filectx.rolevars
and isinstance(other, jinja2.nodes.Const)
and isinstance(other.value, (int, float))
):
op_name = jinja_node.operator
value = other.value
else:
var_name = None
return var_name, op_name, value
def find_plugins(args, filectx):
if args is None or not args:
return
if isinstance(args, bytes):
args = args.decode()
if isinstance(args, str):
try:
tmpl = filectx.templar.environment.parse(source=args)
except jinja2.exceptions.TemplateSyntaxError:
logging.warning(
f"the string [{args}] could not be processed as a Jinja2 template "
f"at {filectx.filename}:{filectx.get_lineno(1)}"
)
return
node_types = (
jinja2.nodes.Call,
jinja2.nodes.Filter,
jinja2.nodes.Test,
jinja2.nodes.Macro,
jinja2.nodes.Compare,
) + MATH_OPS
for item in tmpl.find_all(node_types):
if isinstance(item, MATH_OPS) or isinstance(item, jinja2.nodes.Compare):
var_name, op_name, value = get_bare_numeric_op(item, filectx)
if var_name:
filectx.add_bare_numeric_op(
var_name, op_name, value, filectx.get_lineno(item.lineno)
)
continue
elif hasattr(item, "name"):
item_name = item.name
elif hasattr(item.node, "name"):
item_name = item.node.name
elif isinstance(item.node, jinja2.nodes.Getattr):
logging.debug(f"\tskipping getattr call {item}")
continue
else:
logging.warning(
f"unknown item {item} at {filectx.filename}:{item.lineno}"
)
continue
if isinstance(item, jinja2.nodes.Macro):
global jinja2_macros
jinja2_macros.add(item_name)
logging.debug(f"\titem {item_name} {item.__class__}")
continue
filectx.add_plugin(
item_name, item.__class__, filectx.get_lineno(item.lineno)
)
if item_name in ["selectattr", "rejectattr"] and len(item.args) > 1:
filectx.add_plugin(
item.args[1].value,
jinja2.nodes.Test,
filectx.get_lineno(item.lineno),
)
if item_name in ["select", "reject"] and item.args:
filectx.add_plugin(
item.args[0].value,
jinja2.nodes.Test,
filectx.get_lineno(item.lineno),
)
if item_name == "map" and item.args:
filectx.add_plugin(
item.args[0].value,
jinja2.nodes.Filter,
filectx.get_lineno(item.lineno),
)
if item_name in ["lookup", "query", "q"] and item.args:
filectx.add_plugin(
item.args[0].value,
jinja2.nodes.Call,
filectx.get_lineno(item.lineno),
)
elif isinstance(args, list):
for item in args:
find_plugins(item, filectx)
elif isinstance(args, dict):
for item in args.values():
find_plugins(item, filectx)
elif isinstance(args, (bool, int, float)):
pass
else:
logging.error(
"Ignoring module argument %s of type %s at %s:%s",
args,
args.__class__,
filectx.filename,
filectx.get_lineno(1),
)
return
def find_plugins_for_when_that(val, filectx):
"""when or that - val can be string or list"""
if val is None or isinstance(val, (bool, int, float)):
pass
elif isinstance(val, list):
for item in val:
find_plugins_for_when_that(item, filectx)
else:
if filectx.templar.is_template(val):
find_plugins(val, filectx)
else:
find_plugins("{{ " + val + " }}", filectx)
def handle_task(task, filectx):
"""handle a single task"""
mod_arg_parser = ModuleArgsParser(task)
try:
action, args, _ = mod_arg_parser.parse(skip_action_validation=True)
except AnsibleParserError as e:
logging.warning("Couldn't parse task at %s (%s)\n%s" % (task, e.message, task))
return
filectx.lineno = task.ansible_pos[1]
if filectx.templar.is_template(args):
logging.debug(f"\tmodule {action} has template {args}")
find_plugins(args, filectx)
elif action == "assert":
find_plugins_for_when_that(args["that"], filectx)
else:
logging.debug(f"\tmodule {action} has no template {args}")
if "when" in task:
find_plugins_for_when_that(task["when"], filectx)
filectx.add_plugin(action, "module", task.ansible_pos[1])
def handle_tasks(item, filectx):
"""item has one or more fields which hold a list of Task objects"""
for kw in TASK_LIST_KWS:
if kw in item:
for task in item[kw]:
handle_item(task, filectx)
def handle_item(item, filectx):
handle_vars(item, filectx)
item_type = get_item_type(item)
if item_type == "task":
handle_task(item, filectx)
else:
handle_other(item, filectx)
handle_tasks(item, filectx)
def os_walk(from_path):
if os.path.isdir(from_path) and not os.path.islink(from_path):
for dirpath, _, filenames in os.walk(from_path):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
yield filepath
def os_listdir(from_path):
if os.path.isdir(from_path) and not os.path.islink(from_path):
for dirent in os.scandir(from_path):
if dirent.is_symlink():
continue
yield dirent.name, dirent.path
def process_yml_file(filepath, ctx):
ctx.filename = filepath
ctx.lineno = 0
if filepath.endswith("/vault-variables.yml"):
logging.debug(f"skipping vault-variables.yml file {filepath}")
return
dl = DataLoader()
ans_data = dl.load_from_file(filepath)
if ans_data is None:
logging.debug(f"file is empty {filepath}")
return
file_type = get_file_type(ans_data)
if file_type == "vars":
__do_handle_vars(ans_data, ctx)
elif file_type == "meta":
handle_meta(ans_data, ctx)
elif ctx.in_tests() and filepath.endswith("requirements.yml"):
handle_meta(ans_data, ctx)
elif file_type == "unknown":
logging.warning("Skipping file of unknown type: %s", filepath)
else:
for item in ans_data:
handle_item(item, ctx)
ctx.filename = None
ctx.lineno = 0
def process_template(filepath, ctx):
ctx.filename = filepath
ctx.lineno = 0
find_plugins(open(filepath).read(), ctx)
ctx.filename = None
ctx.lineno = 0
def process_templates_path(templates_path, ctx):
for filepath in os_walk(templates_path):
process_template(filepath, ctx)
def process_ansible_file(filepath, ctx):
if filepath.endswith(".yml"):
process_yml_file(filepath, ctx)
elif filepath.endswith(".yaml"):
process_yml_file(filepath, ctx)
elif filepath.endswith(".j2"):
process_template(filepath, ctx)
def process_ansible_yml_path(yml_path, ctx):
"""For role directories like tasks/, defaults/, vars/"""
for filepath in os_walk(yml_path):
process_ansible_file(filepath, ctx)
def process_reqs_file(path, ctx):
legacy_rqf = "requirements.yml"
coll_rqf = "collection-requirements.yml"
for rqf in [legacy_rqf, coll_rqf]:
reqs_file = os.path.join(path, rqf)
if os.path.isfile(reqs_file):
reqs = yaml.safe_load(open(reqs_file))
if isinstance(reqs, dict):
if ctx.in_tests():
ctx.role_test_reqs = reqs
else:
ctx.role_reqs = reqs
ctx.add_dependencies()
if rqf == legacy_rqf:
logging.warning(
"Still using %s - please convert to %s instead",
reqs_file,
coll_rqf,
)
def process_role_meta_path(meta_path, ctx):
process_reqs_file(meta_path, ctx)
meta_main = os.path.join(meta_path, "main.yml")
if os.path.isfile(meta_main) and not os.path.islink(meta_main):
process_yml_file(meta_main, ctx)
def process_playbooks_path(playbooks_path, ctx):
# see if there are any local plugins first
ctx.add_local_plugins(playbooks_path, "library")
ctx.add_local_plugins(playbooks_path, "filter_plugins")
for _, itempath in os_listdir(playbooks_path):
if os.path.isfile(itempath):
process_ansible_file(itempath, ctx)
process_role(playbooks_path, False, ctx)
def process_role_tests_path(tests_path, ctx):
"""Treat like a playbooks/ directory."""
ctx.enter_tests()
process_reqs_file(tests_path, ctx)
process_playbooks_path(tests_path, ctx)
ctx.exit_tests()
def process_integration_tests(integration_path, ctx):
"""Not sure what could be in here - just process as if it contains
directories of plain old ansible yml files like tasks/
except for the targets/ directory which is like a roles/
directory"""
ctx.in_collection_integration_tests = True
for dirname, itempath in os_listdir(integration_path):
if dirname == "files":
continue
elif dirname == "targets":
process_roles_path(itempath, ctx)
else:
process_ansible_yml_path(itempath, ctx)
ctx.in_collection_integration_tests = False
def process_role(role_path, is_real_role, ctx):
if is_real_role:
if ctx.found_role_name is None:
ctx.found_role_name = get_role_name(role_path)
ctx.enter_role(ctx.found_role_name, role_path)
ctx.add_local_plugins(role_path, "library")
ctx.add_local_plugins(role_path, "filter_plugins")
# process defaults to get role public api variables
dirname = "defaults"
dirpath = os.path.join(role_path, dirname)
if os.path.isdir(dirpath) and not os.path.islink(dirpath):
process_ansible_yml_path(dirpath, ctx)
for dirname in ROLE_DIRS:
if dirname == "defaults" or dirname == "files":
continue
dirpath = os.path.join(role_path, dirname)
if not os.path.isdir(dirpath) or os.path.islink(dirpath):
continue
elif dirname == "meta":
process_role_meta_path(dirpath, ctx)
elif dirname == "templates":
process_templates_path(dirpath, ctx)
elif dirname == "tests":
process_role_tests_path(dirpath, ctx)
elif dirname == "playbooks":
process_playbooks_path(dirpath, ctx)
elif dirname == "roles":
process_roles_path(dirpath, ctx)
else:
process_ansible_yml_path(dirpath, ctx)
if is_real_role:
ctx.exit_role()
def process_roles_path(pathname, ctx):
"""Pathname is the name of a directory containing one or more role subdirectories."""
if not os.path.isdir(pathname) or os.path.islink(pathname):
return
for item_name, role_path in os_listdir(pathname):
if item_name.startswith(".git"):
continue
if ctx.is_role(role_path):
ctx.found_role_name = item_name
process_role(role_path, True, ctx)
elif ctx.in_collection_integration_tests:
process_ansible_yml_path(role_path, ctx)
else:
logging.warning(f"Unexpected item {role_path} - not a role")
def process_collection_tests(pathname, ctx):
"""Look for system roles tests"""
ctx.enter_tests()
for dirname, dirpath in os_listdir(pathname):
if dirname == "integration" and os.path.isdir(dirpath):
process_integration_tests(dirpath, ctx)
elif os.path.isfile(os.path.join(dirpath, "tests_default.yml")):
ctx.enter_role(dirname, dirpath)
process_role_tests_path(dirpath, ctx)
ctx.exit_role()
elif os.path.isdir(dirpath) and dirname in SKIP_COLLECTION_TEST_DIRS:
continue
elif os.path.isfile(dirpath):
process_ansible_file(dirpath, ctx)
elif os.path.isdir(dirpath):
# don't know what this is - process like ansible yml files
process_ansible_yml_path(dirpath, ctx)
ctx.exit_tests()
def process_collection_roles(pathname, ctx):
process_roles_path(pathname, ctx)
def get_collection_plugins(pathname, ctx):
plugin_dir = os.path.join(pathname, "plugins")
if os.path.isdir(plugin_dir):
for dirname, _ in os_listdir(plugin_dir):
ctx.add_local_plugins(plugin_dir, dirname)
def process_collection(pathname, ctx):
"""Pathname is a directory like /path/to/ansible_collections/NAMESPACE/NAME."""
collection_pth = Path(pathname)
if ctx.found_collection_name is None:
ctx.found_collection_name = ".".join(collection_pth.parts[-2:])
ctx.enter_collection(ctx.found_collection_name, pathname)
ctx.add_dependencies()
get_collection_plugins(pathname, ctx)
process_collection_roles(str(collection_pth / "roles"), ctx)
process_collection_tests(str(collection_pth / "tests"), ctx)
ctx.exit_collection()
def process_collections(pathname, ctx):
for namespace, coll_path in os_listdir(pathname):
if not os.path.isdir(coll_path):
logging.warning(
f"Unexpected item {coll_path} is not a collection directory"
)
continue
for name, collection_path in os_listdir(coll_path):
ctx.found_collection_name = namespace + "." + name
process_collection(collection_path, ctx)
def process_path(pathname, ctx):
pathname = os.path.abspath(pathname)
ary = os.path.split(pathname)
if ary[-1] == "ansible_collections":
process_collections(pathname, ctx)
elif ary[-1] == "roles":
process_roles_path(pathname, ctx)
elif ctx.is_collection(pathname):
process_collection(pathname, ctx)
elif ctx.is_role(pathname):
process_role(pathname, True, ctx)
else:
logging.warning(f"{pathname} is not a recognized path - skipping")
def collection_match(coll1, coll2):
return coll1 == coll2 or coll2.startswith(coll1 + ".")
def is_builtin_collection(collection):
for coll in COLLECTION_BUILTINS:
if collection_match(coll, collection):
return True
return False
class SearchCtx(object):
def __init__(self):
self.reset()
def reset(self):
self.plugins = []
self.collection_name = []
self.role_name = []
self.templar = Templar(loader=None)
self.local_plugins = [] # plugins defined by the collection/role being scanned
self.found_collection_name = None
self.found_role_name = None
self.tests_stack = []
self.pathname = None
self.filename = None
self.role_pathname = None
self.lineno = 0
self.in_collection_integration_tests = False
self.errors = []
self.dependencies = [] # collection and/or role dependencies
self.test_dependencies = [] # collection and/or role dependencies for tests
self.role_reqs = {} # role meta/collection-requirements.yml, if any
self.role_test_reqs = {} # role tests/collection-requirements.yml, if any
self.manifest_json = {}
self.galaxy_yml = {}
self.rolevars = set()
self.numeric_ops = []
def enter_collection(self, collection_name, pathname):
if len(self.collection_name) > 0:
raise Exception(
f"Error: cannot enter collection {collection_name} - already in collection {self.collection_name}"
)
self.collection_name.append(collection_name)
self.local_plugins.insert(0, set())
self.dependencies.insert(0, set())
self.test_dependencies.insert(0, set())
self.pathname = pathname
def exit_collection(self):
if len(self.collection_name) < 1:
raise Exception("Error: cannot exit collection - not in a collection")
self.collection_name.pop()
self.found_collection_name = None
if len(self.local_plugins) > 0:
del self.local_plugins[0]
if len(self.dependencies) > 0:
del self.dependencies[0]
if len(self.test_dependencies) > 0:
del self.test_dependencies[0]
self.pathname = None
self.manifest_json = {}
self.galaxy_yml = {}
def enter_role(self, role_name, pathname):
if self.in_tests():
self.role_name.append("tests")
if len(self.role_name) == 0:
self.role_pathname = pathname
self.role_name.append(role_name)
self.local_plugins.insert(0, set())
self.dependencies.insert(0, set())
self.test_dependencies.insert(0, set())
def exit_role(self):
if len(self.role_name) < 1:
raise Exception("Error: cannot exit role - not in a role")
self.role_name.pop()
if self.in_tests():
self.role_name.pop()
self.found_role_name = None
if len(self.local_plugins) > 0:
del self.local_plugins[0]
if len(self.dependencies) > 0:
del self.dependencies[0]
if len(self.test_dependencies) > 0:
del self.test_dependencies[0]
if len(self.role_name) == 0:
self.role_pathname = None
self.role_reqs = {}
self.role_test_reqs = {}
def enter_tests(self):
self.tests_stack.append(True)
def exit_tests(self):
if self.in_tests():
self.tests_stack.pop()
def in_tests(self):
return len(self.tests_stack) > 0
def get_collection(self):
if len(self.collection_name) > 0:
return self.collection_name[-1]
else:
return None
def get_role_fq(self):
if len(self.role_name) > 0:
return ".".join(self.role_name)
else:
return None
def get_role_current(self):
if len(self.role_name) > 0:
return self.role_name[-1]
else:
return None
def is_collection(self, pathname):
manifest_json = Path(os.path.join(pathname, "MANIFEST.json"))
if manifest_json.is_file():
with open(manifest_json) as mjf:
hsh = json.load(mjf)
self.found_collection_name = (
hsh["collection_info"]["namespace"]
+ "."
+ hsh["collection_info"]["name"]
)
self.manifest_json = hsh
return True
galaxy_yml = Path(os.path.join(pathname, "galaxy.yml"))
if galaxy_yml.is_file():
with open(galaxy_yml) as gyf:
hsh = yaml.safe_load(gyf)
self.found_collection_name = hsh["namespace"] + "." + hsh["name"]
self.galaxy_yml = hsh
return True
self.found_collection_name = None
self.manifest_json = {}
self.galaxy_yml = {}
return False
def is_role(self, pathname):
tasks_main = Path(os.path.join(pathname, "tasks", "main.yml"))
if not tasks_main.is_file():
tasks_main = Path(os.path.join(pathname, "tasks", "main.yaml"))
if tasks_main.is_file():
self.found_role_name = tasks_main.parts[-3]
return True
self.found_role_name = None
return False
def _load_plugins_from_file(self, plugin_subdir, plugin_file, plugins):
filename = str(plugin_subdir / plugin_file)
file_str = open(filename).read()
match = re.search(r"\n(class FilterModule.*?)(\n\S|$)", file_str, re.S)
if match and match.group(1):
myg = {}
myl = {}
code_str = match.group(1) + "\n"
while True:
try:
exec(code_str, myg, myl)
fm = myl["FilterModule"]()
fltrs = fm.filters()
plugins.update(set(fltrs.keys()))
break
except NameError as ne:
match = re.match(r"^name '(\S+)' is not defined$", str(ne))
if match and match.groups() and match.group(1):
myg[match.group(1)] = True
myl[match.group(1)] = True
else:
logging.error(
"unable to parse filter plugins from {filename}: {code_str}"
)
raise ne
match = re.search(r"\n(class TestModule.*?)(\n\S|$)", file_str, re.S)
if match and match.group(1):
myg = {}
myl = {}
code_str = match.group(1) + "\n"
while True:
try:
exec(code_str, myg, myl)
fm = myl["TestModule"]()
tests = fm.tests()
plugins.update(set(tests.keys()))
break
except NameError as ne:
match = re.match(r"^name '(\S+)' is not defined$", str(ne))
if match and match.groups() and match.group(1):
myg[match.group(1)] = True
myl[match.group(1)] = True
else:
logging.error(
"unable to parse test plugins from {filename}: {code_str}"
)
raise ne
# I think this method is not possible - loading an entire module has a
# much larger chance of random code execution than just the FilterModule
# part of the code
# def _load_plugins_from_file(self, plugin_subdir, plugin_file, plugins):
# filename = str(plugin_subdir / plugin_file)
# module_name = filename.replace("/", ".")
# spec = importlib.util.spec_from_file_location(module_name, filename)
# module = importlib.util.module_from_spec(spec)
# sys.modules[module_name] = module
# spec.loader.exec_module(module)
def add_local_plugins(self, parent_dir, plugin_dir):
if plugin_dir == "module_utils":
return
plugin_subdir = Path(os.path.join(parent_dir, plugin_dir))
if plugin_subdir.is_dir():
for plugin_file in plugin_subdir.iterdir():
plugins = set()
if plugin_file.is_file() and str(plugin_file).endswith(".py"):
try:
self._load_plugins_from_file(
plugin_subdir, plugin_file, plugins
)
except Exception as exc:
logging.error(
"Could not parse plugins from {plugin_file} - skipping"
)
logging.debug("Exception %s", exc)
continue
if (
not plugins
and plugin_file.is_file()
and plugin_file.stem != "__init__"
):
# assumes local plugins can be referred to by FQCN but not FQRN
plugins.add(plugin_file.stem)
if plugins:
self.local_plugins[0].update(plugins)
collection_name = self.get_collection()
if collection_name:
for plugin in plugins:
self.local_plugins[0].add(collection_name + "." + plugin)
def is_local_plugin(self, plugin_name):
for plugin_set in self.local_plugins:
if plugin_name in plugin_set:
return True
return False
def add_plugin(self, plugin_name, plugin_type, lineno):
pathname = self.pathname
if pathname is None:
pathname = self.role_pathname
pth = Path(pathname)
fpth = Path(self.filename)
relpth = str(fpth.relative_to(pth))
collection_name = self.get_collection()
role_name = self.get_role_fq()
if plugin_name == "include_role" or plugin_name == "import_role":
collection = ANSIBLE_BUILTIN
plugin_type = "module"
elif self.is_local_plugin(plugin_name):
collection = LOCAL
plugin_type = node2plugin_type(plugin_type)
logging.debug(
f"\tplugin {plugin_name}:{plugin_type} at {relpth}:{lineno} is local to the collection/role"
)
elif self.templar.is_template(plugin_name):
logging.warning(
f"Unable to find plugin from template [{plugin_name}] at {relpth}:{lineno}"
)
return
else:
collection, plugin_type = self.get_plugin_collection(
plugin_name,