Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Add API key requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
aekeus committed Apr 6, 2020
1 parent 1a8a6e6 commit 471dad2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start": "./node_modules/.bin/babel-node src/index.js",
"lint": "standard",
"verify": "node tools/verify.js",
"test": "S3_DOWNLOAD_KEY=1 S3_DOWNLOAD_SECRET=1 BEHIND_FASTLY=1 tap test/*.js",
"test": "S3_DOWNLOAD_KEY=1 S3_DOWNLOAD_SECRET=1 BEHIND_FASTLY=1 API_KEYS=a,b,c tap test/*.js",
"test-win": "set BEHIND_FASTLY=1 && tap test/*.js"
},
"author": "Brave",
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Boom = require('boom')
const moment = require('moment')
const storage = require('../storage')
const uuid = require('uuid/v4')
const verification = require('../verification')

const FEEDBACK_COLLECTION = process.env.FEEDBACK_COLLECTION || 'feedback'

Expand Down Expand Up @@ -59,7 +60,10 @@ exports.setup = (runtime) => {
try {
// phase 2 - to be implemented - rate limit on IP address

// phase 2 - to be implemented - callout to referral server to verify api key
// verify API key
if (!verification.isValidAPIKey(request.payload.api_key)) {
return reply(Boom.notAcceptable('invalid api key'))
}

// build event
const storageObject = buildStorageObject(request.payload)
Expand Down
15 changes: 14 additions & 1 deletion src/verification.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('underscore')
const moment = require('moment')

// verification libraries
Expand All @@ -12,6 +13,13 @@ const verifiers = [
linuxCore.variousVersions,
]

const API_KEYS = _.object(
(process.env.API_KEYS || '')
.split(',')
.map((k) => { return k.trim() })
.map((k) => { return [k, true] })
)

// public function to determine is a request should be verified, and if so,
// if the usage ping is valid (by iterating over a set of verifiers)
const isUsagePingValid = (request, usage, apiKeys = [], tlsSignatures = []) => {
Expand All @@ -33,7 +41,12 @@ const writeFilteredUsagePing = (mg, usage, cb) => {
filteredCollection.insertOne(usage, cb)
}

const isValidAPIKey = (k) => {
return !!API_KEYS[k]
}

module.exports = {
isUsagePingValid,
writeFilteredUsagePing
writeFilteredUsagePing,
isValidAPIKey,
}
4 changes: 4 additions & 0 deletions test/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const tap = require('tap')
const moment = require('moment')

const feedback = require('../src/controllers/feedback')
const verification = require('../src/verification')

tap.test('feedback', (t) => {
let results = feedback.buildStorageObject({
Expand All @@ -22,5 +23,8 @@ tap.test('feedback', (t) => {
t.equal(feedback.successResult('1').status, 'ok', 'ok result well formed')
t.ok(feedback.successResult('1').id, 'ok result has id')

t.ok(verification.isValidAPIKey('a'), 'verification key found')
t.notok(verification.isValidAPIKey('z'), 'verification key not found')

t.done()
})

0 comments on commit 471dad2

Please sign in to comment.