Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EC2 network ACL: Use get method to access dict keys to prevent exception #1392

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

cmm-lyft
Copy link
Contributor

Summary

Correcting error:

File "/code/venvs/venv/lib/python3.10/site-packages/cartography/intel/aws/ec2/network_acls.py", line 72, in transform_network_acl_data
    'CidrBlock': rule['CidrBlock'],
KeyError: 'CidrBlock'

Checklist

Provide proof that this works (this makes reviews move faster). Please perform one or more of the following:

  • Update/add unit or integration tests.
  • Include a screenshot showing what the graph looked like before and after your changes.
  • Include console log trace showing what happened before and after your changes.

If you are changing a node or relationship:

If you are implementing a new intel module:

@cmm-lyft
Copy link
Contributor Author

👀 @achantavy

@cmm-lyft
Copy link
Contributor Author

👀 @heryxpc @serge-wq @khanhldt

Comment on lines 71 to 78
'Id': f"{network_acl['NetworkAclId']}/{direction}/{rule.get('RuleNumber')}",
'CidrBlock': rule.get('CidrBlock'),
'Egress': rule.get('Egress'),
'Protocol': rule.get('Protocol'),
'RuleAction': rule.get('RuleAction'),
'RuleNumber': rule.get('RuleNumber'),
# Add pointer back to the nacl to create an edge
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switching to get comes with the question: what's the default value? And what's the implication of having such broken / default data in our DB?

I think the data model for this is defined here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-networkaclentry.html

CIDR block is indicated as "Conditional", and it looks like either this field or Ipv6CidrBlock must be present. So we should update our code here to take one or another instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth reviewing the rest of the parameters too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't consider this data as "broken". We trust the incoming data from the AWS ACLs API, considering the optional fields. Any required transformations or enrichments can be performed ad hoc by the client consuming this data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that, not sure if a validator / enrichment function for to consider the edge cases (like CidrBlock not being present) will actually be valuable here. 🤔
WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the question here is whether cartography should provide an accurate representation of the AWS data and its constraints.

If so, then we should adhere to what @kledo-lyft and provide some kind of validation to spot the cases where we don't get any of the two values. If not, then it's fine to just store the data as is and let the cartography consumers think about this.

I think the first option is better. Even though it can mean more maintenance work, consumers can be sure the data is as accurate as possible to AWS and it will also make easier to spot any bug when the data we're storing doesn't follow these constraints.

Copy link
Contributor

@achantavy achantavy Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmm-lyft Sorry for the crash and thanks for working on this. It's annoying that AWS APIs are inconsistent and sometimes leave out entire fields.

I think the data model for this is defined here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-networkaclentry.html

CIDR block is indicated as "Conditional", and it looks like either this field or Ipv6CidrBlock must be present. So we should update our code here to take one or another instead.

@kledo-lyft - good catch. I was referring to https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_NetworkAclEntry.html when I wrote this code. It looks like the cloudformation docs are better.


This fix is simple:

  1. do a .get('CidrBlock').
  2. do a .get('Ipv6CidrBlock').
  3. leave everything else the same (unless we know there are others where the API leaves it out).
  4. update
    cidrblock: PropertyRef = PropertyRef('CidrBlock')
    to have an ipv6cidrblock field

This works because the neo4j python driver treats Nones as if the field does not even exist, so assuming that the aws conditional is correct, our end result is that the node written to the graph will have either cidrblock or ipv6cidrblock but not both.


As seen here, cidrblock and ipv6cidrblock are both not id fields so it's ok for these to be None.

cidrblock: PropertyRef = PropertyRef('CidrBlock')

@cmm-lyft cmm-lyft requested a review from kledo-lyft November 28, 2024 14:14
Comment on lines 71 to 78
'Id': f"{network_acl['NetworkAclId']}/{direction}/{rule.get('RuleNumber')}",
'CidrBlock': rule.get('CidrBlock'),
'Egress': rule.get('Egress'),
'Protocol': rule.get('Protocol'),
'RuleAction': rule.get('RuleAction'),
'RuleNumber': rule.get('RuleNumber'),
# Add pointer back to the nacl to create an edge
Copy link
Contributor

