Skip to content

Commit

Permalink
Merge pull request #11099 from pontusmelke/3.4-token-access
Browse files Browse the repository at this point in the history
More token access in Kernel API
  • Loading branch information
fickludd committed Mar 2, 2018
2 parents ae89035 + 486e414 commit 1716c84
Show file tree
Hide file tree
Showing 58 changed files with 699 additions and 416 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.internal.kernel.api;

/**
* A token with its associated name.
*/
public final class NamedToken
{
private final int id;
private final String name;

public NamedToken( String name, int id )
{
this.id = id;
this.name = name;
}

/**
* Id of token
*
* @return the id of the token
*/
public int id()
{
return id;
}

/**
* The name associated with the token
*
* @return The name corresponding to the token
*/
public String name()
{
return name;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}

NamedToken that = (NamedToken) o;

return id == that.id && name.equals( that.name );
}

@Override
public int hashCode()
{
int result = id;
result = 31 * result + name.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public interface SchemaRead
*/
CapableIndexReference index( int label, int... properties );

/**
* Returns all indexes associated with the given label
* @param labelId The id of the label which associated indexes you are looking for
* @return The index associated with the given label
*/
Iterator<CapableIndexReference> indexesGetForLabel( int labelId );

/**
* Finds all constraints for the given schema
* @param descriptor The descriptor of the schema
Expand All @@ -64,4 +71,11 @@ public interface SchemaRead
* @return An iterator of all the constraints in the database.
*/
Iterator<ConstraintDescriptor> constraintsGetAll( );

/**
* Get all constraints applicable to relationship type.
* @param typeId the id of the relationship type
* @return An iterator of constraints associated with the given type.
*/
Iterator<ConstraintDescriptor> constraintsGetForRelationshipType( int typeId );
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public interface Session extends AutoCloseable
*/
Transaction beginTransaction( Transaction.Type type ) throws KernelException;

Token token();

@Override
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.neo4j.internal.kernel.api;

import java.util.Iterator;

import org.neo4j.internal.kernel.api.exceptions.KernelException;
import org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException;
import org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException;
Expand Down Expand Up @@ -79,4 +81,10 @@ public interface TokenRead
* @throws PropertyKeyIdNotFoundKernelException if no key is associated with the id
*/
String propertyKeyName( int propertyKeyId ) throws PropertyKeyIdNotFoundKernelException;

Iterator<NamedToken> labelsGetAllTokens();

Iterator<NamedToken> propertyKeyGetAllTokens();

Iterator<NamedToken> relationshipTypesGetAllTokens();
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ enum Type
*/
TokenWrite tokenWrite();

/**
* @return Token read and write operations
*/
Token token();

/**
* @return The schema index read operations of the graph, used for finding indexes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*/
package org.neo4j.internal.kernel.api.security;

import org.neo4j.internal.kernel.api.Token;
import java.util.function.Function;

/**
* The LoginContext hold the executing authenticated user (subject).
* By calling {@link #authorize(Token)} the user is also authorized, and a full SecurityContext is returned,
* By calling {@link #authorize(Function<String,Integer>)} the user is also authorized, and a full SecurityContext is returned,
* which can be used to assert user permissions during query execution.
*/
public interface LoginContext
Expand All @@ -36,10 +36,10 @@ public interface LoginContext
/**
* Authorize the user and return a SecurityContext.
*
* @param token token lookup, used to compile property level security verification
* @param propertyIdLookup token lookup, used to compile property level security verification
* @return the security context
*/
SecurityContext authorize( Token token );
SecurityContext authorize( Function<String, Integer> propertyIdLookup );

LoginContext AUTH_DISABLED = new LoginContext()
{
Expand All @@ -50,7 +50,7 @@ public AuthSubject subject()
}

@Override
public SecurityContext authorize( Token token )
public SecurityContext authorize( Function<String, Integer> propertyIdLookup )
{
return SecurityContext.AUTH_DISABLED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.internal.kernel.api.security;

import org.neo4j.internal.kernel.api.Token;
import java.util.function.Function;

import static org.neo4j.graphdb.security.AuthorizationViolationException.PERMISSION_DENIED;

Expand Down Expand Up @@ -62,7 +62,7 @@ public AuthSubject subject()
}

@Override
public SecurityContext authorize( Token token )
public SecurityContext authorize( Function<String, Integer> propertyIdLookup )
{
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public abstract class KernelAPIReadTestBase<ReadSupport extends KernelAPIReadTes
protected static final TemporaryFolder folder = new TemporaryFolder();
protected static KernelAPIReadTestSupport testSupport;
protected Session session;
private Transaction tx;
protected Transaction tx;
protected Read read;
protected ExplicitIndexRead indexRead;
protected SchemaRead schemaRead;
Expand Down Expand Up @@ -81,7 +81,7 @@ public void setupGraph() throws IOException, KernelException
session = kernel.beginSession( LoginContext.AUTH_DISABLED );
cursors = new ManagedTestCursors( kernel.cursors() );
tx = session.beginTransaction( Transaction.Type.explicit );
token = session.token();
token = tx.token();
read = tx.dataRead();
indexRead = tx.indexRead();
schemaRead = tx.schemaRead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void shouldSeeNewLabeledNodeInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
labelId = session.token().labelGetOrCreateForName( labelName );
labelId = tx.token().labelGetOrCreateForName( labelName );
tx.dataWrite().nodeAddLabel( nodeId, labelId );

try ( NodeCursor node = cursors.allocateNodeCursor() )
Expand Down Expand Up @@ -108,8 +108,8 @@ public void shouldSeeLabelChangesInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
toRetain = session.token().labelGetOrCreateForName( toRetainName );
toDelete = session.token().labelGetOrCreateForName( toDeleteName );
toRetain = tx.token().labelGetOrCreateForName( toRetainName );
toDelete = tx.token().labelGetOrCreateForName( toDeleteName );
tx.dataWrite().nodeAddLabel( nodeId, toRetain );
tx.dataWrite().nodeAddLabel( nodeId, toDelete );
tx.success();
Expand All @@ -124,7 +124,7 @@ public void shouldSeeLabelChangesInTransaction() throws Exception

try ( Transaction tx = session.beginTransaction() )
{
toAdd = session.token().labelGetOrCreateForName( toAddName );
toAdd = tx.token().labelGetOrCreateForName( toAddName );
tx.dataWrite().nodeAddLabel( nodeId, toAdd );
tx.dataWrite().nodeRemoveLabel( nodeId, toDelete );

Expand Down Expand Up @@ -197,8 +197,8 @@ public void shouldSeeNewNodePropertyInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
int prop1 = session.token().propertyKeyGetOrCreateForName( propKey1 );
int prop2 = session.token().propertyKeyGetOrCreateForName( propKey2 );
int prop1 = tx.token().propertyKeyGetOrCreateForName( propKey1 );
int prop2 = tx.token().propertyKeyGetOrCreateForName( propKey2 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop1, stringValue( "hello" ) ), NO_VALUE );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, prop2, stringValue( "world" ) ), NO_VALUE );

