Skip to content

Commit

Permalink
#29578: adding license check at dotAI's ConfigService (#29622)
Browse files Browse the repository at this point in the history
To return dummy config when no valid license is provided.
  • Loading branch information
victoralfaro-dotcms authored Aug 16, 2024
1 parent 8f7cae3 commit 3e4e07b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 12 additions & 4 deletions dotCMS/src/main/java/com/dotcms/ai/app/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.dotcms.api.web.HttpServletRequestThreadLocal;
import com.dotcms.security.apps.AppSecrets;
import com.dotcms.util.LicenseValiditySupplier;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.Logger;
import com.liferay.portal.model.User;
import io.vavr.control.Try;

Expand All @@ -20,16 +20,25 @@ public class ConfigService {

public static final ConfigService INSTANCE = new ConfigService();

private final LicenseValiditySupplier licenseValiditySupplier;

private ConfigService() {
licenseValiditySupplier = new LicenseValiditySupplier() {};
}

/**
* Gets the secrets from the App - this will check the current host then the SYSTEM_HOST for a valid configuration. This lookup is low overhead and cached
* by dotCMS.
*/
public AppConfig config(final Host host) {
final User systemUser = APILocator.systemUser();
final Host resolved = resolveHost(host);

if (!licenseValiditySupplier.hasValidLicense()) {
Logger.debug(this, "No valid license found, returning empty configuration");
return new AppConfig(resolved.getHostname(), Map.of());
}

final User systemUser = APILocator.systemUser();
Optional<AppSecrets> appSecrets = Try
.of(() -> APILocator.getAppsAPI().getSecrets(AppKeys.APP_KEY, false, resolved, systemUser))
.get();
Expand All @@ -43,7 +52,6 @@ public AppConfig config(final Host host) {
realHost = resolved;
}


return new AppConfig(realHost.getHostname(), appSecrets.map(AppSecrets::getSecrets).orElse(Map.of()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dotcms.ai.viewtool;

import com.dotcms.IntegrationTestBase;
import com.dotcms.ai.AiTest;
import com.dotcms.ai.app.AppConfig;
import com.dotcms.ai.app.ConfigService;
Expand All @@ -23,6 +24,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

/**
Expand All @@ -33,7 +35,7 @@
*
* @author vico
*/
public class AIViewToolTest {
public class AIViewToolTest extends IntegrationTestBase {

private static AppConfig config;
private static WireMockServer wireMockServer;
Expand Down Expand Up @@ -129,6 +131,22 @@ public void test_generateImage_fromMapPrompt() {
assertImageResponse(response, prompt.get("prompt").toString(), "dalailama");
}

/**
* Scenario: No license
* When initializing the AIView tool
* Then should NOT throw exception
*/
@Test
public void test_noLicense_initShouldNotFail() throws Exception {
runNoLicense(()-> {
try {
aiViewTool.init(mock(ViewContext.class));
} catch (Exception e) {
fail("Should not throw exception");
}
});
}

private void assertTextResponse(final JSONObject response, final String containedText) {
assertNotNull(response);
assertTrue(response.containsKey("choices"));
Expand Down

0 comments on commit 3e4e07b

Please sign in to comment.