insightconnect-plugin-runtime 6.3.10__tar.gz → 6.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. {insightconnect_plugin_runtime-6.3.10/insightconnect_plugin_runtime.egg-info → insightconnect_plugin_runtime-6.4.0}/PKG-INFO +5 -4
  2. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/README.md +4 -3
  3. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/connection.py +15 -16
  4. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0/insightconnect_plugin_runtime.egg-info}/PKG-INFO +5 -4
  5. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime.egg-info/SOURCES.txt +1 -0
  6. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/setup.py +1 -1
  7. insightconnect_plugin_runtime-6.4.0/tests/unit/test_connection.py +113 -0
  8. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/MANIFEST.in +0 -0
  9. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect-plugin-swagger.json +0 -0
  10. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/__init__.py +0 -0
  11. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/action.py +0 -0
  12. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/api/__init__.py +0 -0
  13. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/api/endpoints.py +0 -0
  14. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/api/schemas.py +0 -0
  15. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/cli.py +0 -0
  16. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/clients/__init__.py +0 -0
  17. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/clients/aws_client.py +0 -0
  18. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/clients/oauth.py +0 -0
  19. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/data/input_message_schema.json +0 -0
  20. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/data/output_message_schema.json +0 -0
  21. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/dispatcher.py +0 -0
  22. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/exceptions.py +0 -0
  23. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/helper.py +0 -0
  24. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/metrics.py +0 -0
  25. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/plugin.py +0 -0
  26. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/schema.py +0 -0
  27. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/server.py +0 -0
  28. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/step.py +0 -0
  29. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/task.py +0 -0
  30. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/telemetry.py +0 -0
  31. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/trigger.py +0 -0
  32. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/util.py +0 -0
  33. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime/variables.py +0 -0
  34. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime.egg-info/dependency_links.txt +0 -0
  35. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime.egg-info/requires.txt +0 -0
  36. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/insightconnect_plugin_runtime.egg-info/top_level.txt +0 -0
  37. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/setup.cfg +0 -0
  38. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/__init__.py +0 -0
  39. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/__init__.py +0 -0
  40. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/__init__.py +0 -0
  41. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/__init__.py +0 -0
  42. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/__init__.py +0 -0
  43. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/__init__.py +0 -0
  44. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/hello/__init__.py +0 -0
  45. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/hello/action.py +0 -0
  46. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/hello/schema.py +0 -0
  47. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/return_bad_json/__init__.py +0 -0
  48. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/return_bad_json/action.py +0 -0
  49. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/return_bad_json/schema.py +0 -0
  50. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/throw_exception/__init__.py +0 -0
  51. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/throw_exception/action.py +0 -0
  52. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/actions/throw_exception/schema.py +0 -0
  53. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/connection/__init__.py +0 -0
  54. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/connection/connection.py +0 -0
  55. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/connection/schema.py +0 -0
  56. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/tasks/__init__.py +0 -0
  57. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/tasks/monitor_events/__init__.py +0 -0
  58. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/tasks/monitor_events/schema.py +0 -0
  59. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/tasks/monitor_events/task.py +0 -0
  60. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/__init__.py +0 -0
  61. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/hello_trigger/__init__.py +0 -0
  62. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/hello_trigger/schema.py +0 -0
  63. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/hello_trigger/trigger.py +0 -0
  64. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/return_bad_json_trigger/__init__.py +0 -0
  65. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/return_bad_json_trigger/schema.py +0 -0
  66. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/return_bad_json_trigger/trigger.py +0 -0
  67. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/throw_exception_trigger/__init__.py +0 -0
  68. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/throw_exception_trigger/schema.py +0 -0
  69. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/triggers/throw_exception_trigger/trigger.py +0 -0
  70. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/komand_hello_world/util/__init__.py +0 -0
  71. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/hello_world/setup.py +0 -0
  72. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/tests/__init__.py +0 -0
  73. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/tests/conftest.py +0 -0
  74. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/tests/test_cli.py +0 -0
  75. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/tests/test_hello_world.py +0 -0
  76. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/plugin/hello_world/tests/test_server.py +0 -0
  77. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/__init__.py +0 -0
  78. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_action.py +0 -0
  79. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_api.py +0 -0
  80. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_aws_action.py +0 -0
  81. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_custom_encoder.py +0 -0
  82. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_endpoints.py +0 -0
  83. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_exceptions.py +0 -0
  84. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_helpers.py +0 -0
  85. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_metrics.py +0 -0
  86. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_oauth.py +0 -0
  87. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_plugin.py +0 -0
  88. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_schema.py +0 -0
  89. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_server_cloud_plugins.py +0 -0
  90. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_server_spec.py +0 -0
  91. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_trigger.py +0 -0
  92. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/test_variables.py +0 -0
  93. {insightconnect_plugin_runtime-6.3.10 → insightconnect_plugin_runtime-6.4.0}/tests/unit/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: insightconnect-plugin-runtime
