Skip to content

Fix: prevent cross-project floating IP attach - #5177

Merged
FelixMcFelix merged 6 commits into
mainfrom
felixmcfelix/fip-cross-project-fix
Mar 1, 2024
Merged

FelixMcFelix merged 6 commits into
mainfrom
felixmcfelix/fip-cross-project-fix

Conversation

@FelixMcFelix

@FelixMcFelix FelixMcFelix commented Mar 1, 2024 •

Copy link
Copy Markdown
Contributor

This PR ensures that attached FIPs have the same project ID as their target instance, and prevents double lookup of a FIP in the FIP [name + project] + Instance [ID] attach case.

@david-crespo

Copy link
Copy Markdown
Contributor

A tip: you can set the base of the PR to my branch so the diff will only show your commits, and then when mine merges it will automatically reset the base on this PR to main.

@david-crespo
david-crespo changed the base branch from main to attach-floating-ip-fix March 1, 2024 15:25
Base automatically changed from attach-floating-ip-fix to main March 1, 2024 17:38
Comment thread nexus/src/app/external_ip.rs Outdated
) {
fip_selector.project = project;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this even happen? Is this about the case where floating IP is specified by name but there is no project, but instance is given by ID, so we can infer the project from that, and then we give that project to the floating IP as well?

This is subtle and a preference, but even though we're bending over backwards in my PR to be flexible, I think this bit is actually too accommodating because it allows the user to violate a contract that's enforced for every other endpoint to my knowledge — if you specify the resource by name (the primary resource for the endpoint, in this case the floating IP) you need to include the project. Even though we can technically infer it from the instance ID of the post body, I don't think we should. I think you should get rid of this if and let floating_ip_lookup error. That also has the advantage of letting you get rid of the mut, which freaks me out in general.

But back to "can this happen?" — I think this whole if might be ruled out by the floating_ip_lookup call in the match above — I think it will error there if the floating IP is by name and there's no project.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, that should not be possible (and we should not allow it). The main intent of this PR was initially to catch a double name lookup that could happen on FIP [name+ project] + Instance [ID] with the FIP attach saga, but I mistakenly tunnelled on 'always having a valid FIP selector' while building out the surrounding logic.

I think looking at it now we should make the FIP lookup unconditional outside of the top level match: I expect any other resources will need it, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure, that makes sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This looks considerably cleaner now, thanks!

This PR ensures that attached FIPs have the same project ID as their
target instance, and prevents double lookup of a FIP in some
circumstances.

Depends on #5173.
@FelixMcFelix
FelixMcFelix force-pushed the felixmcfelix/fip-cross-project-fix branch from b9bd8be to 570921b Compare March 1, 2024 18:00
Plumbing through the `authz::FloatingIp` all the way from here makes
this logic considerably cleaner, as it turns out.
@FelixMcFelix
FelixMcFelix marked this pull request as ready for review March 1, 2024 18:20
Comment thread nexus/src/app/external_ip.rs Outdated
(NameOrId::Name(_), None) => {
Some(authz_project.id().into())
}
};

@david-crespo david-crespo Mar 1, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Much cleaner! And actually... we don't even need to match on fip_selector.project anymore — we always know the project ID. And the comment can be shrunk. There's a lot less to explain.

let instance_selector = params::InstanceSelector {
    project: match &target.parent {
        NameOrId::Id(_) => None,
        NameOrId::Name(_) => Some(authz_project.id().into()),
    },
    instance: target.parent,
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fantastic; done.

@david-crespo david-crespo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks great. Since you're already in the zone, how about eliminating this bit where we pass an external API params type into datastore? It looks like it only needs the IP (optional) and the name of the floating IP. Passing the name by itself would be slightly strange — when I tried to fix this I got hung up deciding what the args to the function should be.

params: params::FloatingIpCreate,

@FelixMcFelix

Copy link
Copy Markdown
Contributor Author

This looks great. Since you're already in the zone, how about eliminating this bit where we pass an external API params type into datastore? It looks like it only needs the IP (optional) and the name of the floating IP. Passing the name by itself would be slightly strange — when I tried to fix this I got hung up deciding what the args to the function should be.

params: params::FloatingIpCreate,

Done: I've just destructured the params one layer up. I was a little worried about getting too_many_args'd, but we're in the clear.

@FelixMcFelix
FelixMcFelix enabled auto-merge (squash) March 1, 2024 20:04
@FelixMcFelix
FelixMcFelix merged commit 6c54c82 into main Mar 1, 2024
@FelixMcFelix
FelixMcFelix deleted the felixmcfelix/fip-cross-project-fix branch March 1, 2024 21:30
@FelixMcFelix FelixMcFelix added this to the 7 milestone Mar 6, 2024
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.

2 participants