All files / lib/feature-operations/scripts appsync-ops.js

71.76% Statements 61/85
40% Branches 18/45
91.67% Functions 11/12
71.76% Lines 61/85

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                            2x 2x 2x 2x 2x 2x 2x 2x   2x   2x           2x           3x   3x 3x 3x     3x                 3x 2x   1x   1x 1x                         3x 3x 3x   3x         2x 2x         1x 1x   1x 1x     2x 2x 2x   2x   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                                                                   2x          
/* 
 * 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.
*/
"use strict";
 
const fs = require('fs-extra')
const path = require('path')
const inquirer = require('inquirer')
const awsMobileRegions = require('../../aws-operations/aws-regions.js').regions
const appsyncManager = require('../../backend-operations/appsync-operations/appsync-manager.js')
const pathManager = require('../../utils/awsmobilejs-path-manager.js')
const awsmobilejsConstant = require('../../utils/awsmobilejs-constant.js')
const dfOps = require('../../utils/directory-file-ops.js')
 
const _featureName = 'appsync'
 
const AUTH_TYPES = [ 
  "AWS_IAM", 
  "API_KEY", 
  "AMAZON_COGNITO_USER_POOLS"
]
 
const DEFAULT_ACTIONS = [
  "ALLOW",
  "DENY"
]
 
function specify(projectInfo) {
  let graphqlApi = ensureFeatureEnabled(projectInfo)
 
  let defaultAuthType = "AWS_IAM"
  Eif(graphqlApi.authenticationType){
    defaultAuthType = graphqlApi.authenticationType
  }
 
  inquirer.prompt([
    {
        type: 'list',
        name: 'authType',
        message: "Please specify the auth type: ",
        choices: AUTH_TYPES,
        default: defaultAuthType
    }
  ]).then(function (answers) {
    if(answers.authType != graphqlApi.authenticationType){
      switchApiAuthType(projectInfo, graphqlApi, answers.authType)
    }else{
      switch(answers.authType){
        case "AWS_IAM": 
          configureForAuthTypeIAM(projectInfo, graphqlApi)
        break
        case "API_KEY": 
          configureForAuthTypeApiKey(projectInfo, graphqlApi)
        break
        case "AMAZON_COGNITO_USER_POOLS": 
          configureForAuthTypeCognitoUserPool(projectInfo, graphqlApi)
        break
      }
    }
  })
}
 
function ensureFeatureEnabled(projectInfo){
  let enabled = appsyncManager.getEnabledFeatures(projectInfo.ProjectPath)
  Eif(enabled.length == 0){
    appsyncManager.enable(projectInfo.ProjectPath)
  }
  return appsyncManager.getGraphqlApi(projectInfo.ProjectPath)
}
 
 
function switchApiAuthType(projectInfo, appsyncInfo, authType){
  let srcFileName = 'graphqlApi_iam.json'
  switch(authType){
    case "AWS_IAM": 
      srcFileName = 'graphqlApi_iam.json'
    break
    case "API_KEY": 
      srcFileName = 'graphqlApi_apiKey.json'
    break
    case "AMAZON_COGNITO_USER_POOLS": 
      srcFileName = 'graphqlApi_cognito.json'
    break
  }
 
  let templateDirPath = pathManager.getAppSyncTemplateDirPath()
  let configurableDirPath = path.join(templateDirPath, awsmobilejsConstant.AppSyncConfigurablesDirName)
  let srcFilePath = path.join(configurableDirPath, srcFileName)
 
  let graphqlApi = dfOps.readJsonFile(srcFilePath)
 
  switch(authType){
    case "AWS_IAM": 
      configureForAuthTypeIAM(projectInfo, graphqlApi)
    break
    case "API_KEY": 
      configureForAuthTypeApiKey(projectInfo, graphqlApi)
    break
    case "AMAZON_COGNITO_USER_POOLS": 
      configureForAuthTypeCognitoUserPool(projectInfo, graphqlApi)
    break
  }
}
 
