Skip to content

Commit

Permalink
Add header-checking function (kubernetes#9880)
Browse files Browse the repository at this point in the history
* 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
lucperkins authored and k8s-ci-robot committed Oct 3, 2018
1 parent 09c4ae5 commit 57584f9
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ public/

# User-specific editorconfig files
.editorconfig

# Netlify Functions build output
package-lock.json
functions/
node_modules/
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ DOCKER = docker
HUGO_VERSION = 0.49
DOCKER_IMAGE = kubernetes-hugo
DOCKER_RUN = $(DOCKER) run --rm --interactive --tty --volume $(PWD):/src
NODE_BIN = node_modules/.bin
NETLIFY_FUNC = $(NODE_BIN)/netlify-lambda

.PHONY: all build sass build-preview help serve

Expand All @@ -16,6 +18,9 @@ build: ## Build site with production settings and put deliverables in ./public
build-preview: ## Build site with drafts and future posts enabled
hugo -D -F

functions-build:
$(NETLIFY_FUNC) build functions-src

check-headers-file:
scripts/check-headers-file.sh

Expand Down
77 changes: 77 additions & 0 deletions functions-src/deploy-succeeded.js
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();
}
1 change: 1 addition & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# It is turned off for only for the production site by using [context.master] below
# DO NOT REMOVE THIS (contact @chenopis or @sig-docs-maintainers)
publish = "public"
functions = "functions"
command = "make non-production-build"

[build.environment]
Expand Down
10 changes: 10 additions & 0 deletions package.json
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"
}
}

0 comments on commit 57584f9

Please sign in to comment.