From a8012fc9aa12073c48799631ceb8b6a93f346050 Mon Sep 17 00:00:00 2001 From: amazon-meaisiah Date: Fri, 20 Mar 2020 14:05:18 -0700 Subject: [PATCH 1/3] Fix asset uploader --- lambdas/static-asset-uploader/index.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lambdas/static-asset-uploader/index.js b/lambdas/static-asset-uploader/index.js index cb0784b96..cfbfefcf7 100644 --- a/lambdas/static-asset-uploader/index.js +++ b/lambdas/static-asset-uploader/index.js @@ -3,6 +3,7 @@ const path = require('path') const AWS = require('aws-sdk') const notifyCFN = require('dev-portal-common/notify-cfn') +const { inspectStringify } = require('dev-portal-common/inspect-stringify') const fs = require('fs') const klaw = require('klaw') // const crypto = require('crypto') @@ -67,9 +68,9 @@ async function cleanS3Bucket (bucketName) { Bucket: bucketName }).promise() - console.log(`result: ${JSON.stringify(result, null, 4)}`) + console.log(`result: ${inspectStringify(result)}`) const keys = result.Contents.map((obj) => { - console.log(`obj: ${JSON.stringify(obj)}`) + console.log(`obj: ${inspectStringify(obj)}`) return { Key: obj.Key } }) @@ -81,7 +82,7 @@ async function cleanS3Bucket (bucketName) { Objects: keys } }) - console.log(`deleteObjects result: ${JSON.stringify(result, null, 4)}`) + console.log(`deleteObjects result: ${inspectStringify(result)}`) } } @@ -193,7 +194,7 @@ class State { try { const readResults = await new Promise((resolve, reject) => { - fs.readFile(filePath, 'utf-8', (err, data) => { + fs.readFile(filePath, null, (err, data) => { if (err) reject(err) else resolve(data) }) @@ -210,6 +211,13 @@ class State { params.ACL = 'public-read' } + // body just pollutes logs and takes up space + console.log('uploading to s3', { + Bucket: params.Bucket, + Key: params.Key, + BodyLength: params.Body.byteLength, + ContentType: params.ContentType + }) uploadPromises.push(s3.upload(params, options).promise()) } catch (error) { console.log('Failed to upload:', error) @@ -262,6 +270,7 @@ class State { console.log('pushing b/c Create', data.path) } else if (this.event.ResourceProperties.RebuildMode === 'overwrite-content') { // always write everything on an overwrite + console.log('pushing b/c RebuildMode=overwrite-content', data.path) } else if (!/build\/custom-content/.test(data.path)) { // only write non custom-content files on everything else console.log('pushing b/c not custom', data.path) From 572a220f823fb76396a3556ac53dc9c96a4d0bfb Mon Sep 17 00:00:00 2001 From: amazon-meaisiah Date: Fri, 20 Mar 2020 16:12:46 -0700 Subject: [PATCH 2/3] Handle errors from a few promises correctly --- lambdas/backend/routes/apikey.js | 29 ++++----- lambdas/backend/routes/catalog.js | 7 +-- lambdas/backend/routes/feedback.js | 19 ++---- lambdas/backend/routes/subscriptions/usage.js | 61 ++++++++----------- 4 files changed, 48 insertions(+), 68 deletions(-) diff --git a/lambdas/backend/routes/apikey.js b/lambdas/backend/routes/apikey.js index 7057196cb..88116cac9 100644 --- a/lambdas/backend/routes/apikey.js +++ b/lambdas/backend/routes/apikey.js @@ -3,25 +3,22 @@ const customersController = require('dev-portal-common/customers-controller') const util = require('../util') -exports.get = (req, res) => { +exports.get = async (req, res) => { const cognitoIdentityId = util.getCognitoIdentityId(req) console.log(`GET /apikey for Cognito ID: ${cognitoIdentityId}`) - function errFunc (data) { - console.log(`error: ${data}`) - res.status(500).json(data) - } + const data = await new Promise((resolve, reject) => { + customersController.getApiKeyForCustomer(cognitoIdentityId, reject, resolve) + }) - customersController.getApiKeyForCustomer(cognitoIdentityId, errFunc, (data) => { - if (data.items.length === 0) { - res.status(404).json({ error: 'No API Key for customer' }) - } else { - const item = data.items[0] - const key = { - id: item.id, - value: item.value - } - res.status(200).json(key) + if (data.items.length === 0) { + res.status(404).json({ error: 'No API Key for customer' }) + } else { + const item = data.items[0] + const key = { + id: item.id, + value: item.value } - }) + res.status(200).json(key) + } } diff --git a/lambdas/backend/routes/catalog.js b/lambdas/backend/routes/catalog.js index 7de4a955f..302005da7 100644 --- a/lambdas/backend/routes/catalog.js +++ b/lambdas/backend/routes/catalog.js @@ -2,9 +2,8 @@ const util = require('../util') -exports.get = (req, res) => { +exports.get = async (req, res) => { console.log(`GET /catalog for Cognito ID: ${util.getCognitoIdentityId(req)}`) - util.catalog() - .then(catalog => res.status(200).json(catalog)) - .catch(error => res.status(error.statusCode).json(error)) + const catalog = await util.catalog() + res.status(200).json(catalog) } diff --git a/lambdas/backend/routes/feedback.js b/lambdas/backend/routes/feedback.js index 7532f624d..96bd166c1 100644 --- a/lambdas/backend/routes/feedback.js +++ b/lambdas/backend/routes/feedback.js @@ -5,32 +5,25 @@ const util = require('../util') const feedbackEnabled = !!process.env.FeedbackSnsTopicArn -exports.get = (req, res) => { +exports.get = async (req, res) => { console.log(`GET /feedback for Cognito ID: ${util.getCognitoIdentityId(req)}`) if (!feedbackEnabled) { res.status(401).json('Customer feedback not enabled') } else { - feedbackController.fetchFeedback() - .then(feedback => { - res.status(200).json(feedback) - }) - .catch(err => { - console.log(`error: ${err}`) - res.status(500).json(err) - }) + const feedback = await feedbackController.fetchFeedback() + res.status(200).json(feedback) } } -exports.post = (req, res) => { +exports.post = async (req, res) => { const cognitoIdentityId = util.getCognitoIdentityId(req) console.log(`POST /feedback for Cognito ID: ${cognitoIdentityId}`) if (!feedbackEnabled) { res.status(401).json('Customer feedback not enabled') } else { - feedbackController.submitFeedback(cognitoIdentityId, req.body.message) - .then(() => res.status(200).json('success')) - .catch((err) => res.status(500).json(err)) + await feedbackController.submitFeedback(cognitoIdentityId, req.body.message) + res.status(200).json('success') } } diff --git a/lambdas/backend/routes/subscriptions/usage.js b/lambdas/backend/routes/subscriptions/usage.js index 9c090bdb0..e43154208 100644 --- a/lambdas/backend/routes/subscriptions/usage.js +++ b/lambdas/backend/routes/subscriptions/usage.js @@ -3,46 +3,37 @@ const customersController = require('dev-portal-common/customers-controller') const util = require('../../util') -exports.get = (req, res) => { +exports.get = async (req, res) => { const cognitoIdentityId = util.getCognitoIdentityId(req) - console.log(`GET /usage for Cognito ID: ${cognitoIdentityId}`) + console.log(`GET /subscriptions/:usagePlanId/usage for Cognito ID: ${cognitoIdentityId}`) const usagePlanId = req.params.usagePlanId - function errFunc (data) { - console.log(`error: ${data}`) - res.status(500).json(data) - } - - util.catalog() - .then(catalog => util.getUsagePlanFromCatalog(usagePlanId, catalog)) - .then(usagePlan => { - const isUsagePlanInCatalog = Boolean(usagePlan) + const catalog = await util.catalog() + const usagePlan = util.getUsagePlanFromCatalog(usagePlanId, catalog) - // could error here if customer is not subscribed to usage plan, or save an extra request by just showing 0 usage - if (!isUsagePlanInCatalog) { - res.status(404).json({ error: 'Invalid Usage Plan ID' }) - } else { - customersController.getApiKeyForCustomer(cognitoIdentityId, errFunc, (data) => { - const keyId = data.items[0].id + // could error here if customer is not subscribed to usage plan, or save an extra request by just showing 0 usage + if (!usagePlan) { + res.status(404).json({ error: 'Invalid Usage Plan ID' }) + } else { + const data = await new Promise((resolve, reject) => { + customersController.getApiKeyForCustomer(cognitoIdentityId, reject, resolve) + }) + const keyId = data.items[0].id - const params = { - endDate: req.query.end, - startDate: req.query.start, - usagePlanId, - keyId, - limit: 1000 - } + const params = { + endDate: req.query.end, + startDate: req.query.start, + usagePlanId, + keyId, + limit: 1000 + } - util.apigateway.getUsage(params, (err, usageData) => { - if (err) { - console.log(`get usage err ${JSON.stringify(err)}`) - errFunc(err) - } else { - console.log(`get usage data ${JSON.stringify(usageData)}`) - res.status(200).json(usageData) - } - }) - }) - } + const usageData = await new Promise((resolve, reject) => { + util.apigateway.getUsage(params, (err, data) => { + if (err) reject(err) + else resolve(data) + }) }) + res.status(200).json(usageData) + } } From 9dad8d3bb4e685137bf6aec7c540a6b657dd9ab4 Mon Sep 17 00:00:00 2001 From: amazon-meaisiah Date: Mon, 23 Mar 2020 15:45:13 -0700 Subject: [PATCH 3/3] Fix dev setup to allow signing in in a dev-mode CloudFront deployment Also, rip out some no-longer-used callback URLs --- cloudformation/template.yaml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cloudformation/template.yaml b/cloudformation/template.yaml index c6db65933..e2dc797c3 100644 --- a/cloudformation/template.yaml +++ b/cloudformation/template.yaml @@ -1392,7 +1392,8 @@ Resources: CallbackURL: !If [ LocalDevelopmentMode, [ 'http://localhost:3000/index.html?action=login', - !Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=login' ]] + !Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=login' ]], + !Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=login' ]] ], [ !Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=login' ]] @@ -1402,12 +1403,10 @@ Resources: [ 'http://localhost:3000/index.html?action=logout', !Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=logout' ]], - 'http://localhost:3000/index.html?action=logout&reason=inactive', - !Join [ '', [ 'https://', !GetAtt DevPortalSiteS3Bucket.RegionalDomainName, '/index.html?action=logout&reason=inactive' ]] + !Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout' ]], ], [ !Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout' ]], - !Join [ '', [ 'https://', !If [ UseCustomDomainName, !Ref CustomDomainName, !GetAtt DefaultCloudfrontDistribution.DomainName ], '/index.html?action=logout&reason=inactive' ]] ] ] AllowedOAuthFlowsUserPoolClient: true