Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds bound parameters for enhanced security #31

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ let myQuery = {
```javascript
/*Option 1: object notation*/
let myQuery = {
sql: "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT 3" /* required */,
sql: "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT ?" /* required */,
QueryParams: [3],
db: "sampledb" /* optional. You could specify a database here or in the advance configuration option mentioned above*/
};

Expand All @@ -211,7 +212,8 @@ athenaExpress
(async () => {
/*Option 1: object notation*/
let myQuery = {
sql: "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT 3" /* required */,
sql: "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT ?" /* required */,
QueryParams: [3],
db: "sampledb" /* optional. You could specify a database here or in the configuration constructor*/
};

Expand Down Expand Up @@ -262,7 +264,8 @@ const athenaExpress = new AthenaExpress(athenaExpressConfig);
//Invoking a query on Amazon Athena
(async () => {
let myQuery = {
sql: "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT 3",
sql: "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT ?",
QueryParams: [3],
db: "sampledb"
};

Expand Down Expand Up @@ -297,10 +300,10 @@ const athenaExpressConfig = {
const athenaExpress = new AthenaExpress(athenaExpressConfig);

exports.handler = async event => {
const sqlQuery = "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT 3";
const sqlQuery = "SELECT elb_name, request_port, request_ip FROM elb_logs LIMIT ?";

try {
let results = await athenaExpress.query(sqlQuery);
let results = await athenaExpress.query(sqlQuery, [3]);
return results;
} catch (error) {
return error;
Expand Down
24 changes: 23 additions & 1 deletion lib/athenaExpress.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,25 @@ module.exports = class AthenaExpress {
};
}

async query(query) {
/**
* Executes an Athena SQL Query.
*
* If the 'query' parameter is a string, SQL parameters should be
* passed in via the 'queryParams' optional parameter.
*
* If the 'query' parameter is an object, parameters should be
* passed in via the 'query.QueryParams' optional array.
*
* Examples:
* await athenaExpress.query('SELECT * FROM movies WHERE movie_title = ?', ['Spider-Man']);
* await athenaExpress
* .query({ sql: 'SELECT * FROM movies WHERE movie_title = ?', QueryParams: ['Spider-Man']});
*
* @param {String|Object} query
* @param {Array} [queryParams=[]] Optional
* @returns {Promise<Object>}
*/
async query(query, queryParams) {
const config = this.config;

let initiateQueryInAthena = true;
Expand All @@ -66,6 +84,9 @@ module.exports = class AthenaExpress {
if (loweredCaseKeys.hasOwnProperty("catalog")) {
config.catalog = loweredCaseKeys.catalog;
}
if (loweredCaseKeys.hasOwnProperty("queryparams")) {
config.QueryParams = loweredCaseKeys.queryparams;
}
if (loweredCaseKeys.hasOwnProperty("sql")) {
config.sql = loweredCaseKeys.sql;
}
Expand All @@ -82,6 +103,7 @@ module.exports = class AthenaExpress {
config.QueryExecutionId = query;
} else {
config.sql = query;
config.QueryParams = queryParams;
}

try {
Expand Down
5 changes: 3 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
//helpers.js

const readline = require("readline"),
csv = require("csvtojson");
csv = require("csvtojson"),
SqlString = require("sqlstring");

let s3Metadata = null;

function startQueryExecution(config) {
const params = {
QueryString: config.sql,
QueryString: SqlString.format(config.sql, config.QueryParams || []),
WorkGroup: config.workgroup,
ResultConfiguration: {
OutputLocation: config.s3Bucket,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"mocha-lcov-reporter": "^1.3.0"
},
"dependencies": {
"csvtojson": "^2.0.10"
"csvtojson": "^2.0.10",
"sqlstring": "^2.3.1"
}
}