From d5f5873e74455fcca9c4392cf4493c3c6d6d149f Mon Sep 17 00:00:00 2001 From: Daniele Manni Date: Thu, 19 Mar 2020 11:37:02 +0100 Subject: [PATCH] [#171839291] Refresh of IDP Metadata on interval (#609) --- README.md | 1 + src/app.ts | 17 +++++++++++++++++ src/config.ts | 11 +++++++++++ 3 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 41e8231af..3aa06c59c 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,7 @@ Those are all Environment variables needed by the application: | IDP_METADATA_URL | Url to download SPID IDPs metadata from | string | | SPID_TESTENV_URL | Url to SPID Testenv 2 | string | | SPID_VALIDATOR_URL | Url to SPID Validator | string | +| IDP_METADATA_REFRESH_INTERVAL_SECONDS | The number of seconds when the IDPs Metadata are refreshed | int | | CACHE_MAX_AGE_SECONDS | The value in seconds for duration of in-memory api cache | int | | APICACHE_DEBUG | When is `true` enable the apicache debug mode | boolean | | ALLOW_MULTIPLE_SESSIONS | When is `true` allow multiple sessions for an user (default `false`) | boolean | diff --git a/src/app.ts b/src/app.ts index 396e54ead..75d910700 100644 --- a/src/app.ts +++ b/src/app.ts @@ -10,6 +10,7 @@ import { endpointOrConnectionString, getClientProfileRedirectionUrl, hubName, + IDP_METADATA_REFRESH_INTERVAL_SECONDS, PAGOPA_CLIENT, REDIS_CLIENT, samlConfig, @@ -254,6 +255,22 @@ export function newApp( IDP_METADATA_CONTROLLER ) ); + return _; + }) + .map(_ => { + // Schedule automatic idpMetadataRefresher + const startIdpMetadataRefreshTimer = setInterval( + () => + _.idpMetadataRefresher() + .run() + .catch(e => { + log.error("loadSpidStrategyOptions|error:%s", e); + }), + IDP_METADATA_REFRESH_INTERVAL_SECONDS * 1000 + ); + _.app.on("server:stop", () => + clearInterval(startIdpMetadataRefreshTimer) + ); return _.app; }) .map(_ => { diff --git a/src/config.ts b/src/config.ts index 6af263d09..a183edc95 100644 --- a/src/config.ts +++ b/src/config.ts @@ -142,6 +142,17 @@ const maybeSpidTestenvOption = fromNullable(SPID_TESTENV_URL).map(_ => ({ [_]: true })); +// Set default idp metadata refresh time to 7 days +export const DEFAULT_IDP_METADATA_REFRESH_INTERVAL_SECONDS = 3600 * 24 * 7; +export const IDP_METADATA_REFRESH_INTERVAL_SECONDS: number = process.env + .IDP_METADATA_REFRESH_INTERVAL_SECONDS + ? parseInt(process.env.IDP_METADATA_REFRESH_INTERVAL_SECONDS, 10) + : DEFAULT_IDP_METADATA_REFRESH_INTERVAL_SECONDS; +log.info( + "IDP metadata refresh interval set to %s seconds", + IDP_METADATA_REFRESH_INTERVAL_SECONDS +); + export const serviceProviderConfig: IServiceProviderConfig = { IDPMetadataUrl: IDP_METADATA_URL, organization: {