-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose core functions: revokePermissions and grantPermissions
Expose main functions to allow use by other libraries fixed #3
- Loading branch information
1 parent
65134b3
commit ed39c02
Showing
6 changed files
with
112 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/local/bin/node | ||
const fs = require('fs'); | ||
const yargs = require('yargs'); | ||
const delay = require('timeout-as-promise'); | ||
const request = require('request-promise'); | ||
const debugCore = require('debug'); | ||
const { revokePermissions, grantPermissions, useAWSProfile } = require('../index'); | ||
|
||
const debug = debugCore('aws-manage-sg-cli'); | ||
|
||
debugCore.enable('aws-manage-sg*'); | ||
|
||
async function getIPAddress() { | ||
const response = await request('http://checkip.amazonaws.com/'); | ||
return response.replace(/\s/g, ''); | ||
} | ||
|
||
async function buildConfig(configFile) { | ||
return { | ||
...configFile, | ||
ipAddress: await getIPAddress(), | ||
}; | ||
} | ||
|
||
async function run(options, config) { | ||
async function giveRevocationTimeToSet() { | ||
await delay(1000); | ||
} | ||
|
||
function shouldRunByDefault() { | ||
return !options.revoke && !options.grant; | ||
} | ||
|
||
const revoke = options.revoke || shouldRunByDefault(); | ||
const grant = options.grant || shouldRunByDefault(); | ||
|
||
if (options.profile) { | ||
useAWSProfile(options.profile); | ||
} | ||
|
||
if (revoke) { | ||
await revokePermissions(config); | ||
await giveRevocationTimeToSet(); | ||
} | ||
|
||
if (grant) { | ||
await grantPermissions(config); | ||
} | ||
} | ||
|
||
function getOptions() { | ||
return yargs | ||
.usage('Usage: $0 <command> [options]') | ||
.alias('f', 'file') | ||
.describe('f', 'Path to config file') | ||
.alias('g', 'grant') | ||
.describe('g', 'Run only the grant') | ||
.alias('r', 'revoke') | ||
.describe('r', 'Run only the revoke') | ||
.alias('p', 'profile') | ||
.describe('p', 'AWS profile to use') | ||
.demandOption(['file'], 'Please provide a path to a config file') | ||
.help('h').argv; | ||
} | ||
|
||
function readConfigFile(path) { | ||
return JSON.parse(fs.readFileSync(path)); | ||
} | ||
|
||
(async () => { | ||
try { | ||
const options = getOptions(); | ||
const config = readConfigFile(options.file); | ||
await run(options, await buildConfig(config)); | ||
} catch (e) { | ||
debug(`ERROR: ${e.message}`); | ||
process.exit(1); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters