From 88822760d98458bd3c491bad234aba0340ef5890 Mon Sep 17 00:00:00 2001 From: leeturner Date: Sun, 26 May 2024 17:18:55 +0100 Subject: [PATCH 1/2] Update WireMock to version 3.6.0 to allow use of the start() method WireMock 3.6.0 provided the start() and stop() extension lifecycle methods which can be used to initialise extensions when WireMock starts and allow them to perform and cleanup when WireMock shuts down. This plugin used a hack to initialise the jwt so this change removes the hack in favour of the new start() method --- build.gradle | 2 +- .../extension/jwt/JwtExtensionFactory.java | 2 -- .../extension/jwt/JwtHelpersExtension.java | 17 +++++++++ .../extension/jwt/JwtInitialiser.java | 36 ------------------- 4 files changed, 18 insertions(+), 39 deletions(-) delete mode 100644 src/main/java/org/wiremock/extension/jwt/JwtInitialiser.java diff --git a/build.gradle b/build.gradle index 6704bb2..05d67eb 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ repositories { ext { versions = [ - wiremock : "3.5.2", + wiremock : "3.6.0", jwt : '4.4.0', jose4j : '0.9.6', ] diff --git a/src/main/java/org/wiremock/extension/jwt/JwtExtensionFactory.java b/src/main/java/org/wiremock/extension/jwt/JwtExtensionFactory.java index 7bfba98..22c19d3 100644 --- a/src/main/java/org/wiremock/extension/jwt/JwtExtensionFactory.java +++ b/src/main/java/org/wiremock/extension/jwt/JwtExtensionFactory.java @@ -12,11 +12,9 @@ public class JwtExtensionFactory implements ExtensionFactory { public List create(WireMockServices services) { final Admin admin = services.getAdmin(); final JwtSigningKeySettings jwtSigningKeySettings = new JwtSigningKeySettings(admin); - final JwtInitialiser jwtInitialiser = new JwtInitialiser(jwtSigningKeySettings); return List.of( jwtSigningKeySettings, - jwtInitialiser, new JwtHelpersExtension(jwtSigningKeySettings) ); } diff --git a/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java b/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java index fb3f0d7..0067411 100644 --- a/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java +++ b/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java @@ -7,6 +7,7 @@ public class JwtHelpersExtension implements TemplateHelperProviderExtension { private final JwtSigningKeySettings jwtSigningKeySettings; + private boolean initialised = false; public JwtHelpersExtension(JwtSigningKeySettings jwtSigningKeySettings) { this.jwtSigningKeySettings = jwtSigningKeySettings; @@ -22,7 +23,23 @@ public Map> provideTemplateHelpers() { "jwks", jwksHandlebarsHelper ); } + + @Override + public void start() { + initialise(); + } + private void initialise() { + if (!initialised) { + synchronized (this) { + if (!initialised) { + jwtSigningKeySettings.initialise(); + initialised = true; + } + } + } + } + @Override public String getName() { return "jwt-template-helpers"; diff --git a/src/main/java/org/wiremock/extension/jwt/JwtInitialiser.java b/src/main/java/org/wiremock/extension/jwt/JwtInitialiser.java deleted file mode 100644 index 539feb2..0000000 --- a/src/main/java/org/wiremock/extension/jwt/JwtInitialiser.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.wiremock.extension.jwt; - -import com.github.tomakehurst.wiremock.extension.MappingsLoaderExtension; -import com.github.tomakehurst.wiremock.stubbing.StubMappings; - -public class JwtInitialiser implements MappingsLoaderExtension { - - private final JwtSigningKeySettings jwtSigningKeySettings; - - private boolean initialised = false; - - public JwtInitialiser(JwtSigningKeySettings jwtSigningKeySettings) { - this.jwtSigningKeySettings = jwtSigningKeySettings; - } - - @Override - public void loadMappingsInto(StubMappings stubMappings) { - initialise(); - } - - private void initialise() { - if (!initialised) { - synchronized (this) { - if (!initialised) { - jwtSigningKeySettings.initialise(); - initialised = true; - } - } - } - } - - @Override - public String getName() { - return "jwt-initialiser"; - } -} From bac2d8a5693c689b99a793ab1aa71c0224134b85 Mon Sep 17 00:00:00 2001 From: leeturner Date: Mon, 27 May 2024 10:08:39 +0100 Subject: [PATCH 2/2] Remove the synchronized block As the new start() method is only called once during wiremock initialisation we no longer need the initialised flag or the synchronised block. --- .../extension/jwt/JwtHelpersExtension.java | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java b/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java index 0067411..ffebabc 100644 --- a/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java +++ b/src/main/java/org/wiremock/extension/jwt/JwtHelpersExtension.java @@ -7,7 +7,6 @@ public class JwtHelpersExtension implements TemplateHelperProviderExtension { private final JwtSigningKeySettings jwtSigningKeySettings; - private boolean initialised = false; public JwtHelpersExtension(JwtSigningKeySettings jwtSigningKeySettings) { this.jwtSigningKeySettings = jwtSigningKeySettings; @@ -26,20 +25,9 @@ public Map> provideTemplateHelpers() { @Override public void start() { - initialise(); + jwtSigningKeySettings.initialise(); } - private void initialise() { - if (!initialised) { - synchronized (this) { - if (!initialised) { - jwtSigningKeySettings.initialise(); - initialised = true; - } - } - } - } - @Override public String getName() { return "jwt-template-helpers";