0

以下のコードは、ローカルで実行している場合は正常に動作しますが、aws ラムダ関数内で実行している場合、/webhook エンドポイントに「POST」を実行している場合、「getAccessToken ()」は期待どおりに動作しません。私はノード 8.10、「aws-serverless-express」:「^3.3.6」、「express」:「^4.16.4」および「node-fetch」:「^2.5.0」を使用しています。 jwt トークンですが、node-fetch は何も返しません。サンプル ログ。

START RequestId: c8efba59-1869-4eaa-b9d8-aa15a7507d52 バージョン: $LATEST 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52 start getExecution 2019-05ef-27T19:525:Z96. -4eaa-b9d8-aa15a7507d52 実行 URL: 2019-05-27T19:55:32.328Z c8efba59-1869-4eaa-b9d8-aa15a7507d52 https://cloudmanager.adobe.io/somevalidurl 2019-05-27T19:55:32.328Z 1869-4eaa-b9d8-aa15a7507d52 start getAccessToken END RequestId: c8efba59-1869-4eaa-b9d8-aa15a7507d52 REPORT RequestId: c8efba59-1869-4eaa-b9d8-aa15a7507d52 所要時間: 6657.00 ms : 37MB

ラムダのタイムアウトが30秒であることを確認し、「ノードフェッチ」タイムアウトを0に設定して無効にしようとし、すべてのルート「app.use(timeout("30000"))」にもミドルウェアを使用しましたその特定のWebhookリクエストのタイムアウト。(すぐに 200 pong の応答を受け取りますが、getexectuion async 関数が正しく動作しません)

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