forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add metrics challenge and sign commits
Signed-off-by: Scar26 <mmatty26@gmail.com>
- Loading branch information
Showing
8 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -598,3 +598,8 @@ ctf: | |
type: string | ||
code: | ||
type: string | ||
exposedMetricsChallenge: | ||
name: | ||
type: string | ||
code: | ||
type: string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,3 +283,6 @@ ctf: | |
svgInjectionChallenge: | ||
name: Tunisia | ||
code: TN | ||
exposedMetricsChallenge: | ||
name: Japan | ||
code: JP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2014-2020 Bjoern Kimminich. | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
const Prometheus = require('prom-client') | ||
const orders = require('../data/mongodb').orders | ||
const challenges = require('../data/datacache').challenges | ||
const users = require('../data/datacache').users | ||
const utils = require('../lib/utils') | ||
|
||
exports.serveMetrics = function serveMetrics (reg) { | ||
return (req, res, next) => { | ||
utils.solveIf(challenges.exposedMetricsChallenge, () => (true)) | ||
res.set('Content-Type', reg.contentType) | ||
res.end(reg.metrics()) | ||
} | ||
} | ||
|
||
exports.observeMetrics = function observeMetrics () { | ||
const register = new Prometheus.Registry() | ||
const intervalCollector = Prometheus.collectDefaultMetrics({ timeout: 5000, register }) | ||
register.setDefaultLabels({ app: 'juice-shop' }) | ||
|
||
const orderMetrics = new Prometheus.Gauge({ | ||
name: 'juice_shop_orders_placed_total', | ||
help: 'Number of orders placed in juice-shop so far' | ||
}) | ||
|
||
const challengeMetrics = new Prometheus.Gauge({ | ||
name: 'juice_shop_challenges_solved_total', | ||
help: 'Number of challenges that have been solved so far' | ||
}) | ||
|
||
const userMetrics = new Prometheus.Gauge({ | ||
name: 'juice_shop_users_registered_total', | ||
help: 'Number of users registered' | ||
}) | ||
|
||
register.registerMetric(orderMetrics) | ||
register.registerMetric(challengeMetrics) | ||
register.registerMetric(userMetrics) | ||
|
||
const updateLoop = setInterval(() => { | ||
orders.count({}).then(function (orders) { | ||
orderMetrics.set(orders) | ||
}) | ||
challengeMetrics.set(Object.keys(challenges).filter((key) => (challenges[key].solved)).length) | ||
userMetrics.set(Object.keys(users).length) | ||
}, 5000) | ||
|
||
return { | ||
register: register, | ||
probe: intervalCollector, | ||
updateLoop: updateLoop | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) 2014-2020 Bjoern Kimminich. | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
const frisby = require('frisby') | ||
const METRICS_URL = 'http://localhost:3000/metrics/' | ||
|
||
describe('Metrics API', () => { | ||
it('check if /metrics endpoint is accessible', () => { | ||
return frisby.get(METRICS_URL) | ||
.expect('status', 200) | ||
.expect('header', 'content-type', /text\/plain/) | ||
.expect('bodyContains', /^juice_shop_users_registered_total{app="juice-shop"} [0-9]*$/) | ||
.expect('bodyContains', /^juice_shop_challenges_solved_total{app="juice-shop"} [0-9]*$/) | ||
.expect('bodyContains', /^juice_shop_orders_placed_total{app="juice-shop"} [0-9]*$/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe } from 'mocha' | ||
|
||
/* | ||
* Copyright (c) 2014-2020 Bjoern Kimminich. | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
describe('/metrics/', () => { | ||
describe('challenge "exposedMetrics"', () => { | ||
it('Challenge is solved on accesing the api/metrics route', () => { | ||
browser.get('/api/metrics') | ||
}) | ||
|
||
protractor.expect.challengeSolved({ challenge: 'Exposed Metrics' }) | ||
}) | ||
}) |