Skip to content

Commit

Permalink
remove truelicence dependency
Browse files Browse the repository at this point in the history
Change-Id: Ifbae8ea27041baaf3719f3a5ef84a1ec33010858
  • Loading branch information
javeme committed Dec 16, 2021
1 parent f6a492c commit 251ca9a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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() {
Expand All @@ -75,46 +64,50 @@ 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);
}
}

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)) {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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<ExtraParam> extraParams;
try {
TypeReference<List<ExtraParam>> type;
type = new TypeReference<List<ExtraParam>>() {};
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);
Expand All @@ -123,16 +101,7 @@ private String getServerId() {
return this.config().get(ServerOptions.SERVER_ID);
}

private ExtraParam matchParam(String id, List<ExtraParam> 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;
Expand All @@ -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();
Expand All @@ -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<String> actualIps = this.machineInfo.getIpAddress();
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
/*
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.8.8</version>
<version>2.0.0</version>
</dependency>

<!-- tinkerpop -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 251ca9a

Please sign in to comment.