# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class Certificate::ApplySSLCertificates

  class << self

    SEMAPHORE = Thread::Mutex.new

    # Ensure the SSLContext for the current process has all custom SSL certificates.
    #
    # @return [OpenSSL::X509::Store] the current default store. Clients which do not pick the
    #   default store up from the SSLContext implicitly (e.g. Faraday) can pass it on themselves.
    def ensure_fresh_ssl_context

      SEMAPHORE.synchronize do

        all_certificates = SSLCertificate.all

        # Only update the default store if there are changes with the stored SSL certificates.
        cache_key = all_certificates.cache_key_with_version
        return OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE if @cache_key == cache_key

        @cache_key = cache_key

        # Build a new default store.
        store = OpenSSL::X509::Store.new
        store.set_default_paths
        all_certificates.each { |cert| store.add_cert(cert.certificate_parsed) }
        Kernel.silence_warnings do
          OpenSSL::SSL::SSLContext.const_set(:DEFAULT_CERT_STORE, store)
        end

        store
      end
    end
  end
end
