From 7b8d9f46ca8dc6abe0ed103bb2606816d88b1070 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Tue, 23 Oct 2018 22:36:57 +0800 Subject: [PATCH] fix: HugeGraphAuthProxy cannot be cast to HugeGraph fix #126 Change-Id: Ifff14f5a0606b315f6d370f77ad71585ac801cf4 --- .../com/baidu/hugegraph/api/GraphsAPI.java | 2 +- .../hugegraph/auth/HugeFactoryAuthProxy.java | 3 ++ .../hugegraph/auth/StandardAuthenticator.java | 3 ++ .../baidu/hugegraph/core/GraphManager.java | 53 +++++++++++-------- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java index 7ee91fd6d6..8c1f2e3c9e 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java @@ -64,7 +64,7 @@ public class GraphsAPI extends API { @RolesAllowed({"admin", "$dynamic"}) public Object list(@Context GraphManager manager, @Context SecurityContext sc) { - Set graphs = manager.graphs().keySet(); + Set graphs = manager.graphs(); String role = sc.getUserPrincipal().getName(); if (role.equals("admin")) { return ImmutableMap.of("graphs", graphs); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeFactoryAuthProxy.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeFactoryAuthProxy.java index a7ae8fa7be..2a97231b09 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeFactoryAuthProxy.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/HugeFactoryAuthProxy.java @@ -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()); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java index 549e71eb07..6ff59a31c8 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java @@ -82,6 +82,9 @@ public boolean requireAuthentication() { @Override public AuthenticatedUser authenticate(final Map credentials) throws AuthenticationException { + if (!this.requireAuthentication()) { + return AuthenticatedUser.ANONYMOUS_USER; + } String username = credentials.get(PROPERTY_USERNAME); String password = credentials.get(PROPERTY_PASSWORD); diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 6db9896392..2b39c45ff6 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -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; @@ -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; @@ -68,21 +70,19 @@ public GraphManager(HugeConfig conf) { } public void loadGraphs(final Map graphConfs) { - graphConfs.entrySet().forEach(conf -> { + for (Map.Entry 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 graphs() { - return this.graphs; + public Set graphs() { + return Collections.unmodifiableSet(this.graphs.keySet()); } public HugeGraph graph(String name) { @@ -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(); @@ -120,8 +119,7 @@ public void rollback(final Set 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(); @@ -137,9 +135,9 @@ private void closeTx(final Set graphSourceNamesToCloseTxOn, final Transaction.Status tx) { final Set 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)); } }); @@ -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; } @@ -176,6 +186,7 @@ private void checkBackendVersionOrExit() { System.exit(-1); } if (!info.checkVersion()) { + // Exit if versions are inconsistent System.exit(-1); } } @@ -183,7 +194,7 @@ private void checkBackendVersionOrExit() { 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); @@ -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; }