3
- Version: 6.3.10
3
+ Version: 6.4.0
4
4
  Summary: InsightConnect Plugin Runtime
5
5
  Home-page: https://github.com/rapid7/komand-plugin-sdk-python
6
6
  Author: Rapid7 Integrations Alliance
@@ -199,11 +199,11 @@ Running a specific test file:
199
199
 
200
200
  | | Plugin | Slim Plugin |
201
201
  |:------------------|:-------:|:-----------:|
202
- | Python Version | 3.11.13 | 3.11.13 |
203
- | OS | Alpine | Bullseye |
202
+ | Python Version | 3.13.9 | 3.13.9 |
203
+ | OS | Alpine | Trixie |
204
204
  | Package installer | apk | apt |
205
205
  | Shell | /bin/sh | /bin/bash |
206
- | Image Size | ~370MB | ~230MB |
206
+ | Image Size | ~500MB | ~300MB |
207
207
 
208
208
  Note that for the plugin image, we run `apk update` and `apk add ..` which leads to a longer build time.
209
209
 
@@ -226,6 +226,7 @@ contributed. Black is installed as a test dependency and the hook can be initial
226
226
  after cloning this repository.
227
227
 
228
228
  ## Changelog
229
+ * 6.4.0 - When running in cloud mode, always create a new connection object | Bump Python version to 3.13.9 | Docker build tidyup to reduce image size
229
230
  * 6.3.10 - Fixed tracing name to better allign otel standards
230
231
  * 6.3.9 - Fixed `monitor_task_delay` decorator handling of millisecond epoch timestamps | Allow `test_task` connection method to accept task name parameter | Updated base images to Python 3.11.13
231
232
  * 6.3.8 - Update exception string representation methods to remove newline characters
@@ -155,11 +155,11 @@ Running a specific test file:
155
155
 
156
156
  | | Plugin | Slim Plugin |
157
157
  |:------------------|:-------:|:-----------:|
158
- | Python Version | 3.11.13 | 3.11.13 |
159
- | OS | Alpine | Bullseye |
158
+ | Python Version | 3.13.9 | 3.13.9 |
159
+ | OS | Alpine | Trixie |
160
160
  | Package installer | apk | apt |
161
161
  | Shell | /bin/sh | /bin/bash |
162
- | Image Size | ~370MB | ~230MB |
162
+ | Image Size | ~500MB | ~300MB |
163
163
 
164
164
  Note that for the plugin image, we run `apk update` and `apk add ..` which leads to a longer build time.
165
165
 
@@ -182,6 +182,7 @@ contributed. Black is installed as a test dependency and the hook can be initial
182
182
  after cloning this repository.
183
183
 
184
184
  ## Changelog
185
+ * 6.4.0 - When running in cloud mode, always create a new connection object | Bump Python version to 3.13.9 | Docker build tidyup to reduce image size
185
186
  * 6.3.10 - Fixed tracing name to better allign otel standards
186
187
  * 6.3.9 - Fixed `monitor_task_delay` decorator handling of millisecond epoch timestamps | Allow `test_task` connection method to accept task name parameter | Updated base images to Python 3.11.13
187
188
  * 6.3.8 - Update exception string representation methods to remove newline characters
@@ -5,7 +5,7 @@ import json
5
5
 
6
6
  from jsonschema import validate
7
7
 
8
- from .util import sample as utilsample
8
+ from .util import sample as utilsample, is_running_in_cloud
9
9
 
10
10
 
11
11
  def key(parameters):
@@ -26,28 +26,27 @@ def key(parameters):
26
26
  class ConnectionCache(object):
27
27
  def __init__(self, prototype):
28
28
  self.connections = {}
29
- self.prototype = prototype
29
+ self.prototype = prototype # connection JSON which does not contain a logger or validation on the values
30
30
 
31
31
  def get(self, parameters, logger):
32
- conn_key = key(parameters)
33
-
34
- # we could 'lock' this data structure
35
- # but honestly i don't see the point
36
- # worst case in a race condition is that
37
- # we create 2 copies of the connection, and
38
- # the later one overrides the first one.
39
- #
40
- # for real connection pooling we will need to fix this.
41
- if conn_key in self.connections:
42
- return self.connections[conn_key]
32
+ # when we're running in cloud mode we don't want to create and store connections to reduce the number
33
+ # of connection objects lying around in memory as this isn't safe and can cause OOM errors.
34
+ if is_running_in_cloud():
35
+ conn = self.create_and_validate_connection(parameters, logger)
36
+ else:
37
+ # first check if we have an existing connection obj we can return as it's been validated already
38
+ conn_key = key(parameters)
39
+ if not (conn := self.connections.get(conn_key)): # otherwise create a new conn obj and save in cache
40
+ conn = self.create_and_validate_connection(parameters, logger)
41
+ self.connections[conn_key] = conn
42
+ return conn
43
43
 
44
+ def create_and_validate_connection(self, parameters, logger):
44
45
  conn = copy.copy(self.prototype)
45
46
  conn.logger = logger
46
47
  conn.set_(parameters)
47
- # i don't know why this is needed twice..
48
- # i think for backwards compat reasons
49
48
  conn.connect(parameters)
50
- self.connections[conn_key] = conn
49
+
51
50
  return conn
52
51
 
53
52
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: insightconnect-plugin-runtime
3
- Version: 6.3.10
3
+ Version: 6.4.0
4
4
  Summary: InsightConnect Plugin Runtime
5
5
  Home-page: https://github.com/rapid7/komand-plugin-sdk-python
6
6
  Author: Rapid7 Integrations Alliance
@@ -199,11 +199,11 @@ Running a specific test file:
199
199
 
200
200
  | | Plugin | Slim Plugin |
201
201
  |:------------------|:-------:|:-----------:|
202
- | Python Version | 3.11.13 | 3.11.13 |
203
- | OS | Alpine | Bullseye |
202
+ | Python Version | 3.13.9 | 3.13.9 |
203
+ | OS | Alpine | Trixie |
204
204
  | Package installer | apk | apt |
205
205
  | Shell | /bin/sh | /bin/bash |
206
- | Image Size | ~370MB | ~230MB |
206
+ | Image Size | ~500MB | ~300MB |
207
207
 
208
208
  Note that for the plugin image, we run `apk update` and `apk add ..` which leads to a longer build time.
209
209
 
@@ -226,6 +226,7 @@ contributed. Black is installed as a test dependency and the hook can be initial
226
226
  after cloning this repository.
227
227
 
228
228
  ## Changelog
229
+ * 6.4.0 - When running in cloud mode, always create a new connection object | Bump Python version to 3.13.9 | Docker build tidyup to reduce image size
229
230
  * 6.3.10 - Fixed tracing name to better allign otel standards
230
231
  * 6.3.9 - Fixed `monitor_task_delay` decorator handling of millisecond epoch timestamps | Allow `test_task` connection method to accept task name parameter | Updated base images to Python 3.11.13
231
232
  * 6.3.8 - Update exception string representation methods to remove newline characters
@@ -75,6 +75,7 @@ tests/unit/__init__.py
75
75
  tests/unit/test_action.py
76
76
  tests/unit/test_api.py
77
77
  tests/unit/test_aws_action.py
78
+ tests/unit/test_connection.py
78
79
  tests/unit/test_custom_encoder.py
79
80
  tests/unit/test_endpoints.py
