How to make kotlin spring boot app fail if a combination of configuration is present in it's application.yaml

Viewed 89

I am having a microservice which is spring cloud gateway implementation in kotlin. So as a part of a feature, i need to fail the start-up of this service if I found a specific combination of arguments in the filter configuration present in application.yaml. To give the filter cofiguration we are using inline notation. for example:

 spring:
  cloud:
    gateway:
      routes:
        - id: test1
          predicates:
            - Path=/test1/**
          filters:
            - RewritePath=/test1/(?<segment>.*), /$\{segment}
            - TLS= OPTIONAL, NONE, TEST
        - id: test2
          predicates:
            - Path=/test2/**
          filters:
            - RewritePath=/test2/(?<segment>.*), /$\{segment}
            - TLS= MANDATORY, NONE, TEST

So in this example configuration, TLS filter is having ags combination as MANDATORY with NONE, then in this case this service should fail at start saying "MANDATORY with NONE is not a right combination"

So any suggestion to achieve this ??

2 Answers

One way to achieve this would be to create an ApplicationEventListener. Basically, you would register one of these listeners to listen to a Spring Boot Event: (One of these: https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/event/package-summary.html)

You can see an example implementation here: https://stackoverflow.com/questions/56372260/spring-load-application-properties-in-application-listener. In that example, properties are being loaded. In your case, I imagine that you could check your properties of interest and throw a RuntimeException if anything violates your requirements.

I found another way to do so in kotlin. Using the init block in class. https://blog.mindorks.com/understanding-init-block-in-kotlin

TLSFilter code which will fail if a specific combination found in the route of that filter

class TLSFilter(
    private val filterProperties: TLSFilterProperties,) : GatewayFilter {

private val logger = logger()

init {
    if (filterProperties.mode == DISABLE && (filterProperties.type?.isNotEmpty() == true)) {
        throw IllegalStateException("Security mode `DISABLE` should not be present with any security type in filter configuration.")
    }
    
    if ((filterProperties.mode == MANDATORY || filterProperties.mode == OPTIONAL) && filterProperties.type?.equals(NONE.name) == true) {
        throw IllegalStateException("Security mode = ${filterProperties.mode} with security type = NONE is not a valid configuration.")
    }
}}

TLSFilterProperties class which automatically binds with the application.yml file of spring cloud gateway app.

  /**
 * Properties that should be initialized in filter configuration properties
 */
class TLSFilterProperties {

    lateinit var mode: SecurityMode
    var type: String? = null
    var alias: String? = null

    companion object {

        val configFieldsOrder = listOf(
                "mode",
                "type",
                "alias"
        )
    }
}
Related