Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ config:
description: |
Enforce users to set up and use multi factor authentication.
Disabling this option will allow users to log in with password or webauthn without completing 2fa.
This option is ignored when multitenancy is enabled in the login UI (`byosso_enabled`).
type: boolean
default: True
enable_verification:
description: |
Enable and enforce users to verify their accounts.
Enabling this option will make users verify their accounts after registration or on the next login.
This option requires an SMTP server integration.
This option must be enabled when multitenancy is enabled in the login UI (`byosso_enabled`).
type: boolean
default: False
sender_email:
Expand All @@ -163,6 +165,7 @@ config:
description: |
Enforce setting up a WebAuthn key (e.g. with YubiKey or Google Password Manager on Android)
after signing in with an external identity provider. Requires `enable_passwordless_login_method=False`.
This option must be disabled when multitenancy is enabled in the login UI (`byosso_enabled`).
WARNING: Do not enable this option unless you are sure that this feature applies to your deployment.
type: boolean
default: False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def some_event_function():

# 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

RELATION_NAME = "ui-endpoint-info"
INTERFACE_NAME = "login_ui_endpoints"
Expand All @@ -74,6 +74,8 @@ class LoginUIProviderData(BaseModel):
webauthn_settings_url: Optional[str] = None
account_linking_settings_url: Optional[str] = None
verification_url: Optional[str] = None
# "true" when the login UI runs "Bring Your Own SSO" and decides itself when MFA is required.
byosso_enabled: Optional[str] = None


class LoginUIEndpointsRelationReadyEvent(EventBase):
Expand Down
5 changes: 4 additions & 1 deletion lib/charms/kratos/v0/kratos_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def some_event_function():

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

RELATION_NAME = "kratos-info"
INTERFACE_NAME = "kratos_info"
Expand Down Expand Up @@ -100,6 +100,7 @@ def send_info_relation_data(
oidc_webauthn_sequencing_enabled: bool,
verification_enabled: bool,
feature_flags: Optional[list[str]] = None,
privileged_session_max_age: Optional[str] = None,
) -> None:
"""Updates relation with endpoints, config and configmaps info."""
if not self._charm.unit.is_leader():
Expand All @@ -123,6 +124,8 @@ def send_info_relation_data(

if feature_flags:
info_databag["feature_flags"] = ",".join(feature_flags)
if privileged_session_max_age:
info_databag["privileged_session_max_age"] = privileged_session_max_age

for relation in relations:
relation.data[self._charm.app].update(info_databag)
Expand Down
16 changes: 16 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
LOGIN_WEBHOOK_INTEGRATION_NAME,
PEBBLE_READY_CHECK_NAME,
PEER_INTEGRATION_NAME,
PRIVILEGED_SESSION_MAX_AGE,
PROMETHEUS_SCRAPE_INTEGRATION_NAME,
PUBLIC_ROUTE_INTEGRATION_NAME,
REGISTRATION_WEBHOOK_INTEGRATION_NAME,
Expand Down Expand Up @@ -169,6 +170,7 @@
dict_to_action_output,
external_hostname_is_ready,
migration_is_ready,
multitenancy_config_is_valid,
passwordless_config_is_valid,
peer_integration_exists,
public_route_is_ready,
Expand Down Expand Up @@ -422,6 +424,7 @@ def __init__(self, *args: Any) -> None:
@property
def _pebble_layer(self) -> Layer:
return self._pebble_service.render_pebble_layer(
LoginUIEndpointData.load(self.login_ui_requirer),
self.charm_config,
self.secrets,
DatabaseConfig.load(self.database_requirer),
Expand Down Expand Up @@ -568,6 +571,14 @@ def _on_collect_status(self, event: CollectStatusEvent) -> None:
)
)

if not multitenancy_config_is_valid(self):
event.add_status(
BlockedStatus(
"Multitenancy requires `enable_verification=True` and "
"`enable_oidc_webauthn_sequencing=False`"
)
)

event.add_status(self.resources_patch.get_status())

if can_connect and self._workload_service.is_failing():
Expand Down Expand Up @@ -670,6 +681,10 @@ def _on_resource_patch_failed(self, event: K8sResourcePatchFailedEvent) -> None:
logger.error("Failed to patch resource constraints: %s", event.message)

