Skip to content

Commit

Permalink
fix: disallow empty hosts on _UriData and fetch-libs on build
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Dec 20, 2024
1 parent d5e0542 commit 9ff36a8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions .jujuignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/
1 change: 1 addition & 0 deletions charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ parts:
charm-requirements: ["requirements.txt"]
override-build: |
just requirements
charmcraft fetch-libs
craftctl default
charm-libs:
Expand Down
25 changes: 17 additions & 8 deletions lib/charms/filesystem_client/v0/filesystem_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _on_start(self, event: ops.StartEvent) -> None:

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 1
LIBPATCH = 2

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -217,12 +217,17 @@ def __init__(
) -> None:
if not scheme:
raise FilesystemInfoError("scheme cannot be empty")
if len(hosts) == 0:
str_hosts = []
for host in hosts:
if not host:
raise FilesystemInfoError(f"invalid host `{host}`")
str_hosts.append(str(host))
if len(str_hosts) == 0:
raise FilesystemInfoError("list of hosts cannot be empty")

# Strictly convert to the required types to avoid passing through weird data.
object.__setattr__(self, "scheme", str(scheme))
object.__setattr__(self, "hosts", [str(host) for host in hosts])
object.__setattr__(self, "hosts", str_hosts)
object.__setattr__(self, "user", str(user) if user else "")
object.__setattr__(self, "path", str(path) if path else "/")
object.__setattr__(
Expand All @@ -245,10 +250,14 @@ def from_uri(cls, uri: str) -> "_UriData":
hosts = hostname[1:-1].split(",")
path = unquote(result.path or "")
try:
options = {
key: ",".join(values)
for key, values in parse_qs(result.query, strict_parsing=True).items()
}
options = (
{
key: ",".join(values)
for key, values in parse_qs(result.query, strict_parsing=True).items()
}
if result.query
else {}
)
except ValueError:
raise ParseUriError(f"invalid options for endpoint `{uri}`")
try:
Expand Down Expand Up @@ -378,7 +387,7 @@ def to_uri(self, _model: Model) -> str:
except AddressValueError:
host = self.hostname

hosts = [host + f":{self.port}" if self.port else ""]
hosts = [host + (f":{self.port}" if self.port else "")]

return str(_UriData(scheme=self.filesystem_type(), hosts=hosts, path=self.path))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _on_start(self, event: ops.StartEvent) -> None:

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 1
LIBPATCH = 2

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -217,12 +217,17 @@ def __init__(
) -> None:
if not scheme:
raise FilesystemInfoError("scheme cannot be empty")
if len(hosts) == 0:
str_hosts = []
for host in hosts:
if not host:
raise FilesystemInfoError(f"invalid host `{host}`")
str_hosts.append(str(host))
if len(str_hosts) == 0:
raise FilesystemInfoError("list of hosts cannot be empty")

# Strictly convert to the required types to avoid passing through weird data.
object.__setattr__(self, "scheme", str(scheme))
object.__setattr__(self, "hosts", [str(host) for host in hosts])
object.__setattr__(self, "hosts", str_hosts)
object.__setattr__(self, "user", str(user) if user else "")
object.__setattr__(self, "path", str(path) if path else "/")
object.__setattr__(
Expand All @@ -245,10 +250,14 @@ def from_uri(cls, uri: str) -> "_UriData":
hosts = hostname[1:-1].split(",")
path = unquote(result.path or "")
try:
options = {
key: ",".join(values)
for key, values in parse_qs(result.query, strict_parsing=True).items()
}
options = (
{
key: ",".join(values)
for key, values in parse_qs(result.query, strict_parsing=True).items()
}
if result.query
else {}
)
except ValueError:
raise ParseUriError(f"invalid options for endpoint `{uri}`")
try:
Expand Down Expand Up @@ -378,7 +387,7 @@ def to_uri(self, _model: Model) -> str:
except AddressValueError:
host = self.hostname

hosts = [host + f":{self.port}" if self.port else ""]
hosts = [host + (f":{self.port}" if self.port else "")]

return str(_UriData(scheme=self.filesystem_type(), hosts=hosts, path=self.path))

Expand Down

0 comments on commit 9ff36a8

Please sign in to comment.