Repository: cloud-custodian/cloud-custodian

Issue:
C7N mu.py doesent honor function-prefix on describe_rule
Currently it looks like _mu.CloudWatchEventSource.get()_ doesent end up honoring the _function-prefix_ parameter when CloudWatchEventSource is instantiated. 

Currently mu.PolicyLambda.get_events() does not pass in a prefix variable and therefore the default of _custodian-_ is used in the describe_rule calls, even if you specify your own prefix in your policy files.

![image](https://user-images.githubusercontent.com/40577885/46300140-92810600-c568-11e8-9561-7a7f5acb117e.png)

Passing in the prefix should allow the call to work as intended:
![image](https://user-images.githubusercontent.com/40577885/46300191-b04e6b00-c568-11e8-8819-a7171df2de2a.png)

Sidenote: the put_rule call uses the function's name, which already has the correct prefix applied, so that works as intended. Its just the describe before hand that is failing.



Discussion:
yeah, i noticed that, it results a bad diff check when updating, which means updates on every execution which is suboptimal and a bug, but from a functional perspective its okay (policy lambda deploys and executes).

Resolving patch:
diff --git a/c7n/mu.py b/c7n/mu.py
--- a/c7n/mu.py
+++ b/c7n/mu.py
@@ -357,23 +357,23 @@ def logs(self, func, start, end):
 
     @staticmethod
     def delta_function(old_config, new_config):
-        found = False
+        changed = []
         for k in new_config:
             # Vpc needs special handling as a dict with lists
             if k == 'VpcConfig' and k in old_config and new_config[k]:
                 if set(old_config[k]['SubnetIds']) != set(
                         new_config[k]['SubnetIds']):
-                    found = True
+                    changed.append(k)
                 elif set(old_config[k]['SecurityGroupIds']) != set(
                         new_config[k]['SecurityGroupIds']):
-                    found = True
+                    changed.append(k)
             elif k not in old_config:
                 if k in LAMBDA_EMPTY_VALUES and LAMBDA_EMPTY_VALUES[k] == new_config[k]:
                     continue
-                found = True
+                changed.append(k)
             elif new_config[k] != old_config[k]:
-                found = True
-        return found
+                changed.append(k)
+        return changed
 
     @staticmethod
     def diff_tags(old_tags, new_tags):
@@ -403,22 +403,23 @@ def _create_or_update(self, func, role=None, s3_uri=None, qualifier=None):
         changed = False
         if existing:
             old_config = existing['Configuration']
-            if archive.get_checksum() != old_config[
-                    'CodeSha256'].encode('ascii'):
+            if archive.get_checksum() != old_config['CodeSha256']:
                 log.debug("Updating function %s code", func.name)
                 params = dict(FunctionName=func.name, Publish=True)
                 params.update(code_ref)
                 result = self.client.update_function_code(**params)
                 changed = True
+
             # TODO/Consider also set publish above to false, and publish
             # after configuration change?
 
             new_config = func.get_config()
-            new_config['Role'] = role
             new_tags = new_config.pop('Tags', {})
 
-            if self.delta_function(old_config, new_config):
-                log.debug("Updating function: %s config" % func.name)
+            config_changed = self.delta_function(old_config, new_config)
+            if config_changed:
+                log.debug("Updating function: %s config %s",
+                          func.name, ", ".join(sorted(config_changed)))
                 result = self.client.update_function_configuration(**new_config)
                 changed = True
 
@@ -851,22 +852,14 @@ class CloudWatchEventSource(object):
         'terminate-success': 'EC2 Instance Terminate Successful',
         'terminate-failure': 'EC2 Instance Terminate Unsuccessful'}
 
-    def __init__(self, data, session_factory, prefix="custodian-"):
+    def __init__(self, data, session_factory):
         self.session_factory = session_factory
         self.session = session_factory()
         self.client = self.session.client('events')
         self.data = data
-        self.prefix = prefix
-
-    def _make_notification_id(self, function_name):
-        if not function_name.startswith(self.prefix):
-            return "%s%s" % (self.prefix, function_name)
-        return function_name
 
     def get(self, rule_name):
-        return resource_exists(
-            self.client.describe_rule,
-            Name=self._make_notification_id(rule_name))
+        return resource_exists(self.client.describe_rule, Name=rule_name)
 
     @staticmethod
     def delta(src, tgt):
@@ -953,7 +946,7 @@ def add(self, func):
         rule = self.get(func.name)
 
         if rule and self.delta(rule, params):
-            log.debug("Updating cwe rule for %s" % self)
+            log.debug("Updating cwe rule for %s" % func.name)
             response = self.client.put_rule(**params)
         elif not rule:
             log.debug("Creating cwe rule for %s" % (self))


Test patch:
diff --git a/tests/test_mu.py b/tests/test_mu.py
--- a/tests/test_mu.py
+++ b/tests/test_mu.py
@@ -311,7 +311,8 @@ def test_cwe_update_config_and_code(self):
 
         lines = output.getvalue().strip().split("\n")
         self.assertTrue("Updating function custodian-s3-bucket-policy code" in lines)
-        self.assertTrue("Updating function: custodian-s3-bucket-policy config" in lines)
+        self.assertTrue(
+            "Updating function: custodian-s3-bucket-policy config MemorySize, Role" in lines)
         self.assertEqual(result["FunctionName"], result2["FunctionName"])
         # drive by coverage
         functions = [