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(machines): Allow ports at the end of IPMI addresses LP#2087965 #5560

Merged
merged 2 commits into from
Dec 10, 2024

Conversation

ndv99
Copy link
Contributor

@ndv99 ndv99 commented Dec 6, 2024

Done

  • Added extra validation steps to IP address power field to account for ports
  • Removed IP validation from LXD and Virsh address fields as these can be hostnames or other non-IP-address things.

QA steps

  • Go to a machine with an IPMI power type
  • Go to "Configuration" and edit the power parameters
  • Enter an IPv4 address with a port and tab out of the field afterwards
  • Ensure no errors are shown
  • Enter an invalid IP address and tab out of the field afterwards
  • Ensure an error is shown
  • Enter an IPv6 address with a port (you'll need to wrap the address in square brackets, e.g. [2008:d45::1]:8000
  • Tab out of the field
  • Ensure no errors are shown

Fixes

Fixes LP#2087965
Fixes MAASENG-4172

@webteam-app
Copy link

Copy link
Contributor

@r00ta r00ta left a comment

Choose a reason for hiding this comment

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

LGTM (up to my knowledge of react/ts :) )

@ndv99 ndv99 marked this pull request as ready for review December 6, 2024 14:35
Copy link
Collaborator

@tmerten tmerten left a comment

Choose a reason for hiding this comment

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

QA: Looking good, changed a machine, tried multiple V4 and V6 addresses with and without port.

Code: small edge case nag inline below

);
// We use +2 here to include the `:` before the port number
const port = value.slice(closingBracketIndex + 2);
return isIPv6(ip) && !isNaN(parseInt(port));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This usually works but insane users like myself might try: [aa:cf:f::34fF]: 4 5 6. Results in a parseInt(" 4 5 6") which is 4. I think it's still OK to merge for now but we might want to make this even tighter later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's interesting! By the looks of it, parseInt ignores any leading whitespace, and ignores anything once it reaches the first whitespace after a number (only if the number is also the first non-whitespace text in the string).

I guess the simplest check for this would just be to check for any whitespace in the input, and treat the value as invalid if there is whitespace.

} else if (value.split(":").length === 2) {
// This is an IPv4 address with a port number
const [ip, port] = value.split(":");
return isIP(ip) && !isNaN(parseInt(port));
Copy link
Collaborator

Choose a reason for hiding this comment

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

as above. Could error out if the port number has anything but decimals.

Copy link
Collaborator

@tmerten tmerten left a comment

Choose a reason for hiding this comment

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

I really wanted to break it. LTGM.

import { isValidPortNumber } from ".";

it("returns true for any number between 0 and 65535", () => {
for (let i = 0; i <= 65535; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

That is a computationally expensive test for the function it tests.... Edge cases and some common ports might be enough. But I'm happy to merge if it runs quickly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Locally it seems to run quickly enough, but thought I'd get your thoughts first.

* @param port The port number to check.
* @returns True if valid, false otherwise.
*/
export const isValidPortNumber = (port: number): boolean => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like that its port: number :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

me too :)

@ndv99 ndv99 merged commit 847d318 into canonical:main Dec 10, 2024
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants