aws-get-credentials
Advanced tools
Comparing version 0.1.0 to 1.0.0
{ | ||
"name": "aws-get-credentials", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Create an AWS credentials instance from local credentials", | ||
"main": "source/index.js", | ||
"scripts": { | ||
"lint": "eslint source/**/*.js", | ||
"test": "npm run lint" | ||
"docs": "jsdoc2md source/**/*.js > API.md", | ||
"format": "prettier --write \"{source,test}/**/*.js\"", | ||
"test": "npm run test:unit", | ||
"test:format": "prettier-check \"{source,test}/**/*.js\"", | ||
"test:unit": "nyc mocha -r test/index.js test/unit/**/*.spec.js" | ||
}, | ||
"files": [ | ||
"source/**/*.js", | ||
"*.md" | ||
], | ||
"lint-staged": { | ||
"{source,test}/**/*.js": [ | ||
"prettier --write" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"repository": { | ||
@@ -33,9 +50,17 @@ "type": "git", | ||
"ini": "^1.3.4", | ||
"os-homedir": "^1.0.2", | ||
"pify": "^2.3.0", | ||
"pify": "^4.0.1", | ||
"verror": "^1.9.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^3.19.0" | ||
"aws-sdk": "^2.618.0", | ||
"chai": "^4.2.0", | ||
"husky": "^4.2.3", | ||
"jsdoc-to-markdown": "^5.0.3", | ||
"lint-staged": "^10.0.7", | ||
"mocha": "^7.0.1", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.19.1", | ||
"prettier-check": "^2.0.0", | ||
"sinon": "^8.1.1" | ||
} | ||
} |
# aws-get-credentials | ||
Create an AWS credentials instance from local credentials | ||
> Create an AWS credentials instance from local credentials | ||
[![Build Status](https://travis-ci.org/perry-mitchell/aws-get-credentials.svg?branch=master)](https://travis-ci.org/perry-mitchell/aws-get-credentials) | ||
[![npm version](https://badge.fury.io/js/aws-get-credentials.svg)](https://www.npmjs.com/package/aws-get-credentials) [![Build Status](https://travis-ci.org/perry-mitchell/aws-get-credentials.svg?branch=master)](https://travis-ci.org/perry-mitchell/aws-get-credentials) | ||
## About | ||
Fetch a credentials instance from your local machine using a very simple fetcher method. | ||
@@ -12,4 +13,14 @@ | ||
## Usage | ||
The exported method `getAWSCredentials` takes two parameters: | ||
The `aws-get-credentials` module exports two methods: | ||
* `getAWSCredentials` - Fetch an AWS `Credentials` instance from local configuration | ||
* `getAWSProfiles` - Get an array of profile names available | ||
Check out the [API documentation](API.md) for more information. | ||
### getAWSCredentials | ||
The method `getAWSCredentials` takes two parameters: | ||
```javascript | ||
@@ -21,7 +32,8 @@ getAWSCredentials(optionalProfileOverride, optionalPathOverride) // => Promise.<AWS.Credentials> | ||
### Example | ||
#### Example | ||
Firstly, import the function: | ||
```javascript | ||
const getAWSCredentials = require("aws-get-credentials"); | ||
const { getAWSCredentials } = require("aws-get-credentials"); | ||
``` | ||
@@ -49,1 +61,17 @@ | ||
``` | ||
### getAWSProfiles | ||
This method asynchronously fetches a list of profile names in the credentials file: | ||
```javascript | ||
const { getAWSProfiles } = require("aws-get-credentials"); | ||
getAWSProfiles().then(profiles => { | ||
// [ | ||
// "company-prod", | ||
// "company-stag", | ||
// "default" | ||
// ] | ||
}); | ||
``` |
const path = require("path"); | ||
const fs = require("fs"); | ||
const os = require("os"); | ||
const pify = require("pify"); | ||
const osHomedir = require("os-homedir"); | ||
const ini = require("ini"); | ||
const VError = require("verror"); | ||
const { Credentials } = require("aws-sdk"); | ||
const readFile = pify(fs.readFile); | ||
const getCredentialsClass = () => require("aws-sdk").Credentials; | ||
@@ -16,2 +16,6 @@ // Credentials file should be at the following path: | ||
/** | ||
* @module GetAWSCredentials | ||
*/ | ||
/** | ||
* Get an AWS.Credentials instance by reading a local credentials configuration | ||
@@ -26,13 +30,14 @@ * @param {String=} profileOverride Optional override for the profile to use | ||
* Credentials instance | ||
* @memberof module:GetAWSCredentials | ||
*/ | ||
module.exports = function getAWSCredentials(profileOverride, pathOverride) { | ||
const awsCredentialsPath = pathOverride || | ||
function getAWSCredentials(profileOverride, pathOverride) { | ||
const Credentials = getCredentialsClass(); | ||
const awsCredentialsPath = | ||
pathOverride || | ||
process.env.AWS_CREDENTIALS_PATH || | ||
path.resolve(osHomedir(), "./.aws/credentials"); | ||
const awsCredentialsProfile = profileOverride || | ||
process.env.AWS_DEFAULT_PROFILE || | ||
"default"; | ||
path.resolve(os.homedir(), "./.aws/credentials"); | ||
const awsCredentialsProfile = profileOverride || process.env.AWS_DEFAULT_PROFILE || "default"; | ||
return readFile(awsCredentialsPath, "utf8") | ||
.then(rawData => ini.parse(rawData)) | ||
.then(function _handleCredentialsConfig(credentialsData) { | ||
.then(credentialsData => { | ||
const { | ||
@@ -50,5 +55,31 @@ aws_access_key_id: accessKey, | ||
}) | ||
.catch(function _handleFailure(error) { | ||
.catch(error => { | ||
throw new VError(error, "Failed getting credentials"); | ||
}); | ||
} | ||
/** | ||
* Get an array of available AWS profiles | ||
* @param {String=} pathOverride Optional AWS credentials file path override | ||
* @returns {Promise.<String[]>} | ||
* @memberof module:GetAWSCredentials | ||
*/ | ||
function getAWSProfiles(pathOverride) { | ||
const awsCredentialsPath = | ||
pathOverride || | ||
process.env.AWS_CREDENTIALS_PATH || | ||
path.resolve(os.homedir(), "./.aws/credentials"); | ||
return readFile(awsCredentialsPath, "utf8") | ||
.then(rawData => ini.parse(rawData)) | ||
.then(credentials => | ||
credentials && typeof credentials === "object" ? Object.keys(credentials) : [] | ||
) | ||
.catch(err => { | ||
throw new VError(error, "Failed getting profiles"); | ||
}); | ||
} | ||
module.exports = { | ||
getAWSCredentials, | ||
getAWSProfiles | ||
}; |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4
76
0
75
10342
10
6
4
+ Addedpify@4.0.1(transitive)
- Removedos-homedir@^1.0.2
- Removedos-homedir@1.0.2(transitive)
- Removedpify@2.3.0(transitive)
Updatedpify@^4.0.1