Skip to content

Commit

Permalink
Pulling aws signer into separate namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Harsha Vamsi Kalluri <harshavamsi096@gmail.com>
  • Loading branch information
harshavamsi committed Aug 25, 2022
1 parent 59bbbf4 commit caf4d74
Show file tree
Hide file tree
Showing 13 changed files with 604 additions and 274 deletions.
562 changes: 445 additions & 117 deletions index.d.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (clientVersion.includes('-')) {
// clean prerelease
clientVersion = clientVersion.slice(0, clientVersion.indexOf('-')) + 'p';
}
const AwsV4Signer = require('./lib/AwsV4Signer');
const AwsV4Signer = require('./lib/aws/AwsV4Signer');

const kInitialOptions = Symbol('opensearchjs-initial-options');
const kChild = Symbol('opensearchjs-child');
Expand Down
47 changes: 0 additions & 47 deletions lib/AwsV4Signer.d.ts

This file was deleted.

28 changes: 28 additions & 0 deletions lib/aws/AwsV4Signer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import { Credentials } from '@aws-sdk/types';
import Connection from '../Connection';
import * as http from 'http';

interface AwsV4SignerOptions {
credentials: Credentials;
region: string;
}

export interface AwsV4SignerResponse {
Connection: Connection;
buildSignedRequestObject(request: any): http.ClientRequestArgs;
}

export default function AwsV4Signer(opts: AwsV4SignerOptions): AwsV4SignerResponse;

export {};
21 changes: 1 addition & 20 deletions lib/AwsV4Signer.js → lib/aws/AwsV4Signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,8 @@
* GitHub history for details.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

'use strict';
const Connection = require('./Connection');
const Connection = require('../Connection');
const aws4 = require('aws4');
const { AwsV4SignerError } = require('./errors');

Expand Down
18 changes: 18 additions & 0 deletions lib/aws/errors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
import { OpenSearchClientError } from '../errors';

export declare class AwsV4SignerError extends OpenSearchClientError {
name: string;
message: string;
data: any;
constructor(message: string, data: any);
}
27 changes: 27 additions & 0 deletions lib/aws/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

'use strict';
const { OpenSearchClientError } = require('../errors');

class AwsV4SignerError extends OpenSearchClientError {
constructor(message, data) {
super(message, data);
Error.captureStackTrace(this, AwsV4SignerError);
this.name = 'AwsV4SignerError';
this.message = message || 'AwsV4Signer Error';
this.data = data;
}
}

module.exports = {
AwsV4SignerError,
};
7 changes: 0 additions & 7 deletions lib/errors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,3 @@ export declare class NotCompatibleError<
meta: ApiResponse<TResponse, TContext>;
constructor(meta: ApiResponse);
}

export declare class AwsV4SignerError extends OpenSearchClientError {
name: string;
message: string;
data: any;
constructor(message: string, data: any);
}
11 changes: 0 additions & 11 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,6 @@ class NotCompatibleError extends OpenSearchClientError {
}
}

class AwsV4SignerError extends OpenSearchClientError {
constructor(message, data) {
super(message, data);
Error.captureStackTrace(this, AwsV4SignerError);
this.name = 'AwsV4SignerError';
this.message = message || 'AwsV4Signer Error';
this.data = data;
}
}

module.exports = {
OpenSearchClientError,
TimeoutError,
Expand All @@ -179,5 +169,4 @@ module.exports = {
ResponseError,
RequestAbortedError,
NotCompatibleError,
AwsV4SignerError,
};
2 changes: 1 addition & 1 deletion test/types/awsv4signer.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import { expectType } from 'tsd';
import { AwsV4Signer } from '../../';
import { AwsV4SignerResponse } from '../../lib/AwsV4Signer';
import { AwsV4SignerResponse } from '../../lib/aws/AwsV4Signer';

const mockCreds = {
accessKeyId: 'mockCredAccessKeyId',
Expand Down
4 changes: 2 additions & 2 deletions test/types/connection.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import { ConnectionOptions } from '../../lib/Connection';
agent: { keepAlive: false },
status: 'alive',
roles: {},
auth: { username: 'username', password: 'password' }
})
auth: { username: 'username', password: 'password' },
});

