Search in sources :

Example 6 with ManagementSystem

use of org.janusgraph.graphdb.database.management.ManagementSystem in project janusgraph by JanusGraph.

the class SaslAndHMACAuthenticatorTest method testSetupEmptyCredGraphNoUserIndex.

@Test
public void testSetupEmptyCredGraphNoUserIndex() {
    final SaslAndHMACAuthenticator authenticator = createMockBuilder(SaslAndHMACAuthenticator.class).addMockedMethod("openGraph").addMockedMethod("createCredentialGraph").addMockedMethod("createSimpleAuthenticator").addMockedMethod("createHMACAuthenticator").createMock();
    final Map<String, Object> configMap = new HashMap<String, Object>();
    configMap.put(CONFIG_CREDENTIALS_DB, "configCredDb");
    configMap.put(SaslAndHMACAuthenticator.CONFIG_DEFAULT_PASSWORD, "pass");
    configMap.put(SaslAndHMACAuthenticator.CONFIG_DEFAULT_USER, "user");
    final JanusGraph graph = createMock(JanusGraph.class);
    final CredentialGraph credentialGraph = createMock(CredentialGraph.class);
    final ManagementSystem mgmt = createMock(ManagementSystem.class);
    final JanusGraphSimpleAuthenticator janusSimpleAuthenticator = createMock(JanusGraphSimpleAuthenticator.class);
    final HMACAuthenticator hmacAuthenticator = createMock(HMACAuthenticator.class);
    final SimpleAuthenticator simpleAuthenticator = createMock(SimpleAuthenticator.class);
    final Transaction tx = createMock(Transaction.class);
    final PropertyKey pk = createMock(PropertyKey.class);
    final PropertyKeyMaker pkm = createMock(PropertyKeyMaker.class);
    final JanusGraphManagement.IndexBuilder indexBuilder = createMock(JanusGraphManagement.IndexBuilder.class);
    final JanusGraphIndex index = createMock(JanusGraphIndex.class);
    final PropertyKey[] pks = { pk };
    expect(authenticator.openGraph(isA(String.class))).andReturn(graph);
    expect(authenticator.createCredentialGraph(isA(JanusGraph.class))).andReturn(credentialGraph);
    expect(authenticator.createSimpleAuthenticator()).andReturn(janusSimpleAuthenticator);
    expect(authenticator.createHMACAuthenticator()).andReturn(hmacAuthenticator);
    hmacAuthenticator.setup(configMap);
    expectLastCall();
    expect(janusSimpleAuthenticator.createSimpleAuthenticator()).andReturn(simpleAuthenticator);
    simpleAuthenticator.setup(configMap);
    expectLastCall();
    expect(credentialGraph.findUser("user")).andReturn(null);
    expect(credentialGraph.createUser(eq("user"), eq("pass"))).andReturn(null);
    expect(graph.openManagement()).andReturn(mgmt).times(2);
    expect(graph.tx()).andReturn(tx);
    expect(index.getFieldKeys()).andReturn(pks);
    expect(index.getIndexStatus(eq(pk))).andReturn(SchemaStatus.ENABLED);
    tx.rollback();
    expectLastCall();
    expect(mgmt.containsGraphIndex(eq("byUsername"))).andReturn(false);
    expect(mgmt.makePropertyKey(PROPERTY_USERNAME)).andReturn(pkm);
    expect(pkm.dataType(eq(String.class))).andReturn(pkm);
    expect(pkm.cardinality(Cardinality.SINGLE)).andReturn(pkm);
    expect(pkm.make()).andReturn(pk);
    expect(mgmt.buildIndex(eq("byUsername"), eq(Vertex.class))).andReturn(indexBuilder);
    expect(mgmt.getGraphIndex(eq("byUsername"))).andReturn(index);
    expect(indexBuilder.addKey(eq(pk))).andReturn(indexBuilder);
    expect(indexBuilder.unique()).andReturn(indexBuilder);
    expect(indexBuilder.buildCompositeIndex()).andReturn(index);
    mgmt.commit();
    expectLastCall();
    mgmt.rollback();
    expectLastCall();
    replayAll();
    authenticator.setup(configMap);
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CredentialGraph(org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraph) HashMap(java.util.HashMap) JanusGraph(org.janusgraph.core.JanusGraph) ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) PropertyKeyMaker(org.janusgraph.core.schema.PropertyKeyMaker) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) SimpleAuthenticator(org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 7 with ManagementSystem

use of org.janusgraph.graphdb.database.management.ManagementSystem in project janusgraph by JanusGraph.

the class JanusGraphSimpleAuthenticatorTest method testSetupEmptyCredGraphNoUserIndex.

@Test
public void testSetupEmptyCredGraphNoUserIndex() {
    final JanusGraphSimpleAuthenticator authenticator = createMockBuilder(JanusGraphSimpleAuthenticator.class).addMockedMethod("openGraph").addMockedMethod("createCredentialGraph").addMockedMethod("createSimpleAuthenticator").createMock();
    final Map<String, Object> configMap = new HashMap<String, Object>();
    configMap.put(CONFIG_CREDENTIALS_DB, "configCredDb");
    configMap.put(JanusGraphSimpleAuthenticator.CONFIG_DEFAULT_PASSWORD, "pass");
    configMap.put(JanusGraphSimpleAuthenticator.CONFIG_DEFAULT_USER, "user");
    final JanusGraph graph = createMock(JanusGraph.class);
    final CredentialGraph credentialGraph = createMock(CredentialGraph.class);
    final ManagementSystem mgmt = createMock(ManagementSystem.class);
    final Transaction tx = createMock(Transaction.class);
    final PropertyKey pk = createMock(PropertyKey.class);
    final PropertyKeyMaker pkm = createMock(PropertyKeyMaker.class);
    final JanusGraphManagement.IndexBuilder indexBuilder = createMock(JanusGraphManagement.IndexBuilder.class);
    final JanusGraphIndex index = createMock(JanusGraphIndex.class);
    final PropertyKey[] pks = { pk };
    final SimpleAuthenticator sa = createMock(SimpleAuthenticator.class);
    expect(authenticator.openGraph(isA(String.class))).andReturn(graph);
    expect(authenticator.createCredentialGraph(isA(JanusGraph.class))).andReturn(credentialGraph);
    expect(authenticator.createSimpleAuthenticator()).andReturn(sa);
    expect(credentialGraph.findUser("user")).andReturn(null);
    expect(credentialGraph.createUser(eq("user"), eq("pass"))).andReturn(null);
    expect(graph.openManagement()).andReturn(mgmt);
    expect(graph.tx()).andReturn(tx);
    sa.setup(configMap);
    EasyMock.expectLastCall();
    graph.close();
    EasyMock.expectLastCall();
    tx.rollback();
    EasyMock.expectLastCall();
    expect(mgmt.containsGraphIndex(eq("byUsername"))).andReturn(false);
    expect(mgmt.makePropertyKey(PROPERTY_USERNAME)).andReturn(pkm);
    expect(pkm.dataType(eq(String.class))).andReturn(pkm);
    expect(pkm.cardinality(Cardinality.SINGLE)).andReturn(pkm);
    expect(pkm.make()).andReturn(pk);
    expect(mgmt.buildIndex(eq("byUsername"), eq(Vertex.class))).andReturn(indexBuilder);
    expect(mgmt.getGraphIndex(eq("byUsername"))).andReturn(index);
    expect(indexBuilder.addKey(eq(pk))).andReturn(indexBuilder);
    expect(indexBuilder.unique()).andReturn(indexBuilder);
    expect(indexBuilder.buildCompositeIndex()).andReturn(index);
    expect(index.getFieldKeys()).andReturn(pks);
    expect(index.getIndexStatus(eq(pk))).andReturn(SchemaStatus.ENABLED);
    expect(graph.tx()).andReturn(tx);
    expect(graph.openManagement()).andReturn(mgmt);
    tx.rollback();
    EasyMock.expectLastCall().atLeastOnce();
    mgmt.commit();
    EasyMock.expectLastCall();
    mgmt.rollback();
    EasyMock.expectLastCall();
    replayAll();
    authenticator.setup(configMap);
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CredentialGraph(org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraph) HashMap(java.util.HashMap) JanusGraph(org.janusgraph.core.JanusGraph) ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) PropertyKeyMaker(org.janusgraph.core.schema.PropertyKeyMaker) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) SimpleAuthenticator(org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 8 with ManagementSystem

use of org.janusgraph.graphdb.database.management.ManagementSystem in project janusgraph by JanusGraph.

the class ConfiguredGraphFactory method removeGraphFromCache.

private static void removeGraphFromCache(final JanusGraph graph) {
    final JanusGraphManager jgm = JanusGraphManagerUtility.getInstance();
    Preconditions.checkState(jgm != null, JANUS_GRAPH_MANAGER_EXPECTED_STATE_MSG);
    jgm.removeGraph(((StandardJanusGraph) graph).getGraphName());
    final ManagementSystem mgmt = (ManagementSystem) graph.openManagement();
    mgmt.evictGraphFromCache();
    mgmt.commit();
}
Also used : ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) JanusGraphManager(org.janusgraph.graphdb.management.JanusGraphManager)

