forked from kubernetes/website
-
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 header-checking function (kubernetes#9880)
* Add basic header-checking function * Add Slack notification logic * Modify Netlify Functions setup * Rework function logic * Add missing dependencies * Remove package-lock.json from Git * Separate header checking function * Isolate env check into a separate function
- Loading branch information
1 parent
09c4ae5
commit 57584f9
Showing
5 changed files
with
98 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
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,77 @@ | ||
"use strict"; | ||
|
||
const | ||
{ IncomingWebhook } = require('@slack/client'), | ||
kubernetesSiteRoot = 'https://kubernetes.io', | ||
fetch = require('node-fetch').default, | ||
{ SLACK_WEBHOOK_URL } = process.env; | ||
|
||
const webhook = new IncomingWebhook(SLACK_WEBHOOK_URL); | ||
|
||
// A random smattering of Kubernetes documentation pages | ||
// We can add as many pages here as we'd like | ||
const kubernetesEndpoints = [ | ||
'docs/home', | ||
'docs/tutorials/configuration/configure-redis-using-configmap', | ||
|
||
] | ||
|
||
// Ensure that the SLACK_WEBHOOK_URL environment variable is set | ||
const checkEnv = () => { | ||
if (!SLACK_WEBHOOK_URL) { | ||
return { | ||
statusCode: 422, | ||
body: "[FAILURE] The Slack webhook URL must be set via the SLACK_WEBHOOK_URL environment variable" | ||
} | ||
} | ||
} | ||
|
||
// This function posts a warning message to Slack | ||
const sendSlackMessage = (msg) => { | ||
const slackMessageObject = { | ||
username: "noindex checker", | ||
text: msg | ||
} | ||
|
||
// Send the message to the webhook | ||
webhook.send(slackMessageObject, (err, res) => { | ||
return (err) ? { statusCode: 422, body: `[ERROR] Slack webhook error: ${err}` } : | ||
{ statusCode: 200, body: `[SUCCESS] Response received from Slack: ${JSON.stringify(res)}` }; | ||
}); | ||
} | ||
|
||
// Iterate through each Kubernetes endpoint to check for noindex headers | ||
const checkEndpoints = () => { | ||
kubernetesEndpoints.forEach((endpoint) => { | ||
const url = `${kubernetesSiteRoot}/${endpoint}`; | ||
|
||
fetch(url) | ||
.then(res => { | ||
const headers = res.headers; | ||
|
||
if ('x-robots-tag' in headers.raw() && (headers.get('x-robots-tag') == 'noindex')) { | ||
const msg = `[WARNING] "X-Robots-Tag: noindex" header found on the following page: ${url}`; | ||
|
||
// Send Slack notification | ||
sendSlackMessage(msg); | ||
|
||
return { statusCode: 404, body: msg }; | ||
} else { | ||
const msg = `[SUCCESS] No improper X-Robots-Tag: noindex headers found on ${url}`; | ||
|
||
return { statusCode: 200, body: msg }; | ||
} | ||
}) | ||
.catch(err => { | ||
return { statusCode: 422, body: err }; | ||
}); | ||
}); | ||
} | ||
|
||
// The handler function | ||
exports.handler = async (event, context) => { | ||
checkEnv(); | ||
|
||
// Below are the various deploy succeeded checks | ||
checkEndpoints(); | ||
} |
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,10 @@ | ||
{ | ||
"private": true, | ||
"devDependencies": { | ||
"@babel/core": "^7.0.1", | ||
"@slack/client": "^4.4.0", | ||
"babel-loader": "^8.0.2", | ||
"netlify-lambda": "^0.4.0", | ||
"node-fetch": "^2.2.0" | ||
} | ||
} |