git-off

git off handles large files in git repos
git clone https://noulin.net/git/git-off.git
Log | Files | Refs | README

credential_provider_chain.js (5767B)


      1 var AWS = require('../core');
      2 
      3 /**
      4  * Creates a credential provider chain that searches for AWS credentials
      5  * in a list of credential providers specified by the {providers} property.
      6  *
      7  * By default, the chain will use the {defaultProviders} to resolve credentials.
      8  * These providers will look in the environment using the
      9  * {AWS.EnvironmentCredentials} class with the 'AWS' and 'AMAZON' prefixes.
     10  *
     11  * ## Setting Providers
     12  *
     13  * Each provider in the {providers} list should be a function that returns
     14  * a {AWS.Credentials} object, or a hardcoded credentials object. The function
     15  * form allows for delayed execution of the credential construction.
     16  *
     17  * ## Resolving Credentials from a Chain
     18  *
     19  * Call {resolve} to return the first valid credential object that can be
     20  * loaded by the provider chain.
     21  *
     22  * For example, to resolve a chain with a custom provider that checks a file
     23  * on disk after the set of {defaultProviders}:
     24  *
     25  * ```javascript
     26  * var diskProvider = new AWS.FileSystemCredentials('./creds.json');
     27  * var chain = new AWS.CredentialProviderChain();
     28  * chain.providers.push(diskProvider);
     29  * chain.resolve();
     30  * ```
     31  *
     32  * The above code will return the `diskProvider` object if the
     33  * file contains credentials and the `defaultProviders` do not contain
     34  * any credential settings.
     35  *
     36  * @!attribute providers
     37  *   @return [Array<AWS.Credentials, Function>]
     38  *     a list of credentials objects or functions that return credentials
     39  *     objects. If the provider is a function, the function will be
     40  *     executed lazily when the provider needs to be checked for valid
     41  *     credentials. By default, this object will be set to the
     42  *     {defaultProviders}.
     43  *   @see defaultProviders
     44  */
     45 AWS.CredentialProviderChain = AWS.util.inherit(AWS.Credentials, {
     46 
     47   /**
     48    * Creates a new CredentialProviderChain with a default set of providers
     49    * specified by {defaultProviders}.
     50    */
     51   constructor: function CredentialProviderChain(providers) {
     52     if (providers) {
     53       this.providers = providers;
     54     } else {
     55       this.providers = AWS.CredentialProviderChain.defaultProviders.slice(0);
     56     }
     57   },
     58 
     59   /**
     60    * @!method  resolvePromise()
     61    *   Returns a 'thenable' promise.
     62    *   Resolves the provider chain by searching for the first set of
     63    *   credentials in {providers}.
     64    *
     65    *   Two callbacks can be provided to the `then` method on the returned promise.
     66    *   The first callback will be called if the promise is fulfilled, and the second
     67    *   callback will be called if the promise is rejected.
     68    *   @callback fulfilledCallback function(credentials)
     69    *     Called if the promise is fulfilled and the provider resolves the chain
     70    *     to a credentials object
     71    *     @param credentials [AWS.Credentials] the credentials object resolved
     72    *       by the provider chain.
     73    *   @callback rejectedCallback function(error)
     74    *     Called if the promise is rejected.
     75    *     @param err [Error] the error object returned if no credentials are found.
     76    *   @return [Promise] A promise that represents the state of the `resolve` method call.
     77    *   @example Calling the `resolvePromise` method.
     78    *     var promise = chain.resolvePromise();
     79    *     promise.then(function(credentials) { ... }, function(err) { ... });
     80    */
     81 
     82   /**
     83    * Resolves the provider chain by searching for the first set of
     84    * credentials in {providers}.
     85    *
     86    * @callback callback function(err, credentials)
     87    *   Called when the provider resolves the chain to a credentials object
     88    *   or null if no credentials can be found.
     89    *
     90    *   @param err [Error] the error object returned if no credentials are
     91    *     found.
     92    *   @param credentials [AWS.Credentials] the credentials object resolved
     93    *     by the provider chain.
     94    * @return [AWS.CredentialProviderChain] the provider, for chaining.
     95    */
     96   resolve: function resolve(callback) {
     97     if (this.providers.length === 0) {
     98       callback(new Error('No providers'));
     99       return this;
    100     }
    101 
    102     var index = 0;
    103     var providers = this.providers.slice(0);
    104 
    105     function resolveNext(err, creds) {
    106       if ((!err && creds) || index === providers.length) {
    107         callback(err, creds);
    108         return;
    109       }
    110 
    111       var provider = providers[index++];
    112       if (typeof provider === 'function') {
    113         creds = provider.call();
    114       } else {
    115         creds = provider;
    116       }
    117 
    118       if (creds.get) {
    119         creds.get(function(getErr) {
    120           resolveNext(getErr, getErr ? null : creds);
    121         });
    122       } else {
    123         resolveNext(null, creds);
    124       }
    125     }
    126 
    127     resolveNext();
    128     return this;
    129   }
    130 });
    131 
    132 /**
    133  * The default set of providers used by a vanilla CredentialProviderChain.
    134  *
    135  * In the browser:
    136  *
    137  * ```javascript
    138  * AWS.CredentialProviderChain.defaultProviders = []
    139  * ```
    140  *
    141  * In Node.js:
    142  *
    143  * ```javascript
    144  * AWS.CredentialProviderChain.defaultProviders = [
    145  *   function () { return new AWS.EnvironmentCredentials('AWS'); },
    146  *   function () { return new AWS.EnvironmentCredentials('AMAZON'); },
    147  *   function () { return new AWS.SharedIniFileCredentials(); },
    148  *   function () {
    149  *     // if AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is set
    150  *       return new AWS.ECSCredentials();
    151  *     // else
    152  *       return new AWS.EC2MetadataCredentials();
    153  *   }
    154  * ]
    155  * ```
    156  */
    157 AWS.CredentialProviderChain.defaultProviders = [];
    158 
    159 /**
    160  * @api private
    161  */
    162 AWS.CredentialProviderChain.addPromisesToClass = function addPromisesToClass(PromiseDependency) {
    163   this.prototype.resolvePromise = AWS.util.promisifyMethod('resolve', PromiseDependency);
    164 };
    165 
    166 /**
    167  * @api private
    168  */
    169 AWS.CredentialProviderChain.deletePromisesFromClass = function deletePromisesFromClass() {
    170   delete this.prototype.resolvePromise;
    171 };
    172 
    173 AWS.util.addPromises(AWS.CredentialProviderChain);