Example 9 with ManagementSystem

use of org.janusgraph.graphdb.database.management.ManagementSystem in project janusgraph by JanusGraph.

the class JanusGraphAbstractAuthenticator method setup.

@Override
public void setup(final Map<String, Object> config) {
    logger.info("Initializing authentication with the {}", this.getClass().getName());
    Preconditions.checkArgument(config != null, String.format("Could not configure a %s - provide a 'config' in the 'authentication' settings", this.getClass().getName()));
    Preconditions.checkState(config.containsKey(CONFIG_CREDENTIALS_DB), String.format("Credential configuration missing the %s key that points to a graph config file or graph name", CONFIG_CREDENTIALS_DB));
    if (!config.containsKey(CONFIG_DEFAULT_USER) || !config.containsKey(CONFIG_DEFAULT_PASSWORD)) {
        logger.warn(String.format("%s and %s should be defined to bootstrap authentication", CONFIG_DEFAULT_USER, CONFIG_DEFAULT_PASSWORD));
    }
    final JanusGraph graph = openGraph(config.get(CONFIG_CREDENTIALS_DB).toString());
    backingGraph = graph;
    credentialStore = createCredentialGraph(graph);
    graph.tx().rollback();
    ManagementSystem mgmt = (ManagementSystem) graph.openManagement();
    if (!mgmt.containsGraphIndex(USERNAME_INDEX_NAME)) {
        final PropertyKey username = mgmt.makePropertyKey(PROPERTY_USERNAME).dataType(String.class).cardinality(Cardinality.SINGLE).make();
        mgmt.buildIndex(USERNAME_INDEX_NAME, Vertex.class).addKey(username).unique().buildCompositeIndex();
        mgmt.commit();
        mgmt = (ManagementSystem) graph.openManagement();
        final JanusGraphIndex index = mgmt.getGraphIndex(USERNAME_INDEX_NAME);
        if (!index.getIndexStatus(username).equals(SchemaStatus.ENABLED)) {
            try {
                mgmt = (ManagementSystem) graph.openManagement();
                mgmt.updateIndex(mgmt.getGraphIndex(USERNAME_INDEX_NAME), SchemaAction.REINDEX);
                ManagementSystem.awaitGraphIndexStatus(graph, USERNAME_INDEX_NAME).status(SchemaStatus.ENABLED).call();
                mgmt.commit();
            } catch (InterruptedException rude) {
                mgmt.rollback();
                throw new RuntimeException("Timed out waiting for byUsername index to be created on credential graph", rude);
            }
        }
    }
    final String defaultUser = config.get(CONFIG_DEFAULT_USER).toString();
    if (credentialStore.findUser(defaultUser) == null) {
        credentialStore.createUser(defaultUser, config.get(CONFIG_DEFAULT_PASSWORD).toString());
    }
}
Also used : ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey)

