0

下面的代码在本地运行时工作正常,但在 aws lambda 函数中运行时,在对 /webhook 端点执行“POST”时,“getAccessToken ()”无法按预期工作。我正在使用节点 8.10,“aws-serverless-express”:“^3.3.6”,“express”:“^4.16.4”和“node-fetch”:“^2.5.0”,基本上它打印正确jwt 令牌,但 node-fetch 不返回任何内容,示例日志。

开始请求 ID:c8efba59-1869-4eaa-b9d8-aa15a7507d52 版本:$LATEST 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52 开始执行 2019-05-27T19:55:92-862929 -4eaa-b9d8-aa15a7507d52 execution_url: 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52 https://cloudmanager.adobe.io/somevalidurl 2019-05-27T19:5559-328Z c8efba 1869-4eaa-b9d8-aa15a7507d52 start getAccessToken END RequestId:c8efba59-1869-4eaa-b9d8-aa15a7507d52 REPORT RequestId:c8efba59-1869-4eaa-b9d8-aa15a7507d52 持续时间:6657.00 ms 已用内存大小:1 ms 最大内存大小:2 ms : 37 MB

我确保 lambda 超时为 30 秒,尝试通过将其设置为 0 来禁用“node-fetch”超时,并为所有路由“app.use(timeout(“30000”))”使用了中间件,也用于该特定的 webhook请求超时。(我立即收到 200 pong 响应,但 getexectuion 异步功能无法正常工作)

const express = require('express')
const bodyParser = require('body-parser')
const crypto = require('crypto')
const jsrsasign = require('jsrsasign')
const fetch = require('node-fetch')
const timeout = require('connect-timeout')

const URL  = require('url').URL
const URLSearchParams = require('url').URLSearchParams



//require('dotenv').config()
const app = express()



async function getAccessToken () {
  console.log("start getAccessToken")
  const EXPIRATION = 60 * 60 // 1 hour

  const header = {
    'alg': 'RS256',
    'typ': 'JWT'
  }



  const payload = {
    'exp': Math.round(new Date().getTime() / 1000) + EXPIRATION,
    'iss': process.env.ORGANIZATION_ID,
    'sub': process.env.TECHNICAL_ACCOUNT_ID,
    'aud': `https://ims-na1.adobelogin.com/c/${process.env.API_KEY}`,
    'https://ims-na1.adobelogin.com/s/ent_cloudmgr_sdk': true
  }

  const jwtToken = jsrsasign.jws.JWS.sign('RS256', JSON.stringify(header), JSON.stringify(payload), process.env.PRIVATE_KEY)
  //console.log("jwt token:")
  //console.log(jwtToken)

  const body = new URLSearchParams({
      client_id: process.env.API_KEY,
      client_secret: process.env.CLIENT_SECRET,
      jwt_token: jwtToken
    })


   const response = await fetch('https://ims-na1.adobelogin.com/ims/exchange/jwt', {
    method: 'POST',
    options: { timeout: 0},
    timeout: 0,
    size: 0,
    body: body
  })//.catch(error => {
    //        console.log("an error happend in fetchg")
    //        console.log(error)
    //})


  const json = await response.json()

  if ((response.status !== 200) && (response.status !== 201)) {
      console.error(`Invalid response status ${ response.status }.`);
      throw json;
  }

  console.log("access_token:")
  console.log(json['access_token'])
  return json['access_token']
}

async function makeApiCall (accessToken, url, method) {
  console.log("start make api call")
  const response = await fetch(url, {
    'method': method,
    'headers': {
      'x-gw-ims-org-id': process.env.ORGANIZATION_ID,
      'x-api-key': process.env.API_KEY,
      'Authorization': `Bearer ${accessToken}`
    }
  })

  console.log("finish make api call")
  const json = await response.json()
  return json
}

function getLink (obj, linkType) {
  return obj['_links'][linkType].href
}

async function getExecution (executionUrl) {
  console.log("start getExecution")
  console.log("exectution_url:")
  console.log(executionUrl)
  const accessToken = await getAccessToken()
  console.log("access-token:")
  console.log(accessToken)
  const execution = await makeApiCall(accessToken, executionUrl, 'GET')
  console.log(execution)
  console.log("aaaa")
  const program = await makeApiCall(accessToken, new URL(getLink(execution, 'http://ns.adobe.com/adobecloud/rel/program'), executionUrl))
  console.log(execution)
  console.log("here")
  execution.program = program

  return execution
}


//app.use(bodyParser.json())

app.use(bodyParser.json({
  verify: (req, res, buf, encoding) => {
    const signature = req.header('x-adobe-signature')
    if (signature) {
      const hmac = crypto.createHmac('sha256', process.env.CLIENT_SECRET)
      hmac.update(buf)
      const digest = hmac.digest('base64')

      if (signature !== digest) {
        throw new Error('x-adobe-signature HMAC check failed')
      }
    } else if (!process.env.DEBUG && req.method === 'POST') {
      throw new Error('x-adobe-signature required')
    }
  }
}))

app.use(timeout("30000"))



app.post('/webhook', (req, res) => {

  req.setTimeout(120000, function(){
      console.log('Request has timed out.');
         res.send(408);
  });
  res.writeHead(200, { 'Content-Type': 'application/text' })
  res.end('pong')


    getExecution("https://cloudmanager.adobe.io/<somevalidurl>").then(execution => {
    console.log(`Execution for ${execution.program.name} started`)
    })




})



module.exports = app;

//const port = process.env.PORT || 3000

//app.listen(port, () =>
//  console.log(`App is listening on port ${port}.`)
//)

4

0 回答 0