Here's an implementation of the Connection
class I've been using with TypeScript:
import { Connection as UnsignedConnection } from '@elastic/elasticsearch';
import * as AWS from 'aws-sdk';
import RequestSigner from 'aws-sdk/lib/signers/v4';
import { ClientRequest, IncomingMessage } from 'http';
class AwsElasticsearchError extends Error {}
type RequestOptions = Parameters<UnsignedConnection['request']>[0];
class AwsSignedConnection extends UnsignedConnection {
public request(
params: RequestOptions,
callback: (err: Error | null, response: IncomingMessage | null) => void,
): ClientRequest {
const signedParams = this.signParams(params);
return super.request(signedParams, callback);
}
private signParams(params: RequestOptions): RequestOptions {
const region = AWS.config.region || process.env.AWS_DEFAULT_REGION;
if (!region) throw new AwsElasticsearchError('missing region configuration');
if (!params.method) throw new AwsElasticsearchError('missing request method');
if (!params.path) throw new AwsElasticsearchError('missing request path');
if (!params.headers) throw new AwsElasticsearchError('missing request headers');
const endpoint = new AWS.Endpoint(this.url.href);
const request = new AWS.HttpRequest(endpoint, region);
request.method = params.method;
request.path = params.querystring
? `${params.path}/?${params.querystring}`
: params.path;
request.body = params.body;
Object.entries(params.headers).forEach(([header, value]) => {
if (value === undefined) return;
if (typeof value === 'string') request.headers[header] = value;
else if (typeof value === 'number') request.headers[header] = `${value}`;
else request.headers[header] = value.join('; ');
});
request.headers.Host = endpoint.host;
const signer = new RequestSigner(request, 'es');
signer.addAuthorization(AWS.config.credentials, new Date());
return request;
}
}
export { AwsSignedConnection, UnsignedConnection, AwsElasticsearchError };
Then you can provide it only if credentials are available, so you can use it to point to a local (e.g. Docker) Elasticsearch without credentials:
import awsSdk from 'aws-sdk';
import elasticsearch from '@elastic/elasticsearch';
import { AwsSignedConnection, UnsignedConnection } from '../aws-es-connector';
client = new elasticsearch.Client({
Connection: awsSdk.config.credentials ? AwsSignedConnection : UnsignedConnection,
node: elasticsearchEndpoint,
});
requestTimeout
and it seems to work, but will have a deeper look later. :) – Mr. B. Aug 30 '17 at 12:29