-
Notifications
You must be signed in to change notification settings - Fork 7
Add SAML integration #199
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
Merged
Merged
Add SAML integration #199
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
0004c17
test SAML integration
gtrkiller f8cdbd8
test SAML integration
gtrkiller 11a7762
Merge branch 'main' into saml_integ
gtrkiller 92e0b06
test SAML integration
gtrkiller dbc4c2f
test SAML integration
gtrkiller e27fb4e
test SAML integration
gtrkiller ac488ff
test SAML integration
gtrkiller 531723b
test SAML integration
gtrkiller 0a7bffe
test SAML integration
gtrkiller 723ff67
test SAML integration
gtrkiller f7b7e46
test SAML integration
gtrkiller 0f5e102
Fix SAML integration test (#202)
gtrkiller f63a2c2
follow up with the SAML fix
gtrkiller 8ceb365
Merge branch 'main' into saml_integ
gtrkiller 0008656
pin cosl
gtrkiller c055522
add focal compatibility
gtrkiller d2cf8bb
pin pydantic
gtrkiller 80e8618
pin pydantic
gtrkiller 4c276bc
pin pydantic
gtrkiller 9aa93cc
Merge branch 'main' into saml_integ
gtrkiller 1d42742
address comments
gtrkiller 09ba6f8
address comments
gtrkiller 042f837
address comments
gtrkiller 8422f13
address comments
gtrkiller 83a8089
address comments
gtrkiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,305 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Canonical Ltd. | ||
# Licensed under the Apache2.0. See LICENSE file in charm source for details. | ||
|
||
"""Library to manage the relation data for the SAML Integrator charm. | ||
|
||
This library contains the Requires and Provides classes for handling the relation | ||
between an application and a charm providing the `saml`relation. | ||
It also contains a `SamlRelationData` class to wrap the SAML data that will | ||
be shared via the relation. | ||
|
||
### Requirer Charm | ||
|
||
```python | ||
|
||
from charms.saml_integrator.v0 import SamlDataAvailableEvent, SamlRequires | ||
|
||
class SamlRequirerCharm(ops.CharmBase): | ||
def __init__(self, *args): | ||
super().__init__(*args) | ||
self.saml = saml.SamlRequires(self) | ||
self.framework.observe(self.saml.on.saml_data_available, self._handler) | ||
... | ||
|
||
def _handler(self, events: SamlDataAvailableEvent) -> None: | ||
... | ||
|
||
``` | ||
|
||
As shown above, the library provides a custom event to handle the scenario in | ||
which new SAML data has been added or updated. | ||
|
||
### Provider Charm | ||
|
||
Following the previous example, this is an example of the provider charm. | ||
|
||
```python | ||
from charms.saml_integrator.v0 import SamlDataAvailableEvent, SamlRequires | ||
|
||
class SamlRequirerCharm(ops.CharmBase): | ||
def __init__(self, *args): | ||
super().__init__(*args) | ||
self.saml = SamlRequires(self) | ||
self.framework.observe(self.saml.on.saml_data_available, self._on_saml_data_available) | ||
... | ||
|
||
def _on_saml_data_available(self, events: SamlDataAvailableEvent) -> None: | ||
... | ||
|
||
def __init__(self, *args): | ||
super().__init__(*args) | ||
self.saml = SamlProvides(self) | ||
|
||
``` | ||
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. | ||
""" | ||
|
||
# The unique Charmhub library identifier, never change it | ||
LIBID = "511cdfa7de3d43568bf9b512f9c9f89d" | ||
|
||
# Increment this major API version when introducing breaking changes | ||
LIBAPI = 0 | ||
|
||
# Increment this PATCH version before using `charmcraft publish-lib` or reset | ||
# to 0 if you are raising the major API version | ||
LIBPATCH = 5 | ||
|
||
# pylint: disable=wrong-import-position | ||
import re | ||
import typing | ||
|
||
import ops | ||
from pydantic import AnyHttpUrl, BaseModel, Field | ||
from pydantic.tools import parse_obj_as | ||
|
||
DEFAULT_RELATION_NAME = "saml" | ||
|
||
|
||
class SamlEndpoint(BaseModel): | ||
"""Represent a SAML endpoint. | ||
|
||
Attrs: | ||
name: Endpoint name. | ||
url: Endpoint URL. | ||
binding: Endpoint binding. | ||
response_url: URL to address the response to. | ||
""" | ||
|
||
name: str = Field(..., min_length=1) | ||
url: AnyHttpUrl | ||
binding: str = Field(..., min_length=1) | ||
response_url: typing.Optional[AnyHttpUrl] | ||
|
||
def to_relation_data(self) -> typing.Dict[str, str]: | ||
"""Convert an instance of SamlEndpoint to the relation representation. | ||
|
||
Returns: | ||
Dict containing the representation. | ||
""" | ||
result: typing.Dict[str, str] = {} | ||
# Get the HTTP method from the SAML binding | ||
http_method = self.binding.split(":")[-1].split("-")[-1].lower() | ||
# Transform name into snakecase | ||
lowercase_name = re.sub(r"(?<!^)(?=[A-Z])", "_", self.name).lower() | ||
prefix = f"{lowercase_name}_{http_method}_" | ||
result[f"{prefix}url"] = str(self.url) | ||
result[f"{prefix}binding"] = self.binding | ||
if self.response_url: | ||
result[f"{prefix}response_url"] = str(self.response_url) | ||
return result | ||
|
||
@classmethod | ||
def from_relation_data(cls, relation_data: typing.Dict[str, str]) -> "SamlEndpoint": | ||
"""Initialize a new instance of the SamlEndpoint class from the relation data. | ||
|
||
Args: | ||
relation_data: the relation data. | ||
|
||
Returns: | ||
A SamlEndpoint instance. | ||
""" | ||
url_key = "" | ||
for key in relation_data: | ||
# A key per method and entpoint type that is always present | ||
if key.endswith("_redirect_url") or key.endswith("_post_url"): | ||
url_key = key | ||
# Get endpoint name from the relation data key | ||
lowercase_name = "_".join(url_key.split("_")[:-2]) | ||
name = "".join(x.capitalize() for x in lowercase_name.split("_")) | ||
# Get HTTP method from the relation data key | ||
http_method = url_key.split("_")[-2] | ||
prefix = f"{lowercase_name}_{http_method}_" | ||
return cls( | ||
name=name, | ||
url=parse_obj_as(AnyHttpUrl, relation_data[f"{prefix}url"]), | ||
binding=relation_data[f"{prefix}binding"], | ||
response_url=( | ||
parse_obj_as(AnyHttpUrl, relation_data[f"{prefix}response_url"]) | ||
if f"{prefix}response_url" in relation_data | ||
else None | ||
), | ||
) | ||
|
||
|
||
class SamlRelationData(BaseModel): | ||
"""Represent the relation data. | ||
|
||
Attrs: | ||
entity_id: SAML entity ID. | ||
metadata_url: URL to the metadata. | ||
certificates: List of SAML certificates. | ||
endpoints: List of SAML endpoints. | ||
""" | ||
|
||
entity_id: str = Field(..., min_length=1) | ||
metadata_url: AnyHttpUrl | ||
certificates: typing.List[str] | ||
endpoints: typing.List[SamlEndpoint] | ||
|
||
def to_relation_data(self) -> typing.Dict[str, str]: | ||
"""Convert an instance of SamlDataAvailableEvent to the relation representation. | ||
|
||
Returns: | ||
Dict containing the representation. | ||
""" | ||
result = { | ||
"entity_id": self.entity_id, | ||
"metadata_url": str(self.metadata_url), | ||
"x509certs": ",".join(self.certificates), | ||
} | ||
for endpoint in self.endpoints: | ||
result.update(endpoint.to_relation_data()) | ||
return result | ||
|
||
|
||
class SamlDataAvailableEvent(ops.RelationEvent): | ||
"""Saml event emitted when relation data has changed. | ||
|
||
Attrs: | ||
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 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") | ||
|
||
@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")) | ||
|
||
@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(",")) | ||
|
||
@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) | ||
|
||
|
||
class SamlRequiresEvents(ops.CharmEvents): | ||
"""SAML events. | ||
|
||
This class defines the events that a SAML requirer can emit. | ||
|
||
Attrs: | ||
saml_data_available: the SamlDataAvailableEvent. | ||
""" | ||
|
||
saml_data_available = ops.EventSource(SamlDataAvailableEvent) | ||
|
||
|
||
class SamlRequires(ops.Object): | ||
"""Requirer side of the SAML relation. | ||
|
||
Attrs: | ||
on: events the provider can emit. | ||
""" | ||
|
||
on = SamlRequiresEvents() | ||
|
||
def __init__(self, charm: ops.CharmBase, relation_name: str = DEFAULT_RELATION_NAME) -> None: | ||
"""Construct. | ||
|
||
Args: | ||
charm: the provider charm. | ||
relation_name: the relation name. | ||
""" | ||
super().__init__(charm, relation_name) | ||
self.charm = charm | ||
self.relation_name = relation_name | ||
self.framework.observe(charm.on[relation_name].relation_changed, self._on_relation_changed) | ||
|
||
def _on_relation_changed(self, event: ops.RelationChangedEvent) -> None: | ||
"""Event emitted when the relation has changed. | ||
|
||
Args: | ||
event: event triggering this handler. | ||
""" | ||
assert event.relation.app | ||
if event.relation.data[event.relation.app]: | ||
self.on.saml_data_available.emit(event.relation, app=event.app, unit=event.unit) | ||
|
||
|
||
class SamlProvides(ops.Object): | ||
"""Provider side of the SAML relation. | ||
|
||
Attrs: | ||
relations: list of charm relations. | ||
""" | ||
|
||
def __init__(self, charm: ops.CharmBase, relation_name: str = DEFAULT_RELATION_NAME) -> None: | ||
"""Construct. | ||
|
||
Args: | ||
charm: the provider charm. | ||
relation_name: the relation name. | ||
""" | ||
super().__init__(charm, relation_name) | ||
self.charm = charm | ||
self.relation_name = relation_name | ||
|
||
@property | ||
def relations(self) -> typing.List[ops.Relation]: | ||
"""The list of Relation instances associated with this relation_name. | ||
|
||
Returns: | ||
List of relations to this charm. | ||
""" | ||
return list(self.model.relations[self.relation_name]) | ||
|
||
def update_relation_data(self, relation: ops.Relation, saml_data: SamlRelationData) -> None: | ||
"""Update the relation data. | ||
|
||
Args: | ||
relation: the relation for which to update the data. | ||
saml_data: a SamlRelationData instance wrapping the data to be updated. | ||
""" | ||
relation.data[self.charm.model.app].update(saml_data.to_relation_data()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
ops-lib-pgsql | ||
pydantic==1.10.14 | ||
gtrkiller marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.