expectType<Connection>(conn);
expectType<URL>(conn.url);
Expand Down
81 changes: 81 additions & 0 deletions test/unit/awsv4.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
const { test } = require('tap');
const { URL } = require('url');
const { v4: uuidv4 } = require('uuid');
const { AwsV4Signer } = require('../../index');
const { AwsV4SignerError } = require('../../lib/aws/errors');
const { Connection } = require('../../index');

test('Sign with SigV4', (t) => {
t.plan(2);

const mockCreds = {
accessKeyId: uuidv4(),
secretAccessKey: uuidv4(),
};

const mockRegion = 'us-west-2';

const AwsV4SignerOptions = { credentials: mockCreds, region: mockRegion };

const auth = AwsV4Signer(AwsV4SignerOptions);

const connection = new Connection({
url: new URL('https://localhost:9200'),
});

const request = connection.buildRequestObject({
path: '/hello',
method: 'GET',
headers: {
'X-Custom-Test': true,
},
});
const signedRequest = auth.buildSignedRequestObject(request);
t.hasProp(signedRequest.headers, 'X-Amz-Date');
t.hasProp(signedRequest.headers, 'Authorization');
});

test('Sign with SigV4 failure (with empty region)', (t) => {
t.plan(2);

const mockCreds = {
accessKeyId: uuidv4(),
secretAccessKey: uuidv4(),
};

const AwsV4SignerOptions = { credentials: mockCreds };

try {
AwsV4Signer(AwsV4SignerOptions);
t.fail('Should fail');
} catch (err) {
t.ok(err instanceof AwsV4SignerError);
t.equal(err.message, 'Region cannot be empty');
}
});

test('Sign with SigV4 failure (with empty credentials)', (t) => {
t.plan(2);

const mockRegion = 'us-west-2';

const AwsV4SignerOptions = { region: mockRegion };

try {
AwsV4Signer(AwsV4SignerOptions);
t.fail('Should fail');
} catch (err) {
t.ok(err instanceof AwsV4SignerError);
t.equal(err.message, 'Credentials cannot be empty');
}
});
68 changes: 0 additions & 68 deletions test/unit/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@
const { test } = require('tap');
const { URL } = require('url');
const buffer = require('buffer');
const { v4: uuidv4 } = require('uuid');
const intoStream = require('into-stream');
const { ConnectionPool, Transport, Connection, errors } = require('../../index');
const { Client, buildServer } = require('../utils');
const { AwsV4Signer } = require('../../index');
const { AwsV4SignerError } = require('../../lib/errors');
let clientVersion = require('../../package.json').version;
if (clientVersion.includes('-')) {
clientVersion = clientVersion.slice(0, clientVersion.indexOf('-')) + 'p';
Expand Down Expand Up @@ -1192,68 +1189,3 @@ test('API compatibility header (x-ndjson)', (t) => {
});
});
});

test('Sign with SigV4', (t) => {
t.plan(2);

const mockCreds = {
accessKeyId: uuidv4(),
secretAccessKey: uuidv4(),
};

const mockRegion = 'us-west-2';

const AwsV4SignerOptions = { credentials: mockCreds, region: mockRegion };

const auth = AwsV4Signer(AwsV4SignerOptions);

const connection = new Connection({
url: new URL('https://localhost:9200'),
});

const request = connection.buildRequestObject({
path: '/hello',
method: 'GET',
headers: {
'X-Custom-Test': true,
},
});
const signedRequest = auth.buildSignedRequestObject(request);
t.hasProp(signedRequest.headers, 'X-Amz-Date');
t.hasProp(signedRequest.headers, 'Authorization');
});

test('Sign with SigV4 failure (with empty region)', (t) => {
t.plan(2);

const mockCreds = {
accessKeyId: uuidv4(),
secretAccessKey: uuidv4(),
};

const AwsV4SignerOptions = { credentials: mockCreds };

try {
AwsV4Signer(AwsV4SignerOptions);
t.fail('Should fail');
} catch (err) {
t.ok(err instanceof AwsV4SignerError);
t.equal(err.message, 'Region cannot be empty');
}
});

test('Sign with SigV4 failure (with empty credentials)', (t) => {
t.plan(2);

const mockRegion = 'us-west-2';

const AwsV4SignerOptions = { region: mockRegion };

try {
AwsV4Signer(AwsV4SignerOptions);
t.fail('Should fail');
} catch (err) {
t.ok(err instanceof AwsV4SignerError);
t.equal(err.message, 'Credentials cannot be empty');
}
});

0 comments on commit caf4d74

Please sign in to comment.