Wish Access to sslFactory in jServer or implementation of SslContextFactory reload

OliverA

Expert
Licensed User
Longtime User
Looks like Jetty supports "hot loading" of the keystore used for HTTPS connections (necessary when trying to update certificate without having to stop/start the whole server process). In order to implement this though, I would either need access to the private sslFactory variable of jServer or (jReflection does not work in a non-JavaFX environment) a new method provided by jServer that would just reload the keystore (no logistics needed as to why and can be bare bones of just reloading existing keystore w/o changing paths/passwords).

Links:
1) https://github.com/eclipse/jetty.pr...e/jetty/util/ssl/SslContextFactory.java#L1877
This link shows the method of SslContextFactory that I would try to use (or have implemented)
2) https://www.b4x.com/android/forum/threads/solved-abmaterial-using-https-secure.104818/post-802622
Where this all started
 

OliverA

Expert
Licensed User
Longtime User
May not need this anymore. I forgot that I actually have to pass the SslContextFactory object to jServer via B4J's SslConfiguration! Here's the code
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private srvr As Server
    Private sslConfig As SslConfiguration
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("")
    srvr.Port = 80
    ConfigureSSL(443)
    srvr.Start
    UpdateSSL
    StartMessageLoop
End Sub

Private Sub ConfigureSSL (SslPort As Int)
    'example of SSL connector configuration
    Dim ssl As SslConfiguration
    ssl.Initialize
    ssl.SetKeyStorePath(File.DirApp, "keystore.jks") 'path to keystore file
    ssl.KeyStorePassword = "UseYourOwnPassword"
    'ssl.KeyManagerPassword = "UseYourOwnPasswordCouldBeOptional"
    srvr.SetSslConfiguration(ssl, SslPort)
    sslConfig = ssl
    'add filter to redirect all traffic from http to https (optional)
    'srvr.AddFilter("/*", "HttpsFilter", False)
End Sub

Private Sub UpdateSSL
    Dim timestamp As Long = File.LastModified(File.DirApp, "keystore.jks")
    Dim jo As JavaObject = Me
    Dim sslConfigJO As JavaObject = sslConfig
    Do While True
        Sleep(30000)
        If File.LastModified(File.DirApp, "keystore.jks") <> timestamp Then
            timestamp = File.LastModified(File.DirApp, "keystore.jks")
            Log("Attempting to reload keystore")
            Try
                jo.RunMethod("reloadSSLConfiguration", Array(sslConfigJO))
            Catch
                Log(LastException)
            End Try
        End If
    Loop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

#if Java
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.server.Server;

public static void reloadSSLConfiguration(SslContextFactory sslFactory) throws Exception {
    sslFactory.reload(sfc -> {});
    //sslFactory.reload(sslFactory -> {});
}
#End If
Note: Had a "slight" bug in code
 
Last edited:
Top