Skip to content

Commit

Permalink
chore: update charm libraries (#204)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] authored Mar 18, 2024
1 parent cdd64c2 commit 35b737b
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions lib/charms/saml_integrator/v0/saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def __init__(self, *args):
The SamlProvides object wraps the list of relations into a `relations` property
and provides an `update_relation_data` method to update the relation data by passing
a `SamlRelationData` data object.
Additionally, SamlRelationData can be used to directly parse the relation data with the
class method `from_relation_data`.
"""

# The unique Charmhub library identifier, never change it
Expand All @@ -66,7 +68,7 @@ def __init__(self, *args):

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

# pylint: disable=wrong-import-position
import re
Expand Down Expand Up @@ -151,14 +153,14 @@ class SamlRelationData(BaseModel):
Attrs:
entity_id: SAML entity ID.
metadata_url: URL to the metadata.
certificates: List of SAML certificates.
endpoints: List of SAML endpoints.
certificates: Tuple of SAML certificates.
endpoints: Tuple of SAML endpoints.
"""

entity_id: str = Field(..., min_length=1)
metadata_url: AnyHttpUrl
certificates: typing.List[str]
endpoints: typing.List[SamlEndpoint]
certificates: typing.Tuple[str, ...]
endpoints: typing.Tuple[SamlEndpoint, ...]

def to_relation_data(self) -> typing.Dict[str, str]:
"""Convert an instance of SamlDataAvailableEvent to the relation representation.
Expand All @@ -175,53 +177,74 @@ def to_relation_data(self) -> typing.Dict[str, str]:
result.update(endpoint.to_relation_data())
return result

@classmethod
def from_relation_data(cls, relation_data: typing.Dict[str, str]) -> "SamlRelationData":
"""Get a SamlRelationData wrapping the relation data.
Arguments:
relation_data: the relation data.
Returns: a SamlRelationData instance with the relation data.
"""
# mypy is not aware of the relation data being present
endpoints = [
SamlEndpoint.from_relation_data(
{
key2: relation_data.get(key2) # type: ignore
for key2 in relation_data
if key2.startswith("_".join(key.split("_")[:-1]))
}
)
for key in relation_data
if key.endswith("_redirect_url") or key.endswith("_post_url")
]
endpoints.sort(key=lambda ep: ep.name)
return cls(
entity_id=relation_data.get("entity_id"), # type: ignore
metadata_url=parse_obj_as(
AnyHttpUrl, relation_data.get("metadata_url")
), # type: ignore
certificates=tuple(relation_data.get("x509certs").split(",")), # type: ignore
endpoints=tuple(endpoints),
)


class SamlDataAvailableEvent(ops.RelationEvent):
"""Saml event emitted when relation data has changed.
Attrs:
saml_relation_data: the SAML relation data
entity_id: SAML entity ID.
metadata_url: URL to the metadata.
certificates: Tuple containing the SAML certificates.
endpoints: Tuple containing the SAML endpoints.
"""

@property
def saml_relation_data(self) -> SamlRelationData:
"""Get a SamlRelationData for the relation data."""
assert self.relation.app
return SamlRelationData.from_relation_data(self.relation.data[self.relation.app])

@property
def entity_id(self) -> str:
"""Fetch the SAML entity ID from the relation."""
assert self.relation.app
return self.relation.data[self.relation.app].get("entity_id")
return self.saml_relation_data.entity_id

@property
def metadata_url(self) -> str:
"""Fetch the SAML metadata URL from the relation."""
assert self.relation.app
return parse_obj_as(AnyHttpUrl, self.relation.data[self.relation.app].get("metadata_url"))
return str(self.saml_relation_data.metadata_url)

@property
def certificates(self) -> typing.Tuple[str, ...]:
"""Fetch the SAML certificates from the relation."""
assert self.relation.app
return tuple(self.relation.data[self.relation.app].get("x509certs").split(","))
return self.saml_relation_data.certificates

@property
def endpoints(self) -> typing.Tuple[SamlEndpoint, ...]:
"""Fetch the SAML endpoints from the relation."""
assert self.relation.app
relation_data = self.relation.data[self.relation.app]
endpoints = [
SamlEndpoint.from_relation_data(
{
key2: relation_data.get(key2)
for key2 in relation_data
if key2.startswith("_".join(key.split("_")[:-1]))
}
)
for key in relation_data
if key.endswith("_redirect_url") or key.endswith("_post_url")
]
endpoints.sort(key=lambda ep: ep.name)
return tuple(endpoints)
return self.saml_relation_data.endpoints


class SamlRequiresEvents(ops.CharmEvents):
Expand Down

0 comments on commit 35b737b

Please sign in to comment.