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

fix: Switch from MAC address stored in Nautobot asset_tag to custom field #502

Merged
merged 3 commits into from
Nov 21, 2024

Conversation

stevekeay
Copy link
Contributor

The custom fields have been added to ansible, and to the dev and staging environments, and in those environments the data has been copied from the asset_tag field to the new custom field.

We had to change how we do the graphql filter - you could search in Nautobot's built-in string attributes by passing an array of possible values to find, but that doesn't work for custom fields.

@stevekeay stevekeay changed the title Switch from MAC address stored in Nautobot asset_tag to custom field fix: Switch from MAC address stored in Nautobot asset_tag to custom field Nov 20, 2024
Copy link
Collaborator

@skrobul skrobul left a comment

Choose a reason for hiding this comment

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

please rebase to pass CI.

also - minor suggestion:

Comment on lines 150 to 160
formatted_list = json.dumps(list(mac_addresses))
pattern = "|".join(mac_addresses)

query = (
"""{
devices(asset_tag: %s){
devices(cf_chassis_mac_address__re: "(%s)"){
id name
mac: asset_tag
mac: cf_chassis_mac_address
location { id name }
rack { id name }
}
}"""
% formatted_list
% pattern
Copy link
Collaborator

@skrobul skrobul Nov 20, 2024

Choose a reason for hiding this comment

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

string interpolation creates the GraphQL injection vulnerability. For example, one could say

mac_addresses={"a.*", "dummy_input\") { devices { id passwords } } #"}

Consider replacing this with query variables (untested):

query = """
query($pattern: String!) {
    devices(cf_chassis_mac_address__re: $pattern) {
        id name
        mac: cf_chassis_mac_address
        location { id name }
        rack { id name }
    }
}
"""

variables = {"pattern": "|".join(mac_addresses)}
result = nautobot.graphql.query(query, variables=variables)

@stevekeay
Copy link
Contributor Author

I updated all the queries to avoid suspicion due to string interpolation.

Steve Keay added 3 commits November 20, 2024 17:24
We have to change the graphql filter - you could search in Nautobot's
built-in string attributes by passing an array of possible values to
find, but that doesn't work for custom fields.
Learn the lessons of the 1990s and use "variables", as God intended.
Use the "variables" feature to avoid string interpolation in making
these queries.
@cardoe cardoe added this pull request to the merge queue Nov 21, 2024
Merged via the queue into main with commit 0a9510f Nov 21, 2024
25 checks passed
@cardoe cardoe deleted the mac-addr-custom-field branch November 21, 2024 06:34
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.

3 participants