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 f47c3d3 commit adb9631
Show file tree
Hide file tree
Showing 4 changed files with 100 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 @@ -69,6 +69,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
import io.gravitee.node.api.license.LicenseModelService;
import io.gravitee.node.api.license.NodeLicenseService;
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 java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* @author Antoine CORDIER (antoine.cordier at graviteesource.com)
* @author GraviteeSource Team
*/
@Component
@Slf4j
public class NodeLicenseServiceImpl implements NodeLicenseService {

private static final String LICENSE_TIER_KEY = "tier";
Expand Down Expand Up @@ -89,8 +93,14 @@ private String readTier() {
private Set<String> readPacks() {
Set<String> licensePacks = new HashSet<>(readList(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 @@ -101,7 +111,13 @@ private Set<String> readFeatures() {
licenseFeatures.addAll(readList(LICENSE_FEATURES_KEY));
licenseFeatures.addAll(getLegacyFeatures());
for (String pack : getPacks()) {
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 @@ -70,7 +70,6 @@ void setUp() {
@Test
void shouldReturnTier() {
when(tier.getString()).thenReturn("planet");
when(license.feature("tier")).thenReturn(Optional.of(tier));
service.refresh();
assertThat(service.getTier()).isEqualTo("planet");
}
Expand Down Expand Up @@ -198,4 +197,51 @@ void shouldHaveLegacyFeatureEnabled() {
service.refresh();
assertThat(service.isFeatureEnabled("apim-api-designer")).isTrue();
}

@Test
void shouldNotFailIfUnknownTier() {
when(tier.getString()).thenReturn("unknown");
when(packs.getString()).thenReturn("enterprise-legacy-upgrade");
service.refresh();
assertThat(service.getTier()).isEqualTo("unknown");
assertThat(service.getPacks()).containsExactly("enterprise-legacy-upgrade");
assertThat(service.getFeatures()).containsExactlyInAnyOrder("apim-policy-xslt", "apim-policy-ws-security-authentication");
}

@Test
void shouldNotFailIfUnknownPack() {
when(tier.getString()).thenReturn("planet");
when(packs.getString()).thenReturn("unknown");
service.refresh();
assertThat(service.getTier()).isEqualTo("planet");
assertThat(service.getPacks())
.containsExactlyInAnyOrder("enterprise-features", "enterprise-legacy-upgrade", "enterprise-identity-provider", "unknown");
assertThat(service.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"
);
}
}
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 adb9631

Please sign in to comment.