@achantavy achantavy Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmm-lyft Sorry for the crash and thanks for working on this. It's annoying that AWS APIs are inconsistent and sometimes leave out entire fields.

I think the data model for this is defined here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-networkaclentry.html

CIDR block is indicated as "Conditional", and it looks like either this field or Ipv6CidrBlock must be present. So we should update our code here to take one or another instead.

@kledo-lyft - good catch. I was referring to https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_NetworkAclEntry.html when I wrote this code. It looks like the cloudformation docs are better.


This fix is simple:

  1. do a .get('CidrBlock').
  2. do a .get('Ipv6CidrBlock').
  3. leave everything else the same (unless we know there are others where the API leaves it out).
  4. update
    cidrblock: PropertyRef = PropertyRef('CidrBlock')
    to have an ipv6cidrblock field

This works because the neo4j python driver treats Nones as if the field does not even exist, so assuming that the aws conditional is correct, our end result is that the node written to the graph will have either cidrblock or ipv6cidrblock but not both.


As seen here, cidrblock and ipv6cidrblock are both not id fields so it's ok for these to be None.

cidrblock: PropertyRef = PropertyRef('CidrBlock')

@cmm-lyft
Copy link
Contributor Author

cmm-lyft commented Dec 2, 2024

@achantavy done!
Thanks Alex!
#1392 (comment)

@achantavy achantavy changed the title Using get method to access dict keys to prevent exception EC2 network ACL: Use get method to access dict keys to prevent exception Dec 2, 2024
@achantavy achantavy merged commit 4480b90 into cartography-cncf:master Dec 2, 2024
7 checks passed
Comment on lines 23 to 25
cidrblock: PropertyRef = PropertyRef('CidrBlock')
Ipv6CidrBlock: PropertyRef = PropertyRef('Ipv6CidrBlock')
egress: PropertyRef = PropertyRef('Egress')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmm-lyft a bit late to comment but probably more consistent to use lowercase ip. If we haven't cut a release, maybe can still do a quick fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah good catch. lol sorry i got too eager with the approval

@cmm-lyft cmm-lyft mentioned this pull request Dec 4, 2024
5 tasks
cmm-lyft added a commit to cmm-lyft/cartography that referenced this pull request Dec 6, 2024
…ion (cartography-cncf#1392)

### Summary
Correcting error:
```
File "/code/venvs/venv/lib/python3.10/site-packages/cartography/intel/aws/ec2/network_acls.py", line 72, in transform_network_acl_data
    'CidrBlock': rule['CidrBlock'],
KeyError: 'CidrBlock'
```

### Checklist

Provide proof that this works (this makes reviews move faster). Please
perform one or more of the following:
- [ ] Update/add unit or integration tests.
- [ ] Include a screenshot showing what the graph looked like before and
after your changes.
- [ ] Include console log trace showing what happened before and after
your changes.

If you are changing a node or relationship:
- [ ] Update the
[schema](https://github.com/lyft/cartography/tree/master/docs/root/modules)
and
[readme](https://github.com/lyft/cartography/blob/master/docs/schema/README.md).

If you are implementing a new intel module:
- [ ] Use the NodeSchema [data
model](https://cartography-cncf.github.io/cartography/dev/writing-intel-modules.html#defining-a-node).

Signed-off-by: cmm-lyft <[email protected]>
achantavy pushed a commit that referenced this pull request Dec 6, 2024
### Summary
Fixing typo

#1392 (comment)


### Related issues or links
> Include links to relevant issues or other pages.

- https://github.com/lyft/cartography/issues/...


### Checklist

Provide proof that this works (this makes reviews move faster). Please
perform one or more of the following:
- [ ] Update/add unit or integration tests.
- [ ] Include a screenshot showing what the graph looked like before and
after your changes.
- [ ] Include console log trace showing what happened before and after
your changes.

If you are changing a node or relationship:
- [ ] Update the
[schema](https://github.com/lyft/cartography/tree/master/docs/root/modules)
and
[readme](https://github.com/lyft/cartography/blob/master/docs/schema/README.md).

If you are implementing a new intel module:
- [ ] Use the NodeSchema [data
model](https://cartography-cncf.github.io/cartography/dev/writing-intel-modules.html#defining-a-node).

---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants