From 8d5ac6bfd262660344b58c5d0959711e8b3b062b Mon Sep 17 00:00:00 2001 From: Dreamstar Enterprises <39380005+dreamstar-enterprises@users.noreply.github.com> Date: Mon, 9 Sep 2024 23:15:37 +0100 Subject: [PATCH] Delete Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing directory --- .../frontiers/bff/routing/RoutingConfig.kt | 63 --------------- .../circuitbreaker/CircuitBreakerConfig.kt | 42 ---------- .../deduplicate/DedupeResponseConfig.kt | 52 ------------- .../bff/routing/headers/HeadersConfig.kt | 36 --------- .../bff/routing/ignore/IgnoreFilterConfig.kt | 55 -------------- .../routing/ratelimiter/RateLimiterConfig.kt | 76 ------------------- .../routing/savesession/SaveSessionConfig.kt | 30 -------- 7 files changed, 354 deletions(-) delete mode 100644 Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/RoutingConfig.kt delete mode 100644 Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/circuitbreaker/CircuitBreakerConfig.kt delete mode 100644 Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/deduplicate/DedupeResponseConfig.kt delete mode 100644 Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/headers/HeadersConfig.kt delete mode 100644 Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ignore/IgnoreFilterConfig.kt delete mode 100644 Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ratelimiter/RateLimiterConfig.kt delete mode 100644 Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/savesession/SaveSessionConfig.kt diff --git a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/RoutingConfig.kt b/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/RoutingConfig.kt deleted file mode 100644 index d017225..0000000 --- a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/RoutingConfig.kt +++ /dev/null @@ -1,63 +0,0 @@ -package com.frontiers.bff.routing - -import com.frontiers.bff.props.ServerProperties -import org.springframework.cloud.gateway.filter.factory.TokenRelayGatewayFilterFactory -import org.springframework.cloud.gateway.route.RouteLocator -import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.http.HttpMethod -import java.time.Duration - -/**********************************************************************************************************************/ -/***************************************************** GATEWAY ROUTING ************************************************/ -/**********************************************************************************************************************/ - -@Configuration -internal class RoutingConfig( - private val serverProperties: ServerProperties, -) { - - @Bean - fun routeLocator( - builder: RouteLocatorBuilder, - tokenRelayGatewayFilterFactory: TokenRelayGatewayFilterFactory - ): RouteLocator { - return builder.routes() - - // routing for Resource Server - .route("resource-server") { r -> - r.path("/api${serverProperties.resourceServerPrefix}/**") - .filters { f -> - - // token relay filter - f.filter(tokenRelayGatewayFilterFactory.apply()) - - // retry configuration - f.retry { retryConfig -> - retryConfig.retries = 3 - retryConfig.setMethods(HttpMethod.GET) - retryConfig.setBackoff( - Duration.ofMillis(50), - Duration.ofMillis(500), - 2, - true - ) - } - - // pass-through filter (optional, can be omitted if not needed) - f.filter { exchange, chain -> - chain.filter(exchange) - } - .removeRequestHeader("Cookie") - } - .uri(serverProperties.resourceServerUri) - } - .build() - } - -} - -/**********************************************************************************************************************/ -/**************************************************** END OF KOTLIN ***************************************************/ -/**********************************************************************************************************************/ \ No newline at end of file diff --git a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/circuitbreaker/CircuitBreakerConfig.kt b/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/circuitbreaker/CircuitBreakerConfig.kt deleted file mode 100644 index 3287d7d..0000000 --- a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/circuitbreaker/CircuitBreakerConfig.kt +++ /dev/null @@ -1,42 +0,0 @@ -package com.example.bff.gateway.circuitbreaker - -import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig -import io.github.resilience4j.timelimiter.TimeLimiterConfig -import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4JCircuitBreakerFactory -import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder -import org.springframework.cloud.client.circuitbreaker.Customizer -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import java.time.Duration - -/**********************************************************************************************************************/ -/***************************************************** CIRCUIT BREAKER ************************************************/ -/**********************************************************************************************************************/ - -@Configuration -class CircuitBreakerConfig { - - @Bean - fun defaultCustomizer(): Customizer { - return Customizer { factory -> - factory.configureDefault { id -> - Resilience4JConfigBuilder(id) - .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults()) - .timeLimiterConfig( - TimeLimiterConfig.custom() - .timeoutDuration( - // 4.5 seconds - Duration.ofSeconds(4, 500_000_000L) - ) - .build() - ) - .build() - } - } - } - -} - -/**********************************************************************************************************************/ -/**************************************************** END OF KOTLIN ***************************************************/ -/**********************************************************************************************************************/ \ No newline at end of file diff --git a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/deduplicate/DedupeResponseConfig.kt b/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/deduplicate/DedupeResponseConfig.kt deleted file mode 100644 index cd08f1b..0000000 --- a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/deduplicate/DedupeResponseConfig.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.frontiers.bff.routing.deduplicate - -import org.springframework.cloud.gateway.filter.GlobalFilter -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.http.HttpHeaders -import reactor.core.publisher.Mono - -/**********************************************************************************************************************/ -/*************************************************** DEDUPE RESPONSE **************************************************/ -/**********************************************************************************************************************/ - -@Configuration -internal class DedupeResponseConfig { - - @Bean - fun dedupeResponseHeaderFilter(): GlobalFilter { - return GlobalFilter { exchange, chain -> - val headers = exchange.response.headers - dedupeHeaders(headers) - chain.filter(exchange) - } - } - - private fun dedupeHeaders(headers: HttpHeaders) { - // Deduplicate specific response headers - headers.dedupeResponseHeader( - "Access-Control-Allow-Credentials", - "RETAIN_UNIQUE" - ) - headers.dedupeResponseHeader( - "Access-Control-Allow-Origin", - "RETAIN_UNIQUE" - ) - } - - private fun HttpHeaders.dedupeResponseHeader(headerName: String, strategy: String) { - val headerValues = this[headerName] ?: return - - val uniqueValues = when (strategy) { - "RETAIN_UNIQUE" -> headerValues.toSet().toList() - else -> headerValues - } - - this[headerName] = uniqueValues - } - -} - -/**********************************************************************************************************************/ -/**************************************************** END OF KOTLIN ***************************************************/ -/**********************************************************************************************************************/ \ No newline at end of file diff --git a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/headers/HeadersConfig.kt b/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/headers/HeadersConfig.kt deleted file mode 100644 index 1f24f31..0000000 --- a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/headers/HeadersConfig.kt +++ /dev/null @@ -1,36 +0,0 @@ -package com.frontiers.bff.routing.headers - -import org.springframework.cloud.gateway.filter.GlobalFilter -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.web.server.ServerWebExchange -import reactor.core.publisher.Mono - -/**********************************************************************************************************************/ -/****************************************************** HEADERS *******************************************************/ -/**********************************************************************************************************************/ - -@Configuration -internal class HeaderConfig { - - @Bean - fun customHeaderFilter(): GlobalFilter { - return GlobalFilter { exchange, chain -> - addCustomHeaders(exchange) - chain.filter(exchange) - } - } - - private fun addCustomHeaders(exchange: ServerWebExchange) { - val headers = exchange.response.headers - headers.add( - "X-Powered-By", - "DreamStar Enterprises" - ) - } - -} - -/**********************************************************************************************************************/ -/**************************************************** END OF KOTLIN ***************************************************/ -/**********************************************************************************************************************/ \ No newline at end of file diff --git a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ignore/IgnoreFilterConfig.kt b/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ignore/IgnoreFilterConfig.kt deleted file mode 100644 index b552223..0000000 --- a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ignore/IgnoreFilterConfig.kt +++ /dev/null @@ -1,55 +0,0 @@ -package com.frontiers.bff.routing.ignore - -import org.slf4j.LoggerFactory -import org.springframework.context.annotation.Configuration - -/**********************************************************************************************************************/ -/************************************************ IGNORE FILTER CONFIG ************************************************/ -/**********************************************************************************************************************/ - -/** - * Configures endpoints considered that should be skipped - */ -@Configuration -internal class IgnoreFilterConfig { - - private val logger = LoggerFactory.getLogger(IgnoreFilterConfig::class.java) - - // define base paths and file extensions for security context loading - private val skipRequestPath: Map> = mapOf( - "/login-options" to listOf(), - ) - - // main function that checks if the request path should skip static resources - fun shouldSkipRequestPath(requestPath: String): Boolean { - val isSkipped = getRequestPathMatchers().any { it.matches(requestPath) } - if (isSkipped) { - logger.info("Request path '$requestPath' is skipped as a skippable path.") - } - return isSkipped - } - - // function that generates matchers for request path - private fun getRequestPathMatchers(): List { - return skipRequestPath.flatMap { (basePath, extensions) -> - if (extensions.isEmpty()) { - listOf(RequestPathMatcher(basePath)) - } else { - extensions.map { extension -> RequestPathMatcher(basePath, extension) } - } - } - } - - // data class to encapsulate the logic for matching request paths - private data class RequestPathMatcher(val basePath: String, val extension: String? = null) { - fun matches(requestPath: String): Boolean { - return requestPath.startsWith(basePath) && - (extension == null || requestPath.endsWith(".$extension", ignoreCase = true)) - } - } - -} - -/**********************************************************************************************************************/ -/**************************************************** END OF KOTLIN ***************************************************/ -/**********************************************************************************************************************/ \ No newline at end of file diff --git a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ratelimiter/RateLimiterConfig.kt b/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ratelimiter/RateLimiterConfig.kt deleted file mode 100644 index b5ac231..0000000 --- a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/ratelimiter/RateLimiterConfig.kt +++ /dev/null @@ -1,76 +0,0 @@ -package com.frontiers.bff.routing.ratelimiter - -import com.frontiers.bff.props.SessionProperties -import org.slf4j.LoggerFactory -import org.springframework.cloud.gateway.filter.GlobalFilter -import org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory -import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver -import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.http.HttpStatus -import org.springframework.web.server.ServerWebExchange -import reactor.core.publisher.Mono - -/**********************************************************************************************************************/ -/**************************************************** RATE LIMITER ****************************************************/ -/**********************************************************************************************************************/ - -@Configuration -internal class RateLimiterConfig( - private val requestRateLimiterGatewayFilterFactory: RequestRateLimiterGatewayFilterFactory, - private val sessionProperties: SessionProperties -) { - - private val logger = LoggerFactory.getLogger(RateLimiterConfig::class.java) - - @Bean - fun requestRateLimiterGatewayFilterFactory(): GlobalFilter { - - // create the rate limiter filter with the desired configuration - val rateLimiterConfig = RequestRateLimiterGatewayFilterFactory.Config().apply { - rateLimiter = redisRateLimiter() - keyResolver = defaultKeyResolver() - denyEmptyKey = true - statusCode = HttpStatus.TOO_MANY_REQUESTS - emptyKeyStatus = HttpStatus.BAD_REQUEST.name - } - - val rateLimiterFilter = requestRateLimiterGatewayFilterFactory.apply(rateLimiterConfig) - - return GlobalFilter { exchange: ServerWebExchange, chain -> - // log the incoming request URI - logger.info("Processing request: ${exchange.request.uri}") - - rateLimiterFilter.filter(exchange, chain) - .doOnSubscribe { - logger.info("Rate limiter filter applied to request: ${exchange.request.uri}") - } - .doOnSuccess { - logger.info("Request successfully processed with rate limiting for: ${exchange.request.uri}") - } - .doOnError { throwable -> - logger.error("Error processing request with rate limiting for: ${exchange.request.uri}", throwable) - } - .then(Mono.fromRunnable { - // optionally, add more logging here if needed - }) - } - } - - private fun redisRateLimiter(): RedisRateLimiter { - return RedisRateLimiter(10, 20) - } - - private fun defaultKeyResolver(): KeyResolver { - return KeyResolver { exchange: ServerWebExchange -> - val sessionId = exchange.request.cookies[sessionProperties.SESSION_COOKIE_NAME]?.first()?.value - Mono.justOrEmpty(sessionId) - } - } - -} - -/**********************************************************************************************************************/ -/**************************************************** END OF KOTLIN ***************************************************/ -/**********************************************************************************************************************/ \ No newline at end of file diff --git a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/savesession/SaveSessionConfig.kt b/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/savesession/SaveSessionConfig.kt deleted file mode 100644 index bf6aef6..0000000 --- a/Spring BFF/BFF/src/main/kotlin/com/frontiers/bff/routing/savesession/SaveSessionConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.frontiers.bff.routing.savesession - -import org.springframework.cloud.gateway.filter.GlobalFilter -import org.springframework.cloud.gateway.filter.factory.SaveSessionGatewayFilterFactory -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/**********************************************************************************************************************/ -/**************************************************** SAVE SESSION ****************************************************/ -/**********************************************************************************************************************/ - -@Configuration -internal class SaveSessionConfig( - private val saveSessionGatewayFilterFactory: SaveSessionGatewayFilterFactory -) { - - @Bean - fun saveSessionGlobalFilter(): GlobalFilter { - return GlobalFilter { exchange, chain -> - saveSessionGatewayFilterFactory.apply { - - // optionally configure futher if needed - }.filter(exchange, chain) - } - } -} - -/**********************************************************************************************************************/ -/**************************************************** END OF KOTLIN ***************************************************/ -/**********************************************************************************************************************/