def _on_kratos_info_provider_ready(self, event: RelationEvent) -> None:
# Blocked on this, the charm plans nothing and Kratos runs what was published before.
if not multitenancy_config_is_valid(self):
return

internal_endpoints = InternalRouteData.load(self.internal_route)
providers_configmap_name = self.providers_configmap.name
schemas_configmap_name = self.schemas_configmap.name
Expand All @@ -693,6 +708,7 @@ def _on_kratos_info_provider_ready(self, event: RelationEvent) -> None:
oidc_webauthn_sequencing_enabled,
verification_enabled,
active_flags,
privileged_session_max_age=PRIVILEGED_SESSION_MAX_AGE,
)

def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
Expand Down
2 changes: 2 additions & 0 deletions src/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
EMAIL_TEMPLATE_FILE_PATH,
IDENTITY_SCHEMAS_LOCAL_DIR_PATH,
MAPPERS_LOCAL_DIR_PATH,
PRIVILEGED_SESSION_MAX_AGE,
PROVIDERS_CONFIGMAP_FILE_NAME,
VERIFICATION_EMAIL_TEMPLATE_RESOURCE_NAME,
)
Expand Down Expand Up @@ -134,6 +135,7 @@ def to_service_configs(self) -> ServiceConfigs:
"enable_passwordless_login_method": self._config["enable_passwordless_login_method"],
"enable_oidc_webauthn_sequencing": self._config["enable_oidc_webauthn_sequencing"],
"enable_verification": self._config["enable_verification"],
"privileged_session_max_age": PRIVILEGED_SESSION_MAX_AGE,
}


Expand Down
3 changes: 3 additions & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
# Action constants
ALLOWED_MFA_CREDENTIAL_TYPES = ("totp", "lookup_secret", "webauthn")

# Kratos's own default, set explicitly so that kratos-info can publish it
PRIVILEGED_SESSION_MAX_AGE = "1h"

# Secret constants
COOKIE_SECRET_LABEL = "cookie_secret"
COOKIE_SECRET_CONTENT_KEY = "cookiesecret"
Expand Down
13 changes: 12 additions & 1 deletion src/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,23 @@ class LoginUIEndpointData:
account_linking_settings_url: str = ""
registration_url: str = ""
consent_url: str = ""
byosso_enabled: str = ""

@property
def multitenancy(self) -> bool:
"""Whether the login UI runs multitenancy and decides itself when MFA is required."""
return self.byosso_enabled == "true"

def is_ready(self) -> bool:
return all(v for _, v in self.to_service_configs().items())
return all(v for k, v in self.to_service_configs().items() if k != "multitenancy")

def to_env_vars(self) -> EnvVars:
# Sessions that have not completed a second factor have to reach whoami.
return {"SESSION_WHOAMI_REQUIRED_AAL": "aal1"} if self.multitenancy else {}

def to_service_configs(self) -> ServiceConfigs:
return {
"multitenancy": self.multitenancy,
"default_browser_return_url": self.settings_url,
"login_ui_url": self.login_url,
"error_ui_url": self.error_url,
Expand Down
18 changes: 17 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
PUBLIC_ROUTE_INTEGRATION_NAME,
WORKLOAD_CONTAINER,
)
from integrations import PublicRouteData
from integrations import LoginUIEndpointData, PublicRouteData

if TYPE_CHECKING:
from charm import KratosCharm
Expand Down Expand Up @@ -124,6 +124,21 @@ def passwordless_config_is_valid(charm: "KratosCharm") -> bool:
)


def multitenancy_config_is_valid(charm: "KratosCharm") -> bool:
"""Checks the config against the login UI's multitenancy, when it runs it.

The login UI then decides when MFA is required, which OIDC-WebAuthn sequencing would
decide too, and relies on Kratos's verification of the addresses an external identity
provider did not confirm.
"""
if not LoginUIEndpointData.load(charm.login_ui_requirer).multitenancy:
return True

return bool(charm.charm_config["enable_verification"]) and not bool(
charm.charm_config["enable_oidc_webauthn_sequencing"]
)


# Condition failure causes early return without doing anything
NOOP_CONDITIONS: tuple[Condition, ...] = (
peer_integration_exists,
Expand All @@ -134,6 +149,7 @@ def passwordless_config_is_valid(charm: "KratosCharm") -> bool:
external_hostname_is_ready,
public_route_is_ready,
passwordless_config_is_valid,
multitenancy_config_is_valid,
)

