forked from joona/aws-es-curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (124 loc) · 3.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var AWS = require('aws-sdk');
var co = require('co');
var url = require('url');
var options = require('optimist')
.argv;
var credentials;
var getMetadataCredentials = function() {
credentials = new AWS.EC2MetadataCredentials();
return credentials.getPromise()
};
var getCredentials = function*() {
var profile = process.env.AWS_PROFILE || options.profile;
if(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY && !profile) {
credentials = new AWS.EnvironmentCredentials('AWS');
return;
}
if(!profile) {
try {
yield getMetadataCredentials();
return;
} catch(err) {
console.error('Unable to access metadata service.', err.message);
}
}
credentials = new AWS.SharedIniFileCredentials({
profile: profile || 'default',
// the filename to use when loading credentials
// use of AWS_SHARED_CREDENTIALS_FILE environment variable
// see also 'The Shared Credentials File' http://docs.aws.amazon.com/cli/latest/topic/config-vars.html
filename: process.env.AWS_SHARED_CREDENTIALS_FILE
});
return credentials.getPromise();
}
var readStdin = function() {
return new Promise((resolve, reject) => {
var data = '';
var stdin = process.stdin;
stdin.on('readable', function() {
var chunk = this.read();
if(chunk) {
data += chunk;
}
});
stdin.on('end', function() {
resolve(data.trim());
});
});
};
var execute = function(endpoint, region, path, method, body) {
return new Promise((resolve, reject) => {
var req = new AWS.HttpRequest(endpoint);
req.method = method || 'GET';
req.path = path;
req.region = region;
if(body) {
if(typeof body === "object") {
req.body = JSON.stringify(body);
} else {
req.body = body;
}
}
req.headers['presigned-expires'] = false;
req.headers['content-type'] = 'application/json';
req.headers.Host = endpoint.host;
var signer = new AWS.Signers.V4(req, 'es');
signer.addAuthorization(credentials, new Date());
var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, (httpResp) => {
var body = '';
httpResp.on('data', (chunk) => {
body += chunk;
});
httpResp.on('end', (chunk) => {
resolve(body);
});
}, (err) => {
console.log('Error: ' + err);
reject(err);
});
});
};
var main = function() {
co(function*(){
var maybeUrl = options._[0];
var method = options.X || options.method || 'GET';
var region = options.region || process.env.AWS_DEFAULT_REGION || process.env.AWS_REGION || 'eu-west-1';
yield getCredentials();
var input;
if(!process.stdin.isTTY) {
input = yield readStdin();
}
if(!input) {
input = options.data || options.d;
}
if(!maybeUrl || (maybeUrl && maybeUrl == 'help') || options.help || options.h) {
console.log('Usage: aws-es-curl [options] <url>');
console.log();
console.log('Options:');
console.log("\t-X, --method \tHTTP method \t(Default: GET)");
console.log("\t--profile \tAWS profile \t(Default: default)");
console.log("\t--region \tAWS region \t(Default: eu-west-1)");
console.log("\t-d, --data \tSends the specified data in a POST request");
process.exit(1);
}
if(maybeUrl && maybeUrl.indexOf('http') === 0) {
var uri = url.parse(maybeUrl);
var endpoint = new AWS.Endpoint(uri.host);
var response = yield execute(endpoint, region, uri.path, method, input);
process.stdout.write(response + "\n");
}
})
.then(res => {
process.exit(0);
})
.catch(err => {
console.error('Error:', err.message);
console.log(err.stack);
process.exit(1);
});
};
if(!module.parent) {
main();
}
module.exports = main;