All files / lib/aws-operations aws-config-new-user.js

12.68% Statements 9/71
0% Branches 0/20
0% Functions 0/10
12.68% Lines 9/71
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                        11x 11x 11x 11x   11x 11x 11x 11x                                                                                                                                                                                                                                                   11x      
/* 
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
*/
const fs = require('fs-extra')
const opn = require('opn')
const chalk = require('chalk')
const inquirer = require('inquirer')
 
const awsMobileRegions = require('./aws-regions.js').regions
const awsConfigFileManager = require('./aws-config-info-manager.js')
const awsmobileJSConstant = require('../utils/awsmobilejs-constant.js')
const nameManager = require('../utils/awsmobilejs-name-manager.js')
 
function setupNewUser(awsDetails, callback){
    let awsInfo = awsDetails.info
    let awsConfig = awsDetails.config
    
    console.log('Please follow these steps to setup your awsmobile access')
    console.log()
    console.log('Please sign up/in your aws account with Administrator Access:')
    console.log(chalk.green(awsmobileJSConstant.AWSAmazonConsoleUrl))
    opn(awsmobileJSConstant.AWSAmazonConsoleUrl, {wait: false})
    PressAnyKeyToContinue('Press Enter to continue', function(){
        console.log('Please specify the new IAM user to be created for awsmobile-cli')
        inquirer.prompt([
            {
                type: 'input',
                name: 'userName',
                message: "user name: ",
                default: nameManager.generateIAMUserName()
            },
            {
                type: 'list',
                name: 'region',
                message: "region: ",
                choices: awsMobileRegions,
                default: 'us-east-1'
            }
        ]).then(function (answers) {
            let deepLinkURL = awsmobileJSConstant.AWSCreateIAMUsersUrl.replace('{userName}', answers.userName).replace('{region}', answers.region)
            console.log('Please complete the user creation on the aws console')
            console.log(chalk.green(deepLinkURL))
            opn(deepLinkURL, {wait: false})
            return answers.region
        }).then(function(region){
            PressAnyKeyToContinue('Press Enter to continue', function(){
                console.log('Please enter the access key of the newly created user:')
                inquirer.prompt([
                    {
                        type: 'input',
                        name: 'accessKeyId',
                        message: "accessKeyId: ",
                        default: awsmobileJSConstant.DefaultAWSAccessKeyId
                    },
                    {
                        type: 'input',
                        name: 'secretAccessKey',
                        message: "secretAccessKey: ",
                        default: awsmobileJSConstant.DefaultAWSSecretAccessKey
                    }
                ]).then(function (answers) {
                    let awsConfigChanged = false
                    if(answers.accessKeyId){
                        let newKeyId = answers.accessKeyId.trim()
                        if(awsConfig.accessKeyId != newKeyId){
                            awsConfig.accessKeyId = newKeyId
                            awsConfigChanged = true
                        }
                    }
                    if(answers.secretAccessKey){
                        let newKey = answers.secretAccessKey.trim()
                        if( awsConfig.secretAccessKey != newKey){
                            awsConfig.secretAccessKey = newKey
                            awsConfigChanged = true
                        }  
                    }
                    if(region){
                        let newRegion = region.trim()
                        if( awsConfig.region != newRegion){
                            awsConfig.region = newRegion
                            awsConfigChanged = true
                        }  
                    }
                    if(awsConfigChanged){
                        let jsonString = JSON.stringify(awsConfig)
                        fs.writeFileSync(awsInfo.AWSConfigFilePath, jsonString, 'utf8')
                        awsConfigFileManager.setNoProfileSync(awsInfo)
                    }
                    return awsConfig
                }).then(function(awsConfig){
                    if(awsConfigFileManager.validateAWSConfig(awsDetails.config)){
                        console.log()
                        console.log('Successfully set the aws configurations for the awsmobile-cli')
                        if(callback){
                            console.log()
                            callback(awsDetails)
                        }else{
                            promptConfigInstruction()
                            process.exit()
                        }
                    }else{
                        console.log()
                        console.log('Something went wrong, please try again')
                        process.exit()
                    }
                })
            })
        })
    })
}
 
function promptConfigInstruction(){
    console.log()
    console.log(chalk.gray('# to change the credentials for the awsmobile-cli'))
    console.log('    $ awsmobile configure aws')
    console.log()
    console.log(chalk.gray('# to set the awsmobile-cli to use a named profile for aws access'))
    console.log('    $ awsmobile configure aws --profile <name>')
}
 
function PressAnyKeyToContinue(message, callback){
    process.stdin.resume()
    process.stdin.setEncoding('utf8')
 
    console.log(message)
    process.stdin.once('data', function(data){
        if(callback){
            callback()
        }
    })
}
 
 
module.exports = {
    setupNewUser
}