Skip to content

Commit

Permalink
fix: HugeGraphAuthProxy cannot be cast to HugeGraph
Browse files Browse the repository at this point in the history
fix #126

Change-Id: Ifff14f5a0606b315f6d370f77ad71585ac801cf4
  • Loading branch information
javeme authored and zhoney committed Oct 25, 2018
1 parent 4bdbc87 commit 7b8d9f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class GraphsAPI extends API {
@RolesAllowed({"admin", "$dynamic"})
public Object list(@Context GraphManager manager,
@Context SecurityContext sc) {
Set<String> graphs = manager.graphs().keySet();
Set<String> graphs = manager.graphs();
String role = sc.getUserPrincipal().getName();
if (role.equals("admin")) {
return ImmutableMap.of("graphs", graphs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

public class HugeFactoryAuthProxy {

public static final String GRAPH_FACTORY =
"gremlin.graph=com.baidu.hugegraph.auth.HugeFactoryAuthProxy";

static {
HugeGraphAuthProxy.setContext(Context.admin());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public boolean requireAuthentication() {
@Override
public AuthenticatedUser authenticate(final Map<String, String> credentials)
throws AuthenticationException {
if (!this.requireAuthentication()) {
return AuthenticatedUser.ANONYMOUS_USER;
}
String username = credentials.get(PROPERTY_USERNAME);
String password = credentials.get(PROPERTY_PASSWORD);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.core;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand All @@ -34,6 +35,7 @@
import org.slf4j.Logger;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.auth.HugeFactoryAuthProxy;
import com.baidu.hugegraph.auth.HugeGraphAuthProxy;
import com.baidu.hugegraph.auth.StandardAuthenticator;
import com.baidu.hugegraph.backend.cache.Cache;
Expand Down Expand Up @@ -68,21 +70,19 @@ public GraphManager(HugeConfig conf) {
}

public void loadGraphs(final Map<String, String> graphConfs) {
graphConfs.entrySet().forEach(conf -> {
for (Map.Entry<String, String> conf : graphConfs.entrySet()) {
String name = conf.getKey();
String path = conf.getValue();
try {
final Graph newGraph = GraphFactory.open(conf.getValue());
this.graphs.put(conf.getKey(), newGraph);
LOG.info("Graph '{}' was successfully configured via '{}'",
conf.getKey(), conf.getValue());
this.loadGraph(name, path);
} catch (RuntimeException e) {
LOG.error("Graph '{}': '{}' can't be instantiated",
conf.getKey(), conf.getValue(), e);
LOG.error("Graph '{}' can't be loaded: '{}'", name, path, e);
}
});
}
}

public Map<String, Graph> graphs() {
return this.graphs;
public Set<String> graphs() {
return Collections.unmodifiableSet(this.graphs.keySet());
}

public HugeGraph graph(String name) {
Expand All @@ -106,8 +106,7 @@ public Serializer serializer(Graph g) {
}

public void rollbackAll() {
this.graphs.entrySet().forEach(e -> {
final Graph graph = e.getValue();
this.graphs.values().forEach(graph -> {
if (graph.features().graph().supportsTransactions() &&
graph.tx().isOpen()) {
graph.tx().rollback();
Expand All @@ -120,8 +119,7 @@ public void rollback(final Set<String> graphSourceNamesToCloseTxOn) {
}

public void commitAll() {
this.graphs.entrySet().forEach(e -> {
final Graph graph = e.getValue();
this.graphs.values().forEach(graph -> {
if (graph.features().graph().supportsTransactions() &&
graph.tx().isOpen()) {
graph.tx().commit();
Expand All @@ -137,9 +135,9 @@ private void closeTx(final Set<String> graphSourceNamesToCloseTxOn,
final Transaction.Status tx) {
final Set<Graph> graphsToCloseTxOn = new HashSet<>();

graphSourceNamesToCloseTxOn.forEach(r -> {
if (this.graphs.containsKey(r)) {
graphsToCloseTxOn.add(this.graphs.get(r));
graphSourceNamesToCloseTxOn.forEach(name -> {
if (this.graphs.containsKey(name)) {
graphsToCloseTxOn.add(this.graphs.get(name));
}
});

Expand All @@ -163,9 +161,21 @@ public String authenticate(String username, String password) {
return this.authenticator.authenticate(username, password);
}

private void loadGraph(String name, String path) {
final Graph graph = GraphFactory.open(path);
this.graphs.put(name, graph);
LOG.info("Graph '{}' was successfully configured via '{}'", name, path);

if (this.authenticator.requireAuthentication() &&
!(graph instanceof HugeGraphAuthProxy)) {
LOG.warn("You may need to support access control for '{}' with {}",
path, HugeFactoryAuthProxy.GRAPH_FACTORY);
}
}

private void checkBackendVersionOrExit() {
for (Graph graph : this.graphs.values()) {
HugeGraph hugegraph = (HugeGraph) graph;
for (String graph : this.graphs()) {
HugeGraph hugegraph = this.graph(graph);
if (InMemoryDBStoreProvider.matchType(hugegraph.backend())) {
continue;
}
Expand All @@ -176,14 +186,15 @@ private void checkBackendVersionOrExit() {
System.exit(-1);
}
if (!info.checkVersion()) {
// Exit if versions are inconsistent
System.exit(-1);
}
}
}

private void addMetrics(HugeConfig config) {
final MetricManager metrics = MetricManager.INSTANCE;
// Force add server reporter
// Force to add server reporter
ServerReporter reporter = ServerReporter.instance(metrics.getRegistry());
reporter.start(60L, TimeUnit.SECONDS);

Expand Down Expand Up @@ -220,8 +231,8 @@ private void registerCacheMetrics() {
String exp = String.format("%s.%s", key, "expire");
String size = String.format("%s.%s", key, "size");
String cap = String.format("%s.%s", key, "capacity");
// Avoid register multi times

// Avoid registering multiple times
if (names.stream().anyMatch(name -> name.endsWith(hits))) {
continue;
}
Expand Down

0 comments on commit 7b8d9f4

Please sign in to comment.