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

80.88% Statements 55/68
50% Branches 9/18
87.5% Functions 7/8
80.88% Lines 55/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        27x 27x 27x 27x   27x 27x 27x 27x 27x     1x 1x 1x   1x 1x 1x 1x 1x   1x   1x 1x                             1x 1x 1x 1x 1x 1x   1x   1x 1x                             1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x         1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x                                           27x      
/* 
 * 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')
const pressEnterKeyToContinue = require('../utils/press-enter-to-continue.js')
 
function setupNewUser(awsDetails, callback){
    let awsInfo = awsDetails.info
    let awsConfig = awsDetails.config
    let userSelectedRegion = 'us-east-1'
    
    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})
 
    return pressEnterKeyToContinue.run({message: 'Press Enter to continue'})
    .then((handle)=>{
        console.log('Please specify the new IAM user to be created for awsmobile-cli')
        return 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((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})
        userSelectedRegion = answers.region
        return answers.region
    }).then((userSelectedRegion)=>{
        return pressEnterKeyToContinue.run({message: 'Press Enter to continue'})
    }).then((handle)=>{
        console.log('Please enter the access key of the newly created user:')
        return inquirer.prompt([
                {
                    type: 'input',
                    name: 'accessKeyId',
                    message: "accessKeyId: ",
                    default: awsmobilejsConstant.DefaultAWSAccessKeyId
                },
                {
                    type: 'input',
                    name: 'secretAccessKey',
                    message: "secretAccessKey: ",
                    default: awsmobilejsConstant.DefaultAWSSecretAccessKey
                }
            ])
    }).then((answers)=>{
        let awsConfigChanged = false
        Eif(answers.accessKeyId){
            let newKeyId = answers.accessKeyId.trim()
            Eif(awsConfig.accessKeyId != newKeyId){
                awsConfig.accessKeyId = newKeyId
                awsConfigChanged = true
            }
        }
        Eif(answers.secretAccessKey){
            let newKey = answers.secretAccessKey.trim()
            Eif( awsConfig.secretAccessKey != newKey){
                awsConfig.secretAccessKey = newKey
                awsConfigChanged = true
            }  
        }
        Eif(userSelectedRegion){
            let newRegion = userSelectedRegion.trim()
            Iif( awsConfig.region != newRegion){
                awsConfig.region = newRegion
                awsConfigChanged = true
            }  
        }
        Eif(awsConfigChanged){
            let jsonString = JSON.stringify(awsConfig)
            fs.writeFileSync(awsInfo.AWSConfigFilePath, jsonString, 'utf8')
            awsConfigFileManager.setNoProfileSync(awsInfo)
        }
        return awsConfig
    }).then(function(awsConfig){
        Eif(awsConfigFileManager.validateAWSConfig(awsDetails.config)){
            console.log()
            console.log('Successfully set the aws configurations for the awsmobile-cli')
            Eif(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>')
}
 
module.exports = {
    setupNewUser
}