From 41ea4a901bb02874b7244b5120aa6c95b1a5d0f7 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 5 Aug 2022 18:25:00 +0530 Subject: [PATCH 01/69] Common: Add IEventQueue --- monkey/common/event_queue/__init__.py | 0 monkey/common/event_queue/i_event_queue.py | 53 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 monkey/common/event_queue/__init__.py create mode 100644 monkey/common/event_queue/i_event_queue.py diff --git a/monkey/common/event_queue/__init__.py b/monkey/common/event_queue/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py new file mode 100644 index 000000000..c0de3aeab --- /dev/null +++ b/monkey/common/event_queue/i_event_queue.py @@ -0,0 +1,53 @@ +from abc import ABC, abstractmethod +from typing import Any, Callable, Sequence + +from common.events import AbstractEvent + + +class IEventQueue(ABC): + """ + Manages subscription and publishing of events + """ + + @abstractmethod + def subscribe_all(self, subscriber: Callable[..., Any]): + """ + Subscribes a subscriber to all events + + :param subscriber: Callable that should subscribe to events + """ + + pass + + @abstractmethod + def subscribe_types(self, types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + """ + Subscribes a subscriber to all specifed event types + + :param types: Event types to which the subscriber should subscribe + :param subscriber: Callable that should subscribe to events + """ + + pass + + @abstractmethod + def subscribe_tags(self, tags: Sequence[str], subscriber: Callable[..., Any]): + """ + Subscribes a subscriber to all specified event tags + + :param tags: Event tags to which the subscriber should subscribe + :param subscriber: Callable that should subscribe to events + """ + + pass + + @abstractmethod + def publish(self, event: AbstractEvent, data: Any): + """ + Publishes an event with the given data + + :param event: Event to publish + :param data: Data to pass to subscribers with the event publish + """ + + pass From b027686b2f8928cba4d52dfd9bb84a3afab9b2e1 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 5 Aug 2022 18:44:41 +0530 Subject: [PATCH 02/69] Island: Add pypubsub as a dependency --- monkey/monkey_island/Pipfile | 1 + monkey/monkey_island/Pipfile.lock | 73 +++++++++++++++++-------------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/monkey/monkey_island/Pipfile b/monkey/monkey_island/Pipfile index af867d904..ee3bdd71a 100644 --- a/monkey/monkey_island/Pipfile +++ b/monkey/monkey_island/Pipfile @@ -33,6 +33,7 @@ readerwriterlock = "*" pymongo = "*" cryptography = "*" semantic-version = "*" +pypubsub = "*" [dev-packages] virtualenv = ">=20.0.26" diff --git a/monkey/monkey_island/Pipfile.lock b/monkey/monkey_island/Pipfile.lock index 52894413a..0ac6b6c49 100644 --- a/monkey/monkey_island/Pipfile.lock +++ b/monkey/monkey_island/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "2af80b266ceacbce17f2f3a7bfbc08dbf2429ebb5901357c93b1fb17b380ca4b" + "sha256": "82ce85b7c4c14c663026a659d3f35f869af944ba5dd52ed8cfe6ff83b20e9633" }, "pipfile-spec": 6, "requires": { @@ -32,11 +32,11 @@ }, "attrs": { "hashes": [ - "sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4", - "sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd" + "sha256:29adc2665447e5191d0e7c568fde78b21f9672d344281d0c6e1ab085429b22b6", + "sha256:86efa402f67bf2df34f51a335487cf46b1ec130d02b8d39fd248abfd30da551c" ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", - "version": "==21.4.0" + "markers": "python_version >= '3.5'", + "version": "==22.1.0" }, "bcrypt": { "hashes": [ @@ -210,19 +210,19 @@ }, "flask": { "hashes": [ - "sha256:15972e5017df0575c3d6c090ba168b6db90259e620ac8d7ea813a396bad5b6cb", - "sha256:9013281a7402ad527f8fd56375164f3aa021ecfaff89bfe3825346c24f87e04c" + "sha256:3c604c48c3d5b4c63e72134044c0b4fe90ff01ef65280b9fe2d38c8860d99fe5", + "sha256:9c2b81b9b1edcc835af72d600f1955e713a065e7cb41d7e51ee762b449d9c65d" ], "index": "pypi", - "version": "==2.1.3" + "version": "==2.2.1" }, "flask-jwt-extended": { "hashes": [ - "sha256:793f9e720d0e679cea8f99af6436819bfd1622fc345afeff48878c09f42548f6", - "sha256:f582bba980fcf728ab7250dbb6fbdca8d4e074e24eb2915b01184376ffff98c7" + "sha256:188907ea9332bdd123a95a457e7487556770480264ce3b78c8835b4347e324cc", + "sha256:a2571df484c5635ad996d364242ec28fc69f386915cd69b1842639712b84c36d" ], "index": "pypi", - "version": "==4.4.2" + "version": "==4.4.3" }, "flask-pymongo": { "hashes": [ @@ -694,6 +694,13 @@ "markers": "python_full_version >= '3.6.8'", "version": "==3.0.9" }, + "pypubsub": { + "hashes": [ + "sha256:7f716bae9388afe01ff82b264ba8a96a8ae78b42bb1f114f2716ca8f9e404e2a" + ], + "index": "pypi", + "version": "==4.0.3" + }, "pyrsistent": { "hashes": [ "sha256:0e3e1fcc45199df76053026a51cc59ab2ea3fc7c094c6627e93b7b44cdae2c8c", @@ -807,11 +814,11 @@ }, "setuptools": { "hashes": [ - "sha256:0d33c374d41c7863419fc8f6c10bfe25b7b498aa34164d135c622e52580c6b16", - "sha256:c04b44a57a6265fe34a4a444e965884716d34bae963119a76353434d6f18e450" + "sha256:7c7854ee1429a240090297628dc9f75b35318d193537968e2dc14010ee2f5bca", + "sha256:dc2662692f47d99cb8ae15a784529adeed535bcd7c277fee0beccf961522baf6" ], "markers": "python_version >= '3.7'", - "version": "==63.2.0" + "version": "==63.4.1" }, "six": { "hashes": [ @@ -831,19 +838,19 @@ }, "urllib3": { "hashes": [ - "sha256:8298d6d56d39be0e3bc13c1c97d133f9b45d797169a0e11cdd0e0489d786f7ec", - "sha256:879ba4d1e89654d9769ce13121e0f94310ea32e8d2f8cf587b77c08bbcdb30d6" + "sha256:c33ccba33c819596124764c23a97d25f32b28433ba0dedeb77d873a38722c9bc", + "sha256:ea6e8fb210b19d950fab93b60c9009226c63a28808bc8386e05301e25883ac0a" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5' and python_version < '4'", - "version": "==1.26.10" + "version": "==1.26.11" }, "werkzeug": { "hashes": [ - "sha256:1ce08e8093ed67d638d63879fd1ba3735817f7a80de3674d293f5984f25fb6e6", - "sha256:72a4b735692dd3135217911cbeaa1be5fa3f62bffb8745c5215420a03dc55255" + "sha256:4d7013ef96fd197d1cdeb03e066c6c5a491ccb44758a5b2b91137319383e5a5a", + "sha256:7e1db6a5ba6b9a8be061e47e900456355b8714c0f238b0313f53afce1a55a79a" ], "index": "pypi", - "version": "==2.1.2" + "version": "==2.2.1" }, "wirerope": { "hashes": [ @@ -941,11 +948,11 @@ }, "attrs": { "hashes": [ - "sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4", - "sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd" + "sha256:29adc2665447e5191d0e7c568fde78b21f9672d344281d0c6e1ab085429b22b6", + "sha256:86efa402f67bf2df34f51a335487cf46b1ec130d02b8d39fd248abfd30da551c" ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", - "version": "==21.4.0" + "markers": "python_version >= '3.5'", + "version": "==22.1.0" }, "babel": { "hashes": [ @@ -1334,11 +1341,11 @@ }, "setuptools": { "hashes": [ - "sha256:0d33c374d41c7863419fc8f6c10bfe25b7b498aa34164d135c622e52580c6b16", - "sha256:c04b44a57a6265fe34a4a444e965884716d34bae963119a76353434d6f18e450" + "sha256:7c7854ee1429a240090297628dc9f75b35318d193537968e2dc14010ee2f5bca", + "sha256:dc2662692f47d99cb8ae15a784529adeed535bcd7c277fee0beccf961522baf6" ], "markers": "python_version >= '3.7'", - "version": "==63.2.0" + "version": "==63.4.1" }, "six": { "hashes": [ @@ -1491,19 +1498,19 @@ }, "urllib3": { "hashes": [ - "sha256:8298d6d56d39be0e3bc13c1c97d133f9b45d797169a0e11cdd0e0489d786f7ec", - "sha256:879ba4d1e89654d9769ce13121e0f94310ea32e8d2f8cf587b77c08bbcdb30d6" + "sha256:c33ccba33c819596124764c23a97d25f32b28433ba0dedeb77d873a38722c9bc", + "sha256:ea6e8fb210b19d950fab93b60c9009226c63a28808bc8386e05301e25883ac0a" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5' and python_version < '4'", - "version": "==1.26.10" + "version": "==1.26.11" }, "virtualenv": { "hashes": [ - "sha256:288171134a2ff3bfb1a2f54f119e77cd1b81c29fc1265a2356f3e8d14c7d58c4", - "sha256:b30aefac647e86af6d82bfc944c556f8f1a9c90427b2fb4e3bfbf338cb82becf" + "sha256:0ef5be6d07181946891f5abc8047fda8bc2f0b4b9bf222c64e6e8963baee76db", + "sha256:635b272a8e2f77cb051946f46c60a54ace3cb5e25568228bd6b57fc70eca9ff3" ], "index": "pypi", - "version": "==20.15.1" + "version": "==20.16.2" }, "vulture": { "hashes": [ From ac62281c84a672e74655a02a2dbf7fdd541f286f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 5 Aug 2022 18:45:34 +0530 Subject: [PATCH 03/69] Agent: Add pypubsub as a dependency --- monkey/infection_monkey/Pipfile | 1 + monkey/infection_monkey/Pipfile.lock | 350 ++++++++++++++------------- 2 files changed, 188 insertions(+), 163 deletions(-) diff --git a/monkey/infection_monkey/Pipfile b/monkey/infection_monkey/Pipfile index 7df872e2d..73923cd43 100644 --- a/monkey/infection_monkey/Pipfile +++ b/monkey/infection_monkey/Pipfile @@ -26,6 +26,7 @@ pefile = {version = "*", sys_platform = "== 'win32'"} # Pyinstaller requirement paramiko = {editable = true, ref = "2.10.3.dev1", git = "https://github.com/VakarisZ/paramiko.git"} marshmallow = "*" marshmallow-enum = "*" +pypubsub = "*" [dev-packages] ldap3 = "*" diff --git a/monkey/infection_monkey/Pipfile.lock b/monkey/infection_monkey/Pipfile.lock index 48f0e9b87..7d2e940ef 100644 --- a/monkey/infection_monkey/Pipfile.lock +++ b/monkey/infection_monkey/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "202bc4667be3a990bdf39be19ce2ddbacc0b3189810d89f75464ad361c9142b2" + "sha256": "4b34d3b744fa5d28b36d6f1dae271474b6a92db3a221b8c14d3c04eedfab5d9d" }, "pipfile-spec": 6, "requires": { @@ -56,11 +56,11 @@ }, "attrs": { "hashes": [ - "sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4", - "sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd" + "sha256:29adc2665447e5191d0e7c568fde78b21f9672d344281d0c6e1ab085429b22b6", + "sha256:86efa402f67bf2df34f51a335487cf46b1ec130d02b8d39fd248abfd30da551c" ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", - "version": "==21.4.0" + "markers": "python_version >= '3.5'", + "version": "==22.1.0" }, "automat": { "hashes": [ @@ -96,74 +96,88 @@ }, "cffi": { "hashes": [ - "sha256:00c878c90cb53ccfaae6b8bc18ad05d2036553e6d9d1d9dbcf323bbe83854ca3", - "sha256:0104fb5ae2391d46a4cb082abdd5c69ea4eab79d8d44eaaf79f1b1fd806ee4c2", - "sha256:06c48159c1abed75c2e721b1715c379fa3200c7784271b3c46df01383b593636", - "sha256:0808014eb713677ec1292301ea4c81ad277b6cdf2fdd90fd540af98c0b101d20", - "sha256:10dffb601ccfb65262a27233ac273d552ddc4d8ae1bf93b21c94b8511bffe728", - "sha256:14cd121ea63ecdae71efa69c15c5543a4b5fbcd0bbe2aad864baca0063cecf27", - "sha256:17771976e82e9f94976180f76468546834d22a7cc404b17c22df2a2c81db0c66", - "sha256:181dee03b1170ff1969489acf1c26533710231c58f95534e3edac87fff06c443", - "sha256:23cfe892bd5dd8941608f93348c0737e369e51c100d03718f108bf1add7bd6d0", - "sha256:263cc3d821c4ab2213cbe8cd8b355a7f72a8324577dc865ef98487c1aeee2bc7", - "sha256:2756c88cbb94231c7a147402476be2c4df2f6078099a6f4a480d239a8817ae39", - "sha256:27c219baf94952ae9d50ec19651a687b826792055353d07648a5695413e0c605", - "sha256:2a23af14f408d53d5e6cd4e3d9a24ff9e05906ad574822a10563efcef137979a", - "sha256:31fb708d9d7c3f49a60f04cf5b119aeefe5644daba1cd2a0fe389b674fd1de37", - "sha256:3415c89f9204ee60cd09b235810be700e993e343a408693e80ce7f6a40108029", - "sha256:3773c4d81e6e818df2efbc7dd77325ca0dcb688116050fb2b3011218eda36139", - "sha256:3b96a311ac60a3f6be21d2572e46ce67f09abcf4d09344c49274eb9e0bf345fc", - "sha256:3f7d084648d77af029acb79a0ff49a0ad7e9d09057a9bf46596dac9514dc07df", - "sha256:41d45de54cd277a7878919867c0f08b0cf817605e4eb94093e7516505d3c8d14", - "sha256:4238e6dab5d6a8ba812de994bbb0a79bddbdf80994e4ce802b6f6f3142fcc880", - "sha256:45db3a33139e9c8f7c09234b5784a5e33d31fd6907800b316decad50af323ff2", - "sha256:45e8636704eacc432a206ac7345a5d3d2c62d95a507ec70d62f23cd91770482a", - "sha256:4958391dbd6249d7ad855b9ca88fae690783a6be9e86df65865058ed81fc860e", - "sha256:4a306fa632e8f0928956a41fa8e1d6243c71e7eb59ffbd165fc0b41e316b2474", - "sha256:57e9ac9ccc3101fac9d6014fba037473e4358ef4e89f8e181f8951a2c0162024", - "sha256:59888172256cac5629e60e72e86598027aca6bf01fa2465bdb676d37636573e8", - "sha256:5e069f72d497312b24fcc02073d70cb989045d1c91cbd53979366077959933e0", - "sha256:64d4ec9f448dfe041705426000cc13e34e6e5bb13736e9fd62e34a0b0c41566e", - "sha256:6dc2737a3674b3e344847c8686cf29e500584ccad76204efea14f451d4cc669a", - "sha256:74fdfdbfdc48d3f47148976f49fab3251e550a8720bebc99bf1483f5bfb5db3e", - "sha256:75e4024375654472cc27e91cbe9eaa08567f7fbdf822638be2814ce059f58032", - "sha256:786902fb9ba7433aae840e0ed609f45c7bcd4e225ebb9c753aa39725bb3e6ad6", - "sha256:8b6c2ea03845c9f501ed1313e78de148cd3f6cad741a75d43a29b43da27f2e1e", - "sha256:91d77d2a782be4274da750752bb1650a97bfd8f291022b379bb8e01c66b4e96b", - "sha256:91ec59c33514b7c7559a6acda53bbfe1b283949c34fe7440bcf917f96ac0723e", - "sha256:920f0d66a896c2d99f0adbb391f990a84091179542c205fa53ce5787aff87954", - "sha256:a5263e363c27b653a90078143adb3d076c1a748ec9ecc78ea2fb916f9b861962", - "sha256:abb9a20a72ac4e0fdb50dae135ba5e77880518e742077ced47eb1499e29a443c", - "sha256:c2051981a968d7de9dd2d7b87bcb9c939c74a34626a6e2f8181455dd49ed69e4", - "sha256:c21c9e3896c23007803a875460fb786118f0cdd4434359577ea25eb556e34c55", - "sha256:c2502a1a03b6312837279c8c1bd3ebedf6c12c4228ddbad40912d671ccc8a962", - "sha256:d4d692a89c5cf08a8557fdeb329b82e7bf609aadfaed6c0d79f5a449a3c7c023", - "sha256:da5db4e883f1ce37f55c667e5c0de439df76ac4cb55964655906306918e7363c", - "sha256:e7022a66d9b55e93e1a845d8c9eba2a1bebd4966cd8bfc25d9cd07d515b33fa6", - "sha256:ef1f279350da2c586a69d32fc8733092fd32cc8ac95139a00377841f59a3f8d8", - "sha256:f54a64f8b0c8ff0b64d18aa76675262e1700f3995182267998c31ae974fbc382", - "sha256:f5c7150ad32ba43a07c4479f40241756145a1f03b43480e058cfd862bf5041c7", - "sha256:f6f824dc3bce0edab5f427efcfb1d63ee75b6fcb7282900ccaf925be84efb0fc", - "sha256:fd8a250edc26254fe5b33be00402e6d287f562b6a5b2152dec302fa15bb3e997", - "sha256:ffaa5c925128e29efbde7301d8ecaf35c8c60ffbcd6a1ffd3a552177c8e5e796" + "sha256:00a9ed42e88df81ffae7a8ab6d9356b371399b91dbdf0c3cb1e84c03a13aceb5", + "sha256:03425bdae262c76aad70202debd780501fabeaca237cdfddc008987c0e0f59ef", + "sha256:04ed324bda3cda42b9b695d51bb7d54b680b9719cfab04227cdd1e04e5de3104", + "sha256:0e2642fe3142e4cc4af0799748233ad6da94c62a8bec3a6648bf8ee68b1c7426", + "sha256:173379135477dc8cac4bc58f45db08ab45d228b3363adb7af79436135d028405", + "sha256:198caafb44239b60e252492445da556afafc7d1e3ab7a1fb3f0584ef6d742375", + "sha256:1e74c6b51a9ed6589199c787bf5f9875612ca4a8a0785fb2d4a84429badaf22a", + "sha256:2012c72d854c2d03e45d06ae57f40d78e5770d252f195b93f581acf3ba44496e", + "sha256:21157295583fe8943475029ed5abdcf71eb3911894724e360acff1d61c1d54bc", + "sha256:2470043b93ff09bf8fb1d46d1cb756ce6132c54826661a32d4e4d132e1977adf", + "sha256:285d29981935eb726a4399badae8f0ffdff4f5050eaa6d0cfc3f64b857b77185", + "sha256:30d78fbc8ebf9c92c9b7823ee18eb92f2e6ef79b45ac84db507f52fbe3ec4497", + "sha256:320dab6e7cb2eacdf0e658569d2575c4dad258c0fcc794f46215e1e39f90f2c3", + "sha256:33ab79603146aace82c2427da5ca6e58f2b3f2fb5da893ceac0c42218a40be35", + "sha256:3548db281cd7d2561c9ad9984681c95f7b0e38881201e157833a2342c30d5e8c", + "sha256:3799aecf2e17cf585d977b780ce79ff0dc9b78d799fc694221ce814c2c19db83", + "sha256:39d39875251ca8f612b6f33e6b1195af86d1b3e60086068be9cc053aa4376e21", + "sha256:3b926aa83d1edb5aa5b427b4053dc420ec295a08e40911296b9eb1b6170f6cca", + "sha256:3bcde07039e586f91b45c88f8583ea7cf7a0770df3a1649627bf598332cb6984", + "sha256:3d08afd128ddaa624a48cf2b859afef385b720bb4b43df214f85616922e6a5ac", + "sha256:3eb6971dcff08619f8d91607cfc726518b6fa2a9eba42856be181c6d0d9515fd", + "sha256:40f4774f5a9d4f5e344f31a32b5096977b5d48560c5592e2f3d2c4374bd543ee", + "sha256:4289fc34b2f5316fbb762d75362931e351941fa95fa18789191b33fc4cf9504a", + "sha256:470c103ae716238bbe698d67ad020e1db9d9dba34fa5a899b5e21577e6d52ed2", + "sha256:4f2c9f67e9821cad2e5f480bc8d83b8742896f1242dba247911072d4fa94c192", + "sha256:50a74364d85fd319352182ef59c5c790484a336f6db772c1a9231f1c3ed0cbd7", + "sha256:54a2db7b78338edd780e7ef7f9f6c442500fb0d41a5a4ea24fff1c929d5af585", + "sha256:5635bd9cb9731e6d4a1132a498dd34f764034a8ce60cef4f5319c0541159392f", + "sha256:59c0b02d0a6c384d453fece7566d1c7e6b7bae4fc5874ef2ef46d56776d61c9e", + "sha256:5d598b938678ebf3c67377cdd45e09d431369c3b1a5b331058c338e201f12b27", + "sha256:5df2768244d19ab7f60546d0c7c63ce1581f7af8b5de3eb3004b9b6fc8a9f84b", + "sha256:5ef34d190326c3b1f822a5b7a45f6c4535e2f47ed06fec77d3d799c450b2651e", + "sha256:6975a3fac6bc83c4a65c9f9fcab9e47019a11d3d2cf7f3c0d03431bf145a941e", + "sha256:6c9a799e985904922a4d207a94eae35c78ebae90e128f0c4e521ce339396be9d", + "sha256:70df4e3b545a17496c9b3f41f5115e69a4f2e77e94e1d2a8e1070bc0c38c8a3c", + "sha256:7473e861101c9e72452f9bf8acb984947aa1661a7704553a9f6e4baa5ba64415", + "sha256:8102eaf27e1e448db915d08afa8b41d6c7ca7a04b7d73af6514df10a3e74bd82", + "sha256:87c450779d0914f2861b8526e035c5e6da0a3199d8f1add1a665e1cbc6fc6d02", + "sha256:8b7ee99e510d7b66cdb6c593f21c043c248537a32e0bedf02e01e9553a172314", + "sha256:91fc98adde3d7881af9b59ed0294046f3806221863722ba7d8d120c575314325", + "sha256:94411f22c3985acaec6f83c6df553f2dbe17b698cc7f8ae751ff2237d96b9e3c", + "sha256:98d85c6a2bef81588d9227dde12db8a7f47f639f4a17c9ae08e773aa9c697bf3", + "sha256:9ad5db27f9cabae298d151c85cf2bad1d359a1b9c686a275df03385758e2f914", + "sha256:a0b71b1b8fbf2b96e41c4d990244165e2c9be83d54962a9a1d118fd8657d2045", + "sha256:a0f100c8912c114ff53e1202d0078b425bee3649ae34d7b070e9697f93c5d52d", + "sha256:a591fe9e525846e4d154205572a029f653ada1a78b93697f3b5a8f1f2bc055b9", + "sha256:a5c84c68147988265e60416b57fc83425a78058853509c1b0629c180094904a5", + "sha256:a66d3508133af6e8548451b25058d5812812ec3798c886bf38ed24a98216fab2", + "sha256:a8c4917bd7ad33e8eb21e9a5bbba979b49d9a97acb3a803092cbc1133e20343c", + "sha256:b3bbeb01c2b273cca1e1e0c5df57f12dce9a4dd331b4fa1635b8bec26350bde3", + "sha256:cba9d6b9a7d64d4bd46167096fc9d2f835e25d7e4c121fb2ddfc6528fb0413b2", + "sha256:cc4d65aeeaa04136a12677d3dd0b1c0c94dc43abac5860ab33cceb42b801c1e8", + "sha256:ce4bcc037df4fc5e3d184794f27bdaab018943698f4ca31630bc7f84a7b69c6d", + "sha256:cec7d9412a9102bdc577382c3929b337320c4c4c4849f2c5cdd14d7368c5562d", + "sha256:d400bfb9a37b1351253cb402671cea7e89bdecc294e8016a707f6d1d8ac934f9", + "sha256:d61f4695e6c866a23a21acab0509af1cdfd2c013cf256bbf5b6b5e2695827162", + "sha256:db0fbb9c62743ce59a9ff687eb5f4afbe77e5e8403d6697f7446e5f609976f76", + "sha256:dd86c085fae2efd48ac91dd7ccffcfc0571387fe1193d33b6394db7ef31fe2a4", + "sha256:e00b098126fd45523dd056d2efba6c5a63b71ffe9f2bbe1a4fe1716e1d0c331e", + "sha256:e229a521186c75c8ad9490854fd8bbdd9a0c9aa3a524326b55be83b54d4e0ad9", + "sha256:e263d77ee3dd201c3a142934a086a4450861778baaeeb45db4591ef65550b0a6", + "sha256:ed9cb427ba5504c1dc15ede7d516b84757c3e3d7868ccc85121d9310d27eed0b", + "sha256:fa6693661a4c91757f4412306191b6dc88c1703f780c8234035eac011922bc01", + "sha256:fcd131dd944808b5bdb38e6f5b53013c5aa4f334c5cad0c72742f6eba4b73db0" ], - "version": "==1.15.0" + "version": "==1.15.1" }, "chardet": { "hashes": [ - "sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa", - "sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5" + "sha256:0368df2bfd78b5fc20572bb4e9bb7fb53e2c094f60ae9993339e8671d0afb8aa", + "sha256:d3e64f022d254183001eccc5db4040520c0f23b1a3f33d6413e099eb7f126557" ], - "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", - "version": "==4.0.0" + "markers": "python_version >= '3.6'", + "version": "==5.0.0" }, "charset-normalizer": { "hashes": [ - "sha256:2857e29ff0d34db842cd7ca3230549d1a697f96ee6d3fb071cfa6c7393832597", - "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df" + "sha256:5189b6f22b01957427f35b6a08d9a0bc45b46d3788ef5a92e978433c7a35f8a5", + "sha256:575e708016ff3a5e3681541cb9d79312c416835686d054a23accb873b254f413" ], - "markers": "python_full_version >= '3.5.0'", - "version": "==2.0.12" + "markers": "python_version >= '3.6'", + "version": "==2.1.0" }, "click": { "hashes": [ @@ -190,47 +204,47 @@ }, "cryptography": { "hashes": [ - "sha256:093cb351031656d3ee2f4fa1be579a8c69c754cf874206be1d4cf3b542042804", - "sha256:0cc20f655157d4cfc7bada909dc5cc228211b075ba8407c46467f63597c78178", - "sha256:1b9362d34363f2c71b7853f6251219298124aa4cc2075ae2932e64c91a3e2717", - "sha256:1f3bfbd611db5cb58ca82f3deb35e83af34bb8cf06043fa61500157d50a70982", - "sha256:2bd1096476aaac820426239ab534b636c77d71af66c547b9ddcd76eb9c79e004", - "sha256:31fe38d14d2e5f787e0aecef831457da6cec68e0bb09a35835b0b44ae8b988fe", - "sha256:3b8398b3d0efc420e777c40c16764d6870bcef2eb383df9c6dbb9ffe12c64452", - "sha256:3c81599befb4d4f3d7648ed3217e00d21a9341a9a688ecdd615ff72ffbed7336", - "sha256:419c57d7b63f5ec38b1199a9521d77d7d1754eb97827bbb773162073ccd8c8d4", - "sha256:46f4c544f6557a2fefa7ac8ac7d1b17bf9b647bd20b16decc8fbcab7117fbc15", - "sha256:471e0d70201c069f74c837983189949aa0d24bb2d751b57e26e3761f2f782b8d", - "sha256:59b281eab51e1b6b6afa525af2bd93c16d49358404f814fe2c2410058623928c", - "sha256:731c8abd27693323b348518ed0e0705713a36d79fdbd969ad968fbef0979a7e0", - "sha256:95e590dd70642eb2079d280420a888190aa040ad20f19ec8c6e097e38aa29e06", - "sha256:a68254dd88021f24a68b613d8c51d5c5e74d735878b9e32cc0adf19d1f10aaf9", - "sha256:a7d5137e556cc0ea418dca6186deabe9129cee318618eb1ffecbd35bee55ddc1", - "sha256:aeaba7b5e756ea52c8861c133c596afe93dd716cbcacae23b80bc238202dc023", - "sha256:dc26bb134452081859aa21d4990474ddb7e863aa39e60d1592800a8865a702de", - "sha256:e53258e69874a306fcecb88b7534d61820db8a98655662a3dd2ec7f1afd9132f", - "sha256:ef15c2df7656763b4ff20a9bc4381d8352e6640cfeb95c2972c38ef508e75181", - "sha256:f224ad253cc9cea7568f49077007d2263efa57396a2f2f78114066fd54b5c68e", - "sha256:f8ec91983e638a9bcd75b39f1396e5c0dc2330cbd9ce4accefe68717e6779e0a" + "sha256:190f82f3e87033821828f60787cfa42bff98404483577b591429ed99bed39d59", + "sha256:2be53f9f5505673eeda5f2736bea736c40f051a739bfae2f92d18aed1eb54596", + "sha256:30788e070800fec9bbcf9faa71ea6d8068f5136f60029759fd8c3efec3c9dcb3", + "sha256:3d41b965b3380f10e4611dbae366f6dc3cefc7c9ac4e8842a806b9672ae9add5", + "sha256:4c590ec31550a724ef893c50f9a97a0c14e9c851c85621c5650d699a7b88f7ab", + "sha256:549153378611c0cca1042f20fd9c5030d37a72f634c9326e225c9f666d472884", + "sha256:63f9c17c0e2474ccbebc9302ce2f07b55b3b3fcb211ded18a42d5764f5c10a82", + "sha256:6bc95ed67b6741b2607298f9ea4932ff157e570ef456ef7ff0ef4884a134cc4b", + "sha256:7099a8d55cd49b737ffc99c17de504f2257e3787e02abe6d1a6d136574873441", + "sha256:75976c217f10d48a8b5a8de3d70c454c249e4b91851f6838a4e48b8f41eb71aa", + "sha256:7bc997818309f56c0038a33b8da5c0bfbb3f1f067f315f9abd6fc07ad359398d", + "sha256:80f49023dd13ba35f7c34072fa17f604d2f19bf0989f292cedf7ab5770b87a0b", + "sha256:91ce48d35f4e3d3f1d83e29ef4a9267246e6a3be51864a5b7d2247d5086fa99a", + "sha256:a958c52505c8adf0d3822703078580d2c0456dd1d27fabfb6f76fe63d2971cd6", + "sha256:b62439d7cd1222f3da897e9a9fe53bbf5c104fff4d60893ad1355d4c14a24157", + "sha256:b7f8dd0d4c1f21759695c05a5ec8536c12f31611541f8904083f3dc582604280", + "sha256:d204833f3c8a33bbe11eda63a54b1aad7aa7456ed769a982f21ec599ba5fa282", + "sha256:e007f052ed10cc316df59bc90fbb7ff7950d7e2919c9757fd42a2b8ecf8a5f67", + "sha256:f2dcb0b3b63afb6df7fd94ec6fbddac81b5492513f7b0436210d390c14d46ee8", + "sha256:f721d1885ecae9078c3f6bbe8a88bc0786b6e749bf32ccec1ef2b18929a05046", + "sha256:f7a6de3e98771e183645181b3627e2563dcde3ce94a9e42a3f427d2255190327", + "sha256:f8c0a6e9e1dd3eb0414ba320f85da6b0dcbd543126e30fcc546e7372a7fbf3b9" ], "markers": "python_version >= '3.6'", - "version": "==37.0.2" + "version": "==37.0.4" }, "dnspython": { "hashes": [ "sha256:0f7569a4a6ff151958b64304071d370daa3243d15941a7beedf0c9fe5105603e", "sha256:a851e51367fb93e9e1361732c1d60dab63eff98712e503ea7d92e6eccb109b4f" ], - "markers": "python_version >= '3.6' and python_version < '4.0'", + "markers": "python_version >= '3.6' and python_version < '4'", "version": "==2.2.1" }, "flask": { "hashes": [ - "sha256:315ded2ddf8a6281567edb27393010fe3406188bafbfe65a3339d5787d89e477", - "sha256:fad5b446feb0d6db6aec0c3184d16a8c1f6c3e464b511649c8918a9be100b4fe" + "sha256:3c604c48c3d5b4c63e72134044c0b4fe90ff01ef65280b9fe2d38c8860d99fe5", + "sha256:9c2b81b9b1edcc835af72d600f1955e713a065e7cb41d7e51ee762b449d9c65d" ], "markers": "python_version >= '3.7'", - "version": "==2.1.2" + "version": "==2.2.1" }, "future": { "hashes": [ @@ -251,7 +265,7 @@ "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff", "sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d" ], - "markers": "python_full_version >= '3.5.0'", + "markers": "python_version >= '3.5'", "version": "==3.3" }, "impacket": { @@ -263,11 +277,11 @@ }, "importlib-metadata": { "hashes": [ - "sha256:5d26852efe48c0a32b0509ffbc583fda1a2266545a78d104a6f4aff3db17d700", - "sha256:c58c8eb8a762858f49e18436ff552e83914778e50e9d2f1660535ffb364552ec" + "sha256:637245b8bab2b6502fcbc752cc4b7a6f6243bb02b31c5c26156ad103d3d45670", + "sha256:7401a975809ea1fdc658c3aa4f78cc2195a0e019c5cbc4c06122884e9ae80c23" ], "markers": "python_version < '3.8'", - "version": "==4.11.4" + "version": "==4.12.0" }, "incremental": { "hashes": [ @@ -374,11 +388,11 @@ }, "marshmallow": { "hashes": [ - "sha256:53a1e0ee69f79e1f3e80d17393b25cfc917eda52f859e8183b4af72c3390c1f1", - "sha256:a762c1d8b2bcb0e5c8e964850d03f9f3bffd6a12b626f3c14b9d6b1841999af5" + "sha256:00040ab5ea0c608e8787137627a8efae97fabd60552a05dc889c888f814e75eb", + "sha256:635fb65a3285a31a30f276f30e958070f5214c7196202caa5c7ecf28f5274bc7" ], "index": "pypi", - "version": "==3.16.0" + "version": "==3.17.0" }, "marshmallow-enum": { "hashes": [ @@ -406,11 +420,11 @@ }, "msldap": { "hashes": [ - "sha256:c9e530a5e61a2a4584c3541a1e40787399b9225a4f727ad13ad0b4998bfda03a", - "sha256:cc9129f3f8cf4c06f7469cf25f249db55976b922e94a2ac690987b181ff74307" + "sha256:ac8174ed7e0162eb64b3e9dfeff13b2e1021612d0a4b2cfc6b8e5bed7c00ffe0", + "sha256:e2c22a6e396b4d7d65d73863ed44612120e8e2570ff895b5421ddf6a350085bb" ], "markers": "python_version >= '3.7'", - "version": "==0.3.38" + "version": "==0.3.40" }, "netifaces": { "hashes": [ @@ -492,11 +506,11 @@ }, "prompt-toolkit": { "hashes": [ - "sha256:62291dad495e665fca0bda814e342c69952086afb0f4094d0893d357e5c78752", - "sha256:bd640f60e8cecd74f0dc249713d433ace2ddc62b65ee07f96d358e0b152b6ea7" + "sha256:859b283c50bde45f5f97829f77a4674d1c1fcd88539364f1b28a37805cfd89c0", + "sha256:d8916d3f62a7b67ab353a952ce4ced6a1d2587dfe9ef8ebc30dd7c386751f289" ], "markers": "python_full_version >= '3.6.2'", - "version": "==3.0.29" + "version": "==3.0.30" }, "psutil": { "hashes": [ @@ -582,36 +596,39 @@ }, "pycryptodomex": { "hashes": [ - "sha256:1ca8e1b4c62038bb2da55451385246f51f412c5f5eabd64812c01766a5989b4a", - "sha256:298c00ea41a81a491d5b244d295d18369e5aac4b61b77b2de5b249ca61cd6659", - "sha256:2aa887683eee493e015545bd69d3d21ac8d5ad582674ec98f4af84511e353e45", - "sha256:2ce76ed0081fd6ac8c74edc75b9d14eca2064173af79843c24fa62573263c1f2", - "sha256:3da13c2535b7aea94cc2a6d1b1b37746814c74b6e80790daddd55ca5c120a489", - "sha256:406ec8cfe0c098fadb18d597dc2ee6de4428d640c0ccafa453f3d9b2e58d29e2", - "sha256:4d0db8df9ffae36f416897ad184608d9d7a8c2b46c4612c6bc759b26c073f750", - "sha256:530756d2faa40af4c1f74123e1d889bd07feae45bac2fd32f259a35f7aa74151", - "sha256:77931df40bb5ce5e13f4de2bfc982b2ddc0198971fbd947776c8bb5050896eb2", - "sha256:797a36bd1f69df9e2798e33edb4bd04e5a30478efc08f9428c087f17f65a7045", - "sha256:8085bd0ad2034352eee4d4f3e2da985c2749cb7344b939f4d95ead38c2520859", - "sha256:8536bc08d130cae6dcba1ea689f2913dfd332d06113904d171f2f56da6228e89", - "sha256:a4d412eba5679ede84b41dbe48b1bed8f33131ab9db06c238a235334733acc5e", - "sha256:aebecde2adc4a6847094d3bd6a8a9538ef3438a5ea84ac1983fcb167db614461", - "sha256:b276cc4deb4a80f9dfd47a41ebb464b1fe91efd8b1b8620cf5ccf8b824b850d6", - "sha256:b5a185ae79f899b01ca49f365bdf15a45d78d9856f09b0de1a41b92afce1a07f", - "sha256:c4d8977ccda886d88dc3ca789de2f1adc714df912ff3934b3d0a3f3d777deafb", - "sha256:c5dd3ffa663c982d7f1be9eb494a8924f6d40e2e2f7d1d27384cfab1b2ac0662", - "sha256:ca88f2f7020002638276439a01ffbb0355634907d1aa5ca91f3dc0c2e44e8f3b", - "sha256:d2cce1c82a7845d7e2e8a0956c6b7ed3f1661c9acf18eb120fc71e098ab5c6fe", - "sha256:d709572d64825d8d59ea112e11cc7faf6007f294e9951324b7574af4251e4de8", - "sha256:da8db8374295fb532b4b0c467e66800ef17d100e4d5faa2bbbd6df35502da125", - "sha256:e36c7e3b5382cd5669cf199c4a04a0279a43b2a3bdd77627e9b89778ac9ec08c", - "sha256:e95a4a6c54d27a84a4624d2af8bb9ee178111604653194ca6880c98dcad92f48", - "sha256:ee835def05622e0c8b1435a906491760a43d0c462f065ec9143ec4b8d79f8bff", - "sha256:f75009715dcf4a3d680c2338ab19dac5498f8121173a929872950f4fb3a48fbf", - "sha256:f8524b8bc89470cec7ac51734907818d3620fb1637f8f8b542d650ebec42a126" + "sha256:04cc393045a8f19dd110c975e30f38ed7ab3faf21ede415ea67afebd95a22380", + "sha256:0776bfaf2c48154ab54ea45392847c1283d2fcf64e232e85565f858baedfc1fa", + "sha256:0fadb9f7fa3150577800eef35f62a8a24b9ddf1563ff060d9bd3af22d3952c8c", + "sha256:18e2ab4813883ae63396c0ffe50b13554b32bb69ec56f0afaf052e7a7ae0d55b", + "sha256:191e73bc84a8064ad1874dba0ebadedd7cce4dedee998549518f2c74a003b2e1", + "sha256:35a8f7afe1867118330e2e0e0bf759c409e28557fb1fc2fbb1c6c937297dbe9a", + "sha256:3709f13ca3852b0b07fc04a2c03b379189232b24007c466be0f605dd4723e9d4", + "sha256:4540904c09704b6f831059c0dfb38584acb82cb97b0125cd52688c1f1e3fffa6", + "sha256:463119d7d22d0fc04a0f9122e9d3e6121c6648bcb12a052b51bd1eed1b996aa2", + "sha256:46b3f05f2f7ac7841053da4e0f69616929ca3c42f238c405f6c3df7759ad2780", + "sha256:48697790203909fab02a33226fda546604f4e2653f9d47bc5d3eb40879fa7c64", + "sha256:5676a132169a1c1a3712edf25250722ebc8c9102aa9abd814df063ca8362454f", + "sha256:65204412d0c6a8e3c41e21e93a5e6054a74fea501afa03046a388cf042e3377a", + "sha256:67e1e6a92151023ccdfcfbc0afb3314ad30080793b4c27956ea06ab1fb9bcd8a", + "sha256:6f5b6ba8aefd624834bc177a2ac292734996bb030f9d1b388e7504103b6fcddf", + "sha256:7341f1bb2dadb0d1a0047f34c3a58208a92423cdbd3244d998e4b28df5eac0ed", + "sha256:78d9621cf0ea35abf2d38fa2ca6d0634eab6c991a78373498ab149953787e5e5", + "sha256:8eecdf9cdc7343001d047f951b9cc805cd68cb6cd77b20ea46af5bffc5bd3dfb", + "sha256:94c7b60e1f52e1a87715571327baea0733708ab4723346598beca4a3b6879794", + "sha256:996e1ba717077ce1e6d4849af7a1426f38b07b3d173b879e27d5e26d2e958beb", + "sha256:a07a64709e366c2041cd5cfbca592b43998bf4df88f7b0ca73dca37071ccf1bd", + "sha256:b6306403228edde6e289f626a3908a2f7f67c344e712cf7c0a508bab3ad9e381", + "sha256:b9279adc16e4b0f590ceff581f53a80179b02cba9056010d733eb4196134a870", + "sha256:c4cb9cb492ea7dcdf222a8d19a1d09002798ea516aeae8877245206d27326d86", + "sha256:dd452a5af7014e866206d41751886c9b4bf379a339fdf2dbfc7dd16c0fb4f8e0", + "sha256:e2b12968522a0358b8917fc7b28865acac002f02f4c4c6020fcb264d76bfd06d", + "sha256:e3164a18348bd53c69b4435ebfb4ac8a4076291ffa2a70b54f0c4b80c7834b1d", + "sha256:e47bf8776a7e15576887f04314f5228c6527b99946e6638cf2f16da56d260cab", + "sha256:f8be976cec59b11f011f790b88aca67b4ea2bd286578d0bd3e31bcd19afcd3e4", + "sha256:fc9bc7a9b79fe5c750fc81a307052f8daabb709bdaabb0fb18fb136b66b653b5" ], "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", - "version": "==3.14.1" + "version": "==3.15.0" }, "pyinstaller": { "hashes": [ @@ -632,11 +649,11 @@ }, "pyinstaller-hooks-contrib": { "hashes": [ - "sha256:5fdb97dcae177955db7ab27840cba97b89dc0c7f4fd9142bba0f9b8d8df85c48", - "sha256:6675634279cfe9e475580fb310c3d557037baefb065e6cb5a69a124361b926fd" + "sha256:c4210fc50282c9c6a918e485e0bfae9405592390508e3be9fde19acc2213da56", + "sha256:e46f099934dd4577fb1ddcf37a99fa04027c92f8f5291c8802f326345988d001" ], "markers": "python_version >= '3.7'", - "version": "==2022.7" + "version": "==2022.8" }, "pymssql": { "hashes": [ @@ -713,6 +730,13 @@ "index": "pypi", "version": "==0.8.1" }, + "pypubsub": { + "hashes": [ + "sha256:7f716bae9388afe01ff82b264ba8a96a8ae78b42bb1f114f2716ca8f9e404e2a" + ], + "index": "pypi", + "version": "==4.0.3" + }, "pypykatz": { "hashes": [ "sha256:ad397a6ca72033df70fc6655f8922f1ee16d6c5b05e0e40276899217e2f5dbd3", @@ -730,21 +754,21 @@ }, "pyspnego": { "hashes": [ - "sha256:1fed228edc4b1730844da8237b90489be28c55681cf3934cd04fceb2253e55bf", - "sha256:25fbc90fc6bd16881480316739bab820cc91364765e46340da17f861f89691f1", - "sha256:274b3a2d37e45ad4567bc5754be04b5fefad3f7cdea7d205f739d8a26b5a9189", - "sha256:36db7ec38023a23a545114dfd23825639571f135c72fb4b13a1ed559a0a4d93c", - "sha256:3b1ff3c1d5588b66f8e4ebafa3079a7bf0bdcc6fb144b944c5a101e688a5a280", - "sha256:4b9bda51bd964f40322aa1b33dcfc5d68f23b0680b4b5158175f2e9a04119aa9", - "sha256:5d6d91e35ee63a5de30eb70716bf25274bf16c2c472b046dd21fad60fe63b0b6", - "sha256:7562bc640bf402bb2849f325b0bb41260bd2c0cb06e38b9a8c6f7021b452c873", - "sha256:9c5bdb9f0207e2ce9e5410ee2205bf016755712018534c711ae6c1daff7fa7db", - "sha256:a5c135d20819db3c48f65054d648317f369a61b7b22dc17b9e5ec9c0169541a0", - "sha256:bd95633e7dce69e267579bdbe992fc081a14310236b4e84c3d179b1cf6439ca5", - "sha256:eb41b970dbda0dfe07b1da6fc83fe9f534a66d8dea38c06c0155377697407d9a" + "sha256:1ee612f20c843365fbc6cf7f95c526b4ee8795281641a9bb87083622a2f87939", + "sha256:284ca7a6218344bb90aeae02fb1d2ed73e5c991d6e4c16c0df404aeab5eb58a3", + "sha256:416fd2d67e82b44ba3d2d9062485056e4dde3c141630170e9190379d6b19972a", + "sha256:4c1be83e0aca12d64f5eec638259c77eaa8bf552c89ac69f0af2322a3be9afeb", + "sha256:4d1ea987b9c2539457235793014e0d9c5e4766da9e4e028d4b6b596cfbe53828", + "sha256:725df2030e5d1b1155bb696eca3d684f403164da8e6a6b0bee3eb02f8748f72b", + "sha256:7320539f45da463029e12f3742102085d2a0343bfe77ac550c11d2fdac1d34f5", + "sha256:77b86002082f278c3f5935d8b428a0d0659ea709e305537294ba95fc49907339", + "sha256:aa93d94775d01bf70d16840426d1ddd58c11a6a71c4d0d1d7e529ad541fa0a60", + "sha256:c2abca03b6d3c71d7ec9678c7b2220d99d9a29ef204b4c52549080169e586310", + "sha256:e6645107f200fb7bf6698512d04ea0790b292028861ce169eb97e5ad8eba14ed", + "sha256:f4784d9f8e9c50a36109d715a140150add1990fce16805a56217e8ccaf69d234" ], "markers": "python_version >= '3.6'", - "version": "==0.5.2" + "version": "==0.5.3" }, "pywin32": { "hashes": [ @@ -778,11 +802,11 @@ }, "requests": { "hashes": [ - "sha256:bc7861137fbce630f17b03d3ad02ad0bf978c844f3536d0edda6499dafce2b6f", - "sha256:d568723a7ebd25875d8d1eaf5dfa068cd2fc8194b2e483d7b1f7c81918dbec6b" + "sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983", + "sha256:8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349" ], "index": "pypi", - "version": "==2.28.0" + "version": "==2.28.1" }, "service-identity": { "hashes": [ @@ -793,11 +817,11 @@ }, "setuptools": { "hashes": [ - "sha256:5a844ad6e190dccc67d6d7411d119c5152ce01f7c76be4d8a1eaa314501bba77", - "sha256:bf8a748ac98b09d32c9a64a995a6b25921c96cc5743c1efa82763ba80ff54e91" + "sha256:7c7854ee1429a240090297628dc9f75b35318d193537968e2dc14010ee2f5bca", + "sha256:dc2662692f47d99cb8ae15a784529adeed535bcd7c277fee0beccf961522baf6" ], "markers": "python_version >= '3.7'", - "version": "==62.4.0" + "version": "==63.4.1" }, "six": { "hashes": [ @@ -846,11 +870,11 @@ }, "typing-extensions": { "hashes": [ - "sha256:6657594ee297170d19f67d55c05852a874e7eb634f4f753dbd667855e07c1708", - "sha256:f1c24655a0da0d1b67f07e17a5e6b2a105894e6824b92096378bb3668ef02376" + "sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02", + "sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6" ], "index": "pypi", - "version": "==4.2.0" + "version": "==4.3.0" }, "unicrypto": { "hashes": [ @@ -877,11 +901,11 @@ }, "werkzeug": { "hashes": [ - "sha256:1ce08e8093ed67d638d63879fd1ba3735817f7a80de3674d293f5984f25fb6e6", - "sha256:72a4b735692dd3135217911cbeaa1be5fa3f62bffb8745c5215420a03dc55255" + "sha256:4d7013ef96fd197d1cdeb03e066c6c5a491ccb44758a5b2b91137319383e5a5a", + "sha256:7e1db6a5ba6b9a8be061e47e900456355b8714c0f238b0313f53afce1a55a79a" ], "markers": "python_version >= '3.7'", - "version": "==2.1.2" + "version": "==2.2.1" }, "winacl": { "hashes": [ @@ -917,11 +941,11 @@ }, "zipp": { "hashes": [ - "sha256:56bf8aadb83c24db6c4b577e13de374ccfb67da2078beba1d037c17980bf43ad", - "sha256:c4f6e5bbf48e74f7a38e7cc5b0480ff42b0ae5178957d564d18932525d5cf099" + "sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2", + "sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009" ], "markers": "python_version >= '3.7'", - "version": "==3.8.0" + "version": "==3.8.1" }, "zope.interface": { "hashes": [ From 924fc3f1e26dfebd9c527e390832bad1a87469ed Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 5 Aug 2022 18:51:07 +0530 Subject: [PATCH 04/69] Common: Make all methods of IEventQueue static --- monkey/common/event_queue/i_event_queue.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index c0de3aeab..960b5b385 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,4 +1,4 @@ -from abc import ABC, abstractmethod +from abc import ABC, abstractstaticmethod from typing import Any, Callable, Sequence from common.events import AbstractEvent @@ -9,8 +9,8 @@ class IEventQueue(ABC): Manages subscription and publishing of events """ - @abstractmethod - def subscribe_all(self, subscriber: Callable[..., Any]): + @abstractstaticmethod + def subscribe_all(subscriber: Callable[..., Any]): """ Subscribes a subscriber to all events @@ -19,8 +19,8 @@ class IEventQueue(ABC): pass - @abstractmethod - def subscribe_types(self, types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + @abstractstaticmethod + def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): """ Subscribes a subscriber to all specifed event types @@ -30,8 +30,8 @@ class IEventQueue(ABC): pass - @abstractmethod - def subscribe_tags(self, tags: Sequence[str], subscriber: Callable[..., Any]): + @abstractstaticmethod + def subscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): """ Subscribes a subscriber to all specified event tags @@ -41,8 +41,8 @@ class IEventQueue(ABC): pass - @abstractmethod - def publish(self, event: AbstractEvent, data: Any): + @abstractstaticmethod + def publish(event: AbstractEvent, data: Any): """ Publishes an event with the given data From 40219aa5ff570f83ff05ec885dc95443fd821f4f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 5 Aug 2022 19:05:07 +0530 Subject: [PATCH 05/69] Common: Add TODO in IEventQueue --- monkey/common/event_queue/i_event_queue.py | 3 ++ .../event_queue/pypubsub_event_queue.py | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 monkey/common/event_queue/pypubsub_event_queue.py diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 960b5b385..03689b814 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -51,3 +51,6 @@ class IEventQueue(ABC): """ pass + + +# TODO: Add unsubscribing functions diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py new file mode 100644 index 000000000..486233fd0 --- /dev/null +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -0,0 +1,43 @@ +from typing import Any, Callable, Sequence + +from common.events import AbstractEvent +from .i_event_queue import IEventQueue +from pubsub import pub, ALL_TOPICS + + +class PypubsubEventQueue(IEventQueue): + + @staticmethod + def subscribe_all(subscriber: Callable[..., Any]): + pub.subscribe(listener=subscriber, topicName=ALL_TOPICS) + + def subscribe_types(self, types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + """ + Subscribes a subscriber to all specifed event types + + :param types: Event types to which the subscriber should subscribe + :param subscriber: Callable that should subscribe to events + """ + + pass + + def subscribe_tags(self, tags: Sequence[str], subscriber: Callable[..., Any]): + """ + Subscribes a subscriber to all specified event tags + + :param tags: Event tags to which the subscriber should subscribe + :param subscriber: Callable that should subscribe to events + """ + + pass + + def publish(self, event: AbstractEvent, data: Any): + """ + Publishes an event with the given data + + :param event: Event to publish + :param data: Data to pass to subscribers with the event publish + """ + + pass + From bb76410bac2733f9ba62bbcbeb631c6d563ad0cc Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 5 Aug 2022 19:11:33 +0530 Subject: [PATCH 06/69] Common: Create and partially implement PypubsubEventQueue --- .../event_queue/pypubsub_event_queue.py | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 486233fd0..898b073e6 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,25 +1,25 @@ from typing import Any, Callable, Sequence +from pubsub import ALL_TOPICS, pub + from common.events import AbstractEvent + from .i_event_queue import IEventQueue -from pubsub import pub, ALL_TOPICS class PypubsubEventQueue(IEventQueue): - @staticmethod def subscribe_all(subscriber: Callable[..., Any]): pub.subscribe(listener=subscriber, topicName=ALL_TOPICS) - def subscribe_types(self, types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): - """ - Subscribes a subscriber to all specifed event types + @staticmethod + def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + for event_type in types: + PypubsubEventQueue._subscribe_type(event_type, subscriber) - :param types: Event types to which the subscriber should subscribe - :param subscriber: Callable that should subscribe to events - """ - - pass + def _subscribe_type(event_type: AbstractEvent, subscriber: Callable[..., Any]): + event_type_name = event_type.__name__ + pub.subscribe(subscriber, event_type_name) def subscribe_tags(self, tags: Sequence[str], subscriber: Callable[..., Any]): """ @@ -40,4 +40,3 @@ class PypubsubEventQueue(IEventQueue): """ pass - From 414e8127ed83407a51719e987072998855583af2 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 12:03:17 +0530 Subject: [PATCH 07/69] Common: Implement subscribe_tags() in PypubsubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 898b073e6..ca8c2f7a1 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -18,18 +18,16 @@ class PypubsubEventQueue(IEventQueue): PypubsubEventQueue._subscribe_type(event_type, subscriber) def _subscribe_type(event_type: AbstractEvent, subscriber: Callable[..., Any]): + # pypubsub.pub.subscribe needs a string as the topic/event name event_type_name = event_type.__name__ - pub.subscribe(subscriber, event_type_name) + pub.subscribe(listener=subscriber, topicName=event_type_name) def subscribe_tags(self, tags: Sequence[str], subscriber: Callable[..., Any]): - """ - Subscribes a subscriber to all specified event tags + for tag in tags: + PypubsubEventQueue._subscribe_tag(tag, subscriber) - :param tags: Event tags to which the subscriber should subscribe - :param subscriber: Callable that should subscribe to events - """ - - pass + def _subscribe_tag(tag: str, subscriber: Callable[..., Any]): + pub.subscribe(listener=subscriber, topicName=tag) def publish(self, event: AbstractEvent, data: Any): """ From 1fbe9e5ad4ca1ec88ba155f7ea6f3e771ca84f12 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 12:07:45 +0530 Subject: [PATCH 08/69] Common: Make methods static in PypubsubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index ca8c2f7a1..607c74764 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -17,15 +17,18 @@ class PypubsubEventQueue(IEventQueue): for event_type in types: PypubsubEventQueue._subscribe_type(event_type, subscriber) + @staticmethod def _subscribe_type(event_type: AbstractEvent, subscriber: Callable[..., Any]): # pypubsub.pub.subscribe needs a string as the topic/event name event_type_name = event_type.__name__ pub.subscribe(listener=subscriber, topicName=event_type_name) - def subscribe_tags(self, tags: Sequence[str], subscriber: Callable[..., Any]): + @staticmethod + def subscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): for tag in tags: PypubsubEventQueue._subscribe_tag(tag, subscriber) + @staticmethod def _subscribe_tag(tag: str, subscriber: Callable[..., Any]): pub.subscribe(listener=subscriber, topicName=tag) From 8ae451f240a890b79985e83fbb4bc7bbefa180ac Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 12:49:21 +0530 Subject: [PATCH 09/69] Common: Implement publish() in PypubsubEventQueue --- .../event_queue/pypubsub_event_queue.py | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 607c74764..f3eae5b71 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -32,12 +32,29 @@ class PypubsubEventQueue(IEventQueue): def _subscribe_tag(tag: str, subscriber: Callable[..., Any]): pub.subscribe(listener=subscriber, topicName=tag) - def publish(self, event: AbstractEvent, data: Any): - """ - Publishes an event with the given data + @staticmethod + def publish(event: AbstractEvent, data: Any): + # someClass.mro() returns a list of types that someClass is derived from, + # in order of resolution + # we can be sure that for any valid event, the last three items in the list will be + # , , and - :param event: Event to publish - :param data: Data to pass to subscribers with the event publish - """ + # for some event, say, CredentialsStolenEvent which was derived from SecurityEvent, + # we want to publish the data to both events, so, we loop through the super + # classes of the CredentialsStolenEvent which was initially passed as an argument + # to the function, and publish to each class's type and tags (except the last 3 classes) + for event_type in event.mro()[:-3]: + PypubsubEventQueue._publish_to_type(event_type, data) - pass + # one event can have multiple tags + for tag in event_type.tags: + PypubsubEventQueue._publish_to_tag(tag, data) + + @staticmethod + def _publish_to_type(event_type: AbstractEvent, data: Any): + event_type_name = event_type.__name__ + pub.sendMessage(event_type_name, data) + + @staticmethod + def _publish_to_tag(tag: str, data: Any): + pub.sendMessage(tag, data) From d773bac8c1bb7dcae91ef900dd4102e0ed0ac08f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 13:11:07 +0530 Subject: [PATCH 10/69] Common: Simplify logic in PypubsubEventQueue --- .../event_queue/pypubsub_event_queue.py | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index f3eae5b71..5336a1e14 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -15,22 +15,14 @@ class PypubsubEventQueue(IEventQueue): @staticmethod def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): for event_type in types: - PypubsubEventQueue._subscribe_type(event_type, subscriber) - - @staticmethod - def _subscribe_type(event_type: AbstractEvent, subscriber: Callable[..., Any]): - # pypubsub.pub.subscribe needs a string as the topic/event name - event_type_name = event_type.__name__ - pub.subscribe(listener=subscriber, topicName=event_type_name) + # pypubsub.pub.subscribe needs a string as the topic/event name + event_type_name = event_type.__name__ + pub.subscribe(listener=subscriber, topicName=event_type_name) @staticmethod def subscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): for tag in tags: - PypubsubEventQueue._subscribe_tag(tag, subscriber) - - @staticmethod - def _subscribe_tag(tag: str, subscriber: Callable[..., Any]): - pub.subscribe(listener=subscriber, topicName=tag) + pub.subscribe(listener=subscriber, topicName=tag) @staticmethod def publish(event: AbstractEvent, data: Any): @@ -45,10 +37,7 @@ class PypubsubEventQueue(IEventQueue): # to the function, and publish to each class's type and tags (except the last 3 classes) for event_type in event.mro()[:-3]: PypubsubEventQueue._publish_to_type(event_type, data) - - # one event can have multiple tags - for tag in event_type.tags: - PypubsubEventQueue._publish_to_tag(tag, data) + PypubsubEventQueue._publish_to_tags(event_type, data) @staticmethod def _publish_to_type(event_type: AbstractEvent, data: Any): @@ -56,5 +45,6 @@ class PypubsubEventQueue(IEventQueue): pub.sendMessage(event_type_name, data) @staticmethod - def _publish_to_tag(tag: str, data: Any): - pub.sendMessage(tag, data) + def _publish_to_tags(event_type: AbstractEvent, data: Any): + for tag in event_type.tags: + pub.sendMessage(tag, data) From 84c800b815c7700a6a92fe89d04dcf51a66fd9ed Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 13:29:24 +0530 Subject: [PATCH 11/69] Common: Remove MRO logic from PypubsubEventQueue This isn't needed. The Agent's responsibility is to just declare what it did in the form of events. For example, although CredentialsStolenEvent is a type of SecurityEvent technically, the Agent doesn't care about this. The Island does, which is why this should be handled on the pub/sub implementation on the Island side. The Agent merely conveys the information that it stole credentials. Whatever is to be done or however that information is to be handled after that is the Island's responsibility. I don't see a use case in the Agent where a concrete event is derived from another concrete event. So, the MRO logic is removed in this commit. --- .../event_queue/pypubsub_event_queue.py | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 5336a1e14..7f53dfb15 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -26,25 +26,10 @@ class PypubsubEventQueue(IEventQueue): @staticmethod def publish(event: AbstractEvent, data: Any): - # someClass.mro() returns a list of types that someClass is derived from, - # in order of resolution - # we can be sure that for any valid event, the last three items in the list will be - # , , and - - # for some event, say, CredentialsStolenEvent which was derived from SecurityEvent, - # we want to publish the data to both events, so, we loop through the super - # classes of the CredentialsStolenEvent which was initially passed as an argument - # to the function, and publish to each class's type and tags (except the last 3 classes) - for event_type in event.mro()[:-3]: - PypubsubEventQueue._publish_to_type(event_type, data) - PypubsubEventQueue._publish_to_tags(event_type, data) - - @staticmethod - def _publish_to_type(event_type: AbstractEvent, data: Any): - event_type_name = event_type.__name__ + # publish to event type's topic + event_type_name = event.__name__ pub.sendMessage(event_type_name, data) - @staticmethod - def _publish_to_tags(event_type: AbstractEvent, data: Any): - for tag in event_type.tags: + # publish to tags' topics + for tag in event.tags: pub.sendMessage(tag, data) From 0578a6fcc8b019cb416226d7792ce238b0c6b4f1 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 13:38:27 +0530 Subject: [PATCH 12/69] Common: Add unsubscribing functions to IEventQueue --- monkey/common/event_queue/i_event_queue.py | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 03689b814..af4c1332d 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -52,5 +52,34 @@ class IEventQueue(ABC): pass + @abstractstaticmethod + def unsubscribe_all(subscriber: Callable[..., Any]): + """ + Unsubscribes a subscriber from all events -# TODO: Add unsubscribing functions + :param subscriber: Callable that should unsubscribe from events + """ + + pass + + @abstractstaticmethod + def unsubscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + """ + Unsubscribes a subscriber from all specifed event types + + :param types: Event types from which the subscriber should unsubscribe + :param subscriber: Callable that should unsubscribe from events + """ + + pass + + @abstractstaticmethod + def unsubscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): + """ + Unubscribes a subscriber from all specified event tags + + :param tags: Event tags from which the subscriber should unsubscribe + :param subscriber: Callable that should unsubscribe from events + """ + + pass From b78282ef201992de8461f7ae0a3cc099fbd40257 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 13:42:55 +0530 Subject: [PATCH 13/69] Common: Implement unsubscribing functions in PypubsubEventQueue --- .../common/event_queue/pypubsub_event_queue.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 7f53dfb15..f0667ebe4 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -33,3 +33,19 @@ class PypubsubEventQueue(IEventQueue): # publish to tags' topics for tag in event.tags: pub.sendMessage(tag, data) + + @staticmethod + def unsubscribe_all(subscriber: Callable[..., Any]): + pub.unsubscribe(listener=subscriber, topicName=ALL_TOPICS) + + @staticmethod + def unsubscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + for event_type in types: + # pypubsub.pub.subscribe needs a string as the topic/event name + event_type_name = event_type.__name__ + pub.unsubscribe(listener=subscriber, topicName=event_type_name) + + @staticmethod + def unsubscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): + for tag in tags: + pub.unsubscribe(listener=subscriber, topicName=tag) From 9c5bbfe35c85903c0aee1751dd963285015e0d78 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 23:54:01 +0530 Subject: [PATCH 14/69] Common: Fix some logic in PypubsubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index f0667ebe4..99e1ed32c 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,6 +1,6 @@ from typing import Any, Callable, Sequence -from pubsub import ALL_TOPICS, pub +from pubsub import pub from common.events import AbstractEvent @@ -10,7 +10,7 @@ from .i_event_queue import IEventQueue class PypubsubEventQueue(IEventQueue): @staticmethod def subscribe_all(subscriber: Callable[..., Any]): - pub.subscribe(listener=subscriber, topicName=ALL_TOPICS) + pub.subscribe(listener=subscriber, topicName=pub.ALL_TOPICS) @staticmethod def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): @@ -25,18 +25,20 @@ class PypubsubEventQueue(IEventQueue): pub.subscribe(listener=subscriber, topicName=tag) @staticmethod - def publish(event: AbstractEvent, data: Any): + def publish(event: AbstractEvent, data: Any = None): + data = data if data else {} + # publish to event type's topic event_type_name = event.__name__ - pub.sendMessage(event_type_name, data) + pub.sendMessage(event_type_name, **data) # publish to tags' topics for tag in event.tags: - pub.sendMessage(tag, data) + pub.sendMessage(tag, **data) @staticmethod def unsubscribe_all(subscriber: Callable[..., Any]): - pub.unsubscribe(listener=subscriber, topicName=ALL_TOPICS) + pub.unsubscribe(listener=subscriber, topicName=pub.ALL_TOPICS) @staticmethod def unsubscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): From b1017e22221d1b3c3aaf2bff5d4510b8bff90bcc Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 23:54:29 +0530 Subject: [PATCH 15/69] Common: Fix import in common/events/__init__.py --- monkey/common/events/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/common/events/__init__.py b/monkey/common/events/__init__.py index 230e6635f..65d39de0e 100644 --- a/monkey/common/events/__init__.py +++ b/monkey/common/events/__init__.py @@ -1 +1 @@ -from abstract_event import AbstractEvent +from .abstract_event import AbstractEvent From 1a2a4d165a5905187cfebe7a7e1513fef8725b55 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Mon, 8 Aug 2022 23:56:10 +0530 Subject: [PATCH 16/69] Common: Add tests for PypubsubEventQueue All the unsubscribing tests fails for some reason right now. The subscriber is unsubscribed from the specified topics but the subscriber is still called when an event with that topic is published. --- .../event_queue/test_pypubsub_event_queue.py | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py new file mode 100644 index 000000000..b06c37c86 --- /dev/null +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -0,0 +1,105 @@ +from dataclasses import dataclass +from unittest.mock import MagicMock + +import pytest + +import common.event_queue.pypubsub_event_queue as pypubsub_event_queue_file +from common.event_queue.pypubsub_event_queue import PypubsubEventQueue +from common.events import AbstractEvent + +EVENT_TAG_1 = "event tag 1" +EVENT_TAG_2 = "event tag 2" + + +@dataclass(frozen=True) +class EventType(AbstractEvent): + source = "1234" + target = None + timestamp = 0.0 + tags = [EVENT_TAG_1, EVENT_TAG_2] + + +@pytest.fixture(autouse=True) +def wrap_pypubsub_functions(): + # This is done so that we can use `.call_count` in the tests. + pypubsub_event_queue_file.pub.sendMessage = MagicMock( + side_effect=pypubsub_event_queue_file.pub.sendMessage + ) + + +def test_subscribe_all(): + subscriber = MagicMock() + + PypubsubEventQueue.subscribe_all(subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + +def test_subscribe_types(): + subscriber = MagicMock() + + PypubsubEventQueue.subscribe_types([EventType], subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 1 + + +def test_subscribe_tags(): + subscriber = MagicMock() + + PypubsubEventQueue.subscribe_tags([EVENT_TAG_2], subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 1 + + +def test_unsubscribe_all(): + subscriber = MagicMock() + + PypubsubEventQueue.subscribe_all(subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + PypubsubEventQueue.unsubscribe_all(subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 6 + assert subscriber.call_count == 3 + + +def test_unsubscribe_types(): + subscriber = MagicMock() + + PypubsubEventQueue.subscribe_all(subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + PypubsubEventQueue.unsubscribe_types([EventType], subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 6 + assert subscriber.call_count == 5 + + +def test_unsubscribe_tags(): + subscriber = MagicMock() + + PypubsubEventQueue.subscribe_all(subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + PypubsubEventQueue.unsubscribe_tags([EVENT_TAG_1, EVENT_TAG_2], subscriber) + PypubsubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 6 + assert subscriber.call_count == 4 From 23d3f179a28e91ca7162cb9c5ca3d782c29f9e9d Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 00:04:19 +0530 Subject: [PATCH 17/69] Common: Rename PypubsubEventQueue -> EventQueue and pypubsub_event_queue.py -> event_queue.py --- .../event_queue/{pypubsub_event_queue.py => event_queue.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename monkey/common/event_queue/{pypubsub_event_queue.py => event_queue.py} (97%) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/event_queue.py similarity index 97% rename from monkey/common/event_queue/pypubsub_event_queue.py rename to monkey/common/event_queue/event_queue.py index 99e1ed32c..83f6fb833 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/event_queue.py @@ -7,7 +7,7 @@ from common.events import AbstractEvent from .i_event_queue import IEventQueue -class PypubsubEventQueue(IEventQueue): +class EventQueue(IEventQueue): @staticmethod def subscribe_all(subscriber: Callable[..., Any]): pub.subscribe(listener=subscriber, topicName=pub.ALL_TOPICS) From 77ea3e554d786698c55fc8c099b9c0aae138c8c0 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 00:06:24 +0530 Subject: [PATCH 18/69] UT: Replace PypubsubEventQueue -> EventQueue and test_pypubsub_event_queue.py -> test_event_queue.py --- .../common/event_queue/test_event_queue.py | 103 +++++++++++++++++ .../event_queue/test_pypubsub_event_queue.py | 105 ------------------ 2 files changed, 103 insertions(+), 105 deletions(-) create mode 100644 monkey/tests/unit_tests/common/event_queue/test_event_queue.py delete mode 100644 monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py diff --git a/monkey/tests/unit_tests/common/event_queue/test_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_event_queue.py new file mode 100644 index 000000000..2b2aba3ac --- /dev/null +++ b/monkey/tests/unit_tests/common/event_queue/test_event_queue.py @@ -0,0 +1,103 @@ +from dataclasses import dataclass +from unittest.mock import MagicMock + +import pytest + +import common.event_queue.event_queue as event_queue_file +from common.event_queue.event_queue import EventQueue +from common.events import AbstractEvent + +EVENT_TAG_1 = "event tag 1" +EVENT_TAG_2 = "event tag 2" + + +@dataclass(frozen=True) +class EventType(AbstractEvent): + source = "1234" + target = None + timestamp = 0.0 + tags = [EVENT_TAG_1, EVENT_TAG_2] + + +@pytest.fixture(autouse=True) +def wrap_pypubsub_functions(): + # This is done so that we can use `.call_count` in the tests. + event_queue_file.pub.sendMessage = MagicMock(side_effect=event_queue_file.pub.sendMessage) + + +def test_subscribe_all(): + subscriber = MagicMock() + + EventQueue.subscribe_all(subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + +def test_subscribe_types(): + subscriber = MagicMock() + + EventQueue.subscribe_types([EventType], subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 1 + + +def test_subscribe_tags(): + subscriber = MagicMock() + + EventQueue.subscribe_tags([EVENT_TAG_2], subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 1 + + +def test_unsubscribe_all(): + subscriber = MagicMock() + + EventQueue.subscribe_all(subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + EventQueue.unsubscribe_all(subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 6 + assert subscriber.call_count == 3 + + +def test_unsubscribe_types(): + subscriber = MagicMock() + + EventQueue.subscribe_all(subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + EventQueue.unsubscribe_types([EventType], subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 6 + assert subscriber.call_count == 5 + + +def test_unsubscribe_tags(): + subscriber = MagicMock() + + EventQueue.subscribe_all(subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + EventQueue.unsubscribe_tags([EVENT_TAG_1, EVENT_TAG_2], subscriber) + EventQueue.publish(EventType) + + assert event_queue_file.pub.sendMessage.call_count == 6 + assert subscriber.call_count == 4 diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py deleted file mode 100644 index b06c37c86..000000000 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ /dev/null @@ -1,105 +0,0 @@ -from dataclasses import dataclass -from unittest.mock import MagicMock - -import pytest - -import common.event_queue.pypubsub_event_queue as pypubsub_event_queue_file -from common.event_queue.pypubsub_event_queue import PypubsubEventQueue -from common.events import AbstractEvent - -EVENT_TAG_1 = "event tag 1" -EVENT_TAG_2 = "event tag 2" - - -@dataclass(frozen=True) -class EventType(AbstractEvent): - source = "1234" - target = None - timestamp = 0.0 - tags = [EVENT_TAG_1, EVENT_TAG_2] - - -@pytest.fixture(autouse=True) -def wrap_pypubsub_functions(): - # This is done so that we can use `.call_count` in the tests. - pypubsub_event_queue_file.pub.sendMessage = MagicMock( - side_effect=pypubsub_event_queue_file.pub.sendMessage - ) - - -def test_subscribe_all(): - subscriber = MagicMock() - - PypubsubEventQueue.subscribe_all(subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - -def test_subscribe_types(): - subscriber = MagicMock() - - PypubsubEventQueue.subscribe_types([EventType], subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 1 - - -def test_subscribe_tags(): - subscriber = MagicMock() - - PypubsubEventQueue.subscribe_tags([EVENT_TAG_2], subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 1 - - -def test_unsubscribe_all(): - subscriber = MagicMock() - - PypubsubEventQueue.subscribe_all(subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - PypubsubEventQueue.unsubscribe_all(subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 6 - assert subscriber.call_count == 3 - - -def test_unsubscribe_types(): - subscriber = MagicMock() - - PypubsubEventQueue.subscribe_all(subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - PypubsubEventQueue.unsubscribe_types([EventType], subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 6 - assert subscriber.call_count == 5 - - -def test_unsubscribe_tags(): - subscriber = MagicMock() - - PypubsubEventQueue.subscribe_all(subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - PypubsubEventQueue.unsubscribe_tags([EVENT_TAG_1, EVENT_TAG_2], subscriber) - PypubsubEventQueue.publish(EventType) - - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 6 - assert subscriber.call_count == 4 From f3ef99db641c6b92b4f0d1684fedb938f029a40d Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:37:21 +0530 Subject: [PATCH 19/69] Common: Remove unsubscribing methods from IEventQueue and EventQueue --- monkey/common/event_queue/event_queue.py | 16 ----------- monkey/common/event_queue/i_event_queue.py | 32 ---------------------- 2 files changed, 48 deletions(-) diff --git a/monkey/common/event_queue/event_queue.py b/monkey/common/event_queue/event_queue.py index 83f6fb833..a26ada222 100644 --- a/monkey/common/event_queue/event_queue.py +++ b/monkey/common/event_queue/event_queue.py @@ -35,19 +35,3 @@ class EventQueue(IEventQueue): # publish to tags' topics for tag in event.tags: pub.sendMessage(tag, **data) - - @staticmethod - def unsubscribe_all(subscriber: Callable[..., Any]): - pub.unsubscribe(listener=subscriber, topicName=pub.ALL_TOPICS) - - @staticmethod - def unsubscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): - for event_type in types: - # pypubsub.pub.subscribe needs a string as the topic/event name - event_type_name = event_type.__name__ - pub.unsubscribe(listener=subscriber, topicName=event_type_name) - - @staticmethod - def unsubscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): - for tag in tags: - pub.unsubscribe(listener=subscriber, topicName=tag) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index af4c1332d..960b5b385 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -51,35 +51,3 @@ class IEventQueue(ABC): """ pass - - @abstractstaticmethod - def unsubscribe_all(subscriber: Callable[..., Any]): - """ - Unsubscribes a subscriber from all events - - :param subscriber: Callable that should unsubscribe from events - """ - - pass - - @abstractstaticmethod - def unsubscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): - """ - Unsubscribes a subscriber from all specifed event types - - :param types: Event types from which the subscriber should unsubscribe - :param subscriber: Callable that should unsubscribe from events - """ - - pass - - @abstractstaticmethod - def unsubscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): - """ - Unubscribes a subscriber from all specified event tags - - :param tags: Event tags from which the subscriber should unsubscribe - :param subscriber: Callable that should unsubscribe from events - """ - - pass From ae2ba0e0686f5adebf7b8bdb5d2356b374fad820 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:39:43 +0530 Subject: [PATCH 20/69] UT: Remove event queue's unsubscribing tests --- .../common/event_queue/test_event_queue.py | 48 ------------------- 1 file changed, 48 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_event_queue.py index 2b2aba3ac..7e3b9e62d 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_event_queue.py @@ -53,51 +53,3 @@ def test_subscribe_tags(): assert event_queue_file.pub.sendMessage.call_count == 3 assert subscriber.call_count == 1 - - -def test_unsubscribe_all(): - subscriber = MagicMock() - - EventQueue.subscribe_all(subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - EventQueue.unsubscribe_all(subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 6 - assert subscriber.call_count == 3 - - -def test_unsubscribe_types(): - subscriber = MagicMock() - - EventQueue.subscribe_all(subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - EventQueue.unsubscribe_types([EventType], subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 6 - assert subscriber.call_count == 5 - - -def test_unsubscribe_tags(): - subscriber = MagicMock() - - EventQueue.subscribe_all(subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - EventQueue.unsubscribe_tags([EVENT_TAG_1, EVENT_TAG_2], subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 6 - assert subscriber.call_count == 4 From d346e63a2663e5b14d9d1b5ee2e40137431c63c5 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:42:57 +0530 Subject: [PATCH 21/69] Common: Rename EventQueue -> PyPubSubEventQueue --- .../event_queue/{event_queue.py => pypubsub_event_queue.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename monkey/common/event_queue/{event_queue.py => pypubsub_event_queue.py} (96%) diff --git a/monkey/common/event_queue/event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py similarity index 96% rename from monkey/common/event_queue/event_queue.py rename to monkey/common/event_queue/pypubsub_event_queue.py index a26ada222..97b4e71dc 100644 --- a/monkey/common/event_queue/event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -7,7 +7,7 @@ from common.events import AbstractEvent from .i_event_queue import IEventQueue -class EventQueue(IEventQueue): +class PyPubSubEventQueue(IEventQueue): @staticmethod def subscribe_all(subscriber: Callable[..., Any]): pub.subscribe(listener=subscriber, topicName=pub.ALL_TOPICS) From 9210023876d4551a187f5b87034bda64b6358189 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:43:37 +0530 Subject: [PATCH 22/69] UT: Replace EventQueue with PyPubSubEventQueue --- .../common/event_queue/test_event_queue.py | 55 ------------------ .../event_queue/test_pypubsub_event_queue.py | 57 +++++++++++++++++++ 2 files changed, 57 insertions(+), 55 deletions(-) delete mode 100644 monkey/tests/unit_tests/common/event_queue/test_event_queue.py create mode 100644 monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py diff --git a/monkey/tests/unit_tests/common/event_queue/test_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_event_queue.py deleted file mode 100644 index 7e3b9e62d..000000000 --- a/monkey/tests/unit_tests/common/event_queue/test_event_queue.py +++ /dev/null @@ -1,55 +0,0 @@ -from dataclasses import dataclass -from unittest.mock import MagicMock - -import pytest - -import common.event_queue.event_queue as event_queue_file -from common.event_queue.event_queue import EventQueue -from common.events import AbstractEvent - -EVENT_TAG_1 = "event tag 1" -EVENT_TAG_2 = "event tag 2" - - -@dataclass(frozen=True) -class EventType(AbstractEvent): - source = "1234" - target = None - timestamp = 0.0 - tags = [EVENT_TAG_1, EVENT_TAG_2] - - -@pytest.fixture(autouse=True) -def wrap_pypubsub_functions(): - # This is done so that we can use `.call_count` in the tests. - event_queue_file.pub.sendMessage = MagicMock(side_effect=event_queue_file.pub.sendMessage) - - -def test_subscribe_all(): - subscriber = MagicMock() - - EventQueue.subscribe_all(subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 - - -def test_subscribe_types(): - subscriber = MagicMock() - - EventQueue.subscribe_types([EventType], subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 1 - - -def test_subscribe_tags(): - subscriber = MagicMock() - - EventQueue.subscribe_tags([EVENT_TAG_2], subscriber) - EventQueue.publish(EventType) - - assert event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 1 diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py new file mode 100644 index 000000000..3a02563cc --- /dev/null +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -0,0 +1,57 @@ +from dataclasses import dataclass +from unittest.mock import MagicMock + +import pytest + +import common.event_queue.pypubsub_event_queue as pypubsub_event_queue_file +from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue +from common.events import AbstractEvent + +EVENT_TAG_1 = "event tag 1" +EVENT_TAG_2 = "event tag 2" + + +@dataclass(frozen=True) +class EventType(AbstractEvent): + source = "1234" + target = None + timestamp = 0.0 + tags = [EVENT_TAG_1, EVENT_TAG_2] + + +@pytest.fixture(autouse=True) +def wrap_pypubsub_functions(): + # This is done so that we can use `.call_count` in the tests. + pypubsub_event_queue_file.pub.sendMessage = MagicMock( + side_effect=pypubsub_event_queue_file.pub.sendMessage + ) + + +def test_subscribe_all(): + subscriber = MagicMock() + + PyPubSubEventQueue.subscribe_all(subscriber) + PyPubSubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 3 + + +def test_subscribe_types(): + subscriber = MagicMock() + + PyPubSubEventQueue.subscribe_types([EventType], subscriber) + PyPubSubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 1 + + +def test_subscribe_tags(): + subscriber = MagicMock() + + PyPubSubEventQueue.subscribe_tags([EVENT_TAG_2], subscriber) + PyPubSubEventQueue.publish(EventType) + + assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert subscriber.call_count == 1 From e92d67bfe8dbe065250a8d7fe6311e4149d0c488 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:46:19 +0530 Subject: [PATCH 23/69] Common: Fix type hints in IEventQueue and PyPubSubEventQueue --- monkey/common/event_queue/i_event_queue.py | 8 +++++--- monkey/common/event_queue/pypubsub_event_queue.py | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 960b5b385..06a99b163 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -10,7 +10,7 @@ class IEventQueue(ABC): """ @abstractstaticmethod - def subscribe_all(subscriber: Callable[..., Any]): + def subscribe_all(subscriber: Callable[[AbstractEvent], None]): """ Subscribes a subscriber to all events @@ -20,7 +20,9 @@ class IEventQueue(ABC): pass @abstractstaticmethod - def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + def subscribe_types( + types: Sequence[AbstractEvent], subscriber: Callable[[AbstractEvent], None] + ): """ Subscribes a subscriber to all specifed event types @@ -31,7 +33,7 @@ class IEventQueue(ABC): pass @abstractstaticmethod - def subscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): + def subscribe_tags(tags: Sequence[str], subscriber: Callable[[AbstractEvent], None]): """ Subscribes a subscriber to all specified event tags diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 97b4e71dc..e2e6e8765 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -9,18 +9,20 @@ from .i_event_queue import IEventQueue class PyPubSubEventQueue(IEventQueue): @staticmethod - def subscribe_all(subscriber: Callable[..., Any]): + def subscribe_all(subscriber: Callable[[AbstractEvent], None]): pub.subscribe(listener=subscriber, topicName=pub.ALL_TOPICS) @staticmethod - def subscribe_types(types: Sequence[AbstractEvent], subscriber: Callable[..., Any]): + def subscribe_types( + types: Sequence[AbstractEvent], subscriber: Callable[[AbstractEvent], None] + ): for event_type in types: # pypubsub.pub.subscribe needs a string as the topic/event name event_type_name = event_type.__name__ pub.subscribe(listener=subscriber, topicName=event_type_name) @staticmethod - def subscribe_tags(tags: Sequence[str], subscriber: Callable[..., Any]): + def subscribe_tags(tags: Sequence[str], subscriber: Callable[[AbstractEvent], None]): for tag in tags: pub.subscribe(listener=subscriber, topicName=tag) From ae666d3dd36b057166b9aa2889f50b15ae52d10a Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:50:31 +0530 Subject: [PATCH 24/69] Common: Change IEventQueue and PyPubSubEventQueue's subscribe_tags() -> subscribe_tag() --- monkey/common/event_queue/i_event_queue.py | 6 +++--- monkey/common/event_queue/pypubsub_event_queue.py | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 06a99b163..47788f263 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -33,11 +33,11 @@ class IEventQueue(ABC): pass @abstractstaticmethod - def subscribe_tags(tags: Sequence[str], subscriber: Callable[[AbstractEvent], None]): + def subscribe_tag(tag: str, subscriber: Callable[[AbstractEvent], None]): """ - Subscribes a subscriber to all specified event tags + Subscribes a subscriber to the specified event tag - :param tags: Event tags to which the subscriber should subscribe + :param tag: Event tag to which the subscriber should subscribe :param subscriber: Callable that should subscribe to events """ diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index e2e6e8765..711187577 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -22,9 +22,8 @@ class PyPubSubEventQueue(IEventQueue): pub.subscribe(listener=subscriber, topicName=event_type_name) @staticmethod - def subscribe_tags(tags: Sequence[str], subscriber: Callable[[AbstractEvent], None]): - for tag in tags: - pub.subscribe(listener=subscriber, topicName=tag) + def subscribe_tag(tag: str, subscriber: Callable[[AbstractEvent], None]): + pub.subscribe(listener=subscriber, topicName=tag) @staticmethod def publish(event: AbstractEvent, data: Any = None): From 3cef54c09c00933b192dc6e8a9df469e18f08a2d Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:53:03 +0530 Subject: [PATCH 25/69] Common: Change IEventQueue and PyPubSubEventQueue's subscribe_types() -> subscribe_type() --- monkey/common/event_queue/i_event_queue.py | 10 ++++------ monkey/common/event_queue/pypubsub_event_queue.py | 13 +++++-------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 47788f263..c119726f4 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,5 +1,5 @@ from abc import ABC, abstractstaticmethod -from typing import Any, Callable, Sequence +from typing import Any, Callable from common.events import AbstractEvent @@ -20,13 +20,11 @@ class IEventQueue(ABC): pass @abstractstaticmethod - def subscribe_types( - types: Sequence[AbstractEvent], subscriber: Callable[[AbstractEvent], None] - ): + def subscribe_type(event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None]): """ - Subscribes a subscriber to all specifed event types + Subscribes a subscriber to the specifed event type - :param types: Event types to which the subscriber should subscribe + :param event_type: Event type to which the subscriber should subscribe :param subscriber: Callable that should subscribe to events """ diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 711187577..6ea4111f9 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,4 +1,4 @@ -from typing import Any, Callable, Sequence +from typing import Any, Callable from pubsub import pub @@ -13,13 +13,10 @@ class PyPubSubEventQueue(IEventQueue): pub.subscribe(listener=subscriber, topicName=pub.ALL_TOPICS) @staticmethod - def subscribe_types( - types: Sequence[AbstractEvent], subscriber: Callable[[AbstractEvent], None] - ): - for event_type in types: - # pypubsub.pub.subscribe needs a string as the topic/event name - event_type_name = event_type.__name__ - pub.subscribe(listener=subscriber, topicName=event_type_name) + def subscribe_type(event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None]): + # pypubsub.pub.subscribe needs a string as the topic/event name + event_type_name = event_type.__name__ + pub.subscribe(listener=subscriber, topicName=event_type_name) @staticmethod def subscribe_tag(tag: str, subscriber: Callable[[AbstractEvent], None]): From 00b62dc1a96585d6db1622807406b4466368163f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 11:56:15 +0530 Subject: [PATCH 26/69] UT: Modify PyPubSubEventQueue's tests based on changes --- .../common/event_queue/test_pypubsub_event_queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 3a02563cc..738b91566 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -40,7 +40,7 @@ def test_subscribe_all(): def test_subscribe_types(): subscriber = MagicMock() - PyPubSubEventQueue.subscribe_types([EventType], subscriber) + PyPubSubEventQueue.subscribe_type(EventType, subscriber) PyPubSubEventQueue.publish(EventType) assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 @@ -50,7 +50,7 @@ def test_subscribe_types(): def test_subscribe_tags(): subscriber = MagicMock() - PyPubSubEventQueue.subscribe_tags([EVENT_TAG_2], subscriber) + PyPubSubEventQueue.subscribe_tag(EVENT_TAG_2, subscriber) PyPubSubEventQueue.publish(EventType) assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 From d190f220f7deab575169afe313aea72aeca1d3f8 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 14:31:44 +0530 Subject: [PATCH 27/69] Common: Modify PyPubSubEventQueue to pass publisher in constructor --- .../event_queue/pypubsub_event_queue.py | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 6ea4111f9..0910eee73 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,35 +1,36 @@ from typing import Any, Callable -from pubsub import pub - from common.events import AbstractEvent from .i_event_queue import IEventQueue class PyPubSubEventQueue(IEventQueue): - @staticmethod - def subscribe_all(subscriber: Callable[[AbstractEvent], None]): - pub.subscribe(listener=subscriber, topicName=pub.ALL_TOPICS) + def __init__(self, pypubsub_publisher): + self._pypubsub_publisher = pypubsub_publisher - @staticmethod - def subscribe_type(event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None]): + def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): + self._pypubsub_publisher.subscribe( + listener=subscriber, topicName=self._pypubsub_publisher.ALL_TOPICS + ) + + def subscribe_type( + self, event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None] + ): # pypubsub.pub.subscribe needs a string as the topic/event name event_type_name = event_type.__name__ - pub.subscribe(listener=subscriber, topicName=event_type_name) + self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_name) - @staticmethod - def subscribe_tag(tag: str, subscriber: Callable[[AbstractEvent], None]): - pub.subscribe(listener=subscriber, topicName=tag) + def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): + self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) - @staticmethod - def publish(event: AbstractEvent, data: Any = None): + def publish(self, event: AbstractEvent, data: Any = None): data = data if data else {} # publish to event type's topic event_type_name = event.__name__ - pub.sendMessage(event_type_name, **data) + self._pypubsub_publisher.sendMessage(event_type_name, **data) # publish to tags' topics for tag in event.tags: - pub.sendMessage(tag, **data) + self._pypubsub_publisher.sendMessage(tag, **data) From 2d248151b388291622dc212a40bd4cc741a080e7 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 14:32:33 +0530 Subject: [PATCH 28/69] Island: Register IEventQueue instance in DI container --- monkey/monkey_island/cc/services/initialize.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index f6ff6d309..eeb488b14 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -3,6 +3,7 @@ import logging from pathlib import Path from typing import Sequence +from pubsub import pub from pymongo import MongoClient from common import DIContainer @@ -13,6 +14,8 @@ from common.agent_configuration import ( ) from common.aws import AWSInstance from common.common_consts.telem_categories import TelemCategoryEnum +from common.event_queue.i_event_queue import IEventQueue +from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue from common.utils.file_utils import get_binary_io_sha256_hash from common.version import get_version from monkey_island.cc import Version @@ -71,6 +74,8 @@ def initialize_services(data_dir: Path) -> DIContainer: ILockableEncryptor, RepositoryEncryptor(data_dir / REPOSITORY_KEY_FILE_NAME) ) container.register_instance(Version, container.resolve(Version)) + container.register_instance(IEventQueue, PyPubSubEventQueue(pub)) + _register_repositories(container, data_dir) _register_services(container) From 390757f9cb6fbfdc630fdb0ed5f352c4c50ae911 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 14:33:35 +0530 Subject: [PATCH 29/69] UT: Modify PyPubSubEventQueuetests as per latest changes --- .../event_queue/test_pypubsub_event_queue.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 738b91566..50c9635d9 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -2,8 +2,8 @@ from dataclasses import dataclass from unittest.mock import MagicMock import pytest +from pubsub import pub -import common.event_queue.pypubsub_event_queue as pypubsub_event_queue_file from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue from common.events import AbstractEvent @@ -22,36 +22,38 @@ class EventType(AbstractEvent): @pytest.fixture(autouse=True) def wrap_pypubsub_functions(): # This is done so that we can use `.call_count` in the tests. - pypubsub_event_queue_file.pub.sendMessage = MagicMock( - side_effect=pypubsub_event_queue_file.pub.sendMessage - ) + pub.sendMessage = MagicMock(side_effect=pub.sendMessage) + + +pypubsub_event_queue = PyPubSubEventQueue(pub) def test_subscribe_all(): subscriber = MagicMock() - PyPubSubEventQueue.subscribe_all(subscriber) - PyPubSubEventQueue.publish(EventType) + pypubsub_event_queue.subscribe_all(subscriber) + pypubsub_event_queue.publish(EventType) - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert pub.sendMessage.call_count == 3 assert subscriber.call_count == 3 def test_subscribe_types(): subscriber = MagicMock() - PyPubSubEventQueue.subscribe_type(EventType, subscriber) - PyPubSubEventQueue.publish(EventType) + pypubsub_event_queue.subscribe_type(EventType, subscriber) + pypubsub_event_queue.publish(EventType) - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 + assert pub.sendMessage.call_count == 3 assert subscriber.call_count == 1 def test_subscribe_tags(): subscriber = MagicMock() - PyPubSubEventQueue.subscribe_tag(EVENT_TAG_2, subscriber) - PyPubSubEventQueue.publish(EventType) + pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber) + pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber) + pypubsub_event_queue.publish(EventType) - assert pypubsub_event_queue_file.pub.sendMessage.call_count == 3 - assert subscriber.call_count == 1 + assert pub.sendMessage.call_count == 3 + assert subscriber.call_count == 2 From 585b1ca408c432a57e98efbdebe05c78cf8cd1f0 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 14:35:24 +0530 Subject: [PATCH 30/69] Common: Modify IEventQueue to not have any static methods --- monkey/common/event_queue/i_event_queue.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index c119726f4..e33d12083 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,4 +1,4 @@ -from abc import ABC, abstractstaticmethod +from abc import ABC, abstractmethod from typing import Any, Callable from common.events import AbstractEvent @@ -9,8 +9,8 @@ class IEventQueue(ABC): Manages subscription and publishing of events """ - @abstractstaticmethod - def subscribe_all(subscriber: Callable[[AbstractEvent], None]): + @abstractmethod + def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): """ Subscribes a subscriber to all events @@ -19,8 +19,10 @@ class IEventQueue(ABC): pass - @abstractstaticmethod - def subscribe_type(event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None]): + @abstractmethod + def subscribe_type( + self, event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None] + ): """ Subscribes a subscriber to the specifed event type @@ -30,8 +32,8 @@ class IEventQueue(ABC): pass - @abstractstaticmethod - def subscribe_tag(tag: str, subscriber: Callable[[AbstractEvent], None]): + @abstractmethod + def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): """ Subscribes a subscriber to the specified event tag @@ -41,8 +43,8 @@ class IEventQueue(ABC): pass - @abstractstaticmethod - def publish(event: AbstractEvent, data: Any): + @abstractmethod + def publish(self, event: AbstractEvent, data: Any): """ Publishes an event with the given data From 7a77fd82dfed9ec6097093f26f9c82eaa30b47ae Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 17:12:48 +0530 Subject: [PATCH 31/69] Project: Add event queue's entries to Vulture allowlist --- vulture_allowlist.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index 287619b44..d268f68a1 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -261,3 +261,12 @@ key_list simulation netmap validate_windows_filename_not_reserved +subscribe_all # common\event_queue\i_event_queue.py +subscribe_type # common\event_queue\i_event_queue.py +subscribe_tag # common\event_queue\i_event_queue.py +publish # common\event_queue\i_event_queue.py +subscribe_all # common\event_queue\pypubsub_event_queue.py +subscribe_type # common\event_queue\pypubsub_event_queue.py +subscribe_tag # common\event_queue\pypubsub_event_queue.py +publish # common\event_queue\pypubsub_event_queue.py +PyPubSubEventQueue # common\event_queue\pypubsub_event_queue.py From 8da3abbb5b86ce375f20fca797a0422da7d83e46 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 18:54:17 +0530 Subject: [PATCH 32/69] Common: Add internal all event types topic to PyPubSubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 0910eee73..b28bea802 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -4,6 +4,8 @@ from common.events import AbstractEvent from .i_event_queue import IEventQueue +INTERNAL_ALL_EVENT_TYPES_TOPIC = "internal_all_event_types" + class PyPubSubEventQueue(IEventQueue): def __init__(self, pypubsub_publisher): @@ -21,6 +23,11 @@ class PyPubSubEventQueue(IEventQueue): event_type_name = event_type.__name__ self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_name) + def subscribe_all_event_types(self, subscriber: Callable[[AbstractEvent], None]): + self._pypubsub_publisher.subscribe( + listener=subscriber, topicName=INTERNAL_ALL_EVENT_TYPES_TOPIC + ) + def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) @@ -31,6 +38,9 @@ class PyPubSubEventQueue(IEventQueue): event_type_name = event.__name__ self._pypubsub_publisher.sendMessage(event_type_name, **data) + # publish to all events' topic + self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, **data) + # publish to tags' topics for tag in event.tags: self._pypubsub_publisher.sendMessage(tag, **data) From 3938e2f3773d6c20834379308754d7be4e5383c8 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Tue, 9 Aug 2022 19:00:05 +0530 Subject: [PATCH 33/69] Project: Add subscribe_all_event_types to Vulture allowlist --- vulture_allowlist.py | 1 + 1 file changed, 1 insertion(+) diff --git a/vulture_allowlist.py b/vulture_allowlist.py index d268f68a1..a533be1a9 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -270,3 +270,4 @@ subscribe_type # common\event_queue\pypubsub_event_queue.py subscribe_tag # common\event_queue\pypubsub_event_queue.py publish # common\event_queue\pypubsub_event_queue.py PyPubSubEventQueue # common\event_queue\pypubsub_event_queue.py +subscribe_all_event_types # common\event_queue\pypubsub_event_queue.py From d154a7989c5c189aa51e563279a4f0349dbddfc9 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 13:26:01 +0530 Subject: [PATCH 34/69] Common: Don't pass 'data' argument to IEventQueue's publish() --- monkey/common/event_queue/i_event_queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index e33d12083..ae9371ba4 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import Any, Callable +from typing import Callable from common.events import AbstractEvent @@ -44,7 +44,7 @@ class IEventQueue(ABC): pass @abstractmethod - def publish(self, event: AbstractEvent, data: Any): + def publish(self, event: AbstractEvent): """ Publishes an event with the given data From 32efa0f0f249a3115bdedc14da26aa4ec6fd3b6b Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 13:27:55 +0530 Subject: [PATCH 35/69] Common: Don't pass 'data' argument when publishing in PyPubSubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index b28bea802..82bdc9567 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,4 +1,4 @@ -from typing import Any, Callable +from typing import Callable from common.events import AbstractEvent @@ -31,16 +31,14 @@ class PyPubSubEventQueue(IEventQueue): def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) - def publish(self, event: AbstractEvent, data: Any = None): - data = data if data else {} - + def publish(self, event: AbstractEvent): # publish to event type's topic event_type_name = event.__name__ - self._pypubsub_publisher.sendMessage(event_type_name, **data) + self._pypubsub_publisher.sendMessage(event_type_name, event) # publish to all events' topic - self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, **data) + self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, event) # publish to tags' topics for tag in event.tags: - self._pypubsub_publisher.sendMessage(tag, **data) + self._pypubsub_publisher.sendMessage(tag, event) From f26beb6464485e04c0b972566d89043d750c1efd Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 14:02:53 +0530 Subject: [PATCH 36/69] Common: Pass event_data to subscribers when publishing in PyPubSubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 82bdc9567..ad191fb24 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -32,13 +32,15 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) def publish(self, event: AbstractEvent): + event_data = {"event": event} + # publish to event type's topic event_type_name = event.__name__ - self._pypubsub_publisher.sendMessage(event_type_name, event) + self._pypubsub_publisher.sendMessage(event_type_name, **event_data) # publish to all events' topic - self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, event) + self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, **event_data) # publish to tags' topics for tag in event.tags: - self._pypubsub_publisher.sendMessage(tag, event) + self._pypubsub_publisher.sendMessage(tag, **event_data) From 2fd66bc9e23fd527d9d5e0fabd28290acb40a6ff Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 14:49:31 +0530 Subject: [PATCH 37/69] Common: Add comment about subscribing to all topics in PyPubSubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index ad191fb24..882857050 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -12,6 +12,12 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher = pypubsub_publisher def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): + # From the documentation (https://pypubsub.readthedocs.io/en/v4.0.3/usage/ + # usage_advanced_debug.html#listen-for-messages-from-all-topics): + # "PyPubSub defines a special topic named pub.ALL_TOPICS. A listener that subscribes + # to this topic will receives all messages of every topic. By default, the listener + # will not receive any data since pub.ALL_TOPICS is the parent of all root topics (...)" + self._pypubsub_publisher.subscribe( listener=subscriber, topicName=self._pypubsub_publisher.ALL_TOPICS ) From 3aff182d479f3e21b46b5a562d79ae872df4d422 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 14:53:46 +0530 Subject: [PATCH 38/69] UT: Simplify PyPubSubEventQueue's tests --- .../event_queue/test_pypubsub_event_queue.py | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 50c9635d9..a06eb3f52 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -1,10 +1,12 @@ from dataclasses import dataclass -from unittest.mock import MagicMock import pytest from pubsub import pub -from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue +from common.event_queue.pypubsub_event_queue import ( + INTERNAL_ALL_EVENT_TYPES_TOPIC, + PyPubSubEventQueue, +) from common.events import AbstractEvent EVENT_TAG_1 = "event tag 1" @@ -19,41 +21,57 @@ class EventType(AbstractEvent): tags = [EVENT_TAG_1, EVENT_TAG_2] -@pytest.fixture(autouse=True) -def wrap_pypubsub_functions(): - # This is done so that we can use `.call_count` in the tests. - pub.sendMessage = MagicMock(side_effect=pub.sendMessage) - - pypubsub_event_queue = PyPubSubEventQueue(pub) +subscriber_1_calls = subscriber_2_calls = subscriber_1 = subscriber_2 = None + + +@pytest.fixture(autouse=True, scope="function") +def reset_subscribers(): + global subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls + subscriber_1_calls = [] + subscriber_2_calls = [] + subscriber_1 = lambda event, topic=pub.AUTO_TOPIC: subscriber_1_calls.append(topic.getName()) + subscriber_2 = lambda event, topic=pub.AUTO_TOPIC: subscriber_2_calls.append(topic.getName()) + + +def test_topic_subscription(): + pypubsub_event_queue.subscribe_type(EventType, subscriber_1) + pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_1) + pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_2) + pypubsub_event_queue.publish(EventType) + + assert subscriber_1_calls == [EventType.__name__, EVENT_TAG_2] + assert subscriber_2_calls == [EVENT_TAG_1] + def test_subscribe_all(): - subscriber = MagicMock() + subscriber_calls = [] + subscriber = lambda topic=pub.AUTO_TOPIC: subscriber_calls.append(topic.getName()) pypubsub_event_queue.subscribe_all(subscriber) pypubsub_event_queue.publish(EventType) - assert pub.sendMessage.call_count == 3 - assert subscriber.call_count == 3 + assert subscriber_calls == [ + EventType.__name__, + INTERNAL_ALL_EVENT_TYPES_TOPIC, + EVENT_TAG_1, + EVENT_TAG_2, + ] def test_subscribe_types(): - subscriber = MagicMock() - - pypubsub_event_queue.subscribe_type(EventType, subscriber) + pypubsub_event_queue.subscribe_type(EventType, subscriber_1) pypubsub_event_queue.publish(EventType) - assert pub.sendMessage.call_count == 3 - assert subscriber.call_count == 1 + assert subscriber_1_calls == [EventType.__name__] + assert subscriber_2_calls == [] def test_subscribe_tags(): - subscriber = MagicMock() - - pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber) - pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber) + pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_1) + pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_2) pypubsub_event_queue.publish(EventType) - assert pub.sendMessage.call_count == 3 - assert subscriber.call_count == 2 + assert subscriber_1_calls == [EVENT_TAG_1] + assert subscriber_2_calls == [EVENT_TAG_2] From edad0d8cf200953f59d3d85d90c7fb90e966ceaf Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 15:39:14 +0530 Subject: [PATCH 39/69] UT: Simplify test logic with fixtures in PyPubSubEventQueue --- .../event_queue/test_pypubsub_event_queue.py | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index a06eb3f52..db240dd3d 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -23,19 +23,29 @@ class EventType(AbstractEvent): pypubsub_event_queue = PyPubSubEventQueue(pub) -subscriber_1_calls = subscriber_2_calls = subscriber_1 = subscriber_2 = None + +@pytest.fixture() +def subscriber_1_calls(): + return [] -@pytest.fixture(autouse=True, scope="function") -def reset_subscribers(): - global subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls - subscriber_1_calls = [] - subscriber_2_calls = [] - subscriber_1 = lambda event, topic=pub.AUTO_TOPIC: subscriber_1_calls.append(topic.getName()) - subscriber_2 = lambda event, topic=pub.AUTO_TOPIC: subscriber_2_calls.append(topic.getName()) +@pytest.fixture() +def subscriber_2_calls(): + return [] -def test_topic_subscription(): +@pytest.fixture +def subscriber_1(subscriber_1_calls): + return lambda event, topic=pub.AUTO_TOPIC: subscriber_1_calls.append(topic.getName()) + + +@pytest.fixture +def subscriber_2(subscriber_2_calls): + return lambda event, topic=pub.AUTO_TOPIC: subscriber_2_calls.append(topic.getName()) + + +@pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") +def test_topic_subscription(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): pypubsub_event_queue.subscribe_type(EventType, subscriber_1) pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_1) pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_2) @@ -60,7 +70,8 @@ def test_subscribe_all(): ] -def test_subscribe_types(): +@pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") +def test_subscribe_types(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): pypubsub_event_queue.subscribe_type(EventType, subscriber_1) pypubsub_event_queue.publish(EventType) @@ -68,7 +79,8 @@ def test_subscribe_types(): assert subscriber_2_calls == [] -def test_subscribe_tags(): +@pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") +def test_subscribe_tags(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_1) pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_2) pypubsub_event_queue.publish(EventType) From e905f6eddfbc65250aaa3d16c94fb7c564d2cb20 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 15:41:03 +0530 Subject: [PATCH 40/69] UT: Define functions instead of lambda expressions in PyPubSubEventQueue tests since flake8 complains --- .../event_queue/test_pypubsub_event_queue.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index db240dd3d..1d2fc4fd1 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -36,12 +36,18 @@ def subscriber_2_calls(): @pytest.fixture def subscriber_1(subscriber_1_calls): - return lambda event, topic=pub.AUTO_TOPIC: subscriber_1_calls.append(topic.getName()) + def fn(event, topic=pub.AUTO_TOPIC): + subscriber_1_calls.append(topic.getName()) + + return fn @pytest.fixture def subscriber_2(subscriber_2_calls): - return lambda event, topic=pub.AUTO_TOPIC: subscriber_2_calls.append(topic.getName()) + def fn(event, topic=pub.AUTO_TOPIC): + subscriber_2_calls.append(topic.getName()) + + return fn @pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") @@ -57,7 +63,9 @@ def test_topic_subscription(subscriber_1, subscriber_2, subscriber_1_calls, subs def test_subscribe_all(): subscriber_calls = [] - subscriber = lambda topic=pub.AUTO_TOPIC: subscriber_calls.append(topic.getName()) + + def subscriber(topic=pub.AUTO_TOPIC): + subscriber_calls.append(topic.getName()) pypubsub_event_queue.subscribe_all(subscriber) pypubsub_event_queue.publish(EventType) From 6ecb2f4d4919564919f8586fb3f8022641c46f17 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 15:45:39 +0530 Subject: [PATCH 41/69] UT: Simplify logic in test_pypubsub_event_queue.test_subscribe_types() --- .../common/event_queue/test_pypubsub_event_queue.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 1d2fc4fd1..89cc3f08d 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -78,13 +78,12 @@ def test_subscribe_all(): ] -@pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") -def test_subscribe_types(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): +@pytest.mark.usefixtures("subscriber_1", "subscriber_1_calls") +def test_subscribe_types(subscriber_1, subscriber_1_calls): pypubsub_event_queue.subscribe_type(EventType, subscriber_1) pypubsub_event_queue.publish(EventType) assert subscriber_1_calls == [EventType.__name__] - assert subscriber_2_calls == [] @pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") From 416cb30577690b7341885c46ee6413c6f83458c1 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 07:29:13 -0400 Subject: [PATCH 42/69] Common: Add type hint ty PyPubSubEventQueue constructor --- monkey/common/event_queue/pypubsub_event_queue.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 882857050..efac70881 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,6 +1,7 @@ from typing import Callable from common.events import AbstractEvent +from pubsub.core import Publisher from .i_event_queue import IEventQueue @@ -8,7 +9,7 @@ INTERNAL_ALL_EVENT_TYPES_TOPIC = "internal_all_event_types" class PyPubSubEventQueue(IEventQueue): - def __init__(self, pypubsub_publisher): + def __init__(self, pypubsub_publisher: Publisher): self._pypubsub_publisher = pypubsub_publisher def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): From d09f743100d746971bba7ea076baea685f3f8de7 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 07:31:43 -0400 Subject: [PATCH 43/69] Island: Register pypubsub Publisher in the DIContainer --- monkey/monkey_island/cc/services/initialize.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index eeb488b14..3b55a7cb9 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -3,7 +3,7 @@ import logging from pathlib import Path from typing import Sequence -from pubsub import pub +from pubsub.core import Publisher from pymongo import MongoClient from common import DIContainer @@ -74,7 +74,8 @@ def initialize_services(data_dir: Path) -> DIContainer: ILockableEncryptor, RepositoryEncryptor(data_dir / REPOSITORY_KEY_FILE_NAME) ) container.register_instance(Version, container.resolve(Version)) - container.register_instance(IEventQueue, PyPubSubEventQueue(pub)) + container.register(Publisher, Publisher) + container.register_instance(IEventQueue, container.resolve(PyPubSubEventQueue)) _register_repositories(container, data_dir) _register_services(container) From 49a28b91615d670b636f309b1174721ad01a4048 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 10 Aug 2022 17:23:07 +0530 Subject: [PATCH 44/69] UT: Add test for subscribing to all event types in PyPubSubEventQueue --- .../common/event_queue/test_pypubsub_event_queue.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 89cc3f08d..867673588 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -94,3 +94,12 @@ def test_subscribe_tags(subscriber_1, subscriber_2, subscriber_1_calls, subscrib assert subscriber_1_calls == [EVENT_TAG_1] assert subscriber_2_calls == [EVENT_TAG_2] + + +@pytest.mark.usefixtures("subscriber_1", "subscriber_1_calls") +def test_subscribe_all_event_types(subscriber_1, subscriber_1_calls): + pypubsub_event_queue.subscribe_all_event_types(subscriber_1) + pypubsub_event_queue.publish(EventType) + pypubsub_event_queue.publish(EventType) + + assert subscriber_1_calls == [INTERNAL_ALL_EVENT_TYPES_TOPIC, INTERNAL_ALL_EVENT_TYPES_TOPIC] From 7bbecc4d3fd9b266c7bc6fbd69bced97f1258d21 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 08:50:32 -0400 Subject: [PATCH 45/69] Common: Only send event to `subscribe_all()` subscribers once --- monkey/common/event_queue/pypubsub_event_queue.py | 8 +------- .../common/event_queue/test_pypubsub_event_queue.py | 12 +++++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index efac70881..053d7bdca 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -13,14 +13,8 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher = pypubsub_publisher def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): - # From the documentation (https://pypubsub.readthedocs.io/en/v4.0.3/usage/ - # usage_advanced_debug.html#listen-for-messages-from-all-topics): - # "PyPubSub defines a special topic named pub.ALL_TOPICS. A listener that subscribes - # to this topic will receives all messages of every topic. By default, the listener - # will not receive any data since pub.ALL_TOPICS is the parent of all root topics (...)" - self._pypubsub_publisher.subscribe( - listener=subscriber, topicName=self._pypubsub_publisher.ALL_TOPICS + listener=subscriber, topicName=INTERNAL_ALL_EVENT_TYPES_TOPIC ) def subscribe_type( diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 867673588..bedaf0c44 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -64,18 +64,16 @@ def test_topic_subscription(subscriber_1, subscriber_2, subscriber_1_calls, subs def test_subscribe_all(): subscriber_calls = [] - def subscriber(topic=pub.AUTO_TOPIC): + def subscriber(event, topic=pub.AUTO_TOPIC): subscriber_calls.append(topic.getName()) pypubsub_event_queue.subscribe_all(subscriber) pypubsub_event_queue.publish(EventType) - assert subscriber_calls == [ - EventType.__name__, - INTERNAL_ALL_EVENT_TYPES_TOPIC, - EVENT_TAG_1, - EVENT_TAG_2, - ] + assert len(subscriber_calls) == 1 + assert EventType.__name__ not in subscriber_calls + assert EVENT_TAG_1 not in subscriber_calls + assert EVENT_TAG_2 not in subscriber_calls @pytest.mark.usefixtures("subscriber_1", "subscriber_1_calls") From 1d7b4982901bdc2d517e434d8afdf7574ab9d03d Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 08:55:09 -0400 Subject: [PATCH 46/69] Common: Improve readability in PyPubSubEventQueue --- monkey/common/event_queue/pypubsub_event_queue.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 053d7bdca..17670e8f8 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -21,8 +21,7 @@ class PyPubSubEventQueue(IEventQueue): self, event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None] ): # pypubsub.pub.subscribe needs a string as the topic/event name - event_type_name = event_type.__name__ - self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_name) + self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type.__name__) def subscribe_all_event_types(self, subscriber: Callable[[AbstractEvent], None]): self._pypubsub_publisher.subscribe( @@ -33,15 +32,12 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) def publish(self, event: AbstractEvent): - event_data = {"event": event} - # publish to event type's topic - event_type_name = event.__name__ - self._pypubsub_publisher.sendMessage(event_type_name, **event_data) + self._pypubsub_publisher.sendMessage(event.__name__, event=event) # publish to all events' topic - self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, **event_data) + self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) # publish to tags' topics for tag in event.tags: - self._pypubsub_publisher.sendMessage(tag, **event_data) + self._pypubsub_publisher.sendMessage(tag, event=event) From 8553cd834c1558dd59087657134c7f99306e8818 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 08:59:20 -0400 Subject: [PATCH 47/69] Common: Improve readability of PyPubSubEventQueue.publish() --- monkey/common/event_queue/pypubsub_event_queue.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 17670e8f8..ff98a85f3 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -32,12 +32,16 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) def publish(self, event: AbstractEvent): - # publish to event type's topic - self._pypubsub_publisher.sendMessage(event.__name__, event=event) + self._publish_to_all_events_topic(event) + self._publish_to_type_topic(event) + self._publish_to_tags_topic(event) - # publish to all events' topic + def _publish_to_all_events_topic(self, event: AbstractEvent): self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) - # publish to tags' topics + def _publish_to_type_topic(self, event: AbstractEvent): + self._pypubsub_publisher.sendMessage(event.__name__, event=event) + + def _publish_to_tags_topic(self, event: AbstractEvent): for tag in event.tags: self._pypubsub_publisher.sendMessage(tag, event=event) From 87cbdd9fb8cfaf1317e06f1f0605f164e1709d44 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 09:01:54 -0400 Subject: [PATCH 48/69] Common: Rename subscribe_all() -> subscribe_all_events() --- monkey/common/event_queue/i_event_queue.py | 2 +- monkey/common/event_queue/pypubsub_event_queue.py | 5 +++-- .../common/event_queue/test_pypubsub_event_queue.py | 2 +- vulture_allowlist.py | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index ae9371ba4..1c7334c34 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -10,7 +10,7 @@ class IEventQueue(ABC): """ @abstractmethod - def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): + def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): """ Subscribes a subscriber to all events diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index ff98a85f3..7f56b1162 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,8 +1,9 @@ from typing import Callable -from common.events import AbstractEvent from pubsub.core import Publisher +from common.events import AbstractEvent + from .i_event_queue import IEventQueue INTERNAL_ALL_EVENT_TYPES_TOPIC = "internal_all_event_types" @@ -12,7 +13,7 @@ class PyPubSubEventQueue(IEventQueue): def __init__(self, pypubsub_publisher: Publisher): self._pypubsub_publisher = pypubsub_publisher - def subscribe_all(self, subscriber: Callable[[AbstractEvent], None]): + def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): self._pypubsub_publisher.subscribe( listener=subscriber, topicName=INTERNAL_ALL_EVENT_TYPES_TOPIC ) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index bedaf0c44..b018c5d89 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -67,7 +67,7 @@ def test_subscribe_all(): def subscriber(event, topic=pub.AUTO_TOPIC): subscriber_calls.append(topic.getName()) - pypubsub_event_queue.subscribe_all(subscriber) + pypubsub_event_queue.subscribe_all_events(subscriber) pypubsub_event_queue.publish(EventType) assert len(subscriber_calls) == 1 diff --git a/vulture_allowlist.py b/vulture_allowlist.py index a533be1a9..29359dfe9 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -270,4 +270,4 @@ subscribe_type # common\event_queue\pypubsub_event_queue.py subscribe_tag # common\event_queue\pypubsub_event_queue.py publish # common\event_queue\pypubsub_event_queue.py PyPubSubEventQueue # common\event_queue\pypubsub_event_queue.py -subscribe_all_event_types # common\event_queue\pypubsub_event_queue.py +subscribe_all_events # common\event_queue\pypubsub_event_queue.py From 43e45e14add31cb7dba0b35d9d0ad9d3cee8e145 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 09:14:19 -0400 Subject: [PATCH 49/69] Common: Remove subscribe_all_event_types() https://github.com/guardicore/monkey/pull/2182#discussion_r942427831 --- monkey/common/event_queue/pypubsub_event_queue.py | 5 ----- .../common/event_queue/test_pypubsub_event_queue.py | 9 --------- 2 files changed, 14 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 7f56b1162..253e00836 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -24,11 +24,6 @@ class PyPubSubEventQueue(IEventQueue): # pypubsub.pub.subscribe needs a string as the topic/event name self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type.__name__) - def subscribe_all_event_types(self, subscriber: Callable[[AbstractEvent], None]): - self._pypubsub_publisher.subscribe( - listener=subscriber, topicName=INTERNAL_ALL_EVENT_TYPES_TOPIC - ) - def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index b018c5d89..aac31c0ae 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -92,12 +92,3 @@ def test_subscribe_tags(subscriber_1, subscriber_2, subscriber_1_calls, subscrib assert subscriber_1_calls == [EVENT_TAG_1] assert subscriber_2_calls == [EVENT_TAG_2] - - -@pytest.mark.usefixtures("subscriber_1", "subscriber_1_calls") -def test_subscribe_all_event_types(subscriber_1, subscriber_1_calls): - pypubsub_event_queue.subscribe_all_event_types(subscriber_1) - pypubsub_event_queue.publish(EventType) - pypubsub_event_queue.publish(EventType) - - assert subscriber_1_calls == [INTERNAL_ALL_EVENT_TYPES_TOPIC, INTERNAL_ALL_EVENT_TYPES_TOPIC] From c26c7403a69dda7b55d9a81779164a60a9e5281a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 09:15:13 -0400 Subject: [PATCH 50/69] Common: Make INTERNAL_ALL_EVENT_TYPES_TOPIC private to the module --- monkey/common/event_queue/pypubsub_event_queue.py | 6 +++--- .../common/event_queue/test_pypubsub_event_queue.py | 5 +---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 253e00836..d2b1aa5f5 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -6,7 +6,7 @@ from common.events import AbstractEvent from .i_event_queue import IEventQueue -INTERNAL_ALL_EVENT_TYPES_TOPIC = "internal_all_event_types" +_INTERNAL_ALL_EVENT_TYPES_TOPIC = "internal_all_event_types" class PyPubSubEventQueue(IEventQueue): @@ -15,7 +15,7 @@ class PyPubSubEventQueue(IEventQueue): def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): self._pypubsub_publisher.subscribe( - listener=subscriber, topicName=INTERNAL_ALL_EVENT_TYPES_TOPIC + listener=subscriber, topicName=_INTERNAL_ALL_EVENT_TYPES_TOPIC ) def subscribe_type( @@ -33,7 +33,7 @@ class PyPubSubEventQueue(IEventQueue): self._publish_to_tags_topic(event) def _publish_to_all_events_topic(self, event: AbstractEvent): - self._pypubsub_publisher.sendMessage(INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) + self._pypubsub_publisher.sendMessage(_INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) def _publish_to_type_topic(self, event: AbstractEvent): self._pypubsub_publisher.sendMessage(event.__name__, event=event) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index aac31c0ae..188eb0566 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -3,10 +3,7 @@ from dataclasses import dataclass import pytest from pubsub import pub -from common.event_queue.pypubsub_event_queue import ( - INTERNAL_ALL_EVENT_TYPES_TOPIC, - PyPubSubEventQueue, -) +from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue from common.events import AbstractEvent EVENT_TAG_1 = "event tag 1" From c0a9a8cf4d37efcd9383f28ec063e6cff309eb03 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 09:18:25 -0400 Subject: [PATCH 51/69] Common: Rename _publish_to_tags_topic{,s}() --- monkey/common/event_queue/pypubsub_event_queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index d2b1aa5f5..5813cec3c 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -30,7 +30,7 @@ class PyPubSubEventQueue(IEventQueue): def publish(self, event: AbstractEvent): self._publish_to_all_events_topic(event) self._publish_to_type_topic(event) - self._publish_to_tags_topic(event) + self._publish_to_tags_topics(event) def _publish_to_all_events_topic(self, event: AbstractEvent): self._pypubsub_publisher.sendMessage(_INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) @@ -38,6 +38,6 @@ class PyPubSubEventQueue(IEventQueue): def _publish_to_type_topic(self, event: AbstractEvent): self._pypubsub_publisher.sendMessage(event.__name__, event=event) - def _publish_to_tags_topic(self, event: AbstractEvent): + def _publish_to_tags_topics(self, event: AbstractEvent): for tag in event.tags: self._pypubsub_publisher.sendMessage(tag, event=event) From a461226fec87786dfc21ac5b157eee840cb95b5e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 09:21:54 -0400 Subject: [PATCH 52/69] Common: Fix type hint for PyPubSubEventQueue.subscribe_type() --- monkey/common/event_queue/pypubsub_event_queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 5813cec3c..1e3fdd85e 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,4 +1,4 @@ -from typing import Callable +from typing import Callable, Type from pubsub.core import Publisher @@ -19,7 +19,7 @@ class PyPubSubEventQueue(IEventQueue): ) def subscribe_type( - self, event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None] + self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] ): # pypubsub.pub.subscribe needs a string as the topic/event name self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type.__name__) From 4da0cefa7dc888326a46cf1b0204c0902fd91ec5 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 09:34:09 -0400 Subject: [PATCH 53/69] Common: Publish message to event's class name --- monkey/common/event_queue/pypubsub_event_queue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 1e3fdd85e..dc4a3991d 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -36,7 +36,7 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher.sendMessage(_INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) def _publish_to_type_topic(self, event: AbstractEvent): - self._pypubsub_publisher.sendMessage(event.__name__, event=event) + self._pypubsub_publisher.sendMessage(event.__class__.__name__, event=event) def _publish_to_tags_topics(self, event: AbstractEvent): for tag in event.tags: From d6bb56536d501c1535868428da99518732fb768e Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 09:35:57 -0400 Subject: [PATCH 54/69] UT: Test with instances of AbstractEvent, not classes --- .../event_queue/test_pypubsub_event_queue.py | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 188eb0566..acc3eb8a8 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -1,4 +1,7 @@ from dataclasses import dataclass +from ipaddress import IPv4Address +from typing import FrozenSet, Union +from uuid import UUID import pytest from pubsub import pub @@ -11,11 +14,11 @@ EVENT_TAG_2 = "event tag 2" @dataclass(frozen=True) -class EventType(AbstractEvent): - source = "1234" - target = None - timestamp = 0.0 - tags = [EVENT_TAG_1, EVENT_TAG_2] +class TestEvent(AbstractEvent): + source: UUID = "f811ad00-5a68-4437-bd51-7b5cc1768ad5" + target: Union[UUID, IPv4Address, None] = None + timestamp: float = 0.0 + tags: FrozenSet = frozenset() pypubsub_event_queue = PyPubSubEventQueue(pub) @@ -49,12 +52,12 @@ def subscriber_2(subscriber_2_calls): @pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") def test_topic_subscription(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): - pypubsub_event_queue.subscribe_type(EventType, subscriber_1) + pypubsub_event_queue.subscribe_type(TestEvent, subscriber_1) pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_1) pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_2) - pypubsub_event_queue.publish(EventType) + pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) - assert subscriber_1_calls == [EventType.__name__, EVENT_TAG_2] + assert subscriber_1_calls == [TestEvent.__name__, EVENT_TAG_2] assert subscriber_2_calls == [EVENT_TAG_1] @@ -65,27 +68,27 @@ def test_subscribe_all(): subscriber_calls.append(topic.getName()) pypubsub_event_queue.subscribe_all_events(subscriber) - pypubsub_event_queue.publish(EventType) + pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) assert len(subscriber_calls) == 1 - assert EventType.__name__ not in subscriber_calls + assert TestEvent.__name__ not in subscriber_calls assert EVENT_TAG_1 not in subscriber_calls assert EVENT_TAG_2 not in subscriber_calls @pytest.mark.usefixtures("subscriber_1", "subscriber_1_calls") def test_subscribe_types(subscriber_1, subscriber_1_calls): - pypubsub_event_queue.subscribe_type(EventType, subscriber_1) - pypubsub_event_queue.publish(EventType) + pypubsub_event_queue.subscribe_type(TestEvent, subscriber_1) + pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) - assert subscriber_1_calls == [EventType.__name__] + assert subscriber_1_calls == [TestEvent.__name__] @pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") def test_subscribe_tags(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_1) pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_2) - pypubsub_event_queue.publish(EventType) + pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) assert subscriber_1_calls == [EVENT_TAG_1] assert subscriber_2_calls == [EVENT_TAG_2] From 2bdc16c286af66392c4a8b037c35f368a1d91726 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 11:07:36 -0400 Subject: [PATCH 55/69] UT: Refactor test_pypubsub_event_queue.py --- .../event_queue/test_pypubsub_event_queue.py | 111 +++++++++--------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index acc3eb8a8..77b33592d 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -14,81 +14,86 @@ EVENT_TAG_2 = "event tag 2" @dataclass(frozen=True) -class TestEvent(AbstractEvent): +class TestEvent1(AbstractEvent): source: UUID = "f811ad00-5a68-4437-bd51-7b5cc1768ad5" target: Union[UUID, IPv4Address, None] = None timestamp: float = 0.0 tags: FrozenSet = frozenset() +@dataclass(frozen=True) +class TestEvent2(AbstractEvent): + source: UUID = "e810ad01-6b67-9446-fc58-9b8d717653f7" + target: Union[UUID, IPv4Address, None] = None + timestamp: float = 0.0 + tags: FrozenSet = frozenset() + + pypubsub_event_queue = PyPubSubEventQueue(pub) -@pytest.fixture() -def subscriber_1_calls(): - return [] +def new_subscriber(): + def fn(event): + fn.call_count += 1 + fn.call_types.add(event.__class__) + fn.call_tags |= event.tags - -@pytest.fixture() -def subscriber_2_calls(): - return [] - - -@pytest.fixture -def subscriber_1(subscriber_1_calls): - def fn(event, topic=pub.AUTO_TOPIC): - subscriber_1_calls.append(topic.getName()) + fn.call_count = 0 + fn.call_types = set() + fn.call_tags = set() return fn @pytest.fixture -def subscriber_2(subscriber_2_calls): - def fn(event, topic=pub.AUTO_TOPIC): - subscriber_2_calls.append(topic.getName()) - - return fn +def subscriber(): + return new_subscriber() -@pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") -def test_topic_subscription(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): - pypubsub_event_queue.subscribe_type(TestEvent, subscriber_1) - pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_1) - pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_2) - pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) - - assert subscriber_1_calls == [TestEvent.__name__, EVENT_TAG_2] - assert subscriber_2_calls == [EVENT_TAG_1] - - -def test_subscribe_all(): - subscriber_calls = [] - - def subscriber(event, topic=pub.AUTO_TOPIC): - subscriber_calls.append(topic.getName()) - +def test_subscribe_all(subscriber): pypubsub_event_queue.subscribe_all_events(subscriber) - pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) - assert len(subscriber_calls) == 1 - assert TestEvent.__name__ not in subscriber_calls - assert EVENT_TAG_1 not in subscriber_calls - assert EVENT_TAG_2 not in subscriber_calls + pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) + pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_2})) + pypubsub_event_queue.publish(TestEvent1(tags={"secret_tag"})) + pypubsub_event_queue.publish(TestEvent2()) + + assert subscriber.call_count == 4 + assert TestEvent1 in subscriber.call_types + assert TestEvent2 in subscriber.call_types -@pytest.mark.usefixtures("subscriber_1", "subscriber_1_calls") -def test_subscribe_types(subscriber_1, subscriber_1_calls): - pypubsub_event_queue.subscribe_type(TestEvent, subscriber_1) - pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) +@pytest.mark.parametrize("type_to_subscribe", [TestEvent1, TestEvent2]) +def test_subscribe_types(subscriber, type_to_subscribe): + pypubsub_event_queue.subscribe_type(type_to_subscribe, subscriber) - assert subscriber_1_calls == [TestEvent.__name__] + pypubsub_event_queue.publish(TestEvent1()) + pypubsub_event_queue.publish(TestEvent2()) + + assert subscriber.call_count == 1 + assert type_to_subscribe in subscriber.call_types -@pytest.mark.usefixtures("subscriber_1", "subscriber_2", "subscriber_1_calls", "subscriber_2_calls") -def test_subscribe_tags(subscriber_1, subscriber_2, subscriber_1_calls, subscriber_2_calls): - pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber_1) - pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber_2) - pypubsub_event_queue.publish(TestEvent(tags={EVENT_TAG_1, EVENT_TAG_2})) +def test_subscribe_tags_single_type(subscriber): + pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber) - assert subscriber_1_calls == [EVENT_TAG_1] - assert subscriber_2_calls == [EVENT_TAG_2] + pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) + pypubsub_event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) + + assert subscriber.call_count == 1 + assert len(subscriber.call_types) == 1 + assert TestEvent1 in subscriber.call_types + assert EVENT_TAG_1 in subscriber.call_tags + + +def test_subscribe_tags_multiple_types(subscriber): + pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber) + + pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) + pypubsub_event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) + + assert subscriber.call_count == 2 + assert len(subscriber.call_types) == 2 + assert TestEvent1 in subscriber.call_types + assert TestEvent2 in subscriber.call_types + assert {EVENT_TAG_1, EVENT_TAG_2}.issubset(subscriber.call_tags) From 68dafbfb9d3bf615593494f176bf3fbd97f2e5ff Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 11:09:51 -0400 Subject: [PATCH 56/69] UT: Add event_queue() fixture --- .../event_queue/test_pypubsub_event_queue.py | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 77b33592d..f47a80fe2 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -29,9 +29,6 @@ class TestEvent2(AbstractEvent): tags: FrozenSet = frozenset() -pypubsub_event_queue = PyPubSubEventQueue(pub) - - def new_subscriber(): def fn(event): fn.call_count += 1 @@ -50,13 +47,18 @@ def subscriber(): return new_subscriber() -def test_subscribe_all(subscriber): - pypubsub_event_queue.subscribe_all_events(subscriber) +@pytest.fixture +def event_queue(): + return PyPubSubEventQueue(pub) - pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) - pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_2})) - pypubsub_event_queue.publish(TestEvent1(tags={"secret_tag"})) - pypubsub_event_queue.publish(TestEvent2()) + +def test_subscribe_all(event_queue, subscriber): + event_queue.subscribe_all_events(subscriber) + + event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) + event_queue.publish(TestEvent1(tags={EVENT_TAG_2})) + event_queue.publish(TestEvent1(tags={"secret_tag"})) + event_queue.publish(TestEvent2()) assert subscriber.call_count == 4 assert TestEvent1 in subscriber.call_types @@ -64,21 +66,21 @@ def test_subscribe_all(subscriber): @pytest.mark.parametrize("type_to_subscribe", [TestEvent1, TestEvent2]) -def test_subscribe_types(subscriber, type_to_subscribe): - pypubsub_event_queue.subscribe_type(type_to_subscribe, subscriber) +def test_subscribe_types(event_queue, subscriber, type_to_subscribe): + event_queue.subscribe_type(type_to_subscribe, subscriber) - pypubsub_event_queue.publish(TestEvent1()) - pypubsub_event_queue.publish(TestEvent2()) + event_queue.publish(TestEvent1()) + event_queue.publish(TestEvent2()) assert subscriber.call_count == 1 assert type_to_subscribe in subscriber.call_types -def test_subscribe_tags_single_type(subscriber): - pypubsub_event_queue.subscribe_tag(EVENT_TAG_1, subscriber) +def test_subscribe_tags_single_type(event_queue, subscriber): + event_queue.subscribe_tag(EVENT_TAG_1, subscriber) - pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) - pypubsub_event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) + event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) + event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) assert subscriber.call_count == 1 assert len(subscriber.call_types) == 1 @@ -86,11 +88,11 @@ def test_subscribe_tags_single_type(subscriber): assert EVENT_TAG_1 in subscriber.call_tags -def test_subscribe_tags_multiple_types(subscriber): - pypubsub_event_queue.subscribe_tag(EVENT_TAG_2, subscriber) +def test_subscribe_tags_multiple_types(event_queue, subscriber): + event_queue.subscribe_tag(EVENT_TAG_2, subscriber) - pypubsub_event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) - pypubsub_event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) + event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) + event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) assert subscriber.call_count == 2 assert len(subscriber.call_types) == 2 From 3384c049a4bba6b40134c77fceb80ef3f5210d2d Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 11:10:22 -0400 Subject: [PATCH 57/69] UT: Use new Publisher for each test instead of default `pub` Using the default publisher provided by pypubsub causes state to be carried over between tests. Unit tests should not depend on eachother or the order in which they're run. --- .../common/event_queue/test_pypubsub_event_queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index f47a80fe2..1bd919133 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -4,7 +4,7 @@ from typing import FrozenSet, Union from uuid import UUID import pytest -from pubsub import pub +from pubsub.core import Publisher from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue from common.events import AbstractEvent @@ -49,7 +49,7 @@ def subscriber(): @pytest.fixture def event_queue(): - return PyPubSubEventQueue(pub) + return PyPubSubEventQueue(Publisher()) def test_subscribe_all(event_queue, subscriber): From a6c24af622dd8eb9449e510a1317e8a39b46bb1d Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 11:28:56 -0400 Subject: [PATCH 58/69] Common: Prevent topic collision between types and tags If an event tag happened to be the same as an event type, subscribers could receive event types that they were not prepared to handle, resulting in difficult-to-diagnose bugs. Prevent tags and types from being sent to the wrong subscribers by appending unique strings to the topics. --- .../event_queue/pypubsub_event_queue.py | 22 +++++++++++++++---- .../event_queue/test_pypubsub_event_queue.py | 8 +++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index dc4a3991d..87813311f 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -22,10 +22,12 @@ class PyPubSubEventQueue(IEventQueue): self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] ): # pypubsub.pub.subscribe needs a string as the topic/event name - self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type.__name__) + event_type_topic = PyPubSubEventQueue._get_type_topic(event_type) + self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_topic) def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): - self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag) + tag_topic = PyPubSubEventQueue._get_tag_topic(tag) + self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag_topic) def publish(self, event: AbstractEvent): self._publish_to_all_events_topic(event) @@ -36,8 +38,20 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher.sendMessage(_INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) def _publish_to_type_topic(self, event: AbstractEvent): - self._pypubsub_publisher.sendMessage(event.__class__.__name__, event=event) + event_type_topic = PyPubSubEventQueue._get_type_topic(event.__class__) + self._pypubsub_publisher.sendMessage(event_type_topic, event=event) def _publish_to_tags_topics(self, event: AbstractEvent): for tag in event.tags: - self._pypubsub_publisher.sendMessage(tag, event=event) + tag_topic = PyPubSubEventQueue._get_tag_topic(tag) + self._pypubsub_publisher.sendMessage(tag_topic, event=event) + + # Appending a unique string to the topics for type and tags prevents bugs caused by collisions + # between type names and tag names. + @staticmethod + def _get_type_topic(event_type: Type[AbstractEvent]) -> str: + return f"{event_type.__name__}-type" + + @staticmethod + def _get_tag_topic(tag: str) -> str: + return f"{tag}-tag" diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 1bd919133..449a7776a 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -99,3 +99,11 @@ def test_subscribe_tags_multiple_types(event_queue, subscriber): assert TestEvent1 in subscriber.call_types assert TestEvent2 in subscriber.call_types assert {EVENT_TAG_1, EVENT_TAG_2}.issubset(subscriber.call_tags) + + +def test_type_tag_collision(event_queue, subscriber): + event_queue.subscribe_type(TestEvent1, subscriber) + + event_queue.publish(TestEvent2(tags={TestEvent1.__name__})) + + assert subscriber.call_count == 0 From d151ce12fd6aceb872357a1b4e33463c88f22499 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 11:33:57 -0400 Subject: [PATCH 59/69] Common: Rename _INTERNAL_ALL_EVENT_TYPES_TOPIC -> _ALL_EVENTS_TOPIC --- monkey/common/event_queue/pypubsub_event_queue.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 87813311f..bdf292fdd 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -6,7 +6,7 @@ from common.events import AbstractEvent from .i_event_queue import IEventQueue -_INTERNAL_ALL_EVENT_TYPES_TOPIC = "internal_all_event_types" +_ALL_EVENTS_TOPIC = "all_events_topic" class PyPubSubEventQueue(IEventQueue): @@ -14,9 +14,7 @@ class PyPubSubEventQueue(IEventQueue): self._pypubsub_publisher = pypubsub_publisher def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): - self._pypubsub_publisher.subscribe( - listener=subscriber, topicName=_INTERNAL_ALL_EVENT_TYPES_TOPIC - ) + self._pypubsub_publisher.subscribe(listener=subscriber, topicName=_ALL_EVENTS_TOPIC) def subscribe_type( self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] @@ -35,7 +33,7 @@ class PyPubSubEventQueue(IEventQueue): self._publish_to_tags_topics(event) def _publish_to_all_events_topic(self, event: AbstractEvent): - self._pypubsub_publisher.sendMessage(_INTERNAL_ALL_EVENT_TYPES_TOPIC, event=event) + self._pypubsub_publisher.sendMessage(_ALL_EVENTS_TOPIC, event=event) def _publish_to_type_topic(self, event: AbstractEvent): event_type_topic = PyPubSubEventQueue._get_type_topic(event.__class__) From 83e09ca9576eb72661c96d4147b3289604967bb3 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 11:35:55 -0400 Subject: [PATCH 60/69] Common: Fix type hint for IEventQueue.subscribe_type() --- monkey/common/event_queue/i_event_queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 1c7334c34..17fdb6356 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import Callable +from typing import Callable, Type from common.events import AbstractEvent @@ -21,7 +21,7 @@ class IEventQueue(ABC): @abstractmethod def subscribe_type( - self, event_type: AbstractEvent, subscriber: Callable[[AbstractEvent], None] + self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] ): """ Subscribes a subscriber to the specifed event type From 97a612be2de2923151d843f2e63c1b6e3fe721ed Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:34:06 -0400 Subject: [PATCH 61/69] Common: Add EventSubscriber type --- monkey/common/event_queue/__init__.py | 1 + monkey/common/event_queue/types.py | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 monkey/common/event_queue/types.py diff --git a/monkey/common/event_queue/__init__.py b/monkey/common/event_queue/__init__.py index e69de29bb..de8c0494b 100644 --- a/monkey/common/event_queue/__init__.py +++ b/monkey/common/event_queue/__init__.py @@ -0,0 +1 @@ +from .types import EventSubscriber diff --git a/monkey/common/event_queue/types.py b/monkey/common/event_queue/types.py new file mode 100644 index 000000000..f78651306 --- /dev/null +++ b/monkey/common/event_queue/types.py @@ -0,0 +1,5 @@ +from typing import Callable + +from common.events import AbstractEvent + +EventSubscriber = Callable[[AbstractEvent], None] From 35155c345fd17903cce2ae8f91fb9bf8ed9bfeb3 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:36:59 -0400 Subject: [PATCH 62/69] Common: Use EventSubscriber for IEventQueue type hints --- monkey/common/event_queue/i_event_queue.py | 18 +++++++++--------- .../common/event_queue/pypubsub_event_queue.py | 11 +++++------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 17fdb6356..f13b0d4b8 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -1,8 +1,10 @@ from abc import ABC, abstractmethod -from typing import Callable, Type +from typing import Type from common.events import AbstractEvent +from . import EventSubscriber + class IEventQueue(ABC): """ @@ -10,35 +12,33 @@ class IEventQueue(ABC): """ @abstractmethod - def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): + def subscribe_all_events(self, subscriber: EventSubscriber): """ Subscribes a subscriber to all events - :param subscriber: Callable that should subscribe to events + :param subscriber: A subscriber that well receive events """ pass @abstractmethod - def subscribe_type( - self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] - ): + def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber): """ Subscribes a subscriber to the specifed event type :param event_type: Event type to which the subscriber should subscribe - :param subscriber: Callable that should subscribe to events + :param subscriber: A subscriber that well receive events """ pass @abstractmethod - def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): + def subscribe_tag(self, tag: str, subscriber: EventSubscriber): """ Subscribes a subscriber to the specified event tag :param tag: Event tag to which the subscriber should subscribe - :param subscriber: Callable that should subscribe to events + :param subscriber: A subscriber that well receive events """ pass diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index bdf292fdd..6398db4ff 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -1,9 +1,10 @@ -from typing import Callable, Type +from typing import Type from pubsub.core import Publisher from common.events import AbstractEvent +from . import EventSubscriber from .i_event_queue import IEventQueue _ALL_EVENTS_TOPIC = "all_events_topic" @@ -13,17 +14,15 @@ class PyPubSubEventQueue(IEventQueue): def __init__(self, pypubsub_publisher: Publisher): self._pypubsub_publisher = pypubsub_publisher - def subscribe_all_events(self, subscriber: Callable[[AbstractEvent], None]): + def subscribe_all_events(self, subscriber: EventSubscriber): self._pypubsub_publisher.subscribe(listener=subscriber, topicName=_ALL_EVENTS_TOPIC) - def subscribe_type( - self, event_type: Type[AbstractEvent], subscriber: Callable[[AbstractEvent], None] - ): + def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber): # pypubsub.pub.subscribe needs a string as the topic/event name event_type_topic = PyPubSubEventQueue._get_type_topic(event_type) self._pypubsub_publisher.subscribe(listener=subscriber, topicName=event_type_topic) - def subscribe_tag(self, tag: str, subscriber: Callable[[AbstractEvent], None]): + def subscribe_tag(self, tag: str, subscriber: EventSubscriber): tag_topic = PyPubSubEventQueue._get_tag_topic(tag) self._pypubsub_publisher.subscribe(listener=subscriber, topicName=tag_topic) From e36fb9af73392c9acc010e18f0aa63b9c7386e61 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:40:51 -0400 Subject: [PATCH 63/69] UT: Add type hints to test_pypubsub_event_queue.py --- .../event_queue/test_pypubsub_event_queue.py | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 449a7776a..49dedecd3 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -6,6 +6,8 @@ from uuid import UUID import pytest from pubsub.core import Publisher +from common.event_queue import EventSubscriber +from common.event_queue.i_event_queue import IEventQueue from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue from common.events import AbstractEvent @@ -29,8 +31,8 @@ class TestEvent2(AbstractEvent): tags: FrozenSet = frozenset() -def new_subscriber(): - def fn(event): +def new_subscriber() -> EventSubscriber: + def fn(event: AbstractEvent): fn.call_count += 1 fn.call_types.add(event.__class__) fn.call_tags |= event.tags @@ -43,16 +45,16 @@ def new_subscriber(): @pytest.fixture -def subscriber(): +def subscriber() -> EventSubscriber: return new_subscriber() @pytest.fixture -def event_queue(): +def event_queue() -> IEventQueue: return PyPubSubEventQueue(Publisher()) -def test_subscribe_all(event_queue, subscriber): +def test_subscribe_all(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_all_events(subscriber) event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) @@ -66,7 +68,7 @@ def test_subscribe_all(event_queue, subscriber): @pytest.mark.parametrize("type_to_subscribe", [TestEvent1, TestEvent2]) -def test_subscribe_types(event_queue, subscriber, type_to_subscribe): +def test_subscribe_types(event_queue: IEventQueue, subscriber: EventSubscriber, type_to_subscribe): event_queue.subscribe_type(type_to_subscribe, subscriber) event_queue.publish(TestEvent1()) @@ -76,7 +78,7 @@ def test_subscribe_types(event_queue, subscriber, type_to_subscribe): assert type_to_subscribe in subscriber.call_types -def test_subscribe_tags_single_type(event_queue, subscriber): +def test_subscribe_tags_single_type(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_tag(EVENT_TAG_1, subscriber) event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) @@ -88,7 +90,7 @@ def test_subscribe_tags_single_type(event_queue, subscriber): assert EVENT_TAG_1 in subscriber.call_tags -def test_subscribe_tags_multiple_types(event_queue, subscriber): +def test_subscribe_tags_multiple_types(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_tag(EVENT_TAG_2, subscriber) event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) @@ -101,7 +103,7 @@ def test_subscribe_tags_multiple_types(event_queue, subscriber): assert {EVENT_TAG_1, EVENT_TAG_2}.issubset(subscriber.call_tags) -def test_type_tag_collision(event_queue, subscriber): +def test_type_tag_collision(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_type(TestEvent1, subscriber) event_queue.publish(TestEvent2(tags={TestEvent1.__name__})) From ac69e7d25be27dc5798745a5213b7ba5938d750d Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:42:37 -0400 Subject: [PATCH 64/69] UT: Use frozenset() for tags in test_pypubsub_event_queue.py --- .../event_queue/test_pypubsub_event_queue.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 49dedecd3..972507b89 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -57,9 +57,9 @@ def event_queue() -> IEventQueue: def test_subscribe_all(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_all_events(subscriber) - event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) - event_queue.publish(TestEvent1(tags={EVENT_TAG_2})) - event_queue.publish(TestEvent1(tags={"secret_tag"})) + event_queue.publish(TestEvent1(tags=frozenset({EVENT_TAG_1, EVENT_TAG_2}))) + event_queue.publish(TestEvent1(tags=frozenset({EVENT_TAG_2}))) + event_queue.publish(TestEvent1(tags=frozenset({"secret_tag"}))) event_queue.publish(TestEvent2()) assert subscriber.call_count == 4 @@ -81,8 +81,8 @@ def test_subscribe_types(event_queue: IEventQueue, subscriber: EventSubscriber, def test_subscribe_tags_single_type(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_tag(EVENT_TAG_1, subscriber) - event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) - event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) + event_queue.publish(TestEvent1(tags=frozenset({EVENT_TAG_1, EVENT_TAG_2}))) + event_queue.publish(TestEvent2(tags=frozenset({EVENT_TAG_2}))) assert subscriber.call_count == 1 assert len(subscriber.call_types) == 1 @@ -93,8 +93,8 @@ def test_subscribe_tags_single_type(event_queue: IEventQueue, subscriber: EventS def test_subscribe_tags_multiple_types(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_tag(EVENT_TAG_2, subscriber) - event_queue.publish(TestEvent1(tags={EVENT_TAG_1, EVENT_TAG_2})) - event_queue.publish(TestEvent2(tags={EVENT_TAG_2})) + event_queue.publish(TestEvent1(tags=frozenset({EVENT_TAG_1, EVENT_TAG_2}))) + event_queue.publish(TestEvent2(tags=frozenset({EVENT_TAG_2}))) assert subscriber.call_count == 2 assert len(subscriber.call_types) == 2 @@ -106,6 +106,6 @@ def test_subscribe_tags_multiple_types(event_queue: IEventQueue, subscriber: Eve def test_type_tag_collision(event_queue: IEventQueue, subscriber: EventSubscriber): event_queue.subscribe_type(TestEvent1, subscriber) - event_queue.publish(TestEvent2(tags={TestEvent1.__name__})) + event_queue.publish(TestEvent2(tags=frozenset({TestEvent1.__name__}))) assert subscriber.call_count == 0 From 2bbe56035d5a7161e8a2f406af205cd391196576 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:43:20 -0400 Subject: [PATCH 65/69] UT: Use UUID for source in test_pypubsub_event_queue.py --- .../common/event_queue/test_pypubsub_event_queue.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index 972507b89..f641b36e5 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -17,7 +17,7 @@ EVENT_TAG_2 = "event tag 2" @dataclass(frozen=True) class TestEvent1(AbstractEvent): - source: UUID = "f811ad00-5a68-4437-bd51-7b5cc1768ad5" + source: UUID = UUID("f811ad00-5a68-4437-bd51-7b5cc1768ad5") target: Union[UUID, IPv4Address, None] = None timestamp: float = 0.0 tags: FrozenSet = frozenset() @@ -25,7 +25,7 @@ class TestEvent1(AbstractEvent): @dataclass(frozen=True) class TestEvent2(AbstractEvent): - source: UUID = "e810ad01-6b67-9446-fc58-9b8d717653f7" + source: UUID = UUID("e810ad01-6b67-9446-fc58-9b8d717653f7") target: Union[UUID, IPv4Address, None] = None timestamp: float = 0.0 tags: FrozenSet = frozenset() From 4243f9f3ea5ddcccae43cb583cd6c9d786a15ed9 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:45:37 -0400 Subject: [PATCH 66/69] Common: Export IEventQueue from common.event_queue --- monkey/common/event_queue/__init__.py | 1 + monkey/common/event_queue/pypubsub_event_queue.py | 3 +-- monkey/monkey_island/cc/services/initialize.py | 2 +- .../unit_tests/common/event_queue/test_pypubsub_event_queue.py | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/monkey/common/event_queue/__init__.py b/monkey/common/event_queue/__init__.py index de8c0494b..5d04040e2 100644 --- a/monkey/common/event_queue/__init__.py +++ b/monkey/common/event_queue/__init__.py @@ -1 +1,2 @@ from .types import EventSubscriber +from .i_event_queue import IEventQueue diff --git a/monkey/common/event_queue/pypubsub_event_queue.py b/monkey/common/event_queue/pypubsub_event_queue.py index 6398db4ff..bf4b789d4 100644 --- a/monkey/common/event_queue/pypubsub_event_queue.py +++ b/monkey/common/event_queue/pypubsub_event_queue.py @@ -4,8 +4,7 @@ from pubsub.core import Publisher from common.events import AbstractEvent -from . import EventSubscriber -from .i_event_queue import IEventQueue +from . import EventSubscriber, IEventQueue _ALL_EVENTS_TOPIC = "all_events_topic" diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 3b55a7cb9..6247ea64d 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -14,7 +14,7 @@ from common.agent_configuration import ( ) from common.aws import AWSInstance from common.common_consts.telem_categories import TelemCategoryEnum -from common.event_queue.i_event_queue import IEventQueue +from common.event_queue import IEventQueue from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue from common.utils.file_utils import get_binary_io_sha256_hash from common.version import get_version diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index f641b36e5..b6edc4e46 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -6,8 +6,7 @@ from uuid import UUID import pytest from pubsub.core import Publisher -from common.event_queue import EventSubscriber -from common.event_queue.i_event_queue import IEventQueue +from common.event_queue import EventSubscriber, IEventQueue from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue from common.events import AbstractEvent From 8a02305277037eec0b40132e837103373fd3dfb3 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:46:58 -0400 Subject: [PATCH 67/69] Common: Export PyPubSubEventQueue from common.event_queue --- monkey/common/event_queue/__init__.py | 1 + monkey/monkey_island/cc/services/initialize.py | 3 +-- .../unit_tests/common/event_queue/test_pypubsub_event_queue.py | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/monkey/common/event_queue/__init__.py b/monkey/common/event_queue/__init__.py index 5d04040e2..f88abe300 100644 --- a/monkey/common/event_queue/__init__.py +++ b/monkey/common/event_queue/__init__.py @@ -1,2 +1,3 @@ from .types import EventSubscriber from .i_event_queue import IEventQueue +from .pypubsub_event_queue import PyPubSubEventQueue diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 6247ea64d..fc7b6e432 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -14,8 +14,7 @@ from common.agent_configuration import ( ) from common.aws import AWSInstance from common.common_consts.telem_categories import TelemCategoryEnum -from common.event_queue import IEventQueue -from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue +from common.event_queue import IEventQueue, PyPubSubEventQueue from common.utils.file_utils import get_binary_io_sha256_hash from common.version import get_version from monkey_island.cc import Version diff --git a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py index b6edc4e46..deaa836f4 100644 --- a/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py +++ b/monkey/tests/unit_tests/common/event_queue/test_pypubsub_event_queue.py @@ -6,8 +6,7 @@ from uuid import UUID import pytest from pubsub.core import Publisher -from common.event_queue import EventSubscriber, IEventQueue -from common.event_queue.pypubsub_event_queue import PyPubSubEventQueue +from common.event_queue import EventSubscriber, IEventQueue, PyPubSubEventQueue from common.events import AbstractEvent EVENT_TAG_1 = "event tag 1" From b1c77b7be8538117fb3b1f3496d132f03c6a8497 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:37:25 -0400 Subject: [PATCH 68/69] Common: Fix typo well -> will --- monkey/common/event_queue/i_event_queue.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index f13b0d4b8..6abb273ad 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -16,7 +16,7 @@ class IEventQueue(ABC): """ Subscribes a subscriber to all events - :param subscriber: A subscriber that well receive events + :param subscriber: A subscriber that will receive events """ pass @@ -27,7 +27,7 @@ class IEventQueue(ABC): Subscribes a subscriber to the specifed event type :param event_type: Event type to which the subscriber should subscribe - :param subscriber: A subscriber that well receive events + :param subscriber: A subscriber that will receive events """ pass @@ -38,7 +38,7 @@ class IEventQueue(ABC): Subscribes a subscriber to the specified event tag :param tag: Event tag to which the subscriber should subscribe - :param subscriber: A subscriber that well receive events + :param subscriber: A subscriber that will receive events """ pass From dcc2717ac8dcf3fc0e12b988e72b0dd89a8226d8 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 12:37:46 -0400 Subject: [PATCH 69/69] Common: Fix typo specifed -> specified --- monkey/common/event_queue/i_event_queue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/common/event_queue/i_event_queue.py b/monkey/common/event_queue/i_event_queue.py index 6abb273ad..b205b8a98 100644 --- a/monkey/common/event_queue/i_event_queue.py +++ b/monkey/common/event_queue/i_event_queue.py @@ -24,7 +24,7 @@ class IEventQueue(ABC): @abstractmethod def subscribe_type(self, event_type: Type[AbstractEvent], subscriber: EventSubscriber): """ - Subscribes a subscriber to the specifed event type + Subscribes a subscriber to the specified event type :param event_type: Event type to which the subscriber should subscribe :param subscriber: A subscriber that will receive events