duo-labs / cloudmapper

CloudMapper helps you analyze your Amazon Web Services (AWS) environments.
BSD 3-Clause "New" or "Revised" License
5.98k stars 803 forks source link

find_unused KeyError #617

Open maestretti opened 4 years ago

maestretti commented 4 years ago
(cloudmapper) root@f9efb2165ca6:/opt/cloudmapper# python cloudmapper.py find_unused --account YOUR_ACCOUNT
Traceback (most recent call last):
  File "cloudmapper.py", line 72, in <module>
    main()
  File "cloudmapper.py", line 66, in main
    commands[command].run(arguments)
  File "/opt/cloudmapper/commands/find_unused.py", line 13, in run
    unused_resources = find_unused_resources(accounts)
  File "/opt/cloudmapper/shared/find_unused.py", line 123, in find_unused_resources
    find_unused_elastic_ips(region),
  File "/opt/cloudmapper/shared/find_unused.py", line 70, in find_unused_elastic_ips
    unused_ips.append({"id": ip["AllocationId"], "ip": ip["PublicIp"]})
KeyError: 'AllocationId'
0xdabbad00 commented 4 years ago

Can you see look at the ip resource at https://github.com/duo-labs/cloudmapper/blob/1e60e1fb0bf5ad71d8514c2f5a3a2696b130f4cc/shared/find_unused.py#L70?

Something like print(ip) or print(json.dumps(ip, indent=2)) should be helpful.

I'll admit that command is barely used. It was a one off thing that a single client wanted.

maestretti commented 4 years ago

ya 'won't fix' is a fair response, or maybe a test for the key and skip gracefully if it isn't there. Wasn't sure if AWS changed a name somewhere.

0xdabbad00 commented 4 years ago

I'll fix it or accept a PR if you can identify the specific issue. It looks like maybe the ip object doesn't have an AllocationId in it. That's unexpected to me, so I'm not sure what the appropriate action should be in that case. One option is to just ignore it, but I'd prefer to have more info about why it wasn't there.

maestretti commented 4 years ago

Looking at the ec2-describe-addresses file the first two records are for Domain objects which don't have the keys being looked for, the rest are AllocationID not sure if this filter is skipping the Domain objects properly:

https://github.com/duo-labs/cloudmapper/blob/1e60e1fb0bf5ad71d8514c2f5a3a2696b130f4cc/shared/find_unused.py#L69

As I don't know pyjq very.