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

* added debug logs
* handle better response from store, which will also contain the license
  • Loading branch information
oanalavinia committed Oct 14, 2024
1 parent 4d69cb2 commit 31c639b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ public interface LicensedExtensionManager
* @return a set of installed licensed dependencies
* @since 1.27
*/
@Unstable
Set<ExtensionId> getLicensedDependencies(InstalledExtension installedExtension, String namespace);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public interface LicensingConfiguration
* @return the URL used in renewing an existing license or null if the value of the property is not filled up
* @since 1.27
*/
String getStoreLicenseRenewURL();
String getStoreRenewURL();

/**
* @return the first name of the licensing owner or null if the value of the property is not filled up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
Expand Down Expand Up @@ -98,6 +99,7 @@ public class DefaultLicenseUpdater implements LicenseUpdater
public void renewLicense(ExtensionId extensionId)
{
try {
logger.debug("Try renewing the license of [{}], in order to include new found changes.", extensionId);
URL licenseRenewURL = getLicenseRenewURL(extensionId);
if (licenseRenewURL == null) {
logger.warn("Failed to renew license for [{}] because the licensor configuration is not complete. "
Expand All @@ -106,15 +108,31 @@ public void renewLicense(ExtensionId extensionId)
}

XWikiContext xcontext = contextProvider.get();
String getLicenseRenewResponse = xcontext.getWiki().getURLContent(licenseRenewURL.toString(), xcontext);
String licenseRenewResponseStr = xcontext.getWiki().getURLContent(licenseRenewURL.toString(), xcontext);

if (getLicenseRenewResponse.contains("error")) {
logger.warn("Failed to renew license for [{}] on store.", extensionId.getId());
ObjectMapper objectMapper = new ObjectMapper();
Map<?, ?> licenseRenewResponse = objectMapper.readValue(licenseRenewResponseStr, Map.class);

if (String.valueOf(licenseRenewResponse.get("status")).equals("error")) {
logger.warn(
"Failed to renew license for [{}] on store. Cause: [{}]. Please contact [email protected] for "
+ "eventual problems.", licenseRenewResponse.get("data"), extensionId.getId());
} else {
logger.debug("License renewed for [{}]", extensionId.getId());
// After renewing the license on store, right now we retrieve new updates from store. Maybe we should
// simply return the updated license from the start, to not make a new request?
// getLicensesUpdates();
logger.debug(
"Successful response from store after license renew. Trying to update local licenses too.");

String license = String.valueOf(licenseRenewResponse.get("license"));
if (license != null) {
License retrivedLicense = converter.convert(License.class, base64decoder.decode(license));
if (retrivedLicense != null) {
licenseManagerProvider.get().add(retrivedLicense);
logger.debug("License renewed for [{}].", extensionId.getId());
}
} else {
logger.debug("No license received in store response. Updating all licenses in case there might "
+ "have been updates anyway.");
getLicensesUpdates();
}
}
} catch (Exception e) {
logger.warn("Failed to update license for [{}]. Root cause is [{}]", extensionId,
Expand Down Expand Up @@ -184,7 +202,7 @@ private URL getLicensesUpdatesURL() throws URISyntaxException, MalformedURLExcep

private URL getLicenseRenewURL(ExtensionId extensionId) throws Exception
{
String storeLicenseRenewURL = licensingConfig.getStoreLicenseRenewURL();
String storeLicenseRenewURL = licensingConfig.getStoreRenewURL();
// In case the property has no filled value, the URL cannot be constructed.
if (storeLicenseRenewURL == null) {
return null;
Expand All @@ -198,8 +216,6 @@ private URL getLicenseRenewURL(ExtensionId extensionId) throws Exception
builder.addParameter(INSTANCE_ID, instanceIdManagerProvider.get().getInstanceId().toString());
builder.addParameter(FEATURE_ID, extensionId.getId());
builder.addParameter("extensionVersion", extensionId.getVersion().getValue());
// TODO: take it from existing license.
builder.addParameter("licenseType", "TRIAL");

return builder.build().toURL();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.xwiki.extension.InstalledExtension;
import org.xwiki.extension.ResolveException;
import org.xwiki.extension.repository.InstalledExtensionRepository;
import org.xwiki.stability.Unstable;

import com.xwiki.licensing.LicensedExtensionManager;
import com.xwiki.licensing.LicensedFeatureId;
Expand Down Expand Up @@ -199,7 +198,6 @@ public void invalidateMandatoryLicensedExtensionsCache()
}

@Override
@Unstable
public Set<ExtensionId> getLicensedDependencies(InstalledExtension installedExtension, String namespace)
{
Set<ExtensionId> licensedDependencies = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ public String getLicensingOwnerEmail()
}

@Override
public String getStoreLicenseRenewURL()
public String getStoreRenewURL()
{
return this.storeConfig.getProperty("storeLicenseRenewURL");
return this.storeConfig.getProperty("storeRenewURL");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.xwiki.observation.EventListener;
import org.xwiki.observation.event.Event;

import com.xwiki.licensing.LicenseUpdater;
import com.xwiki.licensing.LicensedExtensionManager;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ public class GetTrialLicenseListener implements EventListener
private LicensedExtensionManager licensedExtensionManager;

@Inject
private DefaultLicenseUpdater licenseUpdater;
private LicenseUpdater licenseUpdater;

@Override
public List<Event> getEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package com.xwiki.licensing.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -30,16 +30,17 @@
import javax.inject.Named;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.extension.ExtensionId;
import org.xwiki.extension.InstalledExtension;
import org.xwiki.extension.event.ExtensionEvent;
import org.xwiki.extension.event.ExtensionInstalledEvent;
import org.xwiki.extension.event.ExtensionUpgradedEvent;
import org.xwiki.extension.repository.InstalledExtensionRepository;
import org.xwiki.observation.EventListener;
import org.xwiki.observation.event.Event;

import com.xwiki.licensing.LicenseUpdater;
import com.xwiki.licensing.LicensedExtensionManager;

/**
Expand All @@ -56,7 +57,7 @@ public class LicenseRenewListener implements EventListener
protected static final String NAME = "com.xwiki.licensing.internal.LicenseRenewListener";

protected static final List<Event> EVENTS =
new ArrayList<>(Arrays.asList(new ExtensionInstalledEvent(), new ExtensionUpgradedEvent()));
new ArrayList<>(Collections.singletonList(new ExtensionUpgradedEvent()));

@Inject
private LicensedExtensionManager licensedExtensionManager;
Expand All @@ -65,7 +66,10 @@ public class LicenseRenewListener implements EventListener
private InstalledExtensionRepository installedExtensionRepository;

@Inject
private DefaultLicenseUpdater licenseUpdater;
private LicenseUpdater licenseUpdater;

@Inject
private Logger logger;

@Override
public List<Event> getEvents()
Expand All @@ -87,20 +91,21 @@ public void onEvent(Event event, Object source, Object data)

ExtensionEvent extensionEvent = (ExtensionEvent) event;
if (licensedExtensionManager.getLicensedExtensions().contains(extensionEvent.getExtensionId())) {
logger.debug("The licensed extension [{}] has been updated. Check if there are dependencies changes, in "
+ "case its license needs to be updated too.", extensionEvent.getExtensionId());
InstalledExtension installedExtension = (InstalledExtension) source;
if (installedExtension == null) {
return;
}
Set<ExtensionId> licensedDependencies =
licensedExtensionManager.getLicensedDependencies(installedExtension, extensionEvent.getNamespace());
System.out.println(licensedDependencies);

Set<ExtensionId> previousDependencies =
getPreviousDependencies(data, installedExtension, extensionEvent.getNamespace());
System.out.println(previousDependencies);

// In progress: ! removed for testing purposes.
if (licensedDependencies.equals(previousDependencies)) {
if (!licensedDependencies.equals(previousDependencies)) {
logger.debug("New licensed dependencies found: from [{}] to [{}]", previousDependencies,
licensedDependencies);
licenseUpdater.renewLicense(installedExtension.getId());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import com.xpn.xwiki.XWikiContext;
import com.xwiki.licensing.License;
import com.xwiki.licensing.LicenseUpdater;
import com.xwiki.licensing.LicensedExtensionManager;
import com.xwiki.licensing.LicensingConfiguration;
import com.xwiki.licensing.Licensor;
Expand Down Expand Up @@ -75,7 +76,7 @@ public class TrialLicenseGenerator
private Provider<XWikiContext> contextProvider;

@Inject
private DefaultLicenseUpdater licenseUpdater;
private LicenseUpdater licenseUpdater;

/**
* Generate trial license for the given extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void configure()
@Test
void renewLicenseWithNullURL() throws Exception
{
when(licensingConfig.getStoreLicenseRenewURL()).thenReturn(null);
when(licensingConfig.getStoreRenewURL()).thenReturn(null);

ExtensionId extensionId = new ExtensionId("application-test", "1.0");
licenseUpdater.renewLicense(extensionId);
Expand All @@ -139,7 +139,7 @@ void renewLicenseWithNullURL() throws Exception
@Test
void renewLicenseWithSuccess() throws Exception
{
when(licensingConfig.getStoreLicenseRenewURL()).thenReturn("https://storeRenew.com");
when(licensingConfig.getStoreRenewURL()).thenReturn("https://storeRenew.com");
when(licensingConfig.getLicensingOwnerFirstName()).thenReturn("John");
when(licensingConfig.getLicensingOwnerLastName()).thenReturn("Doe");
String renewURL =
Expand All @@ -158,7 +158,7 @@ void renewLicenseWithSuccess() throws Exception
@Test
void renewLicenseWithError() throws Exception
{
when(licensingConfig.getStoreLicenseRenewURL()).thenReturn("https://storeRenew.com");
when(licensingConfig.getStoreRenewURL()).thenReturn("https://storeRenew.com");
when(licensingConfig.getLicensingOwnerFirstName()).thenReturn("John");
when(licensingConfig.getLicensingOwnerLastName()).thenReturn("Doe");
String renewURL =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xwiki.licensing.License;
import com.xwiki.licensing.LicenseUpdater;
import com.xwiki.licensing.LicensedExtensionManager;
import com.xwiki.licensing.LicensingConfiguration;
import com.xwiki.licensing.Licensor;
Expand Down Expand Up @@ -105,7 +106,7 @@ public class TrialLicenseGeneratorTest
private Provider<XWikiContext> contextProvider;

@MockComponent
private DefaultLicenseUpdater licenseUpdater;
private LicenseUpdater licenseUpdater;

@BeforeEach
public void configure() throws Exception
Expand Down

0 comments on commit 31c639b

Please sign in to comment.