# Condition failure causes early return with corresponding event deferred
Expand Down
22 changes: 16 additions & 6 deletions templates/kratos.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ selfservice:
{%- set _empty_flow = {"before": {"hooks": []}, "after": {"hooks": []}} %}
{%- set registration_flow = registration_flow | d(_empty_flow) %}
{%- set login_flow = login_flow | d(_empty_flow) %}
{#- With multitenancy the login UI decides when MFA is required, so TOTP and
recovery codes stay available whatever enforce_mfa and enable_local_idp say. -#}
{%- set _multitenancy = multitenancy | d(False) %}
{%- if error_ui_url or login_ui_url or oidc_providers or _has_reg_hooks or _has_login_hooks %}
flows:
{%- if error_ui_url %}
Expand Down Expand Up @@ -87,8 +90,8 @@ selfservice:
{%- set _need_login_oidc = oidc_providers and (_login_all or _login_after.oidc | d([])) %}
{%- set _need_login_code = enable_local_idp and (_login_all or _login_after.code | d([])) %}
{%- set _need_login_webauthn = (enable_passwordless_login_method or enable_oidc_webauthn_sequencing) and (_login_all or _login_after.webauthn | d([])) %}
{%- set _need_login_totp = (enforce_mfa and enable_local_idp) and (_login_all or _login_after.totp | d([])) %}
{%- set _need_login_lookup = (enable_oidc_webauthn_sequencing or (enforce_mfa and enable_local_idp)) and (_login_all or _login_after.lookup_secret | d([])) %}
{%- set _need_login_totp = (enforce_mfa and enable_local_idp or _multitenancy) and (_login_all or _login_after.totp | d([])) %}
{%- set _need_login_lookup = (enable_oidc_webauthn_sequencing or (enforce_mfa and enable_local_idp) or _multitenancy) and (_login_all or _login_after.lookup_secret | d([])) %}
{%- if _need_login_password or _need_login_oidc or _need_login_code or _need_login_webauthn or _need_login_totp or _need_login_lookup %}
after:
{%- if _need_login_password %}
Expand Down Expand Up @@ -133,6 +136,7 @@ selfservice:
settings:
ui_url: {{ settings_ui_url }}
required_aal: highest_available
privileged_session_max_age: {{ privileged_session_max_age }}
after:
oidc:
default_browser_return_url: {{ account_linking_settings_url }}
Expand Down Expand Up @@ -165,6 +169,9 @@ selfservice:
enabled: True
ui_url: {{ registration_ui_url }}
{%- endif %}
{%- if _multitenancy %}
login_hints: True
{%- endif %}
{%- if _has_reg_hooks and registration_flow.before.hooks %}
before:
hooks:
Expand Down Expand Up @@ -194,6 +201,9 @@ selfservice:
hooks:
{{- render_web_hooks(registration_flow.after.hooks | d([]), " ") }}
{{- render_web_hooks(registration_flow.after.oidc | d([]), " ") }}
{%- if enable_verification and _multitenancy %}
- hook: show_verification_ui
{%- endif %}
- hook: session
{%- endif %}
{%- if _need_reg_webauthn %}
Expand All @@ -216,7 +226,7 @@ selfservice:
{%- endif %}
{%- endif %}
{%- endif %}
{%- if oidc_providers or recovery_ui_url or enable_local_idp and login_ui_url %}
{%- if oidc_providers or recovery_ui_url or enable_local_idp and login_ui_url or _multitenancy %}
methods:
{%- if enable_local_idp %}
profile:
Expand All @@ -243,14 +253,14 @@ selfservice:
enabled: True
config:
haveibeenpwned_enabled: False
{%- if enforce_mfa %}
{%- endif %}
{%- if enforce_mfa and enable_local_idp and login_ui_url or _multitenancy %}
totp:
enabled: True
config:
issuer: Identity Platform
{%- endif %}
{%- endif %}
{%- if enable_oidc_webauthn_sequencing or (enforce_mfa and enable_local_idp) %}
{%- if enable_oidc_webauthn_sequencing or (enforce_mfa and enable_local_idp) or _multitenancy %}
lookup_secret:
enabled: True
{%- endif %}
Expand Down
Loading
Loading