Example 10 with ManagementSystem

use of org.janusgraph.graphdb.database.management.ManagementSystem in project janusgraph by JanusGraph.

the class HMACAuthenticatorTest method testAuthenticateBasicAuthValid.

@Test
public void testAuthenticateBasicAuthValid() throws AuthenticationException {
    final Map<String, String> credentials = new HashMap<>();
    credentials.put(PROPERTY_USERNAME, "user");
    credentials.put(PROPERTY_PASSWORD, "pass");
    final HMACAuthenticator authenticator = createMockBuilder(HMACAuthenticator.class).addMockedMethod("openGraph").addMockedMethod("createCredentialGraph").createMock();
    final Map<String, Object> configMap = new HashMap<String, Object>();
    configMap.put(CONFIG_CREDENTIALS_DB, "configCredDb");
    configMap.put(HMACAuthenticator.CONFIG_HMAC_SECRET, "secret");
    configMap.put(HMACAuthenticator.CONFIG_DEFAULT_PASSWORD, "pass");
    configMap.put(HMACAuthenticator.CONFIG_DEFAULT_USER, "user");
    final JanusGraph graph = createMock(JanusGraph.class);
    final CredentialGraph credentialGraph = createMock(CredentialGraph.class);
    final ManagementSystem mgmt = createMock(ManagementSystem.class);
    final Transaction tx = createMock(Transaction.class);
    final Vertex userVertex = createMock(Vertex.class);
    final String bcryptedPass = BCrypt.hashpw("pass", BCrypt.gensalt(4));
    expect(authenticator.openGraph(isA(String.class))).andReturn(graph);
    expect(authenticator.createCredentialGraph(isA(JanusGraph.class))).andReturn(credentialGraph);
    expect(credentialGraph.findUser(eq("user"))).andReturn(userVertex).times(2);
    expect(userVertex.value(eq(PROPERTY_PASSWORD))).andReturn(bcryptedPass);
    expect(graph.openManagement()).andReturn(mgmt);
    expect(graph.tx()).andReturn(tx);
    expect(mgmt.containsGraphIndex(eq("byUsername"))).andReturn(true);
    tx.rollback();
    expectLastCall();
    replayAll();
    authenticator.setup(configMap);
    authenticator.authenticate(credentials);
    verifyAll();
}
Also used : ManagementSystem(org.janusgraph.graphdb.database.management.ManagementSystem) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CredentialGraph(org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraph) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) HashMap(java.util.HashMap) JanusGraph(org.janusgraph.core.JanusGraph) Test(org.junit.Test)

Aggregations

ManagementSystem (org.janusgraph.graphdb.database.management.ManagementSystem)18 HashMap (java.util.HashMap)16 JanusGraph (org.janusgraph.core.JanusGraph)15 CredentialGraph (org.apache.tinkerpop.gremlin.groovy.jsr223.dsl.credential.CredentialGraph)14 Transaction (org.apache.tinkerpop.gremlin.structure.Transaction)14 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)12 Test (org.junit.Test)12 SimpleAuthenticator (org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator)5 PropertyKey (org.janusgraph.core.PropertyKey)4 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)4 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)3 PropertyKeyMaker (org.janusgraph.core.schema.PropertyKeyMaker)3 MapConfiguration (org.apache.commons.configuration.MapConfiguration)2 AuthenticationException (org.apache.tinkerpop.gremlin.server.auth.AuthenticationException)2 CommonsConfiguration (org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration)2 GraphDatabaseConfiguration (org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration)2 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)2 Settings (org.apache.tinkerpop.gremlin.server.Settings)1 JanusGraphManager (org.janusgraph.graphdb.management.JanusGraphManager)1