Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#29578: adding license check at dotAI's ConfigService #29622

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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