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: disallow passing empty hosts to _UriData and fetch-libs at build time #6

Merged
merged 1 commit into from
Jan 6, 2025

Conversation

jedel1043
Copy link
Collaborator

Fixes a bug that allowed passing empty host names to _UriData from the NfsInfo class.
Fixes the charmcraft build.

@jedel1043 jedel1043 force-pushed the fix-uri-data branch 2 times, most recently from 691c717 to 9ff36a8 Compare December 20, 2024 22:54
Copy link
Member

@NucciTheBoss NucciTheBoss left a comment

Choose a reason for hiding this comment

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

Looks good overall. I have some questions/suggestions around how we're going about validating the arguments in _UriData. Let me know if you have any questions!

Comment on lines 221 to 223
for host in hosts:
if not host:
raise FilesystemInfoError(f"invalid host `{host}`")
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit confused what this is doing here? I'm inclined to believe that we're filtering out falsey conditions such as empty strings "" and None, however, doesn't this mean that integers, floats, and True will be treated as valid hostnames as well? Not sure if this is behavior that we want. If we're just trying to quickly validate what's being passed, we could possibly use jsonschema or pydantic.

Personally pydantic is a bit overkill imho, but jsonschema could solve our problem nicely here:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "scheme": {"type": "string"},
        "hosts": {"type": "array", "items": {"type": "string"}, "uniqueItems": True},
        "user": {"type": "array", "items": {"type": "string"}, "uniqueItems": True},
        "path": {"type": "string"},
        "options": {"type": "object", ...},
    },
    "additionalProperties": False,
}

The initialization arguments should be passed as the correct type and/or structure to _UriData rather than put the onus on us to beat the passed values into the desired format that we want. It makes the constructor much simpler, and requires much less edge case handling.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I really don't want to rely on PYDEPS though, since we don't really know if Charmcraft will decide to deprecate it in the future...

Copy link
Member

Choose a reason for hiding this comment

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

That's fair reasoning for not wanting to use jsonschema directly, but I still believe that we shouldn't carry a bunch of code for performing custom type validation at runtime. I think it's fair for us to expect that the arguments provided when a _UriData object is created are of the correct type. This way we potentially don't need to override the __init__ method for the dataclass. IIRC pyright should ideally catch when a dev is passing a value of the wrong type to _UriData.

If we do still want runtime checking, I still think jsonschema would be useful. I've found that custom validators can get messy quick especially if you don't have tight control over the input data.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree. I'll remove the validation code.

Copy link
Member

@NucciTheBoss NucciTheBoss left a comment

Choose a reason for hiding this comment

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

Continuing our last bit of conversation on the runtime checking for _UriData, but other than that I think this PR looks good!

Comment on lines 221 to 223
for host in hosts:
if not host:
raise FilesystemInfoError(f"invalid host `{host}`")
Copy link
Member

Choose a reason for hiding this comment

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

That's fair reasoning for not wanting to use jsonschema directly, but I still believe that we shouldn't carry a bunch of code for performing custom type validation at runtime. I think it's fair for us to expect that the arguments provided when a _UriData object is created are of the correct type. This way we potentially don't need to override the __init__ method for the dataclass. IIRC pyright should ideally catch when a dev is passing a value of the wrong type to _UriData.

If we do still want runtime checking, I still think jsonschema would be useful. I've found that custom validators can get messy quick especially if you don't have tight control over the input data.

Copy link
Member

@NucciTheBoss NucciTheBoss left a comment

Choose a reason for hiding this comment

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

LGTM! Nice work :shipit:

@NucciTheBoss NucciTheBoss merged commit d13e0af into charmed-hpc:main Jan 6, 2025
5 checks passed
@jedel1043 jedel1043 deleted the fix-uri-data branch January 6, 2025 20:16
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