80
81
  tests/unit/test_exceptions.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setup(
7
7
  name="insightconnect-plugin-runtime",
8
- version="6.3.10",
8
+ version="6.4.0",
9
9
  description="InsightConnect Plugin Runtime",
10
10
  long_description=long_description,
11
11
  long_description_content_type="text/markdown",
@@ -0,0 +1,113 @@
1
+ from os import environ
2
+ from unittest import TestCase
3
+ from unittest.mock import patch, MagicMock
4
+
5
+ from insightconnect_plugin_runtime.connection import Connection, ConnectionCache, key
6
+
7
+ """
8
+ Any time an action / task / trigger API is called and we route through `plugin.py` -> `start_step`
9
+ the request body, containing the connection JSON is parsed and we make use of the `ConnectionCache` to retrieve
10
+ a connection that has already been validated or we create the new `Connection` object and call the `connect` method
11
+ which should be implemented within the paryicular plugin.
12
+
13
+ These unit test file is testing that the validation, and caching logic works as expected both when running
14
+ on an orchestrator or when running on the cloud. Cloud enabled plugins we do not want to re-use a previously
15
+ validated connection as more customers use the plugin, more and more connection objects can be left on the pod
16
+ which increases the memory usage and instead these should always be freshly created.
17
+ """
18
+
19
+
20
+ class TestCloudConnections(TestCase):
21
+ def setUp(self):
22
+ # Create a sample connection schema that will be validated
23
+ self.connection_schema = {
24
+ "type": "object",
25
+ "properties": {
26
+ "username": {"type": "string"},
27
+ },
28
+ "required": ["username"],
29
+ }
30
+ # these initialisations happen during plugin start up
31
+ self.connection = Connection(self.connection_schema)
32
+ self.connection_cache = ConnectionCache(self.connection)
33
+
34
+ self.connection = {
35
+ "username": "test_user@rapid7.com"
36
+ }
37
+
38
+ self.logger = MagicMock()
39
+
40
+ def stub_con(self, params):
41
+ # very basic example of what our connections do in plugins
42
+ self.logger.info("Connect: Connecting...")
43
+ self.username = params.get("username")
44
+
45
+ @patch.dict(environ, {"PLUGIN_RUNTIME_ENVIRONMENT": "cloud"})
46
+ @patch.object(Connection, "connect", new=stub_con)
47
+ def test_running_cloud_has_no_cache(self):
48
+ conn = self.connection_cache.get(self.connection, self.logger)
49
+ self.assertEqual(conn.parameters, self.connection)
50
+ self.assertDictEqual(self.connection_cache.connections, {})
51
+
52
+ @patch.dict(environ, {"PLUGIN_RUNTIME_ENVIRONMENT": "cloud"})
53
+ @patch.object(Connection, "connect", new=stub_con)
54
+ def test_running_cloud_has_no_cache_on_subsequent_run(self):
55
+ # call twice and we should still have no connection cache values
56
+ with patch("insightconnect_plugin_runtime.connection.key", MagicMock(side_effect=key)) as stub_key:
57
+ conn = self.connection_cache.get(self.connection, self.logger)
58
+ _ = self.connection_cache.get(self.connection, self.logger)
59
+ self.assertEqual(conn.parameters, self.connection)
60
+ self.assertDictEqual(self.connection_cache.connections, {})
61
+ self.assertEqual(stub_key.call_count, 0) # shouldn't call this at all
62
+
63
+ @patch.object(Connection, "connect", new=stub_con)
64
+ def test_running_onprem_has_cached_connection(self):
65
+ conn = self.connection_cache.get(self.connection, self.logger)
66
+ self.assertEqual(conn.parameters, self.connection)
67
+ self.assertNotEquals(self.connection_cache.connections, {})
68
+
69
+ # the connection cache key should exist
70
+ hashed_key = key(conn.parameters)
71
+ self.assertIn(hashed_key, self.connection_cache.connections)
72
+ self.assertTrue(type(self.connection_cache.connections[hashed_key] == Connection))
73
+
74
+ @patch.object(ConnectionCache, "create_and_validate_connection")
75
+ def test_running_onprem_uses_cache_on_subsequent_calls(self, stub_create):
76
+ with patch("insightconnect_plugin_runtime.connection.key", MagicMock(side_effect=key)) as stub_key:
77
+ # Mock the return value of create_and_validate_connection
78
+ pretend_obj = 'for this unit test pretend this is a conn object'
79
+ stub_create.return_value = pretend_obj
80
+
81
+ # first call the connection cache for the first time adding a new entry
82
+ conn = self.connection_cache.get(self.connection, self.logger)
83
+ self.assertEqual(len(self.connection_cache.connections), 1)
84
+ self.assertEquals(list(self.connection_cache.connections.values())[0], pretend_obj)
85
+ self.assertEquals(conn, pretend_obj)
86
+ self.assertEqual(stub_key.call_count, 1)
87
+
88
+ # on a second call we get the same value but the create_and_validate shouldn't be called again
89
+ conn2 = self.connection_cache.get(self.connection, self.logger)
90
+ self.assertEquals(conn, conn2)
91
+ stub_create.assert_called_once_with(self.connection, self.logger)
92
+
93
+ # on a third call we add a new entry to the cached connections
94
+ new_conn = self.connection.copy()
95
+ new_conn["username"] = "test_user_2@rapid7.com"
96
+ _ = self.connection_cache.get(new_conn, self.logger)
97
+ self.assertEqual(len(self.connection_cache.connections), 2) # new entry added
98
+
99
+ self.assertEqual(stub_key.call_count, 3) # should have been called each time
100
+
101
+ def test_connection_not_implemented_raised(self):
102
+ with self.assertRaises(NotImplementedError): # we haven't stubbed this so the error is expected
103
+ _conn = self.connection_cache.get(self.connection, self.logger)
104
+
105
+ # this means the connection object is not saved in cache
106
+ self.assertEquals(self.connection_cache.connections, {})
107
+
108
+ def test_connection_validation_raised(self):
109
+ with self.assertRaises(Exception): # not the expected input to match the connection schema
110
+ _conn = self.connection_cache.get({"not_expected_key": "value"}, self.logger)
111
+
112
+ # this means the connection object is not saved in cache
113
+ self.assertEquals(self.connection_cache.connections, {})