Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to verify license #645

Merged
merged 10 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hugegraph-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.ServerOptions;
import com.baidu.hugegraph.define.WorkLoad;
import com.baidu.hugegraph.license.LicenseVerifier;
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.E;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -62,6 +63,8 @@ public class LoadDetectFilter implements ContainerRequestFilter {

@Override
public void filter(ContainerRequestContext context) {
LicenseVerifier.instance().verifyIfNeeded();

if (LoadDetectFilter.isWhiteAPI(context)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public static synchronized ServerOptions instance() {
"http://127.0.0.1:8080"
);

public static final ConfigOption<String> SERVER_ID =
new ConfigOption<>(
"server.id",
"The id of hugegraph-server.",
disallowEmpty(),
"server-1"
);

public static final ConfigOption<Integer> MAX_WORKER_THREADS =
new ConfigOption<>(
"restserver.max_worker_threads",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
import com.baidu.hugegraph.auth.HugeAuthenticator;
import com.baidu.hugegraph.auth.HugeFactoryAuthProxy;
import com.baidu.hugegraph.auth.HugeGraphAuthProxy;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.cache.Cache;
import com.baidu.hugegraph.backend.cache.CacheManager;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.ServerOptions;
import com.baidu.hugegraph.exception.NotSupportException;
import com.baidu.hugegraph.license.LicenseVerifier;
import com.baidu.hugegraph.metrics.MetricsUtil;
import com.baidu.hugegraph.metrics.ServerReporter;
import com.baidu.hugegraph.serializer.JsonSerializer;
Expand All @@ -70,6 +72,7 @@ public GraphManager(HugeConfig conf) {
}

this.loadGraphs(conf.getMap(ServerOptions.GRAPHS));
this.installLicense(conf);
this.checkBackendVersionOrExit();
this.restoreUncompletedTasks();
this.addMetrics(conf);
Expand Down Expand Up @@ -135,6 +138,10 @@ public void commit(final Set<String> graphSourceNamesToCloseTxOn) {
closeTx(graphSourceNamesToCloseTxOn, Transaction.Status.COMMIT);
}

private void installLicense(HugeConfig config) {
LicenseVerifier.instance().install(config, this);
}

private void closeTx(final Set<String> graphSourceNamesToCloseTxOn,
final Transaction.Status tx) {
final Set<Graph> graphsToCloseTxOn = new HashSet<>();
Expand Down Expand Up @@ -192,13 +199,13 @@ private void checkBackendVersionOrExit() {
}
BackendStoreSystemInfo info = new BackendStoreSystemInfo(hugegraph);
if (!info.exist()) {
LOG.error("The backend store of '{}' has not been initialized",
throw new BackendException(
"The backend store of '%s' has not been initialized",
hugegraph.name());
System.exit(-1);
}
if (!info.checkVersion()) {
// Exit if versions are inconsistent
System.exit(-1);
throw new BackendException(
"The backend store version is inconsistent");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.
*/

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.slf4j.Logger;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.util.JsonUtil;
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);

private static final String LICENSE_PARAM_PATH = "/verify-license.json";

private static volatile LicenseVerifier INSTANCE = null;

private static final Duration CHECK_INTERVAL = Duration.ofMinutes(10);
private volatile Instant lastCheckTime = Instant.now();

private final LicenseVerifyParam verifyParam;
private final LicenseVerifyManager manager;

private LicenseVerifier() {
this.verifyParam = buildVerifyParam(LICENSE_PARAM_PATH);
LicenseParam licenseParam = this.initLicenseParam(this.verifyParam);
this.manager = new LicenseVerifyManager(licenseParam);
}

public static LicenseVerifier instance() {
if (INSTANCE == null) {
synchronized(LicenseVerifier.class) {
if (INSTANCE == null) {
INSTANCE = new LicenseVerifier();
}
}
}
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) {
this.manager.config(config);
this.manager.graphManager(graphManager);
try {
this.manager.uninstall();
File licenseFile = new File(this.verifyParam.licensePath());
LicenseContent content = this.manager.install(licenseFile);
LOG.info("The license is successfully installed, valid for {} - {}",
content.getNotBefore(), content.getNotAfter());
} catch (Exception e) {
throw new HugeException("Failed to install license", e);
}
}

public void verify() {
try {
LicenseContent content = this.manager.verify();
LOG.info("The license verification passed, valid for {} - {}",
content.getNotBefore(), content.getNotAfter());
} catch (Exception e) {
throw new HugeException("Failed to verify license", e);
}
}

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) {
InputStream stream = LicenseVerifier.class.getResourceAsStream(path);
// NOTE: can't use JsonUtil due to it bind tinkerpop jackson
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(stream, LicenseVerifyParam.class);
Linary marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException e) {
throw new HugeException("Failed to read json stream to %s",
LicenseVerifyParam.class);
}
}
}
Loading