gurudesh/pytracebugs huggingface dataset:
# Buggy code
def __init__(self, policy, data):
    self.policy = policy
    self.data = data
    self.filters = self.data.get("conditions", [])
    # used by c7n-org to extend evaluation conditions
    self.env_vars = {}


# Traceback (AttributeError)
Traceback (most recent call last):
File "/root/c7n/lib/python3.8/site-packages/c7n/cli.py", line 362, in main
command(config)
File "/root/c7n/lib/python3.8/site-packages/c7n/commands.py", line 136, in _load_policies
return f(options, list(policies))
File "/root/c7n/lib/python3.8/site-packages/c7n/commands.py", line 282, in run
policy()
File "/root/c7n/lib/python3.8/site-packages/c7n/policy.py", line 1171, in __call__
elif not self.is_runnable():
File "/root/c7n/lib/python3.8/site-packages/c7n/policy.py", line 1011, in is_runnable
return self.conditions.evaluate(event)
File "/root/c7n/lib/python3.8/site-packages/c7n/policy.py", line 945, in evaluate
state = all([f.process([policy_vars], event) for f in self.filters])
File "/root/c7n/lib/python3.8/site-packages/c7n/policy.py", line 945, in <listcomp>
state = all([f.process([policy_vars], event) for f in self.filters])
File "/root/c7n/lib/python3.8/site-packages/c7n/filters/core.py", line 530, in process
return super(ValueFilter, self).process(resources, event)
File "/root/c7n/lib/python3.8/site-packages/c7n/filters/core.py", line 217, in process
return list(filter(self, resources))
File "/root/c7n/lib/python3.8/site-packages/c7n/filters/core.py", line 517, in __call__
matched = self.match(i)
File "/root/c7n/lib/python3.8/site-packages/c7n/filters/core.py", line 569, in match
values = ValuesFrom(self.data['value_from'], self.manager)
File "/root/c7n/lib/python3.8/site-packages/c7n/resolver.py", line 132, in __init__
'account_id': manager.config.account_id,
AttributeError: 'PolicyConditions' object has no attribute 'config'
/root/c7n/lib/python3.8/site-packages/c7n/resolver.py(132)__init__()
-> 'account_id': manager.config.account_id,
(Pdb)

# Fixed code
def __init__(self, policy, data):
    self.policy = policy
    self.data = data
    self.filters = self.data.get("conditions", [])
    # for value_from usage / we use the conditions class
    # to mimic a resource manager interface. we can't use
    # the actual resource manager as we're overriding block
    # filters which work w/ resource type metadata and our
    # resource here is effectively the execution variables.
    self.config = self.policy.options
    rm = self.policy.resource_manager
    self._cache = rm._cache
    self.session_factory = rm.session_factory
    # used by c7n-org to extend evaluation conditions
    self.env_vars = {}
# Buggy code
def augment(self, resources):
    results = []
    client = local_session(self.session_factory).client("ecs")
    for task_def_set in resources:
        response = client.describe_task_definition(
            taskDefinition=task_def_set, include=["TAGS"]
        )
        r = response["taskDefinition"]
        r["tags"] = response.get("tags", [])
        results.append(r)
    ecs_tag_normalize(results)
    return results


# Traceback (botocore.exceptions.ClientError)
15:22:29
2020-06-30 15:22:29,380: custodian.commands:ERROR Error while executing policy check-ecs-def, continuing

15:22:29
Traceback (most recent call last):

15:22:29
File "/usr/local/lib/python3.7/dist-packages/c7n/commands.py", line 282, in run

15:22:29
policy()

15:22:29
File "/usr/local/lib/python3.7/dist-packages/c7n/policy.py", line 1176, in __call__

15:22:29
resources = mode.run()

15:22:29
File "/usr/local/lib/python3.7/dist-packages/c7n/policy.py", line 291, in run

15:22:29
resources = self.policy.resource_manager.resources()

15:22:29
File "/usr/local/lib/python3.7/dist-packages/c7n/query.py", line 502, in resources

15:22:29
resources = self.augment(resources)

15:22:29
File "/usr/local/lib/python3.7/dist-packages/c7n/resources/ecs.py", line 489, in augment

15:22:29
include=['TAGS'])

15:22:29
File "/usr/local/lib/python3.7/dist-packages/botocore/client.py", line 316, in _api_call

15:22:29
return self._make_api_call(operation_name, kwargs)

15:22:29
File "/usr/local/lib/python3.7/dist-packages/botocore/client.py", line 626, in _make_api_call

15:22:29
raise error_class(parsed_response, operation_name)

15:22:29
botocore.exceptions.ClientError: An error occurred (ThrottlingException) when calling the DescribeTaskDefinition operation (reached max retries: 4): Rate exceeded

# Fixed code
def augment(self, resources):
    results = []
    client = local_session(self.session_factory).client("ecs")
    for task_def_set in resources:
        response = self.retry(
            client.describe_task_definition,
            taskDefinition=task_def_set,
            include=["TAGS"],
        )
        r = response["taskDefinition"]
        r["tags"] = response.get("tags", [])
        results.append(r)
    ecs_tag_normalize(results)
    return results
