Skip to content

Commit

Permalink
Merge branch 'master' into symfony-stars
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright authored Feb 20, 2019
2 parents 3859150 + 96d48dc commit 0ac28e0
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 79 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ parserOptions:
# though that setting is only for ES6 modules, not CommonJS modules.
sourceType: 'script'

overrides:
files:
- gatsby-browser.js
parserOptions:
sourceType: 'module'

plugins:
- mocha
- no-extension-in-require
Expand Down Expand Up @@ -39,6 +45,7 @@ rules:
prefer-template: 'error'
promise/prefer-await-to-then: 'error'
func-style: ['error', 'declaration', { 'allowArrowFunctions': true }]
new-cap: ['error', { 'capIsNew': true }]

# Mocha-related.
mocha/no-exclusive-tests: 'error'
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion config/custom-environment-variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public:
metrics:
prometheus:
enabled: 'METRICS_PROMETHEUS_ENABLED'
allowedIps: 'METRICS_PROMETHEUS_ALLOWED_IPS'

ssl:
isSecure: 'HTTPS'
Expand Down
1 change: 0 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public:
metrics:
prometheus:
enabled: false
allowedIps: []

ssl:
isSecure: false
Expand Down
2 changes: 1 addition & 1 deletion core/base-service/base-svg-scraping.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const leadingWhitespace = /(?:\r\n\s*|\r\s*|\n\s*)/g
class BaseSvgScrapingService extends BaseService {
static valueFromSvgBadge(svg, valueMatcher = defaultValueMatcher) {
if (typeof svg !== 'string') {
throw TypeError('Parameter should be a string')
throw new TypeError('Parameter should be a string')
}
const stripped = svg.replace(leadingWhitespace, '')
const match = valueMatcher.exec(stripped)
Expand Down
24 changes: 4 additions & 20 deletions core/server/prometheus-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

const prometheus = require('prom-client')

class PrometheusMetrics {
module.exports = class PrometheusMetrics {
constructor(config = {}) {
this.enabled = config.enabled || false
const matchNothing = /(?!)/
this.allowedIps = config.allowedIps
? new RegExp(config.allowedIps)
: matchNothing
if (this.enabled) {
console.log(
`Metrics are enabled. Access to /metrics resource is limited to IP addresses matching: ${
this.allowedIps
}`
)
console.log('Metrics are enabled.')
}
}

Expand All @@ -28,16 +20,8 @@ class PrometheusMetrics {

setRoutes(server, register) {
server.route(/^\/metrics$/, (data, match, end, ask) => {
const ip = ask.req.socket.remoteAddress
if (this.allowedIps.test(ip)) {
ask.res.setHeader('Content-Type', register.contentType)
ask.res.end(register.metrics())
} else {
ask.res.statusCode = 403
ask.res.end()
}
ask.res.setHeader('Content-Type', register.contentType)
ask.res.end(register.metrics())
})
}
}

module.exports = PrometheusMetrics
42 changes: 2 additions & 40 deletions core/server/prometheus-metrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,50 +43,12 @@ describe('Prometheus metrics route', function() {
expect(await res.text()).to.not.contains('nodejs_version_info')
})

it('returns metrics for allowed IP', async function() {
new Metrics({
enabled: true,
allowedIps: '^(127\\.0\\.0\\.1|::1|::ffff:127\\.0\\.0\\.1)$',
}).initialize(camp)
it('returns metrics when enabled', async function() {
new Metrics({ enabled: true }).initialize(camp)

const res = await fetch(`${baseUrl}/metrics`)

expect(res.status).to.be.equal(200)
expect(await res.text()).to.contains('nodejs_version_info')
})

it('returns metrics for request from allowed remote address', async function() {
new Metrics({
enabled: true,
allowedIps: '^(127\\.0\\.0\\.1|::1|::ffff:127\\.0\\.0\\.1)$',
}).initialize(camp)

const res = await fetch(`${baseUrl}/metrics`)

expect(res.status).to.be.equal(200)
expect(await res.text()).to.contains('nodejs_version_info')
})

it('returns 403 for not allowed IP', async function() {
new Metrics({
enabled: true,
allowedIps: '^127\\.0\\.0\\.200$',
}).initialize(camp)

const res = await fetch(`${baseUrl}/metrics`)

expect(res.status).to.be.equal(403)
expect(await res.text()).to.not.contains('nodejs_version_info')
})

it('returns 403 for every request when list with allowed IPs not defined', async function() {
new Metrics({
enabled: true,
}).initialize(camp)

const res = await fetch(`${baseUrl}/metrics`)

expect(res.status).to.be.equal(403)
expect(await res.text()).to.not.contains('nodejs_version_info')
})
})
3 changes: 0 additions & 3 deletions core/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ const publicConfigSchema = Joi.object({
metrics: {
prometheus: {
enabled: Joi.boolean().required(),
allowedIps: Joi.array()
.items(Joi.string().ip())
.required(),
},
},
ssl: {
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class Usage extends React.PureComponent {
<section>
<H2 id="your-badge">Your Badge</H2>

<H3 id="static-badge">Static</H3>
<H3>Static</H3>
<StaticBadgeMaker baseUrl={baseUrl} />

<VerticalSpace />
Expand Down Expand Up @@ -215,7 +215,7 @@ export default class Usage extends React.PureComponent {
/>
</p>

<H3 id="endpoint">Endpoint (Beta)</H3>
<H3>Endpoint (Beta)</H3>

<p>
<Snippet
Expand Down
2 changes: 1 addition & 1 deletion frontend/pages/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const EndpointPage = () => (
<GlobalStyle />
<Meta />
<Header />
<H3 id="static-badge">Endpoint (Beta)</H3>
<H3>Endpoint (Beta)</H3>
<Snippet snippet={`${baseUrl}/badge/endpoint.svg?url=...&style=...`} />
<p>Endpoint response:</p>
<JsonExample
Expand Down
16 changes: 16 additions & 0 deletions gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Adapted from https://github.com/gatsbyjs/gatsby/issues/8413

function scrollToElementId(id) {
const el = document.querySelector(id)
if (el) {
return window.scrollTo(0, el.offsetTop - 20)
} else {
return false
}
}

export function onRouteUpdate({ location: { hash } }) {
if (hash) {
window.setTimeout(() => scrollToElementId(hash), 10)
}
}
3 changes: 0 additions & 3 deletions gh-badges/lib/lru-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ function CacheSlot(key, value) {
}

function Cache(capacity, type) {
if (!(this instanceof Cache)) {
return new Cache(capacity, type)
}
type = type || 'unit'
this.capacity = capacity
this.type = typeEnum[type]
Expand Down
5 changes: 0 additions & 5 deletions gh-badges/lib/lru-cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ function expectCacheSlots(cache, keys) {
}

describe('The LRU cache', function() {
it('should support being called without new', function() {
const cache = LRU(1)
expect(cache).to.be.an.instanceof(LRU)
})

it('should support a zero capacity', function() {
const cache = new LRU(0)
cache.set('key', 'value')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"test:js:server": "echo \"Deprecated; run `npm run test:core` instead.\" && npm run test:core",
"test:js:frontend": "echo \"Deprecated; run `npm run test:frontend` instead.\" && npm run test:frontend",
"test:js:package": "echo \"Deprecated; run `npm run test:package` instead.\" && npm run test:package",
"test:frontend": "cross-env NODE_ENV=test mocha --config mocharc-frontend.yml \"frontend/**/*.spec.js\"",
"test:frontend": "cross-env NODE_ENV=test mocha --config .mocharc-frontend.yml \"frontend/**/*.spec.js\"",
"test:core": "cross-env NODE_CONFIG_ENV=test mocha \"core/**/*.spec.js\" \"lib/**/*.spec.js\" \"services/**/*.spec.js\"",
"test:package": "mocha \"gh-badges/**/*.spec.js\"",
"test:entrypoint": "cross-env NODE_CONFIG_ENV=test mocha entrypoint.spec.js",
Expand Down
2 changes: 1 addition & 1 deletion services/github/github-common-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function fetchJsonFromRepo(
try {
decoded = Buffer.from(content, 'base64').toString('utf-8')
} catch (e) {
throw InvalidResponse({ prettyMessage: 'undecodable content' })
throw new InvalidResponse({ prettyMessage: 'undecodable content' })
}
const json = serviceInstance._parseJson(decoded)
return serviceInstance.constructor._validate(json, schema)
Expand Down

0 comments on commit 0ac28e0

Please sign in to comment.