function configureForAuthTypeIAM(projectInfo, graphqlApi){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectInfo.ProjectPath, _featureName)
  let graphqlApiFilePath = path.join(featureDirPath, 'graphqlApi.json')
  dfOps.writeJsonFile(graphqlApiFilePath, graphqlApi)
}
 
function configureForAuthTypeCognitoUserPool(projectInfo, graphqlApi){
  checkUserPoolConfig(projectInfo, graphqlApi)
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectInfo.ProjectPath, _featureName)
  let graphqlApiFilePath = path.join(featureDirPath, 'graphqlApi.json')
  let questions = [
    {
        type: 'input',
        name: 'userPoolId',
        message: "user pool id: ",
        default: graphqlApi.userPoolConfig.userPoolId
    },
    {
        type: 'list',
        name: 'awsRegion',
        message: "region: ",
        choices: awsMobileRegions,
        default: graphqlApi.userPoolConfig.awsRegion
    },
    {
        type: 'list',
        name: 'defaultAction',
        message: "default action: ",
        choices: DEFAULT_ACTIONS,
        default: graphqlApi.userPoolConfig.defaultAction
    }
  ]
  inquirer.prompt(questions).then((answers) => {
    Object.assign(graphqlApi.userPoolConfig, answers)
    dfOps.writeJsonFile(graphqlApiFilePath, graphqlApi)
  })
}
 
function configureForAuthTypeApiKey(projectInfo, graphqlApi){
  let featureDirPath = pathManager.getBackendFeatureDirPath(projectInfo.ProjectPath, _featureName)
  let graphqlApiFilePath = path.join(featureDirPath, 'graphqlApi.json')
  dfOps.writeJsonFile(graphqlApiFilePath, graphqlApi)
  let apiKeys = appsyncManager.getApiKeys(projectInfo.ProjectPath)
  Eif(!apiKeys || apiKeys.length == 0){
    apiKeys = [{}]
    appsyncManager.setApiKeys(projectInfo.ProjectPath, apiKeys)
  }
}
 
function checkUserPoolConfig(projectInfo, graphqlApi){
  Iif(!graphqlApi.userPoolConfig){
    graphqlApi.userPoolConfig = {
      "userPoolId": "<userPoolId>",
      "awsRegion": "us-east-1",
      "defaultAction": "ALLOW",
      "appIdClientRegex": null
    }
  }
  Iif(!graphqlApi.userPoolConfig.userPoolId || 
    graphqlApi.userPoolConfig.userPoolId == '<userPoolId>'){
    let currentUserPool = getCurentUserPool(projectInfo.ProjectPath)
    if(currentUserPool){
      graphqlApi.userPoolConfig.userPoolId = currentUserPool['user-pools-id']
      graphqlApi.userPoolConfig.awsRegion = currentUserPool['region']
    }
  }
  Iif(!graphqlApi.userPoolConfig.defaultAction || !DEFAULT_ACTIONS.includes(graphqlApi.userPoolConfig.defaultAction)){
    graphqlApi.defaultAction = "ALLOW"
  }
}
 
function getCurentUserPool(projectPath){
  let currentUserPool
  let currentBackendDetailsFilePath = pathManager.getCurrentBackendDetailsFilePath(projectPath)
  if(fs.existsSync(currentBackendDetailsFilePath)){
    let backendDetails = dfOps.readJsonFile(currentBackendDetailsFilePath)
    if(backendDetails && backendDetails.resources && backendDetails.resources.length>0){
      for(let i=0; i<backendDetails.resources.length; i++){
        let resource = backendDetails.resources[i]
        if(resource.type == "AWS::Cognito::UserPool" && 
          resource.feature == "user-signin" && 
          resource.attributes && resource.attributes['user-pools-id'] && resource.attributes['region']){
          currentUserPool = {
            'user-pools-id': resource.attributes['user-pools-id'], 
            'region': resource.attributes['region']
          }
          break
        }
      }
    }
  }
  return currentUserPool
}
 
function onFeatureTurnOn(projectInfo, cloudProjectSpec) {
}
 
function onFeatureTurnOff(projectInfo, cloudProjectSpec) {
}
 
module.exports = {
  specify,
  onFeatureTurnOn,
  onFeatureTurnOff
}