<commit_before>package com.netflix.spinnaker.gate.config

import groovy.util.logging.Slf4j
import org.apache.catalina.connector.Connector
import org.apache.coyote.http11.Http11NioProtocol
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory
import org.springframework.boot.context.embedded.Ssl
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@Slf4j
@ConditionalOnExpression('${server.ssl.enabled:false}')
class TomcatConfiguration {
  @Value('${default.legacyServerPort:7101}')
  int legacyServerPort

  @Value('${default.apiPort:-1}')
  int apiPort

  @Value('${server.ssl.keyStore}')
  String keyStore

  @Value('${server.ssl.keyStorePassword}')
  String keyStorePassword

  @Value('${server.ssl.keyStoreType}')
  String keyStoreType

  @Value('${server.ssl.trustStore}')
  String trustStore

  @Value('${server.ssl.trustStorePassword}')
  String trustStorePassword

  @Value('${server.ssl.trustStoreType}')
  String trustStoreType

  /**
   * Setup multiple connectors:
   * - an https connector requiring client auth that will service API requests
   * - an http connector that will service legacy non-https requests
   */
  @Bean
  public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory()

    if (legacyServerPort > 0) {
      def httpConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol")
      httpConnector.setScheme("http")
      httpConnector.setPort(legacyServerPort)
      tomcat.addAdditionalTomcatConnectors(httpConnector)
    }

    if (apiPort > 0) {
      def apiConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol")
      apiConnector.setScheme("https")
      apiConnector.setPort(apiPort)

      tomcat.configureSsl(apiConnector.getProtocolHandler() as Http11NioProtocol, new Ssl(
        keyStore: keyStore,
        keyStorePassword: <PASSWORD>StorePassword,
        keyStoreType: keyStoreType,
        trustStore: trustStore,
        trustStorePassword: <PASSWORD>,
        trustStoreType: trustStoreType,
        clientAuth: Ssl.ClientAuth.NEED
      ))
      tomcat.addAdditionalTomcatConnectors(apiConnector)
    }

    return tomcat
  }
}<commit_msg>Switch to using a EmbeddedServletContainerCustomizer for configuring http/https endpoints<commit_after>package com.netflix.spinnaker.gate.config

import groovy.util.logging.Slf4j
import org.apache.catalina.connector.Connector
import org.apache.coyote.http11.Http11NioProtocol
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory
import org.springframework.boot.context.embedded.Ssl
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@Slf4j
@ConditionalOnExpression('${server.ssl.enabled:false}')
class TomcatConfiguration {
  @Value('${default.legacyServerPort:7101}')
  int legacyServerPort

  @Value('${default.apiPort:-1}')
  int apiPort

  /**
   * Setup multiple connectors:
   * - an https connector requiring client auth that will service API requests
   * - an http connector that will service legacy non-https requests
   */
  @Bean
  EmbeddedServletContainerCustomizer containerCustomizer() throws Exception {
    return { ConfigurableEmbeddedServletContainer container ->
      TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container

      if (legacyServerPort > 0) {
        def httpConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol")
        httpConnector.setScheme("http")
        httpConnector.setPort(legacyServerPort)
        tomcat.addAdditionalTomcatConnectors(httpConnector)
      }

      if (apiPort > 0) {
        def apiConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol")
        apiConnector.setScheme("https")
        apiConnector.setPort(apiPort)

        def ssl = new Ssl()
        tomcat.ssl.properties.each { k, v ->
          try {
            ssl."${k}" = v
          } catch (ReadOnlyPropertyException ignored) {}
        }
        ssl.clientAuth = Ssl.ClientAuth.NEED

        tomcat.configureSsl(apiConnector.getProtocolHandler() as Http11NioProtocol, ssl)
        tomcat.addAdditionalTomcatConnectors(apiConnector)
      }
    } as EmbeddedServletContainerCustomizer
  }
}
{'fraction_non_alphanumeric': 0.08999008919722498, 'fraction_numerical': 0.009117938553022795, 'mean_word_length': 4.631696428571429, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1569793', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1396, 'n_words': 244}