use of io.crate.common.annotations.VisibleForTesting in project crate by crate.
the class HttpAuthUpstreamHandler method credentialsFromRequest.
@VisibleForTesting
static Tuple<String, SecureString> credentialsFromRequest(HttpRequest request, @Nullable SSLSession session, Settings settings) {
String username = null;
if (request.headers().contains(HttpHeaderNames.AUTHORIZATION.toString())) {
// Prefer Http Basic Auth
return Headers.extractCredentialsFromHttpBasicAuthHeader(request.headers().get(HttpHeaderNames.AUTHORIZATION.toString()));
} else {
// prefer commonName as userName over AUTH_TRUST_HTTP_DEFAULT_HEADER user
if (session != null) {
try {
Certificate certificate = session.getPeerCertificates()[0];
username = SSL.extractCN(certificate);
} catch (ArrayIndexOutOfBoundsException | SSLPeerUnverifiedException ignored) {
// client cert is optional
}
}
if (username == null) {
username = AuthSettings.AUTH_TRUST_HTTP_DEFAULT_HEADER.get(settings);
}
}
return new Tuple<>(username, null);
}
use of io.crate.common.annotations.VisibleForTesting in project crate by crate.
the class HttpAuthUpstreamHandler method sendUnauthorized.
@VisibleForTesting
static void sendUnauthorized(Channel channel, @Nullable String body) {
HttpResponse response;
if (body != null) {
if (!body.endsWith("\n")) {
body += "\n";
}
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED, copiedBuffer(body, StandardCharsets.UTF_8));
HttpUtil.setContentLength(response, body.length());
} else {
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
}
// "Tell" the browser to open the credentials popup
// It helps to avoid custom login page in AdminUI
response.headers().set(HttpHeaderNames.WWW_AUTHENTICATE, WWW_AUTHENTICATE_REALM_MESSAGE);
channel.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of io.crate.common.annotations.VisibleForTesting in project crate by crate.
the class DocTableRelation method ensureColumnCanBeUpdated.
/**
* @throws io.crate.exceptions.ColumnValidationException if the column cannot be updated
*/
@VisibleForTesting
void ensureColumnCanBeUpdated(ColumnIdent ci) {
if (ci.isSystemColumn()) {
throw new ColumnValidationException(ci.toString(), tableInfo.ident(), "Updating a system column is not supported");
}
for (ColumnIdent pkIdent : tableInfo.primaryKey()) {
ensureNotUpdated(ci, pkIdent, "Updating a primary key is not supported");
}
if (tableInfo.clusteredBy() != null) {
ensureNotUpdated(ci, tableInfo.clusteredBy(), "Updating a clustered-by column is not supported");
}
List<GeneratedReference> generatedReferences = tableInfo.generatedColumns();
for (Reference partitionRef : tableInfo.partitionedByColumns()) {
ensureNotUpdated(ci, partitionRef.column(), "Updating a partitioned-by column is not supported");
if (!(partitionRef instanceof GeneratedReference)) {
continue;
}
int idx = generatedReferences.indexOf(partitionRef);
if (idx >= 0) {
GeneratedReference generatedReference = generatedReferences.get(idx);
for (Reference reference : generatedReference.referencedReferences()) {
ensureNotUpdated(ci, reference.column(), "Updating a column which is referenced in a partitioned by generated column expression is not supported");
}
}
}
}
use of io.crate.common.annotations.VisibleForTesting in project crate by crate.
the class CopyToPlan method bind.
@VisibleForTesting
public static BoundCopyTo bind(AnalyzedCopyTo copyTo, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults) {
Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
DocTableInfo table = (DocTableInfo) copyTo.tableInfo();
List<String> partitions = resolvePartitions(Lists2.map(copyTo.table().partitionProperties(), x -> x.map(eval)), table);
List<Symbol> outputs = new ArrayList<>();
Map<ColumnIdent, Symbol> overwrites = null;
boolean columnsDefined = false;
final List<String> outputNames = new ArrayList<>(copyTo.columns().size());
if (!copyTo.columns().isEmpty()) {
// TODO: remove outputNames?
for (Symbol symbol : copyTo.columns()) {
assert symbol instanceof Reference : "Only references are expected here";
RefVisitor.visitRefs(symbol, r -> outputNames.add(r.column().sqlFqn()));
outputs.add(DocReferences.toSourceLookup(symbol));
}
columnsDefined = true;
} else {
Reference sourceRef;
if (table.isPartitioned() && partitions.isEmpty()) {
// table is partitioned, insert partitioned columns into the output
overwrites = new HashMap<>();
for (Reference reference : table.partitionedByColumns()) {
if (!(reference instanceof GeneratedReference)) {
overwrites.put(reference.column(), reference);
}
}
if (overwrites.size() > 0) {
sourceRef = table.getReference(DocSysColumns.DOC);
} else {
sourceRef = table.getReference(DocSysColumns.RAW);
}
} else {
sourceRef = table.getReference(DocSysColumns.RAW);
}
outputs = List.of(sourceRef);
}
Settings settings = genericPropertiesToSettings(copyTo.properties().map(eval));
WriterProjection.CompressionType compressionType = settingAsEnum(WriterProjection.CompressionType.class, COMPRESSION_SETTING.get(settings));
WriterProjection.OutputFormat outputFormat = settingAsEnum(WriterProjection.OutputFormat.class, OUTPUT_FORMAT_SETTING.get(settings));
if (!columnsDefined && outputFormat == WriterProjection.OutputFormat.JSON_ARRAY) {
throw new UnsupportedFeatureException("Output format not supported without specifying columns.");
}
WhereClause whereClause = new WhereClause(copyTo.whereClause(), partitions, Collections.emptySet());
return new BoundCopyTo(outputs, table, whereClause, Literal.of(DataTypes.STRING.sanitizeValue(eval.apply(copyTo.uri()))), compressionType, outputFormat, outputNames.isEmpty() ? null : outputNames, columnsDefined, overwrites, settings);
}
use of io.crate.common.annotations.VisibleForTesting in project crate by crate.
the class CopyToPlan method planCopyToExecution.
@VisibleForTesting
static ExecutionPlan planCopyToExecution(AnalyzedCopyTo copyTo, PlannerContext context, TableStats tableStats, ProjectionBuilder projectionBuilder, Row params, SubQueryResults subQueryResults) {
var boundedCopyTo = bind(copyTo, context.transactionContext(), context.nodeContext(), params, subQueryResults);
WriterProjection.OutputFormat outputFormat = boundedCopyTo.outputFormat();
if (outputFormat == null) {
outputFormat = boundedCopyTo.columnsDefined() ? WriterProjection.OutputFormat.JSON_ARRAY : WriterProjection.OutputFormat.JSON_OBJECT;
}
WriterProjection projection = ProjectionBuilder.writerProjection(boundedCopyTo.outputs(), boundedCopyTo.uri(), boundedCopyTo.compressionType(), boundedCopyTo.overwrites(), boundedCopyTo.outputNames(), outputFormat, boundedCopyTo.withClauseOptions());
LogicalPlan collect = Collect.create(new DocTableRelation(boundedCopyTo.table()), boundedCopyTo.outputs(), boundedCopyTo.whereClause(), tableStats, context.params());
LogicalPlan source = optimizeCollect(context, tableStats, collect);
ExecutionPlan executionPlan = source.build(context, Set.of(), projectionBuilder, 0, 0, null, null, params, SubQueryResults.EMPTY);
executionPlan.addProjection(projection);
return Merge.ensureOnHandler(executionPlan, context, List.of(MergeCountProjection.INSTANCE));
}
Aggregations