Skip to content

Commit

Permalink
New dependencies on licensed extensions are not covered by existing l…
Browse files Browse the repository at this point in the history
…icenses when upgrading licensed extensions #174

* refactoring and adding comments
  • Loading branch information
oanalavinia committed Oct 29, 2024
1 parent b37e5e1 commit e628a03
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ public interface LicenseUpdater
/**
* Retrieve licenses updates from the XWiki Store.
*/
void getLicensesUpdates();
void updateLicenses();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class DefaultLicenseUpdater implements LicenseUpdater
private static final String DATA = "data";

private static final String ERROR =
"Failed to update license for [{}]. Please contact [email protected] for eventual problems.";
"Failed to update license for [%s]. Please contact your administrator for eventual problems.";

private static final String FEATURE_ID = "featureId";

Expand Down Expand Up @@ -117,19 +117,18 @@ public class DefaultLicenseUpdater implements LicenseUpdater
@Override
public void renewLicense(ExtensionId extensionId)
{
String errorMsg = String.format(ERROR, extensionId);

try {
logger.debug("Try renewing the license of [{}], in order to include new found changes.", extensionId);

String errorMsg = String.format(
"Failed to update license for [%s]. Please contact [email protected] for eventual problems.",
extensionId);
JsonNode licenseRenewResponse = httpUtils.httpPost(initializeLicenseRenewPost(extensionId), errorMsg);
if (licenseRenewResponse == null) {
return;
}

if (licenseRenewResponse.get("status").textValue().equals("error")) {
logger.warn(ERROR + " Cause: [{}]", extensionId.getId(), licenseRenewResponse.get(DATA).textValue());
logger.warn("{} Cause: [{}]", errorMsg, licenseRenewResponse.get(DATA).textValue());
} else {
logger.debug(
"Successful response from store after license renew. Trying to update local licenses too.");
Expand All @@ -144,17 +143,17 @@ public void renewLicense(ExtensionId extensionId)
} else {
logger.debug("No license received in store response. Updating all licenses in case there might "
+ "have been updates anyway. Cause: [{}]", licenseRenewResponse.get(DATA));
getLicensesUpdates();
updateLicenses();
}
}
} catch (Exception e) {
logger.warn(ERROR + " Root cause is [{}]", extensionId, ExceptionUtils.getRootCauseMessage(e));
logger.warn("{} Root cause is [{}]", errorMsg, ExceptionUtils.getRootCauseMessage(e));
}
}

