Skip to content

Commit bbe53a9

Browse files
authored
Introducing NodeJS SigV4 Connection Example (#18)
* Create project files * Enable grabbing env variables from .env file * Remove redundant .npmignore file * Implement NodeJS plugin functionality * Provide project instructions and context * Add MIT-0 file header
1 parent 052f59f commit bbe53a9

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
package-lock.json
3+
.env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Connecting to Amazon Keyspaces with NodeJS
2+
3+
This sample project shows the use of the DataStax NodeJS Driver for Apache Cassandra using SigV4.
4+
5+
### Prerequisites
6+
7+
You should have NodeJS and NPM installed. This sample used NodeJS 14.x and NPM 6.x.
8+
9+
You should also setup Amazon Keyspaces with an IAM user. See [Accessing Amazon Keyspaces](https://docs.aws.amazon.com/keyspaces/latest/devguide/accessing.html) for more.
10+
11+
### Running the Sample
12+
13+
To run this sample, create a .env file in the same directory as this README.
14+
In your .env file, define the following environment variables:
15+
- AWS_REGION (ex: 'us-east-1')
16+
- AWS_ACCESS_KEY_ID (ex: 'AKIAIOSFODNN7EXAMPLE')
17+
- AWS_SECRET_ACCESS_KEY (ex: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
18+
19+
Install the project's dependencies by running `npm install` in this directory.
20+
21+
Finally, run `node index.js` to start the sample.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
// Variables
5+
const cassandra = require('cassandra-driver');
6+
const fs = require('fs');
7+
const sigV4 = require('aws-sigv4-auth-cassandra-plugin');
8+
require('dotenv').config();
9+
const region = process.env.AWS_REGION;
10+
const accessKey = process.env.AWS_ACCESS_KEY_ID;
11+
const secretKey = process.env.AWS_SECRET_ACCESS_KEY;
12+
13+
// Check that environment variables are not undefined
14+
if (!region) {
15+
console.log("You do not have a region set. Set environment variable AWS_REGION");
16+
process.exit(1);
17+
}
18+
19+
if (!accessKey) {
20+
console.log("You do not have an access key set. Set environment variable AWS_ACCESS_KEY_ID");
21+
process.exit(1);
22+
}
23+
24+
if (!secretKey) {
25+
console.log("You do not have a secret key set. Set environment variable AWS_SECRET_ACCESS_KEY");
26+
process.exit(1);
27+
}
28+
29+
const auth = new sigV4.SigV4AuthProvider({
30+
region: region,
31+
accessKeyId: accessKey,
32+
secretAccessKey: secretKey
33+
});
34+
35+
const host = 'cassandra.' + region + '.amazonaws.com'
36+
const sslOptions = {
37+
ca: [
38+
fs.readFileSync(__dirname + '/resources/sf-class2-root.crt')
39+
],
40+
host: host,
41+
rejectUnauthorized: true
42+
};
43+
44+
const client = new cassandra.Client({
45+
contactPoints: [host],
46+
localDataCenter: region,
47+
authProvider: auth,
48+
sslOptions: sslOptions,
49+
protocolOptions: { port: 9142 }
50+
});
51+
52+
const query = 'SELECT * FROM system_schema.keyspaces';
53+
54+
client.execute(query).then(
55+
result => console.log('Row from Keyspaces %s', result.rows[0])
56+
).catch(
57+
e => console.log(`${e}`)
58+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "nodejs-sigv4-example",
3+
"version": "1.0.0",
4+
"description": "A sigv4 authentication plugin for open-source Datastax NodeJS Driver for Apache Cassandra",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"cassandra",
11+
"cql",
12+
"sigv4",
13+
"connection",
14+
"driver",
15+
"aws"
16+
],
17+
"author": "",
18+
"license": "MIT-0",
19+
"dependencies": {
20+
"aws-sigv4-auth-cassandra-plugin": "^1.0.2",
21+
"cassandra-driver": "^4.6.3",
22+
"dotenv": "^10.0.0",
23+
"fs": "0.0.1-security"
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIEDzCCA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YJKoZIhvcNAQEFBQADggEBAAWdP4id0ckaVaGsafPzWdqbAYcaT1epoXkJKtv3
19+
L7IezMdeatiDh6GX70k1PncGQVhiv45YuApnP+yz3SFmH8lU+nLMPUxA2IGvd56D
20+
eruix/U0F47ZEUD0/CwqTRV/p2JdLiXTAAsgGh1o+Re49L2L7ShZ3U0WixeDyLJl
21+
xy16paq8U4Zt3VekyvggQQto8PT7dL5WXXp59fkdheMtlb71cZBDzI0fmgAKhynp
22+
VSJYACPq4xJDKVtHCN2MQWplBqjlIapBtJUhlbl90TSrE9atvNziPTnNvT51cKEY
23+
WQPJIrSPnNVeKtelttQKbfi3QBFGmh95DmK/D5fs4C8fF5Q=
24+
-----END CERTIFICATE-----

0 commit comments

Comments
 (0)