Permalink
Browse files

init commit

  • Loading branch information...
0 parents commit 48ca8566aea75351114378ef4460ef6ae4e7470b @kilianc committed Oct 31, 2011
Showing with 367 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +22 −0 LICENSE
  3. +128 −0 README.md
  4. +71 −0 lib/ec2metadata.js
  5. +24 −0 package.json
  6. +120 −0 test/ec2metadata-test.js
@@ -0,0 +1,2 @@
+node_modules/
+npm-debug.log
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Kilian Ciuffolo, me@nailik.org
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
128 README.md
@@ -0,0 +1,128 @@
+# node-ec2metadata ![project status](http://dl.dropbox.com/u/2208502/maintained.png)
+
+An API wrapper for the AWS EC2 metadata: [aws userguide](http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?AESDG-chapter-instancedata.html)
+
+## Installation with npm
+
+ $ npm install ec2metadata
+
+## A smart introduction to EC2 Metadata API
+
+Amazon EC2 instances can access instance-specific metadata as well as data supplied when launching the instances. This data can be used to build more generic AMIs that can be modified by configuration files supplied at launch time.
+
+For example, if you run web servers for various small businesses, they can all use the same AMI and retrieve their content from the Amazon S3 bucket you specify at launch. To add a new customer at any time, simply create a bucket for the customer, add their content, and launch your AMI.
+
+Metadata is divided into categories. For a list of the categories, see Appendix B: [Metadata Categories](http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instancedata-data-categories.html).
+
+###Data Retrieval
+
+An instance retrieves the data by querying a web server using a Query API. The base URI of all requests is http://169.254.169.254/latest/.
+
+###Security of Launch Data
+
+Although only your specific instance can access launch data, the data is not protected by cryptographic methods. You should take suitable precautions to protect sensitive data (such as long lived encryption keys).
+
+__You are not billed for HTTP requests used to retrieve metadata and user-supplied data.__
+
+###Metadata Retrieval
+
+Requests for a specific metadata resource returns the appropriate value or a 404 HTTP error code if the resource is not available. All metadata is returned as text (content type text/plain).
+
+Requests for a general metadata resource (i.e. an URI ending with a /) return a list of available resources or a 404 HTTP error code if there is no such resource. The list items are on separate lines terminated by line feeds (ASCII 10).
+
+## Usage
+
+```javascript
+EC2Metadata.get([Array|String], Function);
+```
+
+The first parameter is an array of fields, the second one the complete callback.
+EC2Metadata will request all the fields and call the callback at the and.
+You can retrieve a complete list of fields skipping the first parameter of the get function.
+
+```javascript
+var EC2Metadata = require('ec2metadata');
+
+EC2Metadata.get(function(err, data){
+ console.log(JSON.stringify(data, null, ' '));
+});
+```
+
+will output:
+
+```javascript
+[
+ "amiid",
+ "ami-launch-index",
+ "ami-manifest-path",
+ "block-device-mapping/",
+ "hostname",
+ "instance-action",
+ "instance-id",
+ "instance-type",
+ "kernel-id",
+ "local-hostname",
+ "local-ipv4",
+ "mac",
+ "network/",
+ "placement/",
+ "public-hostname",
+ "public-ipv4",
+ "public-keys/",
+ "reservation-id",
+ "security-groups"
+]
+```
+
+You can use each field as query, the callback data object will contains all the requested
+fields with a camel case converted name.
+
+```javascript
+EC2Metadata.get(['public-hostname', 'local-ipv4', 'instance-id'], function(err, data){
+
+ if(err){
+ console.log('Something went wrong: ' + err.message);
+ return;
+ }
+
+ console.log(data.publicHostname);
+ console.log(data.localIpv4);
+ console.log(data.instanceId);
+});
+```
+
+A request the name of which does not end with a slash, will populate the data object with a string.
+A request the name of which ends with a slash, will populate the data object with an Array.
+
+## Tests
+
+In order to run tests, you must deploy on a amazon ec2 instance and run `npm install`.
+
+ $ npm test
+
+## License
+
+_This software is released under the MIT license cited below_.
+
+ Copyright (c) 2010 Kilian Ciuffolo, me@nailik.org. All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the 'Software'), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
+var
+ http = require('http'),
+ FnQueue = require('fnqueue');
+
+module.exports = {
+
+ get: function(){
+
+ var args = Array.prototype.slice.call(arguments);
+ var callback = args.pop();
+ var what = args.pop();
+
+ if(typeof what === 'string'){
+ what = [what];
+ }
+ else if(what === undefined){
+ callback && get('', 500, callback);
+ return;
+ }
+
+ var awsMetadataFnQueueObj = {};
+
+ for(var i = 0; i < what.length; i++)
+ awsMetadataFnQueueObj[what[i].replace(/((\-|\/)[a-z]?)/g, function(match){ return match[1] ? match[1].toUpperCase() : ''; })] = get.bind(null, what[i], 1000);
+
+ new FnQueue(awsMetadataFnQueueObj, function(err, data){
+
+ if(err){
+ callback && callback(err);
+ return;
+ }
+
+ callback && callback(null, data);
+ }, 1);
+ }
+};
+
+function get(what, maxTime, callback){
+
+ var timeoutId = setTimeout(function(){
+ callback(new Error('EC2 metadata api request timed out.'));
+ }, maxTime);
+
+ var request = http.get({
+ host: '169.254.169.254',
+ port: 80,
+ path: '/latest/meta-data/' + what
+ }, function(response){
+
+ clearTimeout(timeoutId);
+
+ if(response.statusCode !== 200){
+ callback(new Error('Request for field: ' + what + ' returned ' + response.statusCode + ' ' + http.STATUS_CODES[response.statusCode]), 'ciao');
+ return;
+ }
+
+ response.once('data', function(data){
+ if(data instanceof Buffer){
+ data = data.toString('utf8');
+ if(/\/$/.test(what) || what === '')
+ data = data.split('\n');
+ }
+ callback(null, data);
+ });
+ });
+
+ request.once('error', function(err){
+ clearTimeout(timeoutId);
+ callback(err);
+ });
+}
@@ -0,0 +1,24 @@
+{
+ "author": "Kilian Ciuffolo <me@nailik.org> (http://nailik.org)",
+ "name": "ec2metadata",
+ "main" : "./lib/ec2metadata.js",
+ "description": "A wrapper for EC2 Instance Metadata API",
+ "keywords": ["ec2", "aws", "metadata", "api", "amazon", "instance"],
+ "version": "0.1.0",
+ "engines": {
+ "node": ">= v0.4.7"
+ },
+ "scripts": {
+ "test": "vows test/*-test.js --spec"
+ },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/kilianc/node-ec2metadata.git"
+ },
+ "dependencies": {
+ "fnqueue": "1.0.5"
+ },
+ "devDependencies": {
+ "vows": "0.5.x >=0.5.10"
+ }
+}
@@ -0,0 +1,120 @@
+var
+ vows = require('vows'),
+ assert = require('assert'),
+ EC2Metadata = require('../lib/ec2metadata');
+
+vows.describe('ec2metadata').addBatch({
+ 'When we call EC2Metadata.get only with the callback parameter': {
+ topic: function(){
+ EC2Metadata.get(this.callback);
+ },
+ 'error should be null': function(err, data){
+ assert.isNull(err);
+ },
+ 'data should be an array': function(err, data){
+ assert.isArray(data);
+ }
+ }
+}).addBatch({
+ 'When we call EC2Metadata.get with a valid fieldname ending with a slash': {
+ topic: function(){
+ EC2Metadata.get(['block-device-mapping/'], this.callback);
+ },
+ 'error should be null': function(err, data){
+ assert.isNull(err);
+ },
+ 'data should be an Object': function(err, data){
+ assert.isObject(data);
+ },
+ 'the property of data should be an array': function(err, data){
+ assert.isArray(data.blockDeviceMapping);
+ }
+ }
+}).addBatch({
+ 'When we call EC2Metadata.get with a wrong field as parameter': {
+ topic: function(){
+ EC2Metadata.get(['wrong-metadata'], this.callback);
+ },
+ 'error should not be null': function(err, data){
+ assert.isNotNull(err);
+ },
+ 'error should match the "Request for field: [fieldname] returned" messgae': function(err, data){
+ assert.match(err.message, /^Request for field: (.+) returned/);
+ },
+ 'data should be undefined': function(err, data){
+ assert.isUndefined(data);
+ }
+ }
+}).addBatch({
+ 'When we call EC2Metadata.get with multiple fields': {
+ topic: function(){
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'public-keys/0/'], this.callback);
+ },
+ 'error should be null': function(err, data){
+ assert.isNull(err);
+ },
+ 'data should be an Object': function(err, data){
+ assert.isObject(data);
+ },
+ 'data should have predicted properties': function(err, data){
+ assert.include(data, 'publicHostname');
+ assert.include(data, 'localIpv4');
+ assert.include(data, 'publicKeys0');
+ },
+ 'data properties should match types': function(err, data){
+ assert.isString(data.publicHostname);
+ assert.isString(data.localIpv4);
+ assert.isArray(data.publicKeys0);
+ }
+ }
+}).addBatch({
+ 'When we call EC2Metadata.get with a dieldname missing an ending slash': {
+ topic: function(){
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'public-keys/0'], this.callback);
+ },
+ 'error should be not null': function(err, data){
+ assert.isNotNull(err);
+ },
+ 'error should match the "Request for field: [fieldname] returned ..." messgae': function(err, data){
+ assert.match(err.message, /^Request for field: (.+) returned/);
+ },
+ 'data should be undefined': function(err, data){
+ assert.isUndefined(data);
+ }
+ }
+}).addBatch({
+ 'When we call EC2Metadata.get with multiple fields (one of them wrong)': {
+ topic: function(){
+ EC2Metadata.get(['public-hostname', 'local-ipv4(wrong!)', 'instance-id'], this.callback);
+ },
+ 'error should be not null': function(err, data){
+ assert.isNotNull(err);
+ },
+ 'error should match the "Request for field: [fieldname] returned ..." messgae': function(err, data){
+ assert.match(err.message, /^Request for field: (.+) returned/);
+ },
+ 'data should be undefined': function(err, data){
+ assert.isUndefined(data);
+ }
+ }
+}).addBatch({
+ 'When we try a DOS attack to aws API :)': {
+ topic: function(){
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'instance-id', 'public-keys/0/', 'public-hostname', 'local-ipv4(wrong!)', 'instance-id', 'public-keys/0/'], this.callback);
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'instance-id', 'public-keys/0/', 'public-hostname', 'local-ipv4(wrong!)', 'instance-id', 'public-keys/0/'], this.callback);
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'instance-id', 'public-keys/0/', 'public-hostname', 'local-ipv4(wrong!)', 'instance-id', 'public-keys/0/'], this.callback);
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'instance-id', 'public-keys/0/', 'public-hostname', 'local-ipv4(wrong!)', 'instance-id', 'public-keys/0/'], this.callback);
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'instance-id', 'public-keys/0/', 'public-hostname', 'local-ipv4(wrong!)', 'instance-id', 'public-keys/0/'], this.callback);
+ EC2Metadata.get(['public-hostname', 'local-ipv4', 'instance-id', 'public-keys/0/', 'public-hostname', 'local-ipv4(wrong!)', 'instance-id', 'public-keys/0/'], this.callback);
+ },
+ 'error should be not null': function(err, data){
+ assert.isNotNull(err);
+ },
+ 'error should match the "Parse Error" messgae': function(err, data){
+ assert.match(err.message, /^Parse Error/);
+ },
+ 'data should be undefined': function(err, data){
+ assert.isUndefined(data);
+ },
+ }
+}).export(module);

0 comments on commit 48ca856

Please sign in to comment.