Skip to content

Commit 531109e

Browse files
authored
feat: Implement production mode license verification in VaadinServletService (#23839)
Add runtime license verification for production mode. License check failure is only logged, it does not prevent the application from running.
1 parent ce64296 commit 531109e

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorAsyncTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import org.junit.Before;
1414
import org.junit.Test;
1515
import org.mockito.Mock;
16+
import org.mockito.Mockito;
1617
import org.mockito.MockitoAnnotations;
1718

1819
import com.vaadin.flow.component.UI;
1920
import com.vaadin.flow.dom.Element;
21+
import com.vaadin.flow.function.DeploymentConfiguration;
2022
import com.vaadin.flow.internal.Range;
2123
import com.vaadin.flow.server.VaadinRequest;
2224
import com.vaadin.flow.server.VaadinService;
@@ -230,8 +232,9 @@ protected void init(VaadinRequest request) {
230232
private static VaadinSession findOrcreateSession() {
231233
VaadinSession session = VaadinSession.getCurrent();
232234
if (session == null) {
235+
DeploymentConfiguration conf = Mockito.mock(DeploymentConfiguration.class);
233236
session = new AlwaysLockedVaadinSession(
234-
new VaadinServletService(new VaadinServlet(), null));
237+
new VaadinServletService(new VaadinServlet(), conf));
235238
VaadinSession.setCurrent(session);
236239
}
237240
return session;

flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.util.Optional;
2424
import java.util.Properties;
2525

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
2629
import com.vaadin.flow.component.UI;
2730
import com.vaadin.flow.di.Lookup;
2831
import com.vaadin.flow.function.DeploymentConfiguration;
@@ -34,6 +37,7 @@
3437
import com.vaadin.flow.shared.JsonConstants;
3538
import com.vaadin.pro.licensechecker.BuildType;
3639
import com.vaadin.pro.licensechecker.LicenseChecker;
40+
import com.vaadin.pro.licensechecker.LicenseException;
3741

3842
/**
3943
* The main servlet, which handles all incoming requests to the application.
@@ -50,6 +54,9 @@
5054
* @since 1.0
5155
*/
5256
public class VaadinServlet extends HttpServlet {
57+
58+
private static final String PROJECT_NAME = "flow";
59+
5360
private VaadinServletService servletService;
5461
private StaticFileHandler staticFileHandler;
5562
private WebJarServer webJarServer;
@@ -95,9 +102,20 @@ public void init(ServletConfig servletConfig) throws ServletException {
95102
}
96103

97104
private void verifyLicense(boolean productionMode) {
98-
if (!productionMode) {
99-
String flowVersion = Version.getFullVersion();
100-
LicenseChecker.checkLicense("flow", flowVersion,
105+
String frameworkVersion = Version.getFullVersion();
106+
if (productionMode) {
107+
try {
108+
LicenseChecker.checkLicense(PROJECT_NAME, frameworkVersion,
109+
BuildType.PRODUCTION, null);
110+
} catch (LicenseException e) {
111+
getLogger().error(
112+
"This Vaadin version requires an extended maintenance subscription."
113+
+ "Provide either a server key or an online license checking key,"
114+
+ "which you can get from: https://vaadin.com/myaccount/licenses#latest.",
115+
e);
116+
}
117+
} else {
118+
LicenseChecker.checkLicense(PROJECT_NAME, frameworkVersion,
101119
BuildType.DEVELOPMENT);
102120
}
103121
}
@@ -565,4 +583,8 @@ public void init() {
565583
}
566584
}
567585
}
586+
587+
private static Logger getLogger() {
588+
return LoggerFactory.getLogger(VaadinServlet.class.getName());
589+
}
568590
}

flow-server/src/test/java/com/vaadin/flow/server/VaadinServletTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.vaadin.flow.server.MockServletServiceSessionSetup.TestVaadinServletResponse;
2727
import com.vaadin.pro.licensechecker.BuildType;
2828
import com.vaadin.pro.licensechecker.LicenseChecker;
29+
import com.vaadin.pro.licensechecker.LicenseException;
2930

3031
import net.jcip.annotations.NotThreadSafe;
3132

@@ -121,11 +122,27 @@ public void checkLicense_devMode_licenseIsChecked()
121122
}
122123

123124
@Test
124-
public void checkLicense_prodMode_licenseIsNotChecked()
125+
public void checkLicense_prodMode_licenseIsChecked()
125126
throws ServletException {
126127
Mockito.when(configuration.isProductionMode()).thenReturn(true);
127128
triggerLicenseChecking();
128-
licenseChecker.verifyNoInteractions();
129+
licenseChecker.verify(() -> LicenseChecker.checkLicense("flow",
130+
Version.getFullVersion(), BuildType.PRODUCTION, null));
131+
}
132+
133+
@Test
134+
public void checkLicense_prodModeCheck_notFails()
135+
throws ServletException {
136+
licenseChecker
137+
.when(() -> LicenseChecker.checkLicense("flow",
138+
Version.getFullVersion(), BuildType.PRODUCTION, null))
139+
.thenThrow(new LicenseException("Test exception"));
140+
try {
141+
triggerLicenseChecking();
142+
} catch (LicenseException e) {
143+
Assert.fail(
144+
"License check should not throw exception in prod mode");
145+
}
129146
}
130147

131148
private HttpServletRequest createRequest(
@@ -181,4 +198,4 @@ protected DeploymentConfiguration createDeploymentConfiguration() {
181198
return configuration;
182199
}
183200
}
184-
}
201+
}

0 commit comments

Comments
 (0)