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

72.73% Statements 104/143
42.42% Branches 28/66
100% Functions 13/13
72.73% Lines 104/143

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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276                        28x 28x 28x 28x 28x   28x 28x 28x 28x 28x 28x     2x 2x   2x 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 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       28x               28x             3x 3x 3x 3x         1x           1x   1x 1x   1x       1x       1x       1x       1x     28x              
/* 
 * 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 chalk = require('chalk')
const path = require('path')
const moment = require('moment')
const lineByLine = require('n-readlines')
 
const awsMobileRegions = require('./aws-regions.js').regions
const awsConfigProfileReader = require('./aws-config-profile-reader.js')
const projectInfoManager = require('../project-info-manager.js')
const awsmobilejsConstant = require('../utils/awsmobilejs-constant.js')
const nameManager = require('../utils/awsmobilejs-name-manager.js')
const pathManager = require('../utils/awsmobilejs-path-manager.js')
 
function resolve(silentFlag){
    ensureFolderStructure()
    let projectInfo = projectInfoManager.getProjectInfo(true)
 
    if(projectInfo){
        awsInfo = resolve_project_awsConfig(projectInfo, silentFlag)
    }else{
        awsInfo = resolve_general_awsConfig(silentFlag)
    }
 
    return awsInfo
}
 
function setProfile(profileName){
    let isSuccessful = true
    let awsConfig = awsConfigProfileReader.getSystemConfig(profileName)
    Eif(awsConfig){
        ensureFolderStructure()
        let awsInfoFilePath = pathManager.getGeneralAWSInfoFilePath()
    
        let projectInfo = projectInfoManager.getProjectInfo(true)
        Eif(projectInfo){
            awsInfoFilePath = pathManager.getAWSInfoFilePath(projectInfo.ProjectPath)
        }
 
        let awsInfo = awsInfoTemplate
        let executeSet = true
        Eif(fs.existsSync(awsInfoFilePath)){
            awsInfo = JSON.parse(fs.readFileSync(awsInfoFilePath, 'utf8'))
            Iif(awsInfo.IsUsingProfile && awsInfo.ProfileName == profileName){
                executeSet = false
            }
        }
    
        Eif(executeSet){
            awsInfo.IsUsingProfile = true
            awsInfo.ProfileName = profileName
            awsInfo.AWSInfoFilePath = awsInfoFilePath
            Iif(!awsInfo.AWSConfigFilePath){
                awsInfo.AWSConfigFilePath = path.normalize(path.join(pathManager.getSysProjectAWSConfigDirPath(), nameManager.generateAWSConfigFileName(projectInfo)))
            }
            awsInfo.LastProfileSyncTime = moment().format(awsmobilejsConstant.DateTimeFormatString) 
 
            let jsonString = JSON.stringify(awsInfo, null, '\t')
            fs.writeFileSync(awsInfoFilePath, jsonString, 'utf8')
 
            let awsConfigJsonString = JSON.stringify(awsConfig)
            fs.writeFileSync(awsInfo.AWSConfigFilePath, awsConfigJsonString, 'utf8')
        }
    }else{
        isSuccessful = false
    }
 
    return isSuccessful
}
 
function setNoProfileSync(awsInfo){ 
    awsInfo.IsUsingProfile = false
    awsInfo.ProfileName = ""
    awsInfo.LastProfileSyncTime = ""
    let jsonString = JSON.stringify(awsInfo, null, '\t')
    fs.writeFileSync(awsInfo.AWSInfoFilePath, jsonString, 'utf8')
    return awsInfo
}
 
//////////////////////////////////////
function resolve_general_awsConfig(silentFlag){
    let awsInfoFilePath = pathManager.getGeneralAWSInfoFilePath()
    let awsConfigFilePath = pathManager.getGeneralAWSConfigFilePath()
 
    let awsInfo = awsInfoTemplate
    Eif(!fs.existsSync(awsInfoFilePath)){
        Iif(fs.existsSync(awsConfigFilePath)){ //meaning it was generated by older version of the cli
            awsInfo.IsUsingProfile = false
            awsInfo.ProfileName = ""
        }else{ //meaning this usage is started from fresh
            awsInfo.IsUsingProfile = true
            awsInfo.ProfileName = "default"
        }
        awsInfo.AWSInfoFilePath = awsInfoFilePath
        awsInfo.AWSConfigFilePath = awsConfigFilePath
        awsInfo.LastProfileSyncTime = ""
        let jsonString = JSON.stringify(awsInfo, null, '\t')
        fs.writeFileSync(awsInfoFilePath, jsonString, 'utf8')
    }else{
        awsInfo = JSON.parse(fs.readFileSync(awsInfoFilePath, 'utf8'))
    }
 
    let executeSyncProfile = false
    Iif(fs.existsSync(awsConfigFilePath)){
        executeSyncProfile = isSyncProfileNeeded(awsInfo)
    }else{
        Eif(awsInfo.IsUsingProfile){
            executeSyncProfile = true
        }else{
            let awsConfig = awsConfigTemplate
            let jsonString = JSON.stringify(awsConfig)
            fs.writeFileSync(awsConfigFilePath, jsonString, 'utf8')
        }
    }
    Eif(executeSyncProfile){
        syncProfile(awsInfo, silentFlag)
    }
    return awsInfo
}
 
function resolve_project_awsConfig(projectInfo, silentFlag){
    let awsInfo 
    let awsInfoFilePath = pathManager.getAWSInfoFilePath(projectInfo.ProjectPath)
    Iif(!fs.existsSync(awsInfoFilePath)){
        awsInfo = resolve_general_awsConfig(silentFlag)
        let generalAWSConfigFilePath = awsInfo.AWSConfigFilePath
        awsInfo.AWSInfoFilePath = awsInfoFilePath
        awsInfo.AWSConfigFilePath = path.normalize(path.join(pathManager.getSysProjectAWSConfigDirPath(), nameManager.generateAWSConfigFileName(projectInfo)))
        fs.copySync(generalAWSConfigFilePath, awsInfo.AWSConfigFilePath)
        let jsonString = JSON.stringify(awsInfo, null, '\t')
        fs.writeFileSync(awsInfoFilePath, jsonString, 'utf8')
    }else{
        awsInfo = JSON.parse(fs.readFileSync(awsInfoFilePath, 'utf8'))
        let awsConfigFilePath = awsInfo.AWSConfigFilePath
    
        let executeSyncProfile = false
        Eif(fs.existsSync(awsConfigFilePath)){
            executeSyncProfile = isSyncProfileNeeded(awsInfo)
        }else{
            if(awsInfo.IsUsingProfile){
                executeSyncProfile = true
            }else{
                let awsConfig = awsConfigTemplate
                let jsonString = JSON.stringify(awsConfig)
                fs.writeFileSync(awsConfigFilePath, jsonString, 'utf8')
            }
        }
        Iif(executeSyncProfile){
            syncProfile(awsInfo, silentFlag)
        }
    }
    return awsInfo
}
 
function syncProfile(awsInfo, silentFlag){
    let isSuccessful = false
    let awsConfig = awsConfigProfileReader.getSystemConfig(awsInfo.ProfileName, silentFlag)
    Eif(awsConfig){
        let jsonString = JSON.stringify(awsConfig)
        fs.writeFileSync(awsInfo.AWSConfigFilePath, jsonString, 'utf8')
 
        awsInfo.LastProfileSyncTime = moment().format(awsmobilejsConstant.DateTimeFormatString) 
        jsonString = JSON.stringify(awsInfo, null, '\t')
        fs.writeFileSync(awsInfo.AWSInfoFilePath, jsonString, 'utf8')
        isSuccessful = true
    }else if(!fs.existsSync(awsInfo.AWSConfigFilePath)){
        let awsConfig = awsConfigTemplate
        let jsonString = JSON.stringify(awsConfig)
        fs.writeFileSync(awsInfo.AWSConfigFilePath, jsonString, 'utf8')
    }
    return isSuccessful
}
 
function isSyncProfileNeeded(awsInfo){
    let result = false
 
    Iif(awsInfo.IsUsingProfile){
        let sysAwsConfigFilePath = pathManager.getSysAwsConfigFilePath()
        let sysAwsCredentailsFilePath = pathManager.getSysAwsCredentialsFilePath()
        if(fs.existsSync(sysAwsConfigFilePath) && fs.existsSync(sysAwsCredentailsFilePath)){
            let awsConfigFilePath = awsInfo.AWSConfigFilePath
            if(awsConfigFilePath && fs.existsSync(awsConfigFilePath)){ 
                let lastProfileSyncTime = moment(awsInfo.LastProfileSyncTime, awsmobilejsConstant.DateTimeFormatString)
                let lastSysConfigFileModificationTime = moment(fs.lstatSync(sysAwsConfigFilePath).mtime)
                let lastSysCredentialsFileModificationTime = moment(fs.lstatSync(sysAwsCredentailsFilePath).mtime)
                let lastConfigFileModificationTime = moment(fs.lstatSync(awsConfigFilePath).mtime)
 
                result =!lastProfileSyncTime.isValid() ||
                        !lastSysConfigFileModificationTime.isValid() ||
                        !lastSysCredentialsFileModificationTime.isValid() ||
                        !lastConfigFileModificationTime.isValid() ||
                        lastProfileSyncTime.isBefore(lastSysConfigFileModificationTime) ||
                        lastProfileSyncTime.isBefore(lastSysCredentialsFileModificationTime) ||
                        lastProfileSyncTime.isBefore(lastConfigFileModificationTime) 
            }else{
                result = true
            }
        }
    }
 
    return result
}
 
/////////////////////////////////
const awsInfoTemplate = {
    "IsUsingProfile": false,
    "ProfileName": "",
    "AWSConfigFilePath": "",
	"AWSInfoFilePath": "",
	"LastProfileSyncTime": "2018-01-01-01-01-01"
}
 
const awsConfigTemplate = {
    "accessKeyId": awsmobilejsConstant.DefaultAWSAccessKeyId,
    "secretAccessKey": awsmobilejsConstant.DefaultAWSSecretAccessKey,
    "region": "us-west-1"
}
 
function ensureFolderStructure(){
    let sysAWSMobileJSDirPath = pathManager.getSysAWSMobileJSDirPath()
    let sysProjectAWSConfigDirPath = pathManager.getSysProjectAWSConfigDirPath()
    fs.ensureDirSync(sysAWSMobileJSDirPath)
    fs.ensureDirSync(sysProjectAWSConfigDirPath)
}
 
/////////////////////////////////////////////
function validateAWSConfig(awsConfig){
    return validateAccessKeyID(awsConfig.accessKeyId) &&
    validateSecretAccessKey(awsConfig.secretAccessKey) &&
    validateAWSRegion(awsConfig.region)
}
 
function isNewUser(awsDetails){//only called when resolved awsconfig is invalid
    let result = true
 
    let sysAwsConfigFilePath = pathManager.getSysAwsConfigFilePath()
    let sysAwsCredentailsFilePath = pathManager.getSysAwsCredentialsFilePath()
 
    Iif(fs.existsSync(sysAwsConfigFilePath) && fs.existsSync(sysAwsCredentailsFilePath)){
        result = false
    }
 
    return result
}
 
function validateAccessKeyID(accessKeyId){
    return (accessKeyId && accessKeyId != awsmobilejsConstant.DefaultAWSAccessKeyId)
}
 
function validateSecretAccessKey(secretAccessKey){
    return (secretAccessKey && secretAccessKey != awsmobilejsConstant.DefaultAWSSecretAccessKey)
}
 
function validateAWSRegion(region){
    return awsMobileRegions.includes(region)
}
 
module.exports = {
    resolve, 
    setProfile, 
    setNoProfileSync, 
    validateAWSConfig, 
    isNewUser
}