Skip to content

Commit

Permalink
fix: allow to configure unknown tier & pack in the license
Browse files Browse the repository at this point in the history
  • Loading branch information
phiz71 committed Dec 11, 2024
1 parent 8c7c1a0 commit e0bd018
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
4 changes: 4 additions & 0 deletions gravitee-node-license/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,8 +122,14 @@ private Set<String> readPacks(License3J license, String tier) {
final Set<String> licensePacks = new HashSet<>(readList(license, LICENSE_PACKS_KEY));

if (tier != null) {
Set<String> 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<String> tierPacks = licenseTier.getPacks();
licensePacks.addAll(tierPacks);
} else if (log.isDebugEnabled()) {
log.debug("Unknown tier: {}", tier);
}
}

return licensePacks;
Expand All @@ -135,7 +143,13 @@ private Set<String> readFeatures(License3J license, Set<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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));
Expand Down Expand Up @@ -390,7 +447,7 @@ private javax0.license3j.License generateLicense(String tier, List<String> 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) {
Expand Down
30 changes: 30 additions & 0 deletions gravitee-node-license/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright (c) 2015-2016, The Gravitee team (http://www.gravitee.io)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="io.gravitee" level="DEBUG" />

<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit e0bd018

Please sign in to comment.