use of io.crate.action.sql.SessionContext in project crate by crate.
the class SysShardsTableInfo method getRouting.
/**
* Retrieves the routing for sys.shards
* <p>
* This routing contains ALL shards of ALL indices.
* Any shards that are not yet assigned to a node will have a NEGATIVE shard id (see {@link UnassignedShard}
*/
public static Routing getRouting(ClusterState clusterState, RoutingProvider routingProvider, SessionContext sessionContext) {
String[] concreteIndices = Arrays.stream(clusterState.metadata().getConcreteAllIndices()).filter(index -> !IndexParts.isDangling(index)).toArray(String[]::new);
User user = sessionContext != null ? sessionContext.sessionUser() : null;
if (user != null) {
List<String> accessibleTables = new ArrayList<>(concreteIndices.length);
for (String indexName : concreteIndices) {
String tableName = RelationName.fqnFromIndexName(indexName);
if (user.hasAnyPrivilege(Privilege.Clazz.TABLE, tableName)) {
accessibleTables.add(indexName);
}
}
concreteIndices = accessibleTables.toArray(new String[0]);
}
Map<String, Map<String, IntIndexedContainer>> locations = new TreeMap<>();
GroupShardsIterator<ShardIterator> groupShardsIterator = clusterState.getRoutingTable().allAssignedShardsGrouped(concreteIndices, true);
for (final ShardIterator shardIt : groupShardsIterator) {
final ShardRouting shardRouting = shardIt.nextOrNull();
processShardRouting(clusterState.getNodes().getLocalNodeId(), locations, shardRouting, shardIt.shardId());
}
return new Routing(locations);
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class AccessControlMayExecuteTest method test_set_session_user_from_normal_to_originally_authenticated_user_succeeds.
@Test
public void test_set_session_user_from_normal_to_originally_authenticated_user_succeeds() {
e.analyzer.analyze(SqlParser.createStatement("SET SESSION AUTHORIZATION " + superUser.name()), new SessionContext(superUser, user), ParamTypeHints.EMPTY);
assertThat(validationCallArguments.size(), is(0));
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class PostgresWireProtocolTest method test_channel_is_flushed_after_receiving_flush_request.
@Test
public void test_channel_is_flushed_after_receiving_flush_request() throws Exception {
SQLOperations sqlOperations = mock(SQLOperations.class);
Session session = mock(Session.class);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
AtomicBoolean flushed = new AtomicBoolean(false);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler) {
@Override
public Channel flush() {
flushed.set(true);
return super.flush();
}
};
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendParseMessage(buffer, "", "select ?", new int[0]);
ClientMessages.sendFlush(buffer);
channel.writeInbound(buffer);
channel.releaseInbound();
assertThat(flushed.get(), is(true));
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class PostgresWireProtocolTest method test_row_description_for_statement_on_single_table_includes_table_oid.
@Test
public void test_row_description_for_statement_on_single_table_includes_table_oid() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
{
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendParseMessage(buffer, "S1", "SELECT name FROM users", new int[0]);
channel.writeInbound(buffer);
channel.releaseInbound();
// we're not interested in the startup, parse, or bind replies
channel.flushOutbound();
channel.releaseOutbound();
channel.outboundMessages().clear();
}
{
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
channel.writeInbound(buffer);
channel.releaseInbound();
// we should get back a ParameterDescription message, but not interesting for this test case
channel.flushOutbound();
ByteBuf response = channel.readOutbound();
response.release();
// we should get back a RowDescription message
response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 'T'));
assertThat(response.readInt(), is(29));
assertThat(response.readShort(), is((short) 1));
assertThat(PostgresWireProtocol.readCString(response), is("name"));
assertThat("table_oid", response.readInt(), is(893280107));
assertThat("attr_num", response.readShort(), is((short) 1));
var pgType = PGTypes.get(DataTypes.STRING);
assertThat(response.readInt(), is(pgType.oid()));
assertThat(response.readShort(), is(pgType.typeLen()));
assertThat(response.readInt(), is(pgType.typeMod()));
assertThat("format_code", response.readShort(), is((short) 0));
} finally {
response.release();
}
}
}
use of io.crate.action.sql.SessionContext in project crate by crate.
the class PostgresWireProtocolTest method testSessionCloseOnTerminationMessage.
@Test
public void testSessionCloseOnTerminationMessage() throws Exception {
SQLOperations sqlOperations = mock(SQLOperations.class);
Session session = mock(Session.class);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendTermination(buffer);
channel.writeInbound(buffer);
channel.releaseInbound();
verify(session, times(1)).close();
}
Aggregations