Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asset uploader, tweak backend and dev setup #370

Merged
4 commits merged into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cloudformation/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]]
Expand All @@ -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
Expand Down
29 changes: 13 additions & 16 deletions lambdas/backend/routes/apikey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 3 additions & 4 deletions lambdas/backend/routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 6 additions & 13 deletions lambdas/backend/routes/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No 5xx needed or is this caught elsewhere?

Copy link
Author

@ghost ghost Mar 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's caught here, and uncaught errors and rejections are forwarded as 5xx errors by AWS Lambda itself (and logged appropriately).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect.

}

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')
}
}
61 changes: 26 additions & 35 deletions lambdas/backend/routes/subscriptions/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
17 changes: 13 additions & 4 deletions lambdas/static-asset-uploader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 }
})

Expand All @@ -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)}`)
}
}

Expand Down Expand Up @@ -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)
})
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down