# Buggy code
def get_block_operator(self):
    """Determine the immediate parent boolean operator for a filter"""
    # Top level operator is `and`
    block_stack = ["and"]
    for f in self.manager.iter_filters(block_end=True):
        if f is None:
            block_stack.pop()
            continue
        if f.type in ("and", "or", "not"):
            block_stack.append(f.type)
        if f == self:
            break
    return block_stack[-1]


# Traceback (KeyError)
$ custodian run -d -v -s out -p run-asg-auto-tag-creator ./auto-tag-creator.yaml
2020-05-06 15:47:07,205: custodian.commands:DEBUG Loaded file ./auto-tag-creator.yaml. Contains 11 policies
2020-05-06 15:47:07,223: custodian.aws:DEBUG using default region:us-west-2 from boto
2020-05-06 15:47:07,778: custodian.commands:ERROR Error while executing policy run-asg-auto-tag-creator, continuing
Traceback (most recent call last):
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/commands.py", line 281, in run
policy()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 1056, in __call__
resources = PullMode(self).run()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 279, in run
if not self.policy.is_runnable():
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 906, in is_runnable
return self.conditions.evaluate(event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in evaluate
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in <listcomp>
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 299, in process
return self.process_set(resources, event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in process_set
resource_map = {r[rtype_id]: r for r in resources}
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in <dictcomp>
resource_map = {r[rtype_id]: r for r in resources}
KeyError: 'AutoScalingGroupName'

# Fixed code
def get_block_operator(self):
    """Determine the immediate parent boolean operator for a filter"""
    # Top level operator is `and`
    block = self.get_block_parent()
    if block.type in ("and", "or", "not"):
        return block.type
    return "and"
# Buggy code
def iter_filters(self, block_end=False):
    queue = deque(self.filters)
    while queue:
        f = queue.popleft()
        if f and f.type in ("or", "and", "not"):
            if block_end:
                queue.appendleft(None)
            for gf in f.filters:
                queue.appendleft(gf)
        yield f


# Traceback (KeyError)
$ custodian run -d -v -s out -p run-asg-auto-tag-creator ./auto-tag-creator.yaml
2020-05-06 15:47:07,205: custodian.commands:DEBUG Loaded file ./auto-tag-creator.yaml. Contains 11 policies
2020-05-06 15:47:07,223: custodian.aws:DEBUG using default region:us-west-2 from boto
2020-05-06 15:47:07,778: custodian.commands:ERROR Error while executing policy run-asg-auto-tag-creator, continuing
Traceback (most recent call last):
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/commands.py", line 281, in run
policy()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 1056, in __call__
resources = PullMode(self).run()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 279, in run
if not self.policy.is_runnable():
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 906, in is_runnable
return self.conditions.evaluate(event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in evaluate
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in <listcomp>
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 299, in process
return self.process_set(resources, event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in process_set
resource_map = {r[rtype_id]: r for r in resources}
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in <dictcomp>
resource_map = {r[rtype_id]: r for r in resources}
KeyError: 'AutoScalingGroupName'

# Fixed code
def iter_filters(self, block_end=False):
    return iter_filters(self.filters, block_end=block_end)
# Buggy code
def validate(self):
    self.filters.extend(self.convert_deprecated())
    self.filters = self.filter_registry.parse(
        self.filters, self.policy.resource_manager
    )


# Traceback (KeyError)
$ custodian run -d -v -s out -p run-asg-auto-tag-creator ./auto-tag-creator.yaml
2020-05-06 15:47:07,205: custodian.commands:DEBUG Loaded file ./auto-tag-creator.yaml. Contains 11 policies
2020-05-06 15:47:07,223: custodian.aws:DEBUG using default region:us-west-2 from boto
2020-05-06 15:47:07,778: custodian.commands:ERROR Error while executing policy run-asg-auto-tag-creator, continuing
Traceback (most recent call last):
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/commands.py", line 281, in run
policy()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 1056, in __call__
resources = PullMode(self).run()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 279, in run
if not self.policy.is_runnable():
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 906, in is_runnable
return self.conditions.evaluate(event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in evaluate
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in <listcomp>
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 299, in process
return self.process_set(resources, event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in process_set
resource_map = {r[rtype_id]: r for r in resources}
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in <dictcomp>
resource_map = {r[rtype_id]: r for r in resources}
KeyError: 'AutoScalingGroupName'

# Fixed code
def validate(self):
    self.filters.extend(self.convert_deprecated())
    self.filters = self.filter_registry.parse(self.filters, self)
# Buggy code
def __call__(self):
    """Run policy in default mode"""
    mode = self.get_execution_mode()
    if self.options.dryrun:
        resources = PullMode(self).run()
    elif not self.is_runnable():
        resources = []
    elif isinstance(mode, ServerlessExecutionMode):
        resources = mode.provision()
    else:
        resources = mode.run()
    # clear out resource manager post run, to clear cache
    self.resource_manager = self.load_resource_manager()
    return resources


# Traceback (KeyError)
$ custodian run -d -v -s out -p run-asg-auto-tag-creator ./auto-tag-creator.yaml
2020-05-06 15:47:07,205: custodian.commands:DEBUG Loaded file ./auto-tag-creator.yaml. Contains 11 policies
2020-05-06 15:47:07,223: custodian.aws:DEBUG using default region:us-west-2 from boto
2020-05-06 15:47:07,778: custodian.commands:ERROR Error while executing policy run-asg-auto-tag-creator, continuing
Traceback (most recent call last):
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/commands.py", line 281, in run
policy()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 1056, in __call__
resources = PullMode(self).run()
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 279, in run
if not self.policy.is_runnable():
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 906, in is_runnable
return self.conditions.evaluate(event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in evaluate
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/policy.py", line 843, in <listcomp>
state = all([f.process([policy_vars], event) for f in self.filters])
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 299, in process
return self.process_set(resources, event)
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in process_set
resource_map = {r[rtype_id]: r for r in resources}
File "/Users/tstansell/.local/c7n/lib/python3.7/site-packages/c7n/filters/core.py", line 314, in <dictcomp>
resource_map = {r[rtype_id]: r for r in resources}
KeyError: 'AutoScalingGroupName'

# Fixed code
def __call__(self):
    """Run policy in default mode"""
    mode = self.get_execution_mode()
    if isinstance(mode, ServerlessExecutionMode) or self.options.dryrun:
        self._trim_runtime_filters()

    if self.options.dryrun:
        resources = PullMode(self).run()
    elif not self.is_runnable():
        resources = []
    elif isinstance(mode, ServerlessExecutionMode):
        resources = mode.provision()
    else:
        resources = mode.run()
    # clear out resource manager post run, to clear cache
    self.resource_manager = self.load_resource_manager()
    return resources
# Buggy code
def get_asg_templates(self, asgs):
    templates = {}
    for a in asgs:
        t = None
        if "LaunchTemplate" in a:
            t = a["LaunchTemplate"]
        elif "MixedInstancesPolicy" in a:
            t = a["MixedInstancesPolicy"]["LaunchTemplate"][
                "LaunchTemplateSpecification"
            ]
        if t is None:
            continue
        templates.setdefault((t["LaunchTemplateId"], t["Version"]), []).append(
            a["AutoScalingGroupName"]
        )
    return templates


# Traceback (KeyError)
2020-03-17 14:34:01,624: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/Users/user/.local/share/virtualenvs/cloud_custodian-GAUqGNXp/lib/python3.7/site-packages/c7n/policy.py", line 288, in run
resources = self.policy.resource_manager.resources()
File "/Users/user/.local/share/virtualenvs/cloud_custodian-GAUqGNXp/lib/python3.7/site-packages/c7n/resources/ami.py", line 53, in resources
return super(AMI, self).resources(query=query)
File "/Users/user/.local/share/virtualenvs/cloud_custodian-GAUqGNXp/lib/python3.7/site-packages/c7n/query.py", line 460, in resources
resources = self.filter_resources(resources)
File "/Users/user/.local/share/virtualenvs/cloud_custodian-GAUqGNXp/lib/python3.7/site-packages/c7n/manager.py", line 108, in filter_resources
resources = f.process(resources, event)
File "/Users/user/.local/share/virtualenvs/cloud_custodian-GAUqGNXp/lib/python3.7/site-packages/c7n/resources/ami.py", line 305, in process
images = self._pull_ec2_images().union(self._pull_asg_images())
File "/Users/user/.local/share/virtualenvs/cloud_custodian-GAUqGNXp/lib/python3.7/site-packages/c7n/resources/ami.py", line 296, in _pull_asg_images
list(tmpl_mgr.get_asg_templates(asgs).keys())):
File "/Users/user/.local/share/virtualenvs/cloud_custodian-GAUqGNXp/lib/python3.7/site-packages/c7n/resources/ec2.py", line 1902, in get_asg_templates
t['Version']), []).append(a['AutoScalingGroupName'])
KeyError: 'Version'

# Fixed code
def get_asg_templates(self, asgs):
    templates = {}
    for a in asgs:
        t = None
        if "LaunchTemplate" in a:
            t = a["LaunchTemplate"]
        elif "MixedInstancesPolicy" in a:
            t = a["MixedInstancesPolicy"]["LaunchTemplate"][
                "LaunchTemplateSpecification"
            ]
        if t is None:
            continue
        templates.setdefault(
            (t["LaunchTemplateId"], t.get("Version", None)), []
        ).append(a["AutoScalingGroupName"])
    return templates
# Buggy code
def _process_resource(self, resource):
    lock_name = self._get_lock_name(resource)
    lock_notes = self._get_lock_notes(resource)

    if is_resource_group(resource):
        self.client.management_locks.create_or_update_at_resource_group_level(
            resource["name"],
            lock_name,
            ManagementLockObject(level=self.lock_type, notes=lock_notes),
        )
    else:
        self.client.management_locks.create_or_update_at_resource_level(
            resource["resourceGroup"],
            ResourceIdParser.get_namespace(resource["id"]),
            ResourceIdParser.get_resource_name(resource.get("c7n:parent-id")) or "",
            ResourceIdParser.get_resource_type(resource["id"]),
            resource["name"],
            lock_name,
            ManagementLockObject(level=self.lock_type, notes=lock_notes),
        )


# Traceback (AssertionError)
====================================================================== FAILURES =======================================================================
________________________________________________________ ActionsMarkForOpTest.test_mark_for_op ________________________________________________________
[gw4] darwin -- Python 3.7.3 /Users/kapilt/projects/release-custodian/.tox/py37/bin/python
Traceback (most recent call last):
File "/Users/kapilt/projects/release-custodian/tools/c7n_azure/tests_azure/test_actions_mark-for-op.py", line 58, in test_mark_for_op
self.assertEqual(tags, expected_tags)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 1138, in assertDictEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 680, in fail
raise self.failureException(msg)
AssertionError: {'pre[66 chars]dian_status': 'Resource does not meet policy: stop@2019/10/21'} != {'pre[66 chars]dian_status': 'Resource does not meet policy: stop@2019/10/20'}
- {'custodian_status': 'Resource does not meet policy: stop@2019/10/21',
?                                                                    ^

+ {'custodian_status': 'Resource does not meet policy: stop@2019/10/20',
?                                                                    ^

# Fixed code
def _process_resource(self, resource):
    lock_name = self._get_lock_name(resource)
    lock_notes = self._get_lock_notes(resource)

    if is_resource_group(resource):
        self.client.management_locks.create_or_update_at_resource_group_level(
            resource["name"],
            lock_name,
            ManagementLockObject(level=self.lock_type, notes=lock_notes),
        )
    else:
        self.client.management_locks.create_or_update_by_scope(
            resource["id"],
            lock_name,
            ManagementLockObject(level=self.lock_type, notes=lock_notes),
        )
# Buggy code
def get_namespace(resource_id):
    parsed = parse_resource_id(resource_id)
    if parsed.get("children"):
        return "/".join([parsed.get("namespace"), parsed.get("type")])
    return parsed.get("namespace")


# Traceback (AssertionError)
====================================================================== FAILURES =======================================================================
________________________________________________________ ActionsMarkForOpTest.test_mark_for_op ________________________________________________________
[gw4] darwin -- Python 3.7.3 /Users/kapilt/projects/release-custodian/.tox/py37/bin/python
Traceback (most recent call last):
File "/Users/kapilt/projects/release-custodian/tools/c7n_azure/tests_azure/test_actions_mark-for-op.py", line 58, in test_mark_for_op
self.assertEqual(tags, expected_tags)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 1138, in assertDictEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 680, in fail
raise self.failureException(msg)
AssertionError: {'pre[66 chars]dian_status': 'Resource does not meet policy: stop@2019/10/21'} != {'pre[66 chars]dian_status': 'Resource does not meet policy: stop@2019/10/20'}
- {'custodian_status': 'Resource does not meet policy: stop@2019/10/21',
?                                                                    ^

+ {'custodian_status': 'Resource does not meet policy: stop@2019/10/20',
?                                                                    ^

# Fixed code
def get_namespace(resource_id):
    parsed = parse_resource_id(resource_id)
    return parsed.get("namespace")
# Buggy code
def get_resource_type(resource_id):
    parsed = parse_resource_id(resource_id)
    # parse_resource_id returns dictionary with "child_type_#" to represent
    # types sequence. "type" stores root type.
    child_type_keys = [k for k in parsed.keys() if k.find("child_type_") != -1]
    types = [parsed.get(k) for k in sorted(child_type_keys)]
    if not types:
        types.insert(0, parsed.get("type"))
    return "/".join(types)


# Traceback (AssertionError)
====================================================================== FAILURES =======================================================================
________________________________________________________ ActionsMarkForOpTest.test_mark_for_op ________________________________________________________
[gw4] darwin -- Python 3.7.3 /Users/kapilt/projects/release-custodian/.tox/py37/bin/python
Traceback (most recent call last):
File "/Users/kapilt/projects/release-custodian/tools/c7n_azure/tests_azure/test_actions_mark-for-op.py", line 58, in test_mark_for_op
self.assertEqual(tags, expected_tags)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 1138, in assertDictEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 680, in fail
raise self.failureException(msg)
AssertionError: {'pre[66 chars]dian_status': 'Resource does not meet policy: stop@2019/10/21'} != {'pre[66 chars]dian_status': 'Resource does not meet policy: stop@2019/10/20'}
- {'custodian_status': 'Resource does not meet policy: stop@2019/10/21',
?                                                                    ^

+ {'custodian_status': 'Resource does not meet policy: stop@2019/10/20',
?                                                                    ^

# Fixed code
def get_resource_type(resource_id):
    parsed = parse_resource_id(resource_id)
    # parse_resource_id returns dictionary with "child_type_#" to represent
    # types sequence. "type" stores root type.
    child_type_keys = [k for k in parsed.keys() if k.find("child_type_") != -1]
    types = [parsed.get(k) for k in sorted(child_type_keys)]
    types.insert(0, parsed.get("type"))
    return "/".join(types)
# Buggy code
def register_resource(klass, registry, event):
    for rtype, resource_manager in registry.items():
        if "post-finding" in resource_manager.action_registry:
            continue
        resource_manager.action_registry.register("post-finding", klass)


# Traceback (AttributeError)
2019-01-18 02:34:48,434: custodian.policy:INFO policy: support-tag-compliance-import-findings resource:aws.support-case region:us-east-1 count:2 time:0.47
2019-01-18 02:34:48,440: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/policy.py", line 264, in run
results = a.process(resources)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 202, in process
resource_set, finding_id, created_at, updated_at)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 312, in get_finding
finding_resources.append(self.format_resource(r))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 353, in format_resource
'Id': self.manager.get_arns([r])[0],
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 500, in get_arns
arns.append(self.generate_arn(_id))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 513, in generate_arn
resource_type=self.get_model().type,
AttributeError: type object 'resource_type' has no attribute 'type'

# Fixed code
def register_resource(klass, registry, event):
    for rtype, resource_manager in registry.items():
        if not resource_manager.has_arn():
            continue
        if "post-finding" in resource_manager.action_registry:
            continue
        resource_manager.action_registry.register("post-finding", klass)
# Buggy code
def register_resources(klass, registry, resource_class):
    """meta model subscriber on resource registration.

    SecurityHub Findings Filter
    """
    for rtype, resource_manager in registry.items():
        if "post-finding" in resource_manager.action_registry:
            continue
        resource_class.filter_registry.register("finding", klass)


# Traceback (AttributeError)
2019-01-18 02:34:48,434: custodian.policy:INFO policy: support-tag-compliance-import-findings resource:aws.support-case region:us-east-1 count:2 time:0.47
2019-01-18 02:34:48,440: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/policy.py", line 264, in run
results = a.process(resources)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 202, in process
resource_set, finding_id, created_at, updated_at)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 312, in get_finding
finding_resources.append(self.format_resource(r))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 353, in format_resource
'Id': self.manager.get_arns([r])[0],
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 500, in get_arns
arns.append(self.generate_arn(_id))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 513, in generate_arn
resource_type=self.get_model().type,
AttributeError: type object 'resource_type' has no attribute 'type'

# Fixed code
def register_resources(klass, registry, resource_class):
    """meta model subscriber on resource registration.

    SecurityHub Findings Filter
    """
    for rtype, resource_manager in registry.items():
        if not resource_manager.has_arn():
            continue
        if "post-finding" in resource_manager.action_registry:
            continue
        resource_class.filter_registry.register("finding", klass)
# Buggy code
def get_arns(self, resources):
    arns = []

    m = self.get_model()
    arn_key = getattr(m, "arn", None)
    id_key = m.id

    for r in resources:
        _id = r[id_key]
        if arn_key:
            arns.append(r[arn_key])
        elif "arn" in _id[:3]:
            arns.append(_id)
        else:
            arns.append(self.generate_arn(_id))
    return arns


# Traceback (AttributeError)
2019-01-18 02:34:48,434: custodian.policy:INFO policy: support-tag-compliance-import-findings resource:aws.support-case region:us-east-1 count:2 time:0.47
2019-01-18 02:34:48,440: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/policy.py", line 264, in run
results = a.process(resources)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 202, in process
resource_set, finding_id, created_at, updated_at)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 312, in get_finding
finding_resources.append(self.format_resource(r))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 353, in format_resource
'Id': self.manager.get_arns([r])[0],
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 500, in get_arns
arns.append(self.generate_arn(_id))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 513, in generate_arn
resource_type=self.get_model().type,
AttributeError: type object 'resource_type' has no attribute 'type'

# Fixed code
def get_arns(self, resources):
    arns = []

    m = self.get_model()
    arn_key = getattr(m, "arn", None)
    if arn_key is False:
        raise ValueError("%s do not have arns" % self.type)

    id_key = m.id

    for r in resources:
        _id = r[id_key]
        if arn_key:
            arns.append(r[arn_key])
        elif "arn" in _id[:3]:
            arns.append(_id)
        else:
            arns.append(self.generate_arn(_id))
    return arns
# Buggy code
def process_attached(self, client, associated_addrs):
    for aa in list(associated_addrs):
        try:
            client.disassociate_address(AssociationId=aa["AssociationId"])
        except BotoClientError as e:
            # If its already been diassociated ignore, else raise.
            if not (
                e.response["Error"]["Code"] == "InvalidAssocationID.NotFound"
                and aa["AssocationId"] in e.response["Error"]["Message"]
            ):
                raise e
            associated_addrs.remove(aa)
    return associated_addrs


# Traceback (AttributeError)
2019-01-18 02:34:48,434: custodian.policy:INFO policy: support-tag-compliance-import-findings resource:aws.support-case region:us-east-1 count:2 time:0.47
2019-01-18 02:34:48,440: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/policy.py", line 264, in run
results = a.process(resources)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 202, in process
resource_set, finding_id, created_at, updated_at)
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 312, in get_finding
finding_resources.append(self.format_resource(r))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/actions/securityhub.py", line 353, in format_resource
'Id': self.manager.get_arns([r])[0],
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 500, in get_arns
arns.append(self.generate_arn(_id))
File "/home/ec2-user/environment/cloud-custodian/.tox/py37/lib/python3.7/site-packages/c7n/query.py", line 513, in generate_arn
resource_type=self.get_model().type,
AttributeError: type object 'resource_type' has no attribute 'type'

# Fixed code
def process_attached(self, client, associated_addrs):
    for aa in list(associated_addrs):
        try:
            client.disassociate_address(AssociationId=aa["AssociationId"])
        except ClientError as e:
            # If its already been diassociated ignore, else raise.
            if not (
                e.response["Error"]["Code"] == "InvalidAssocationID.NotFound"
                and aa["AssocationId"] in e.response["Error"]["Message"]
            ):
                raise e
            associated_addrs.remove(aa)
    return associated_addrs
# Buggy code
def metrics_cmd(options, policies):
    start, end = _metrics_get_endpoints(options)
    data = {}
    for p in policies:
        log.info("Getting %s metrics", p)
        data[p.name] = p.get_metrics(start, end, options.period)
    print(dumps(data, indent=2))


# Traceback (ValueError)
(cloud-custodian) $ custodian metrics policies/policy.yml
2019-02-20 11:19:18,346: custodian.azure.session:INFO Creating session with Azure CLI Authentication
2019-02-20 11:19:18,347: custodian.azure.session:INFO Session using Subscription ID: <my sub redacted>
2019-02-20 11:19:18,347: custodian.commands:INFO Getting <Policy resource: azure.resourcegroup name: delete-empty-resource-groups region: > metrics
Traceback (most recent call last):
File "/Users/andyluong/Projects/forks/cloud-custodian/bin/custodian", line 11, in <module>
load_entry_point('c7n', 'console_scripts', 'custodian')()
File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/cli.py", line 368, in main
command(config)
File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/commands.py", line 136, in _load_policies
return f(options, list(policies))
File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/commands.py", line 491, in metrics_cmd
data[p.name] = p.get_metrics(start, end, options.period)
File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/policy.py", line 912, in get_metrics
return mode.get_metrics(start, end, period)
File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/policy.py", line 170, in get_metrics
client = session.client('cloudwatch')
File "/Users/andyluong/Projects/forks/cloud-custodian/tools/c7n_azure/c7n_azure/session.py", line 148, in client
service_name, client_name = client.rsplit('.', 1)
ValueError: not enough values to unpack (expected 2, got 1)

# Fixed code
def metrics_cmd(options, policies):
    log.warning("metrics command is deprecated, and will be removed in future")
    policies = [p for p in policies if p.provider_name == "aws"]
    start, end = _metrics_get_endpoints(options)
    data = {}
    for p in policies:
        log.info("Getting %s metrics", p)
        data[p.name] = p.get_metrics(start, end, options.period)
    print(dumps(data, indent=2))
# Buggy code
def initialize(self, options):
    """ """
    _default_region(options)
    _default_account_id(options)
    if options.tracer:
        XrayTracer.initialize()

    return options


# Traceback (NameError)
$ pip list|grep c7n
c7n                0.8.33.0
c7n-org            0.5.0


$ cat list-ec2-instances.yaml
policies:
- name: list-ec2-instances
resource: ec2


$ custodian run -s ~/output -r us-east-1 list-ec2-instances.yaml
2019-01-03 22:20:42,075: custodian.policy:INFO policy: list-ec2-instances resource:ec2 region:us-east-1 count:1 time:0.37


$ custodian report -s ~/output -r us-east-1 list-ec2-instances.yaml
Traceback (most recent call last):
File "/home/ec2-user/custodian/bin/custodian", line 11, in <module>
sys.exit(main())
File "/home/ec2-user/custodian/local/lib/python2.7/site-packages/c7n/cli.py", line 368, in main
command(config)
File "/home/ec2-user/custodian/local/lib/python2.7/site-packages/c7n/commands.py", line 105, in _load_policies
p_options = provider.initialize(options)
File "/home/ec2-user/custodian/local/lib/python2.7/site-packages/c7n/resources/aws.py", line 399, in initialize
XrayTracer.initialize()
File "/home/ec2-user/custodian/local/lib/python2.7/site-packages/c7n/resources/aws.py", line 222, in initialize
xray_recorder.configure(
NameError: global name 'xray_recorder' is not defined


$ cat rollback.txt
c7n==0.8.32.0


$ pip install --upgrade --force-reinstall -r rollback.txt


$ pip list|grep c7n
c7n                0.8.32.0
c7n-org            0.5.0

$ custodian report -s ~/output -r us-east-1 list-ec2-instances.yaml
CustodianDate,InstanceId,tag:Name,InstanceType,LaunchTime,VpcId,PrivateIpAddress
2019-01-03 22:20:42.074099,i-028454455c3bb6e1c,Cloud Custodian Host,t3.micro,2019-01-03T21:23:51+00:00,vpc-b869c1c0,172.31.37.231

# Fixed code
def initialize(self, options):
    """ """
    _default_region(options)
    _default_account_id(options)
    if options.tracer and options.tracer.startswith("xray") and HAVE_XRAY:
        XrayTracer.initialize()

    return options
# Buggy code
def process(self, resources, events=None):
    if self.manager:
        sweeper = AnnotationSweeper(self.manager.get_model().id, resources)

    for f in self.filters:
        resources = f.process(resources, events)

    if self.manager:
        sweeper.sweep(resources)

    return resources


# Traceback (IndexError)
Traceback (most recent call last):
File "/cloud-custodian/c7n/policy.py", line 241, in run
resources = self.policy.resource_manager.resources()
File "/cloud-custodian/c7n/resources/account.py", line 71, in resources
return self.filter_resources([get_account(self.session_factory, self.config)])
File "/cloud-custodian/c7n/manager.py", line 104, in filter_resources
resources = f.process(resources, event)
File "/cloud-custodian/c7n/filters/core.py", line 243, in process
return self.process_set(resources, event)
File "/cloud-custodian/c7n/filters/core.py", line 262, in process_set
resources = f.process(resources, event)
File "/cloud-custodian/c7n/filters/core.py", line 225, in process
resources = f.process(resources, events)
File "/cloud-custodian/c7n/resources/account.py", line 1010, in process
resources[0]['c7n:XrayEncryptionConfig'] = gec_result
IndexError: list index out of range

# Fixed code
def process(self, resources, events=None):
    if self.manager:
        sweeper = AnnotationSweeper(self.manager.get_model().id, resources)

    for f in self.filters:
        resources = f.process(resources, events)
        if not resources:
            break

    if self.manager:
        sweeper.sweep(resources)

    return resources
# Buggy code
def process_set(self, resources, event):
    resource_type = self.manager.get_model()
    resource_map = {r[resource_type.id]: r for r in resources}
    sweeper = AnnotationSweeper(resource_type.id, resources)

    for f in self.filters:
        resources = f.process(resources, event)

    before = set(resource_map.keys())
    after = set([r[resource_type.id] for r in resources])
    results = before - after
    sweeper.sweep([])

    return [resource_map[r_id] for r_id in results]


# Traceback (IndexError)
Traceback (most recent call last):
File "/cloud-custodian/c7n/policy.py", line 241, in run
resources = self.policy.resource_manager.resources()
File "/cloud-custodian/c7n/resources/account.py", line 71, in resources
return self.filter_resources([get_account(self.session_factory, self.config)])
File "/cloud-custodian/c7n/manager.py", line 104, in filter_resources
resources = f.process(resources, event)
File "/cloud-custodian/c7n/filters/core.py", line 243, in process
return self.process_set(resources, event)
File "/cloud-custodian/c7n/filters/core.py", line 262, in process_set
resources = f.process(resources, event)
File "/cloud-custodian/c7n/filters/core.py", line 225, in process
resources = f.process(resources, events)
File "/cloud-custodian/c7n/resources/account.py", line 1010, in process
resources[0]['c7n:XrayEncryptionConfig'] = gec_result
IndexError: list index out of range

# Fixed code
def process_set(self, resources, event):
    resource_type = self.manager.get_model()
    resource_map = {r[resource_type.id]: r for r in resources}
    sweeper = AnnotationSweeper(resource_type.id, resources)

    for f in self.filters:
        resources = f.process(resources, event)
        if not resources:
            break

    before = set(resource_map.keys())
    after = set([r[resource_type.id] for r in resources])
    results = before - after
    sweeper.sweep([])

    return [resource_map[r_id] for r_id in results]
# Buggy code
def process(self, resources, event=None):
    client = local_session(self.manager.session_factory).client("logs")
    accounts = self.get_accounts()
    results = []
    with self.executor_factory(max_workers=2) as w:
        futures = []
        for rset in chunks(resources, 50):
            futures.append(w.submit(self.process_resource_set, client, accounts, rset))
        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Error checking log groups cross-account %s", f.exception()
                )
                continue
            results.extend(f.result())
    return results


# Traceback (AttributeError)
type object 'resource_type' has no attribute 'filter_type': AttributeError
Traceback (most recent call last):
File "/var/task/custodian_policy.py", line 4, in run
return handler.dispatch_event(event, context)
File "/var/task/c7n/handler.py", line 91, in dispatch_event
p.push(event, context)
File "/var/task/c7n/policy.py", line 776, in push
return mode.run(event, lambda_ctx)
File "/var/task/c7n/policy.py", line 441, in run
resources = self.resolve_resources(event)
File "/var/task/c7n/policy.py", line 418, in resolve_resources
resources = self.policy.resource_manager.get_resources(resource_ids)
File "/var/task/c7n/query.py", line 441, in get_resources
resources = self.source.get_resources(ids)
File "/var/task/c7n/query.py", line 212, in get_resources
return self.query.get(self.manager, ids)
File "/var/task/c7n/query.py", line 93, in get
if m.filter_type == 'list':
AttributeError: type object 'resource_type' has no attribute 'filter_type'

# Fixed code
def process(self, resources, event=None):
    client = local_session(self.manager.session_factory).client("logs")
    accounts = self.get_accounts()
    results = []
    with self.executor_factory(max_workers=1) as w:
        futures = []
        for rset in chunks(resources, 50):
            futures.append(w.submit(self.process_resource_set, client, accounts, rset))
        for f in as_completed(futures):
            if f.exception():
                self.log.error(
                    "Error checking log groups cross-account %s", f.exception()
                )
                continue
            results.extend(f.result())
    return results
# Buggy code
def get_snapshots(self, ec2, snap_ids):
    """get snapshots corresponding to id, but tolerant of invalid id's."""
    while True:
        try:
            result = ec2.describe_snapshots(SnapshotIds=snap_ids)
        except ClientError as e:
            bad_snap = NotEncryptedFilter.get_bad_snapshot(e)
            if bad_snap:
                snap_ids.remove(bad_snap)
                continue
            raise
        else:
            return result.get("Snapshots", ())


# Traceback (KeyError)
'snap-xxxxxxxx': KeyError
Traceback (most recent call last):
File "/var/task/custodian_policy.py", line 4, in run
return handler.dispatch_event(event, context)
File "/var/task/c7n/handler.py", line 89, in dispatch_event
p.push(event, context)
File "/var/task/c7n/policy.py", line 739, in push
return mode.run(event, lambda_ctx)
File "/var/task/c7n/policy.py", line 413, in run
resources, event)
File "/var/task/c7n/manager.py", line 90, in filter_resources
resources = f.process(resources, event)
File "/var/task/c7n/resources/asg.py", line 433, in process
self.initialize(asgs)
File "/var/task/c7n/resources/asg.py", line 457, in initialize
self.unencrypted_configs = self.get_unencrypted_configs(ec2)
File "/var/task/c7n/resources/asg.py", line 526, in get_unencrypted_configs
unencrypted_configs.update(snaps[s['SnapshotId']])
KeyError: 'snap-xxxxxxxx'

# Fixed code
def get_snapshots(self, ec2, snap_ids):
    """get snapshots corresponding to id, but tolerant of invalid id's."""
    while snap_ids:
        try:
            result = ec2.describe_snapshots(SnapshotIds=snap_ids)
        except ClientError as e:
            bad_snap = NotEncryptedFilter.get_bad_snapshot(e)
            if bad_snap:
                snap_ids.remove(bad_snap)
                continue
            raise
        else:
            return result.get("Snapshots", ())
    return ()
# Buggy code
def main():
    parser = setup_parser()
    argcomplete.autocomplete(parser)
    options = parser.parse_args()

    _setup_logger(options)

    # Support the deprecated -c option
    if getattr(options, "config", None) is not None:
        options.configs.append(options.config)

    config = Config.empty(**vars(options))

    try:
        command = options.command
        if not callable(command):
            command = getattr(
                importlib.import_module(command.rsplit(".", 1)[0]),
                command.rsplit(".", 1)[-1],
            )

        # Set the process name to something cleaner
        process_name = [os.path.basename(sys.argv[0])]
        process_name.extend(sys.argv[1:])
        setproctitle(" ".join(process_name))
        command(config)
    except Exception:
        if not options.debug:
            raise
        traceback.print_exc()
        pdb.post_mortem(sys.exc_info()[-1])


# Traceback (AttributeError)
$ custodian
Traceback (most recent call last):
File "/Users/kapil/projects/cloud-custodian/bin/custodian", line 11, in <module>
load_entry_point('c7n', 'console_scripts', 'custodian')()
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 335, in main
_setup_logger(options)
File "/Users/kapil/projects/cloud-custodian/c7n/cli.py", line 301, in _setup_logger
level = 3 + (options.verbose or 0) - (options.quiet or 0)
AttributeError: 'Namespace' object has no attribute 'verbose'

# Fixed code
def main():
    parser = setup_parser()
    argcomplete.autocomplete(parser)
    options = parser.parse_args()
    if options.subparser is None:
        parser.print_help(file=sys.stderr)
        return sys.exit(2)

    _setup_logger(options)

    # Support the deprecated -c option
    if getattr(options, "config", None) is not None:
        options.configs.append(options.config)

    config = Config.empty(**vars(options))

    try:
        command = options.command
        if not callable(command):
            command = getattr(
                importlib.import_module(command.rsplit(".", 1)[0]),
                command.rsplit(".", 1)[-1],
            )

        # Set the process name to something cleaner
        process_name = [os.path.basename(sys.argv[0])]
        process_name.extend(sys.argv[1:])
        setproctitle(" ".join(process_name))
        command(config)
    except Exception:
        if not options.debug:
            raise
        traceback.print_exc()
        pdb.post_mortem(sys.exc_info()[-1])