Expand Down Expand Up @@ -240,7 +240,7 @@ public void shouldSeeAddedPropertyFromExistingNodeWithoutPropertiesInTransaction
// When/Then
try ( Transaction tx = session.beginTransaction() )
{
int propToken = session.token().propertyKeyGetOrCreateForName( propKey );
int propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );

try ( NodeCursor node = cursors.allocateNodeCursor();
Expand Down Expand Up @@ -280,15 +280,15 @@ public void shouldSeeAddedPropertyFromExistingNodeWithPropertiesInTransaction()
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken1 = session.token().propertyKeyGetOrCreateForName( propKey1 );
propToken1 = tx.token().propertyKeyGetOrCreateForName( propKey1 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken1, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}

// When/Then
try ( Transaction tx = session.beginTransaction() )
{
propToken2 = session.token().propertyKeyGetOrCreateForName( propKey2 );
propToken2 = tx.token().propertyKeyGetOrCreateForName( propKey2 );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken2, stringValue( "world" ) ), NO_VALUE );

try ( NodeCursor node = cursors.allocateNodeCursor();
Expand Down Expand Up @@ -334,7 +334,7 @@ public void shouldSeeUpdatedPropertyFromExistingNodeWithPropertiesInTransaction(
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}
Expand Down Expand Up @@ -380,7 +380,7 @@ public void shouldSeeRemovedPropertyInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}
Expand Down Expand Up @@ -420,7 +420,7 @@ public void shouldSeeRemovedThenAddedPropertyInTransaction() throws Exception
try ( Transaction tx = session.beginTransaction() )
{
nodeId = tx.dataWrite().nodeCreate();
propToken = session.token().propertyKeyGetOrCreateForName( propKey );
propToken = tx.token().propertyKeyGetOrCreateForName( propKey );
assertEquals( tx.dataWrite().nodeSetProperty( nodeId, propToken, stringValue( "hello" ) ), NO_VALUE );
tx.success();
}
Expand Down

0 comments on commit 1716c84

Please sign in to comment.