Skip to content

Commit

Permalink
Add AwsSigV4 signing functionality (opensearch-project#279)
Browse files Browse the repository at this point in the history
* Add AwsSigV4 signing functionality

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Adlicense text to signer types

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Pulling aws signer into separate namespace

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Adding separate injection point for v4Signer

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Fix name spacing and bump version

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Typo in readme

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Adding 0BSD to allow license

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Split code snippets into USER GUIDE

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Remove un-used package and update license

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Fix language in user guide

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Add types to dev dependencies

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* Update USER_GUIDE.md

Co-authored-by: Graeme <[email protected]>
Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

* add credentials refresh options

Signed-off-by: rawpixel-vincent <[email protected]>

* fix AwsSigv4Signer type with Promise

Signed-off-by: rawpixel-vincent <[email protected]>

* remove JSDoc

Signed-off-by: rawpixel-vincent <[email protected]>

* update example usage

Signed-off-by: rawpixel-vincent <[email protected]>

* update credentials refresh strategy

Signed-off-by: rawpixel-vincent <[email protected]>

* update credentials refresh and expiration

Signed-off-by: rawpixel-vincent <[email protected]>

* fix types

Signed-off-by: rawpixel-vincent <[email protected]>

* add failure to refresh credentials test case

Signed-off-by: rawpixel-vincent <[email protected]>

* cleanup and comments

Signed-off-by: rawpixel-vincent <[email protected]>

* clarify code example in the docs

Signed-off-by: rawpixel-vincent <[email protected]>

* remove explicit async from code example

Signed-off-by: rawpixel-vincent <[email protected]>

* remove unused credentialsState.acquiredAt

Signed-off-by: rawpixel-vincent <[email protected]>

* Minor doc and misc fixes

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>

Signed-off-by: Harsha Vamsi Kalluri <[email protected]>
Signed-off-by: rawpixel-vincent <[email protected]>
Co-authored-by: Graeme <[email protected]>
Co-authored-by: rawpixel-vincent <[email protected]>
Signed-off-by: Miki <[email protected]>
  • Loading branch information
3 people authored and AMoo-Miki committed Jul 12, 2023
1 parent 05c466a commit 9b6b077
Show file tree
Hide file tree
Showing 12 changed files with 1,186 additions and 401 deletions.
154 changes: 4 additions & 150 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OpenSearch Node.js client
- [Welcome!](#welcome)
- [Example use](#example-use)
- [Setup](#setup)
- [Sample code](#sample-code)
- [Sample code](#sample-code)
- [Project Resources](#project-resources)
- [Code of Conduct](#code-of-conduct)
- [License](#license)
Expand Down Expand Up @@ -44,156 +44,10 @@ Then require the client:
const { Client } = require('@opensearch-project/opensearch');
```

### Sample code
## Sample code

Please see the [USER_GUIDE](USER_GUIDE.md) for code snippets.

```javascript
'use strict';

var host = 'localhost';
var protocol = 'https';
var port = 9200;
var auth = 'admin:admin'; // For testing only. Don't store credentials in code.
var ca_certs_path = '/full/path/to/root-ca.pem';

// Optional client certificates if you don't want to use HTTP basic authentication.
// var client_cert_path = '/full/path/to/client.pem'
// var client_key_path = '/full/path/to/client-key.pem'

// Create a client with SSL/TLS enabled.
var { Client } = require('@opensearch-project/opensearch');
var fs = require('fs');
var client = new Client({
node: protocol + '://' + auth + '@' + host + ':' + port,
ssl: {
ca: fs.readFileSync(ca_certs_path),
// You can turn off certificate verification (rejectUnauthorized: false) if you're using self-signed certificates with a hostname mismatch.
// cert: fs.readFileSync(client_cert_path),
// key: fs.readFileSync(client_key_path)
},
});

async function search() {

// Create an index with non-default settings.
var index_name = 'books';
var settings = {
settings: {
index: {
number_of_shards: 4,
number_of_replicas: 3,
},
},
};

var response = await client.indices.create({
index: index_name,
body: settings,
});

console.log('Creating index:');
console.log(response.body);

// Add a document to the index.
var document = {
title: 'The Outsider',
author: 'Stephen King',
year: '2018',
genre: 'Crime fiction',
};

var id = '1';

var response = await client.index({
id: id,
index: index_name,
body: document,
refresh: true,
});

console.log('Adding document:');
console.log(response.body);

// Add documents in bulk
var bulk_documents = [
{
index: {
_index: 'books-king',
_id: '2'
}
},
{
title: 'IT',
author: 'Stephen Kings',
year: '1986',
},
{
create: {
_index: 'test',
_id: '3'
}
},
{
title: 'The Green Mile',
author: 'Stephen Kings',
year: '1996',
},
{
create: {
_index: 'test',
_id: '4'
}
},
{
title: 'Carrie',
author: 'Stephen Kings',
year: '1974',
}
];

var response = await client.bulk({ body: bulk_documents });

console.log('Adding documents using the bulk API')
console.log(response.body);

// Search for a document.
var query = {
query: {
match: {
title: {
query: 'The Outsider',
},
},
},
};

var response = await client.search({
index: index_name,
body: query,
});

console.log('Search results:');
console.log(response.body.hits);

// Delete a document.
var response = await client.delete({
index: index_name,
id: id,
});

console.log('Deleting document:');
console.log(response.body);

// Delete the index.
var response = await client.indices.delete({
index: index_name,
});

console.log('Deleting index:');
console.log(response.body);
}

search().catch(console.log);
```

## Project Resources

Expand Down
Loading

0 comments on commit 9b6b077

Please sign in to comment.