diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifier.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifier.java index 2849b0490b..c48ca93bf2 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifier.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifier.java @@ -19,12 +19,10 @@ package com.baidu.hugegraph.license; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.time.Duration; import java.time.Instant; -import java.util.prefs.Preferences; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; @@ -36,14 +34,6 @@ import com.baidu.hugegraph.util.Log; import com.fasterxml.jackson.databind.ObjectMapper; -import de.schlichtherle.license.CipherParam; -import de.schlichtherle.license.DefaultCipherParam; -import de.schlichtherle.license.DefaultKeyStoreParam; -import de.schlichtherle.license.DefaultLicenseParam; -import de.schlichtherle.license.KeyStoreParam; -import de.schlichtherle.license.LicenseContent; -import de.schlichtherle.license.LicenseParam; - public class LicenseVerifier { private static final Logger LOG = Log.logger(HugeGraph.class); @@ -55,13 +45,12 @@ public class LicenseVerifier { private static final Duration CHECK_INTERVAL = Duration.ofMinutes(10); private volatile Instant lastCheckTime = Instant.now(); - private final LicenseVerifyParam verifyParam; + private final LicenseInstallParam installParam; private final LicenseVerifyManager manager; private LicenseVerifier() { - this.verifyParam = buildVerifyParam(LICENSE_PARAM_PATH); - LicenseParam licenseParam = this.initLicenseParam(this.verifyParam); - this.manager = new LicenseVerifyManager(licenseParam); + this.installParam = buildInstallParam(LICENSE_PARAM_PATH); + this.manager = new LicenseVerifyManager(this.installParam); } public static LicenseVerifier instance() { @@ -75,38 +64,42 @@ public static LicenseVerifier instance() { return INSTANCE; } - public void verifyIfNeeded() { - Instant now = Instant.now(); - Duration interval = Duration.between(this.lastCheckTime, now); - if (!interval.minus(CHECK_INTERVAL).isNegative()) { - this.verify(); - this.lastCheckTime = now; - } - } - public synchronized void install(HugeConfig config, GraphManager graphManager, String md5) { this.manager.config(config); this.manager.graphManager(graphManager); + LicenseManager licenseManager = this.manager.licenseManager(); try { - this.manager.uninstall(); - File licenseFile = new File(this.verifyParam.licensePath()); + licenseManager.uninstallLicense(); this.verifyPublicCert(md5); - LicenseContent content = this.manager.install(licenseFile); - LOG.info("The license is successfully installed, valid for {} - {}", - content.getNotBefore(), content.getNotAfter()); + LicenseParams params = licenseManager.installLicense(); + LOG.info("The license '{}' is successfully installed for '{}', " + + "the term of validity is from {} to {}", + params.subject(), params.consumerType(), + params.notBefore(), params.notAfter()); } catch (Exception e) { LOG.error("Failed to install license", e); throw new HugeException("Failed to install license", e); } } + public void verifyIfNeeded() { + Instant now = Instant.now(); + Duration interval = Duration.between(this.lastCheckTime, now); + if (!interval.minus(CHECK_INTERVAL).isNegative()) { + this.verify(); + this.lastCheckTime = now; + } + } + public void verify() { try { - LicenseContent content = this.manager.verify(); - LOG.info("The license verification passed, valid for {} - {}", - content.getNotBefore(), content.getNotAfter()); + LicenseParams params = this.manager.licenseManager() + .verifyLicense(); + LOG.info("The license verification passed, " + + "the term of validity is from {} to {}", + params.notBefore(), params.notAfter()); } catch (Exception e) { LOG.error("Failed to verify license", e); throw new HugeException("Failed to verify license", e); @@ -114,7 +107,7 @@ public void verify() { } private void verifyPublicCert(String expectMD5) { - String path = this.verifyParam.publicKeyPath(); + String path = this.installParam.publicKeyPath(); try (InputStream is = LicenseVerifier.class.getResourceAsStream(path)) { String actualMD5 = DigestUtils.md5Hex(is); if (!actualMD5.equals(expectMD5)) { @@ -126,30 +119,15 @@ private void verifyPublicCert(String expectMD5) { } } - private LicenseParam initLicenseParam(LicenseVerifyParam param) { - Preferences preferences = Preferences.userNodeForPackage( - LicenseVerifier.class); - CipherParam cipherParam = new DefaultCipherParam( - param.storePassword()); - KeyStoreParam keyStoreParam = new DefaultKeyStoreParam( - LicenseVerifier.class, - param.publicKeyPath(), - param.publicAlias(), - param.storePassword(), - null); - return new DefaultLicenseParam(param.subject(), preferences, - keyStoreParam, cipherParam); - } - - private static LicenseVerifyParam buildVerifyParam(String path) { + private static LicenseInstallParam buildInstallParam(String path) { // NOTE: can't use JsonUtil due to it bind tinkerpop jackson ObjectMapper mapper = new ObjectMapper(); try (InputStream stream = LicenseVerifier.class.getResourceAsStream(path)) { - return mapper.readValue(stream, LicenseVerifyParam.class); + return mapper.readValue(stream, LicenseInstallParam.class); } catch (IOException e) { throw new HugeException("Failed to read json stream to %s", - LicenseVerifyParam.class); + LicenseInstallParam.class); } } } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifyManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifyManager.java index 93d8ecb17d..a4aeb8649a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifyManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/license/LicenseVerifyManager.java @@ -19,7 +19,6 @@ package com.baidu.hugegraph.license; -import java.io.IOException; import java.lang.management.ManagementFactory; import java.net.InetAddress; import java.net.UnknownHostException; @@ -38,30 +37,28 @@ import com.baidu.hugegraph.util.Log; import com.baidu.hugegraph.util.VersionUtil; import com.baidu.hugegraph.version.CoreVersion; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.sun.management.OperatingSystemMXBean; -import de.schlichtherle.license.LicenseContent; -import de.schlichtherle.license.LicenseContentException; -import de.schlichtherle.license.LicenseParam; - -public class LicenseVerifyManager extends CommonLicenseManager { +public class LicenseVerifyManager { private static final Logger LOG = Log.logger(LicenseVerifyManager.class); - private static final ObjectMapper MAPPER = new ObjectMapper(); - private static final int NO_LIMIT = -1; + private final LicenseManager licenseManager; + private final MachineInfo machineInfo; private HugeConfig config; private GraphManager graphManager; - private final MachineInfo machineInfo; - public LicenseVerifyManager(LicenseParam param) { - super(param); + public LicenseVerifyManager(LicenseInstallParam param) { + this.licenseManager = LicenseManagerFactory.create(param, + this::validate); this.machineInfo = new MachineInfo(); } + public LicenseManager licenseManager() { + return this.licenseManager; + } + public void config(HugeConfig config) { this.config = config; } @@ -82,32 +79,13 @@ public GraphManager graphManager() { return this.graphManager; } - @Override - protected synchronized void validate(LicenseContent content) { - // Call super validate firstly to verify the common license parameters - try { - super.validate(content); - } catch (LicenseContentException e) { - LOG.error("Failed to verify license", e); - throw new HugeException("Failed to verify license", e); - } - + private synchronized void validate(LicenseParams params) { // Verify the customized license parameters. - List extraParams; - try { - TypeReference> type; - type = new TypeReference>() {}; - extraParams = MAPPER.readValue((String) content.getExtra(), type); - } catch (IOException e) { - LOG.error("Failed to read extra params", e); - throw new HugeException("Failed to read extra params", e); - } - String serverId = this.getServerId(); - LOG.debug("server id is {}", serverId); - ExtraParam param = this.matchParam(serverId, extraParams); + LOG.debug("Verify server id '{}'", serverId); + LicenseExtraParam param = params.matchParam(serverId); if (param == null) { - throw new HugeException("The current server's id is not authorized"); + throw new HugeException("The current server id is not authorized"); } this.checkVersion(param); @@ -123,16 +101,7 @@ private String getServerId() { return this.config().get(ServerOptions.SERVER_ID); } - private ExtraParam matchParam(String id, List extraParams) { - for (ExtraParam param : extraParams) { - if (param.id().equals(id)) { - return param; - } - } - return null; - } - - private void checkVersion(ExtraParam param) { + private void checkVersion(LicenseExtraParam param) { String expectVersion = param.version(); if (StringUtils.isEmpty(expectVersion)) { return; @@ -145,9 +114,9 @@ private void checkVersion(ExtraParam param) { } } - private void checkGraphs(ExtraParam param) { + private void checkGraphs(LicenseExtraParam param) { int expectGraphs = param.graphs(); - if (expectGraphs == NO_LIMIT) { + if (expectGraphs == LicenseExtraParam.NO_LIMIT) { return; } int actualGraphs = this.graphManager().graphs().size(); @@ -158,7 +127,7 @@ private void checkGraphs(ExtraParam param) { } } - private void checkIpAndMac(ExtraParam param) { + private void checkIpAndMac(LicenseExtraParam param) { String expectIp = param.ip(); boolean matched = false; List actualIps = this.machineInfo.getIpAddress(); @@ -216,9 +185,9 @@ private void checkIpAndMac(ExtraParam param) { } } - private void checkCpu(ExtraParam param) { + private void checkCpu(LicenseExtraParam param) { int expectCpus = param.cpus(); - if (expectCpus == NO_LIMIT) { + if (expectCpus == LicenseExtraParam.NO_LIMIT) { return; } int actualCpus = CoreOptions.CPUS; @@ -229,10 +198,10 @@ private void checkCpu(ExtraParam param) { } } - private void checkRam(ExtraParam param) { + private void checkRam(LicenseExtraParam param) { // Unit MB int expectRam = param.ram(); - if (expectRam == NO_LIMIT) { + if (expectRam == LicenseExtraParam.NO_LIMIT) { return; } OperatingSystemMXBean mxBean = (OperatingSystemMXBean) ManagementFactory @@ -245,9 +214,9 @@ private void checkRam(ExtraParam param) { } } - private void checkThreads(ExtraParam param) { + private void checkThreads(LicenseExtraParam param) { int expectThreads = param.threads(); - if (expectThreads == NO_LIMIT) { + if (expectThreads == LicenseExtraParam.NO_LIMIT) { return; } int actualThreads = this.config().get(ServerOptions.MAX_WORKER_THREADS); @@ -258,10 +227,10 @@ private void checkThreads(ExtraParam param) { } } - private void checkMemory(ExtraParam param) { + private void checkMemory(LicenseExtraParam param) { // Unit MB int expectMemory = param.memory(); - if (expectMemory == NO_LIMIT) { + if (expectMemory == LicenseExtraParam.NO_LIMIT) { return; } /* diff --git a/hugegraph-core/pom.xml b/hugegraph-core/pom.xml index fdd2e7652d..71b34064ad 100644 --- a/hugegraph-core/pom.xml +++ b/hugegraph-core/pom.xml @@ -19,7 +19,7 @@ com.baidu.hugegraph hugegraph-common - 1.8.8 + 2.0.0 diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java index f912d6e722..d8e845d7ae 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/version/CoreVersion.java @@ -39,7 +39,7 @@ public class CoreVersion { public static void check() { // Check version of hugegraph-common - VersionUtil.check(CommonVersion.VERSION, "1.8.0", "1.9", + VersionUtil.check(CommonVersion.VERSION, "2.0.0", "2.1", CommonVersion.NAME); } }