From 4ddd4e7f88756459e8a284d847e228e9a588f8a0 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:38:09 +0200 Subject: [PATCH] outposts: make refresh interval configurable (cherry-pick #10138) (#10700) * outposts: make refresh interval configurable (#10138) * outposts: make refresh interval configurable Signed-off-by: Marc 'risson' Schmitt * frontend Signed-off-by: Marc 'risson' Schmitt * black again Signed-off-by: Marc 'risson' Schmitt * switch to using config attribute Signed-off-by: Marc 'risson' Schmitt * lint Signed-off-by: Marc 'risson' Schmitt --------- Signed-off-by: Marc 'risson' Schmitt * bump api Signed-off-by: Jens Langhammer --------- Signed-off-by: Marc 'risson' Schmitt Signed-off-by: Jens Langhammer Co-authored-by: Marc 'risson' Schmitt Co-authored-by: Jens Langhammer --- authentik/outposts/api/outposts.py | 9 ++++++++- authentik/outposts/models.py | 1 + go.mod | 2 +- go.sum | 4 ++-- internal/outpost/ak/api_ws.go | 3 ++- schema.yml | 4 ++++ website/docs/outposts/_config.md | 5 +++++ 7 files changed, 23 insertions(+), 5 deletions(-) diff --git a/authentik/outposts/api/outposts.py b/authentik/outposts/api/outposts.py index 2106a0f4d9..a79f48f883 100644 --- a/authentik/outposts/api/outposts.py +++ b/authentik/outposts/api/outposts.py @@ -20,6 +20,7 @@ from authentik.core.api.utils import JSONDictField, ModelSerializer, PassiveSeri from authentik.core.models import Provider from authentik.enterprise.license import LicenseKey from authentik.enterprise.providers.rac.models import RACProvider +from authentik.lib.utils.time import timedelta_from_string, timedelta_string_validator from authentik.outposts.api.service_connections import ServiceConnectionSerializer from authentik.outposts.apps import MANAGED_OUTPOST, MANAGED_OUTPOST_NAME from authentik.outposts.models import ( @@ -49,6 +50,10 @@ class OutpostSerializer(ModelSerializer): service_connection_obj = ServiceConnectionSerializer( source="service_connection", read_only=True ) + refresh_interval_s = SerializerMethodField() + + def get_refresh_interval_s(self, obj: Outpost) -> int: + return int(timedelta_from_string(obj.config.refresh_interval).total_seconds()) def validate_name(self, name: str) -> str: """Validate name (especially for embedded outpost)""" @@ -84,7 +89,8 @@ class OutpostSerializer(ModelSerializer): def validate_config(self, config) -> dict: """Check that the config has all required fields""" try: - from_dict(OutpostConfig, config) + parsed = from_dict(OutpostConfig, config) + timedelta_string_validator(parsed.refresh_interval) except DaciteError as exc: raise ValidationError(f"Failed to validate config: {str(exc)}") from exc return config @@ -99,6 +105,7 @@ class OutpostSerializer(ModelSerializer): "providers_obj", "service_connection", "service_connection_obj", + "refresh_interval_s", "token_identifier", "config", "managed", diff --git a/authentik/outposts/models.py b/authentik/outposts/models.py index 1a10e11c3d..327d23ca46 100644 --- a/authentik/outposts/models.py +++ b/authentik/outposts/models.py @@ -61,6 +61,7 @@ class OutpostConfig: log_level: str = CONFIG.get("log_level") object_naming_template: str = field(default="ak-outpost-%(name)s") + refresh_interval: str = "minutes=5" container_image: str | None = field(default=None) diff --git a/go.mod b/go.mod index 3a2864567a..ba583840ac 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 github.com/wwt/guac v1.3.2 - goauthentik.io/api/v3 v3.2024042.11 + goauthentik.io/api/v3 v3.2024060.5 golang.org/x/exp v0.0.0-20230210204819-062eb4c674ab golang.org/x/oauth2 v0.21.0 golang.org/x/sync v0.7.0 diff --git a/go.sum b/go.sum index eb8647a959..da916fe71b 100644 --- a/go.sum +++ b/go.sum @@ -294,8 +294,8 @@ go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= -goauthentik.io/api/v3 v3.2024042.11 h1:cGgUz1E8rlMphGvv04VI7i+MgT8eidZbxTpza5zd96I= -goauthentik.io/api/v3 v3.2024042.11/go.mod h1:zz+mEZg8rY/7eEjkMGWJ2DnGqk+zqxuybGCGrR2O4Kw= +goauthentik.io/api/v3 v3.2024060.5 h1:AjvPUZoObk7a86ZZaz2tmruteY+1vAEfVzIOzQpWSXM= +goauthentik.io/api/v3 v3.2024060.5/go.mod h1:zz+mEZg8rY/7eEjkMGWJ2DnGqk+zqxuybGCGrR2O4Kw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= diff --git a/internal/outpost/ak/api_ws.go b/internal/outpost/ak/api_ws.go index c48cebba37..4b2f1f8c4f 100644 --- a/internal/outpost/ak/api_ws.go +++ b/internal/outpost/ak/api_ws.go @@ -183,7 +183,7 @@ func (ac *APIController) startWSHealth() { func (ac *APIController) startIntervalUpdater() { logger := ac.logger.WithField("loop", "interval-updater") - ticker := time.NewTicker(5 * time.Minute) + ticker := time.NewTicker(time.Duration(ac.Outpost.RefreshIntervalS) * time.Second) for ; true; <-ticker.C { logger.Debug("Running interval update") err := ac.OnRefresh() @@ -198,6 +198,7 @@ func (ac *APIController) startIntervalUpdater() { "build": constants.BUILD("tagged"), }).SetToCurrentTime() } + ticker.Reset(time.Duration(ac.Outpost.RefreshIntervalS) * time.Second) } } diff --git a/schema.yml b/schema.yml index b8e58f7bbf..c5a45bf2d4 100644 --- a/schema.yml +++ b/schema.yml @@ -39498,6 +39498,9 @@ components: allOf: - $ref: '#/components/schemas/ServiceConnection' readOnly: true + refresh_interval_s: + type: integer + readOnly: true token_identifier: type: string description: Get Token identifier @@ -39519,6 +39522,7 @@ components: - pk - providers - providers_obj + - refresh_interval_s - service_connection_obj - token_identifier - type diff --git a/website/docs/outposts/_config.md b/website/docs/outposts/_config.md index 97b3dd52fa..7be85dfab0 100644 --- a/website/docs/outposts/_config.md +++ b/website/docs/outposts/_config.md @@ -3,6 +3,11 @@ # Allowed levels: trace, debug, info, warning, error # Applies to: non-embedded log_level: debug +# Interval at which the outpost will refresh the providers +# from authentik. For caching outposts (such as LDAP), the +# cache will also be invalidated at that interval. +# (Format: hours=1;minutes=2;seconds=3). +refresh_interval: minutes=5 ######################################## # The settings below are only relevant when using a managed outpost ########################################