Repository: aws/aws-sdk-js
Pull request #1894 (2018-01-24 19:03:42)

Allow short-circuiting of requests to the IMDS by setting the AWS_EC2_METADATA_DISABLED environment variable
This allows faster default credential chain resolution if you know you're not running on EC2. This functionality matches https://github.com/aws/aws-sdk-php/pull/1473

chrisradek: Also, needs a changelog entry.
codecov-io: # [Codecov](https://codecov.io/gh/aws/aws-sdk-js/pull/1894?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@77a2693`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/aws/aws-sdk-js/pull/1894/graphs/tree.svg?width=650&src=pr&token=qXdPGBZWAr&height=150)](https://codecov.io/gh/aws/aws-sdk-js/pull/1894?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master    #1894   +/-   ##
=========================================
  Coverage          ?   95.98%           
=========================================
  Files             ?      221           
  Lines             ?     7325           
  Branches          ?     1399           
=========================================
  Hits              ?     7031           
  Misses            ?      294           
  Partials          ?        0
```


| [Impacted Files](https://codecov.io/gh/aws/aws-sdk-js/pull/1894?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [lib/util.js](https://codecov.io/gh/aws/aws-sdk-js/pull/1894/diff?src=pr&el=tree#diff-bGliL3V0aWwuanM=) | `94.33% <ø> (ø)` | |
| [lib/metadata\_service.js](https://codecov.io/gh/aws/aws-sdk-js/pull/1894/diff?src=pr&el=tree#diff-bGliL21ldGFkYXRhX3NlcnZpY2UuanM=) | `96.96% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/aws/aws-sdk-js/pull/1894?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/aws/aws-sdk-js/pull/1894?src=pr&el=footer). Last update [77a2693...1e0308b](https://codecov.io/gh/aws/aws-sdk-js/pull/1894?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.changes/next-release/feature-MetadataService-fe82da01.json b/.changes/next-release/feature-MetadataService-fe82da01.json
new file mode 100644
index 000000000..1918ca83b
--- /dev/null
+++ b/.changes/next-release/feature-MetadataService-fe82da01.json
@@ -0,0 +1,5 @@
+{
+  "type": "feature",
+  "category": "MetadataService",
+  "description": "Allow environmental disabling of the AWS.MetadataService client by setting the AWS_EC2_METADATA_DISABLED environment variable to a truthy value."
+}
\ No newline at end of file
diff --git a/lib/metadata_service.js b/lib/metadata_service.js
index 08f508168..81b877734 100644
--- a/lib/metadata_service.js
+++ b/lib/metadata_service.js
@@ -7,6 +7,9 @@ var inherit = AWS.util.inherit;
  * {request} method, you can receieve metadata about any available resource
  * on the metadata service.
  *
+ * You can disable the use of the IMDS by setting the AWS_EC2_METADATA_DISABLED
+ * environment variable to a truthy value.
+ *
  * @!attribute [r] httpOptions
  *   @return [map] a map of options to pass to the underlying HTTP request:
  *
@@ -64,6 +67,11 @@ AWS.MetadataService = inherit({
    *     the response
    */
   request: function request(path, callback) {
+    if (process.env[AWS.util.imdsDisabledEnv]) {
+      callback(new Error('EC2 Instance Metadata Service access disabled'));
+      return;
+    }
+
     path = path || '/';
     var httpRequest = new AWS.HttpRequest('http://' + this.host + path);
     httpRequest.method = 'GET';
diff --git a/lib/util.js b/lib/util.js
index da4d2b5d2..8ffa71059 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -926,7 +926,12 @@ var util = {
   /**
    * @api private
    */
-  sharedConfigFileEnv: 'AWS_CONFIG_FILE'
+  sharedConfigFileEnv: 'AWS_CONFIG_FILE',
+
+  /**
+   * @api private
+   */
+  imdsDisabledEnv: 'AWS_EC2_METADATA_DISABLED'
 };
 
 module.exports = util;
diff --git a/package.json b/package.json
index 24fa7e389..946ec7c43 100644
--- a/package.json
+++ b/package.json
@@ -139,4 +139,4 @@
     "react-native-test": "npm -s run-script build-react-native && rake reactnative:test && karma start",
     "region-check": "node ./scripts/region-checker/index.js"
   }
-}
\ No newline at end of file
+}


Test patch:
diff --git a/test/metadata_service.spec.js b/test/metadata_service.spec.js
index ae63b3f98..43d288637 100644
--- a/test/metadata_service.spec.js
+++ b/test/metadata_service.spec.js
@@ -82,7 +82,6 @@ if (AWS.util.isNode()) {
             }
           }));
         }
-        return results;
       });
 
       it('should fail if server is not up', function(done) {
@@ -159,8 +158,41 @@ if (AWS.util.isNode()) {
             }
           }));
         }
-        return results;
       });
     });
+
+    describe('request', function() {
+      var disabledEnvAtStart = process.env[AWS.util.imdsDisabledEnv];
+
+      beforeEach(function() {
+        delete process.env[AWS.util.imdsDisabledEnv];
+      });
+
+      afterEach(function() {
+        if (typeof disabledEnvAtStart === 'string') {
+          process.env[AWS.util.imdsDisabledEnv] = disabledEnvAtStart;
+        } else {
+          delete process.env[AWS.util.imdsDisabledEnv];
+        }
+      });
+
+      it(
+        'should not make any requests if the AWS_EC2_METADATA_DISABLED environment variable is set to a truthy value',
+        function(done) {
+          process.env.AWS_EC2_METADATA_DISABLED = '1';
+          var spy = helpers.spyOn(AWS.HttpClient.getInstance(), 'handleRequest')
+            .andCallThrough();
+          var service = new AWS.MetadataService();
+
+          service.request('/foo/bar', function(err, data) {
+            expect(spy.calls.length).to.equal(0);
+            expect(data).not.to.exist;
+            expect(err).to.exist;
+            expect(err.message).to.equal('EC2 Instance Metadata Service access disabled');
+            done();
+          });
+        }
+      );
+    });
   });
 }