Repository: cloud-custodian/cloud-custodian

Issue:
Mailer broken on master
**Describe the bug**

c7n-mailer is broken in current master. It appears that this was caused by changes in #5708, specifically:

1. ``splunk-sdk`` was added to the mailer dependencies/requirements. I'm not sure why this was done, as ``c7n_mailer.splunk_delivery`` doesn't require that, it just uses ``requests`` to send events to the Splunk HTTP Event Collector (HEC).
2. ``splunk-sdk`` was added to ``CORE_DEPS`` in ``c7n_mailer.deploy``. This raises an exception, as ``splunk-sdk`` is the package name but the module name is ``splunklib``.

**To Reproduce**

Attempt to run c7n-mailer:

```
16:54:00 Traceback (most recent call last):
16:54:00   File "/usr/local/bin/manheim-c7n-runner", line 11, in <module>
16:54:00     load_entry_point('manheim-c7n-tools', 'console_scripts', 'manheim-c7n-runner')()
16:54:00   File "/manheim_c7n_tools/manheim_c7n_tools/runner.py", line 651, in main
16:54:00     args.ACTION, args.regions, step_names=args.steps, skip_steps=args.skip
16:54:00   File "/manheim_c7n_tools/manheim_c7n_tools/runner.py", line 513, in run
16:54:00     self._run_step_in_regions(action, step, regions)
16:54:00   File "/manheim_c7n_tools/manheim_c7n_tools/runner.py", line 566, in _run_step_in_regions
16:54:00     step(region_name, region_conf).run()
16:54:00   File "/manheim_c7n_tools/manheim_c7n_tools/runner.py", line 337, in run
16:54:00     functools.partial(session_factory, conf)
16:54:00   File "/manheim_c7n_tools/src/c7n-mailer/tools/c7n_mailer/c7n_mailer/deploy.py", line 98, in provision
16:54:00     archive = get_archive(config)
16:54:00   File "/manheim_c7n_tools/src/c7n-mailer/tools/c7n_mailer/c7n_mailer/deploy.py", line 60, in get_archive
16:54:00     archive = PythonPackageArchive(modules=deps)
16:54:00   File "/manheim_c7n_tools/src/c7n/c7n/mu.py", line 84, in __init__
16:54:00     self.add_modules(None, modules)
16:54:00   File "/manheim_c7n_tools/src/c7n/c7n/mu.py", line 128, in add_modules
16:54:00     module = importlib.import_module(module_name)
16:54:00   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
16:54:00     return _bootstrap._gcd_import(name[level:], package, level)
16:54:00   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
16:54:00   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
16:54:00   File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
16:54:00 ModuleNotFoundError: No module named 'splunk-sdk'
```

Alternatively, I've added the following unit test to manheim-c7n-tools to catch issues like this:

```
arch = c7n_mailer.deploy.get_archive({'templates_folders': []})
assert isinstance(arch, PythonPackageArchive)
assert arch.size > 0
assert len(arch.get_filenames()) > 0
```

Which is currently failing with:

```
self = <manheim_c7n_tools.tests.test_runner.TestMailerStep object at 0x7f5aeb3111f0>

    def test_mailer_archive(self):
        """
        This is a test of ``c7n_mailer.deploy.get_archive()``. We've had a few
        dependency issues in the past that aren't detected until actually
        deploying mailer and generating the archive (zip) for the Lambda, so
        this attempts to detect those issues.
        """
>       arch = get_archive({'templates_folders': []})

manheim_c7n_tools/tests/test_runner.py:671: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.tox/py38/src/c7n-mailer/tools/c7n_mailer/c7n_mailer/deploy.py:60: in get_archive
    archive = PythonPackageArchive(modules=deps)
.tox/py38/src/c7n/c7n/mu.py:84: in __init__
    self.add_modules(None, modules)
.tox/py38/src/c7n/c7n/mu.py:128: in add_modules
    module = importlib.import_module(module_name)
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1014: in _gcd_import
    ???
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

name = 'splunk-sdk', import_ = <function _gcd_import at 0x7f5aeebb6430>

>   ???
E   ModuleNotFoundError: No module named 'splunk-sdk'

<frozen importlib._bootstrap>:973: ModuleNotFoundError
```

**Expected behavior**

Mailer does not throw an exception.

**Background (please complete the following information):**

OS- and version-independent.

**Additional context**

I'm going to try to work up a PR to fix this, but I've never used poetry before, so it might take a while...


Discussion:

Resolving patch:
diff --git a/tools/c7n_mailer/c7n_mailer/deploy.py b/tools/c7n_mailer/c7n_mailer/deploy.py
--- a/tools/c7n_mailer/c7n_mailer/deploy.py
+++ b/tools/c7n_mailer/c7n_mailer/deploy.py
@@ -50,7 +50,6 @@ def dispatch(event, context):
     'requests', 'urllib3', 'idna', 'chardet', 'certifi',
     # used by splunk mailer transport
     'jsonpointer', 'jsonpatch',
-    'splunk-sdk',
     # sendgrid dependencies
     'sendgrid', 'python_http_client']
 
diff --git a/tools/c7n_mailer/setup.py b/tools/c7n_mailer/setup.py
--- a/tools/c7n_mailer/setup.py
+++ b/tools/c7n_mailer/setup.py
@@ -20,8 +20,7 @@
  'python-dateutil>=2.8.1,<3.0.0',
  'pyyaml>=5.3,<6.0',
  'redis>=3.4.1,<4.0.0',
- 'sendgrid>=6.1.1,<7.0.0',
- 'splunk-sdk>=1.6.12,<2.0.0']
+ 'sendgrid>=6.1.1,<7.0.0']
 
 entry_points = \
 {'console_scripts': ['c7n-mailer = c7n_mailer.cli:main',


Test patch:
diff --git a/tools/c7n_mailer/tests/test_misc.py b/tools/c7n_mailer/tests/test_misc.py
--- a/tools/c7n_mailer/tests/test_misc.py
+++ b/tools/c7n_mailer/tests/test_misc.py
@@ -8,6 +8,8 @@
 from c7n_mailer import handle
 from c7n_mailer import sqs_queue_processor
 from c7n_mailer import cli
+from c7n_mailer import deploy
+from c7n.mu import PythonPackageArchive
 from common import MAILER_CONFIG
 
 
@@ -52,3 +54,13 @@ def test_cli_run(self):
             [session.region_name, session.profile_name],
             ['us-east-1', 'default']
         )
+
+
+class DeployTests(unittest.TestCase):
+
+    def test_get_archive(self):
+        archive = deploy.get_archive({'templates_folders': []})
+        assert isinstance(archive, PythonPackageArchive)
+        # basic sanity checks using random, low values
+        assert archive.size > 10000  # this should really be about 1.5 MB
+        assert len(archive.get_filenames()) > 50  # should be > 500