All files / lib/aws-operations aws-config-manager.js

85.59% Statements 95/111
61.9% Branches 26/42
92.31% Functions 12/13
85.59% Lines 95/111

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                        26x 26x 26x 26x 26x   26x 26x 26x 26x     3x   3x 1x   2x 1x 1x 1x   1x 1x               1x 1x 1x     1x 1x                   2x 2x         2x                           2x 1x     2x 2x   2x                                         2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x     2x 2x 2x         2x 2x 2x 2x     2x 1x           1x 1x 1x 1x     1x         2x 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       8x 8x 8x 8x   3x 3x 3x     8x       8x       8x     26x              
/* 
 * 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 util = require('util')
 
const awsMobileRegions = require('./aws-regions.js').regions
const awsConfigInfoManager = require('./aws-config-info-manager.js')
const newUserHelper = require('./aws-config-new-user.js')
const awsmobilejsConstant = require('../utils/awsmobilejs-constant.js')
 
function checkAWSConfig(callback){
    let awsDetails = resolveAWSConfig(true)
 
    if(awsConfigInfoManager.validateAWSConfig(awsDetails.config)){
        callback(awsDetails)
    }else{
        if(awsConfigInfoManager.isNewUser(awsDetails)){
            console.log()
            console.log('awsmobile-cli could not resolve aws account settings')
            newUserHelper.setupNewUser(awsDetails, callback)
        }else{
            console.log(chalk.red('missing aws account settings'))
            inquirer.prompt([
                {
                    type: 'confirm',
                    name: 'configureAWS',
                    message: 'configure aws account settings',
                    default: true
                }
            ]).then(function (answers) {
                Eif(answers.configureAWS){
                    setConfig(awsDetails, function(awsDetails){
                        Iif(awsConfigInfoManager.validateAWSConfig(awsDetails.config)){
                            callback(awsDetails)
                        }else{
                            console.log(chalk.red('invalid aws account settings'))
                            opn(awsmobilejsConstant.AWSCreateIAMUsersUrl, {wait: false})
                        }
                    })
                }
            })
        }
    }
}
 
function newUserSetup(){
    let awsDetails = resolveAWSConfig(true)
    Iif(awsConfigInfoManager.validateAWSConfig(awsDetails.config)){
        console.log('awsmobile-cli already has aws access credentials')
        listAWSConfig(awsDetails)
        promptConfigInstruction()
    }else{
        newUserHelper.setupNewUser(awsDetails)
    }
}
 
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 setConfig(awsDetails, callback){
    if(!awsDetails){
        awsDetails = resolveAWSConfig(true)
    }
    
    let awsInfo = awsDetails.info
    let awsConfig = awsDetails.config
 
    inquirer.prompt([
        {
            type: 'input',
            name: 'accessKeyId',
            message: "accessKeyId: ",
            default: awsConfig.accessKeyId
        },
        {
            type: 'input',
            name: 'secretAccessKey',
            message: "secretAccessKey: ",
            default: awsConfig.secretAccessKey
        },
        {
            type: 'list',
            name: 'region',
            message: "region: ",
            choices: awsMobileRegions,
            default: awsConfig.region
        }
    ]).then(function (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(answers.region){
            let newRegion = answers.region.trim()
            Iif( awsConfig.region != newRegion){
                awsConfig.region = newRegion
                awsConfigChanged = true
            }  
        }
        Eif(awsConfigChanged){
            let jsonString = JSON.stringify(awsConfig)
            fs.writeFileSync(awsInfo.AWSConfigFilePath, jsonString, 'utf8')
            awsConfigInfoManager.setNoProfileSync(awsInfo)
        }
 
        if(callback){
            callback(awsDetails)
        }
    })
}
 
function setProfile(profileName){
    console.log('Setting awsmobile-cli to use named profile: ' + chalk.blue(profileName))
    let isSuccessful = awsConfigInfoManager.setProfile(profileName) 
    console.log()
    Iif(isSuccessful){
        console.log('Done')
    }else{
        console.log('Failed to set profile ' + chalk.blue(profileName))
    }
}
 
function configureAWS(profileName){
    if(profileName){
        setProfile(profileName)
    }else{
        setConfig()
    }
}
 
function configureWithKeyAndRegion(keyID, key, region){
    awsDetails = resolveAWSConfig(true)
    let awsInfo = awsDetails.info
    let awsConfig = awsDetails.config
    let awsConfigChanged = false
 
    let newKeyID = keyID.trim()
    Eif(awsConfig.accessKeyId != newKeyID){
        awsConfig.accessKeyId = newKeyID
        awsConfigChanged = true
    }
 
    let newKey = key.trim()
    Eif( awsConfig.secretAccessKey != newKey){
        awsConfig.secretAccessKey = newKey
        awsConfigChanged = true
    }  
    
    let newRegion = region.trim()
    Iif( awsConfig.region != newRegion){
        awsConfig.region = newRegion
        awsConfigChanged = true
    }  
 
    Eif(awsConfigChanged){
        let jsonString = JSON.stringify(awsConfig)
        fs.writeFileSync(awsInfo.AWSConfigFilePath, jsonString, 'utf8')
        awsConfigInfoManager.setNoProfileSync(awsInfo)
    }
}
 
function listAWSConfig(){
    let awsDetails = resolveAWSConfig(true)
    console.log()
    console.log('IsUsingProfile: ' + awsDetails.info.IsUsingProfile)
    Iif(awsDetails.info.IsUsingProfile){
        console.log('ProfileName: ' + awsDetails.info.ProfileName)
    }
    console.log()
    console.log(util.inspect(awsDetails.config, false, null))
    console.log()
}
 
function resolveAWSConfig(silentFlag){
    let awsConfig = {}
    let awsInfo = awsConfigInfoManager.resolve(silentFlag)
    try{
        awsConfig = JSON.parse(fs.readFileSync(awsInfo.AWSConfigFilePath, 'utf8'))
    }catch(e){
        awsConfig.accessKeyId = awsmobilejsConstant.DefaultAWSAccessKeyId
        awsConfig.secretAccessKey = awsmobilejsConstant.DefaultAWSSecretAccessKey
        awsConfig.region = awsmobilejsConstant.DefaultAWSRegion
    }
    
    return constructAwsDetails(awsInfo, awsConfig)
}
 
function constructAwsDetails(awsInfo, awsConfig){
    let result = {
        info: awsInfo,
        config: awsConfig
    }
    return result
}
 
module.exports = {
    checkAWSConfig,
    configureAWS,
    configureWithKeyAndRegion,
    newUserSetup,
    listAWSConfig
}