@Override
@SuppressWarnings("unchecked")
public void getLicensesUpdates()
public void updateLicenses()
{
try {
URL licensesUpdateURL = getLicensesUpdatesURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void onEvent(Event event, Object source, Object data)

licensedExtensionManager.invalidateMandatoryLicensedExtensionsCache();
// Retrieve license updates to be sure that we don't override an existing license.
licenseUpdater.getLicensesUpdates();
licenseUpdater.updateLicenses();

for (ExtensionId extensionId : extensions) {
InstalledExtension installedExtension = installedExtensionRepository.getInstalledExtension(extensionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String getName()
public void onEvent(Event event, Object source, Object data)
{
// Retrieve license updates from store.
licenseUpdater.getLicensesUpdates();
licenseUpdater.updateLicenses();

ExtensionEvent extensionEvent = (ExtensionEvent) event;
// Only treat top level licensed extensions. If it's not a top level extension, then its license it's handled
Expand All @@ -107,9 +107,6 @@ public void onEvent(Event event, Object source, Object data)
&& isMandatoryLicensedExtension(extensionEvent.getExtensionId()))
{
InstalledExtension installedExtension = (InstalledExtension) source;
if (installedExtension == null) {
return;
}

License license = licensorProvider.get().getLicense(installedExtension.getId());
boolean hasLicense = license != null && !License.UNLICENSED.equals(license);
Expand Down Expand Up @@ -142,6 +139,9 @@ private void extensionUpgraded(InstalledExtension installedExtension, Object dat
InstalledExtension prevInstalledExtension =
getPreviousInstalledExtension(data, installedExtension, extensionEvent.getNamespace());
// Stop if previous version is actually higher, because we don't want to regenerate a license downgrade.
// If at some point a dependency was deleted, then a downgrade shouldn't add it back on the license. On the
// same note, if a dependency was added for the app, then downgrading to a version that doesn't have it will
// actually perform an unnecessary change to the license.
if (prevInstalledExtension != null
&& prevInstalledExtension.getId().getVersion().compareTo(installedExtension.getId().getVersion()) > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void generateTrialLicense(ExtensionId extensionId)
logger.warn("Failed to generate trial license for [{}] on store.", extensionId.getId());
} else {
logger.debug("Trial license added for [{}]", extensionId.getId());
licenseUpdater.getLicensesUpdates();
licenseUpdater.updateLicenses();
}
} catch (Exception e) {
logger.warn("Failed to get trial license for [{}]. Root cause is [{}]", extensionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.xwiki.crypto.BinaryStringEncoder;
import org.xwiki.extension.ExtensionId;
Expand All @@ -44,8 +43,6 @@
import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xpn.xwiki.XWiki;
Expand Down Expand Up @@ -185,24 +182,23 @@ void renewLicenseWithError() throws Exception
ExtensionId extensionId = new ExtensionId("application-test", "1.0");
licenseUpdater.renewLicense(extensionId);

assertEquals(String.format("Failed to update license for [%s]. Please contact [email protected] for eventual "
+ "problems. Cause: [error cause]", extensionId.getId()),
logCaptureWarn.getMessage(0));
assertEquals(String.format("Failed to update license for [%s]. Please contact your administrator for eventual "
+ "problems. Cause: [error cause]", extensionId), logCaptureWarn.getMessage(0));
}

@Test
void getLicensesUpdatesWithNullURL() throws Exception
void updateLicensesWithNullURL() throws Exception
{
when(licensingConfig.getStoreUpdateURL()).thenReturn(null);

licenseUpdater.getLicensesUpdates();
licenseUpdater.updateLicenses();

assertEquals("Failed to update licenses because the licensor configuration is not complete. "
+ "Check your store update URL.", logCaptureWarn.getMessage(0));
}

@Test
void getLicensesUpdatesWithUpdates() throws Exception
void updateLicensesWithUpdates() throws Exception
{
when(licensingConfig.getStoreUpdateURL()).thenReturn("https://storeUpdate.com");

Expand All @@ -223,13 +219,13 @@ void getLicensesUpdatesWithUpdates() throws Exception
License convertedLicense = new License();
when(converter.convert(License.class, licenseBytes)).thenReturn(convertedLicense);

licenseUpdater.getLicensesUpdates();
licenseUpdater.updateLicenses();

verify(licenseManager).add(convertedLicense);
}

@Test
void getLicensesUpdatesWithoutUpdates() throws IOException
void updateLicensesWithoutUpdates() throws IOException
{
when(licensingConfig.getStoreUpdateURL()).thenReturn("https://storeUpdate.com");

Expand All @@ -245,7 +241,7 @@ void getLicensesUpdatesWithoutUpdates() throws IOException
when(licensor.getLicense(licensedExtension)).thenReturn(license);
when(license.getExpirationDate()).thenReturn(Long.valueOf("12"));

licenseUpdater.getLicensesUpdates();
licenseUpdater.updateLicenses();

verify(licenseManager, times(0)).add(any(License.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void onEventWithExtensionInstalledEvent() throws Exception

renewListener.onEvent(event, installedExtension, null);

verify(licenseUpdater).getLicensesUpdates();
verify(licenseUpdater).updateLicenses();
verify(licenseUpdater).renewLicense(extensionId);
}

Expand Down Expand Up @@ -125,7 +125,7 @@ void onEventWithExtensionUpgradedEventAndChangesOnVersions() throws Exception

renewListener.onEvent(event, installedExtension, Collections.singletonList(prevInstalledExtension));

verify(licenseUpdater).getLicensesUpdates();
verify(licenseUpdater).updateLicenses();
verify(licenseUpdater).renewLicense(extensionId);
verify(license, never()).getFeatureIds();
}
Expand Down Expand Up @@ -156,7 +156,7 @@ void onEventWithExtensionUpgradedEventAndChangesOnLicense() throws Exception

renewListener.onEvent(event, installedExtension, Collections.singletonList(prevInstalledExtension));

verify(licenseUpdater).getLicensesUpdates();
verify(licenseUpdater).updateLicenses();
verify(licenseUpdater).renewLicense(extensionId);
}

Expand All @@ -176,7 +176,7 @@ void onEventWithExtensionDowngradeEvent() throws Exception

renewListener.onEvent(event, installedExtension, Collections.singletonList(prevInstalledExtension));

verify(licenseUpdater).getLicensesUpdates();
verify(licenseUpdater).updateLicenses();
verify(licenseUpdater, never()).renewLicense(extensionId);
}

Expand Down Expand Up @@ -204,7 +204,7 @@ void onEventWithExtensionUpgradedEventAndNoChanges() throws Exception

renewListener.onEvent(event, installedExtension, Collections.singletonList(prevInstalledExtension));

verify(licenseUpdater).getLicensesUpdates();
verify(licenseUpdater).updateLicenses();
verify(licenseUpdater, never()).renewLicense(extensionId);
}

Expand All @@ -220,7 +220,7 @@ void onEventWithExtensionUpgradedEventAndNoLicense() throws Exception

renewListener.onEvent(event, installedExtension, Collections.singletonList(prevInstalledExtension));

verify(licenseUpdater).getLicensesUpdates();
verify(licenseUpdater).updateLicenses();
verify(licenseUpdater, never()).renewLicense(extensionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void generateTrialLicense() throws Exception

trialLicenseGenerator.generateTrialLicense(this.extension1);

verify(this.licenseUpdater, times(1)).getLicensesUpdates();
verify(this.licenseUpdater, times(1)).updateLicenses();
}

@Test
Expand Down

0 comments on commit e628a03

Please sign in to comment.