diff --git a/gravitee-node-license/pom.xml b/gravitee-node-license/pom.xml index 05f95f36f..b926eef5f 100644 --- a/gravitee-node-license/pom.xml +++ b/gravitee-node-license/pom.xml @@ -75,6 +75,10 @@ org.slf4j jcl-over-slf4j + + ch.qos.logback + logback-classic + org.springframework spring-context diff --git a/gravitee-node-license/src/main/java/io/gravitee/node/license/DefaultLicenseFactory.java b/gravitee-node-license/src/main/java/io/gravitee/node/license/DefaultLicenseFactory.java index 03cbcad15..041b4628f 100644 --- a/gravitee-node-license/src/main/java/io/gravitee/node/license/DefaultLicenseFactory.java +++ b/gravitee-node-license/src/main/java/io/gravitee/node/license/DefaultLicenseFactory.java @@ -19,6 +19,8 @@ import io.gravitee.node.api.license.*; import io.gravitee.node.api.license.model.LicenseModel; +import io.gravitee.node.api.license.model.LicensePack; +import io.gravitee.node.api.license.model.LicenseTier; import io.gravitee.node.license.license3j.License3J; import io.gravitee.node.license.license3j.License3JFeature; import java.io.ByteArrayInputStream; @@ -120,8 +122,14 @@ private Set readPacks(License3J license, String tier) { final Set licensePacks = new HashSet<>(readList(license, LICENSE_PACKS_KEY)); if (tier != null) { - Set tierPacks = licenseModelService.getLicenseModel().getTiers().get(tier).getPacks(); - licensePacks.addAll(tierPacks); + // Allow to declare a non existing tier. Useful when the license uses a tier that has been created for a newer version of gravitee-node + LicenseTier licenseTier = licenseModelService.getLicenseModel().getTiers().get(tier); + if (licenseTier != null) { + Set tierPacks = licenseTier.getPacks(); + licensePacks.addAll(tierPacks); + } else if (log.isDebugEnabled()) { + log.debug("Unknown tier: {}", tier); + } } return licensePacks; @@ -135,7 +143,13 @@ private Set readFeatures(License3J license, Set packs) { licenseFeatures.addAll(readLegacyFeatures(license)); for (String pack : packs) { - licenseFeatures.addAll(licenseModel.getPacks().get(pack).getFeatures()); + // Allow to declare a non existing pack. Useful when the license uses a pack that has been created for a newer version of gravitee-node + LicensePack licensePack = licenseModel.getPacks().get(pack); + if (licensePack != null) { + licenseFeatures.addAll(licensePack.getFeatures()); + } else if (log.isDebugEnabled()) { + log.debug("Unknown pack: {}", pack); + } } return licenseFeatures; diff --git a/gravitee-node-license/src/test/java/io/gravitee/node/license/DefaultLicenseFactoryTest.java b/gravitee-node-license/src/test/java/io/gravitee/node/license/DefaultLicenseFactoryTest.java index c41821dd2..5b24e2c8f 100644 --- a/gravitee-node-license/src/test/java/io/gravitee/node/license/DefaultLicenseFactoryTest.java +++ b/gravitee-node-license/src/test/java/io/gravitee/node/license/DefaultLicenseFactoryTest.java @@ -139,6 +139,63 @@ void should_return_license_with_legacy_features() throws InvalidLicenseException assertThat(license.getFeatures()).containsExactlyInAnyOrder("apim-api-designer", "apim-bridge-gateway"); } + @Test + void should_return_license_with_unknown_tier() throws InvalidLicenseException, MalformedLicenseException { + final License license = cut.create( + REFERENCE_TYPE_PLATFORM, + REFERENCE_ID_PLATFORM, + generateBase64License("unknown", List.of("enterprise-legacy-upgrade"), null, null) + ); + + assertThat(license.getTier()).isEqualTo("unknown"); + assertThat(license.getPacks()).containsExactly("enterprise-legacy-upgrade"); + assertThat(license.getFeatures()).containsExactlyInAnyOrder("apim-policy-xslt", "apim-policy-ws-security-authentication"); + assertThat(license.getReferenceType()).isEqualTo(REFERENCE_TYPE_PLATFORM); + assertThat(license.getReferenceId()).isEqualTo(REFERENCE_ID_PLATFORM); + } + + @Test + void should_return_license_with_unknown_pack() throws InvalidLicenseException, MalformedLicenseException { + final License license = cut.create( + REFERENCE_TYPE_PLATFORM, + REFERENCE_ID_PLATFORM, + generateBase64License("planet", List.of("unknown"), null, null) + ); + + assertThat(license.getTier()).isEqualTo("planet"); + assertThat(license.getPacks()) + .containsExactlyInAnyOrder("enterprise-features", "enterprise-legacy-upgrade", "enterprise-identity-provider", "unknown"); + assertThat(license.getFeatures()) + .containsExactlyInAnyOrder( + "apim-api-designer", + "apim-dcr-registration", + "apim-custom-roles", + "apim-audit-trail", + "apim-sharding-tags", + "apim-openid-connect-sso", + "apim-debug-mode", + "gravitee-risk-assessment", + "risk-assessment", + "apim-bridge-gateway", + "apim-policy-xslt", + "apim-policy-ws-security-authentication", + "am-idp-salesforce", + "am-idp-saml", + "am-idp-ldap", + "am-idp-kerberos", + "am-idp-azure-ad", + "am-idp-gateway-handler-saml", + "am-gateway-handler-saml-idp", + "am-idp-http-flow", + "http-flow-am-idp", + "am-idp-france-connect", + "am-idp-cas", + "cas-am-idp" + ); + assertThat(license.getReferenceType()).isEqualTo(REFERENCE_TYPE_PLATFORM); + assertThat(license.getReferenceId()).isEqualTo(REFERENCE_ID_PLATFORM); + } + @Test void should_throw_invalid_license_when_platform_license_is_invalid() { assertThrows(InvalidLicenseException.class, () -> cut.create(REFERENCE_TYPE_PLATFORM, REFERENCE_ID_PLATFORM, INVALID_LICENSE)); @@ -398,7 +455,7 @@ private javax0.license3j.License generateLicense(String tier, List packs license.add(Feature.Create.dateFeature("aDate", new Date(0))); if (tier != null) { - license.add(Feature.Create.stringFeature("tier", "universe")); + license.add(Feature.Create.stringFeature("tier", tier)); } if (packs != null) { diff --git a/gravitee-node-license/src/test/resources/logback-test.xml b/gravitee-node-license/src/test/resources/logback-test.xml new file mode 100644 index 000000000..7600d1d28 --- /dev/null +++ b/gravitee-node-license/src/test/resources/logback-test.xml @@ -0,0 +1,30 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + +