You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2021/05/17 09:25:15 UTC

[camel-website] branch main updated: chore: proxy to pass authentication for GitHub API

This is an automated email from the ASF dual-hosted git repository.

zregvart pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-website.git


The following commit(s) were added to refs/heads/main by this push:
     new 091ac97  chore: proxy to pass authentication for GitHub API
091ac97 is described below

commit 091ac97d5aaf10cee909af12a9e945a4a70d31e7
Author: Zoran Regvart <zr...@apache.org>
AuthorDate: Fri May 14 23:40:24 2021 +0200

    chore: proxy to pass authentication for GitHub API
    
    Implements and configures a proxy used add `Authorization` header to
    invocations of GitHub REST API.
    
    As we're sometimes failing the build due to GitHub API rate limits, and
    since in Hugo we can't pass HTTP headers to include the `Authorization`
    header which would allow us a higher API invocation limit. An
    alternative to use a proxy through which all invocations of the GitHub
    API will go through and have the `Authorization` header added.
    
    To use the proxy three environment variables must be set at the same
    time:
     - `HUGO_PARAMS_GitHubAPI=http://localhost:22635`
     - `GITHUB_USR=<GitHub username>`
     - `GITHUB_PSW=<GitHub token>`
---
 .gitignore                                         |  1 +
 .pnp.js                                            |  2 +
 Jenkinsfile                                        |  8 +--
 README.md                                          | 38 ++++++++++++++
 config.toml                                        |  1 +
 github-proxy.js                                    | 60 ++++++++++++++++++++++
 layouts/blog/post.html                             |  2 +-
 layouts/blog/summary.html                          |  2 +-
 layouts/partials/releases/camel-k-runtime.html     |  2 +-
 layouts/partials/releases/camel-k.html             |  2 +-
 .../partials/releases/camel-kafka-connector.html   |  2 +-
 layouts/partials/releases/camel-quarkus.html       |  2 +-
 package.json                                       |  5 +-
 yarn.lock                                          |  3 +-
 14 files changed, 119 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2c94153..1ab379a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,3 +21,4 @@ resources
 !**/.yarn/sdks
 !**/.yarn/unplugged
 !**/.yarn/versions
+*.github-proxy.*
diff --git a/.pnp.js b/.pnp.js
index cd3f692..70c3c7d 100755
--- a/.pnp.js
+++ b/.pnp.js
@@ -62,6 +62,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
             ["gulp-cheerio", "npm:1.0.0"],
             ["gulp-htmlmin", "npm:5.0.1"],
             ["html-validate", "npm:4.11.0"],
+            ["http-proxy", "npm:1.18.1"],
             ["hugo-extended", "npm:0.73.0"],
             ["netlify-cli", "npm:2.69.11"],
             ["npm-run-all", "npm:4.1.5"],
@@ -4389,6 +4390,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
             ["gulp-cheerio", "npm:1.0.0"],
             ["gulp-htmlmin", "npm:5.0.1"],
             ["html-validate", "npm:4.11.0"],
+            ["http-proxy", "npm:1.18.1"],
             ["hugo-extended", "npm:0.73.0"],
             ["netlify-cli", "npm:2.69.11"],
             ["npm-run-all", "npm:4.1.5"],
diff --git a/Jenkinsfile b/Jenkinsfile
index 067d197..757889d 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -35,9 +35,11 @@ pipeline {
     }
 
     environment {
-        ANTORA_CACHE_DIR  = "$WORKSPACE/.antora-cache"
-        HUGO_CACHEDIR     = "$WORKSPACE/.hugo-cache"
-        CAMEL_ENV         = 'production'
+        ANTORA_CACHE_DIR      = "$WORKSPACE/.antora-cache"
+        CAMEL_ENV             = 'production'
+        GITHUB                = credentials('399061d0-5ab5-4142-a186-a52081fef742') // asf-ci credential
+        HUGO_CACHEDIR         = "$WORKSPACE/.hugo-cache"
+        HUGO_PARAMS_GitHubAPI = 'http://localhost:22635'
     }
 
     stages {
diff --git a/README.md b/README.md
index 481d53b..91baa97 100644
--- a/README.md
+++ b/README.md
@@ -376,6 +376,44 @@ all generated sources in the project first.
 
 Of course this then takes some more time than an optimized rebuild (time to grab another coffee!).
 
+# Checks, publishing the website
+
+The content of the website, as built by the [Camel.website](https://ci-builds.apache.org/job/Camel/job/Camel.website/job/main/)
+job, is served from the [asf-site](https://github.com/apache/camel-website/tree/asf-site) branch and served by [ASF
+Infrastrucuture team](https://infra.apache.org/project-site.html).
+
+For the site to be published a number of checks need to pass, these include two levels of link checking:
+[Antora xref](https://gitlab.com/antora/xref-validator) and [HTML link checker](https://github.com/deadlinks/cargo-deadlinks);
+[HTML validation](https://html-validate.org/). In local development those checks can be run with `yarn checks`,
+to increase the local turnaround time some lengthy checks are not run unless `CAMEL_ENV=production` environment
+variable is set. To run all checks use:
+
+    $ CAMEL_ENV=production yarn checks
+
+Publishing the website is done by [ASF Jenkins](https://ci-builds.apache.org/job/Camel/job/Camel.website/job/main/)
+this includes running all the checks. It is common that a check might fail there that hasn't failed when the
+change was made, this could be for any number of reasons, but most commonly there was a change in one of
+the subproject's documentation, and most common issue is an introduction of a broken or absolute link towards
+`camel.apache.org` domnain.
+
+The configuration of the HTML validation rules is in `.htmlvalidate.json`, with exclusions of checks listed in
+`.htmlvalidateignore` file, custom rules are in `rules.js` for mandating relative links to camel.apache.org domain,
+JSON-LD schema validation, and mandating that the HTML title be set.
+
+Some of the content for the website is derived from the data received from GitHub API, even though we cache the
+data, GitHub API rate limits can cause build failures. Until we can use authenticated requests against the GitHub
+API (see https://github.com/gohugoio/hugo/issues/5617), a API "proxy" can be/is used to add authentication to
+the requests as authenticated users receive a higher GitHub API limit. To use the proxy following environment
+variables need to be set (and are set in the production build, see `Jenkinsfile`):
+
+ - `HUGO_PARAMS_GitHubAPI=http://localhost:22635`
+ - `GITHUB_USR=<GitHub username>`
+ - `GITHUB_PSW=<GitHub token>`
+
+When those environment variables are set, requests are sent to `localhost:22635` instead of `api.github.com`
+where the request is modified to set the correct `Host` header and to add the `Authorization` header for
+authenticated access. The proxy is defined in the `github-proxy.js`, and since it's run in the backround
+any logs from the proxy can be found in `.github-proxy.log` file.
 
 ## Pull request previews are powered by Netlify
 
diff --git a/config.toml b/config.toml
index fb3f87f..d24fb6b 100644
--- a/config.toml
+++ b/config.toml
@@ -18,6 +18,7 @@ defaultMarkdownHandler = "blackfriday"
     socialProfiles = ["https://twitter.com/ApacheCamel"]
     organizationLogo = "https://camel.apache.org/_/img/logo-d.svg"
     organizationDescription = "Apache Camel ™ is a versatile open-source integration framework based on known Enterprise Integration Patterns. Camel empowers you to define routing and mediation rules in a variety of domain-specific languages, including a Java-based Fluent API, Spring or Blueprint XML Configuration files, and a Scala DSL."
+    GitHubAPI = "https://api.github.com"
 
 [caches]
 
diff --git a/github-proxy.js b/github-proxy.js
new file mode 100644
index 0000000..9ed1a71
--- /dev/null
+++ b/github-proxy.js
@@ -0,0 +1,60 @@
+const PID_FILE = '.github-proxy.pid';
+
+if (process.argv.indexOf('stop') > 0) {
+  const { readFile, existsSync, unlink } = require('fs');
+
+  if (!existsSync(PID_FILE)) {
+    return;
+  }
+
+  readFile(PID_FILE, (err, data) => {
+    if (err) {
+      throw err;
+    }
+
+    try {
+      process.kill(Number.parseInt(data), 'SIGINT');
+    } catch (e) {
+      unlink(PID_FILE);
+    }
+  });
+} else if (process.argv.indexOf('start') > 0) {
+  const fs = require('fs');
+
+  const log = fs.createWriteStream('.github-proxy.log', { flags: 'a' });
+  process.stdout.write = process.stderr.write = log.write.bind(log);
+
+  if (!(process.env.GITHUB_USR && process.env.GITHUB_PSW)) {
+    console.log('No GITHUB_USR and GITHUB_PSW environment variables set');
+    process.exit(1);
+  }
+
+  fs.writeFile(PID_FILE, process.pid.toString());
+  process.on('SIGINT', (code) => {
+    console.log('Stopping');
+    log.uncork();
+    log.end();
+    fs.unlink(PID_FILE);
+    process.exit(0);
+  });
+
+  const httpProxy = require('http-proxy');
+
+  const proxy = httpProxy.createProxyServer({
+    auth: `${process.env.GITHUB_USR}:${process.env.GITHUB_PSW}`,
+    changeOrigin: true,
+    target: 'https://api.github.com'
+  }).listen(22635)
+  console.info('Proxy started.');
+
+  proxy.on('error', console.error);
+} else {
+  const { spawn } = require('child_process');
+
+  const proxy = spawn(process.argv[0], ['github-proxy.js', 'start'], {
+    detached: true,
+    stdio: 'ignore'
+  });
+
+  proxy.unref();
+}
diff --git a/layouts/blog/post.html b/layouts/blog/post.html
index add0640..33a406a 100644
--- a/layouts/blog/post.html
+++ b/layouts/blog/post.html
@@ -13,7 +13,7 @@
             Posted on <time itemprop="published" datetime="{{ dateFormat "2006-01-02" .PublishDate }}"
                 title="{{ dateFormat "Monday, January 2, 2006" .PublishDate }}">{{ dateFormat "January 2, 2006" .PublishDate }}</time>,
             by {{ range $author := .Params.authors }}
-            {{ with getJSON "https://api.github.com/users/" $author }}
+            {{ with getJSON $.Site.Params.GitHubAPI "/users/" $author }}
             <figure>
                 <img src="{{ .avatar_url }}" alt="{{ .name }}">
                 <figcaption rel="author">{{ .name }}</figcaption>
diff --git a/layouts/blog/summary.html b/layouts/blog/summary.html
index 56f7f2e..a2fb0e5 100644
--- a/layouts/blog/summary.html
+++ b/layouts/blog/summary.html
@@ -2,7 +2,7 @@
 
     <header>
         <a href="{{ .RelPermalink }}"><h1>{{ .Title }}</h1></a>
-        <time itemprop="published" datetime="{{ dateFormat "2006-01-02" .PublishDate }}" title="{{ dateFormat "Monday, January 2, 2006" .PublishDate }}">{{ dateFormat "January 2, 2006" .PublishDate }}</time>, by {{ range $idx, $author := .Params.authors }}{{ if gt $idx 0 }}, {{ end }}<span rel="author">{{ (getJSON  "https://api.github.com/users/" $author).name }}</span>{{ end }}
+        <time itemprop="published" datetime="{{ dateFormat "2006-01-02" .PublishDate }}" title="{{ dateFormat "Monday, January 2, 2006" .PublishDate }}">{{ dateFormat "January 2, 2006" .PublishDate }}</time>, by {{ range $idx, $author := .Params.authors }}{{ if gt $idx 0 }}, {{ end }}<span rel="author">{{ (getJSON $.Site.Params.GitHubAPI "/users/" $author).name }}</span>{{ end }}
     </header>
     <p>{{ .Summary }}</p>
     <p><a class="continue" href="{{ .RelPermalink }}">Continue reading &#10095;</a></p>
diff --git a/layouts/partials/releases/camel-k-runtime.html b/layouts/partials/releases/camel-k-runtime.html
index 026b5b8..1ec1e17 100644
--- a/layouts/partials/releases/camel-k-runtime.html
+++ b/layouts/partials/releases/camel-k-runtime.html
@@ -11,7 +11,7 @@ git checkout camel-k-runtime-parent-{{ .Params.version }}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this release</p>
-{{ $issues := getJSON "https://api.github.com/repos/apache/camel-k-runtime/issues?state=closed&milestone=" (string .Params.milestone ) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI "/repos/apache/camel-k-runtime/issues?state=closed&milestone=" (string .Params.milestone ) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/layouts/partials/releases/camel-k.html b/layouts/partials/releases/camel-k.html
index 4b45397..13c760f 100644
--- a/layouts/partials/releases/camel-k.html
+++ b/layouts/partials/releases/camel-k.html
@@ -11,7 +11,7 @@ git checkout v{{ .Params.version }}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this release</p>
-{{ $issues := getJSON "https://api.github.com/repos/apache/camel-k/issues?state=closed&milestone=" (string .Params.milestone ) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI "/repos/apache/camel-k/issues?state=closed&milestone=" (string .Params.milestone ) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/layouts/partials/releases/camel-kafka-connector.html b/layouts/partials/releases/camel-kafka-connector.html
index 6bed094..287d703 100644
--- a/layouts/partials/releases/camel-kafka-connector.html
+++ b/layouts/partials/releases/camel-kafka-connector.html
@@ -26,7 +26,7 @@ git checkout camel-kafka-connector-{{ .Params.version }}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this release</p>
-{{ $issues := getJSON "https://api.github.com/repos/apache/camel-kafka-connector/issues?state=closed&labels=" (string .Params.version) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI "/repos/apache/camel-kafka-connector/issues?state=closed&labels=" (string .Params.version) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/layouts/partials/releases/camel-quarkus.html b/layouts/partials/releases/camel-quarkus.html
index c5a1f86..bf4519c 100644
--- a/layouts/partials/releases/camel-quarkus.html
+++ b/layouts/partials/releases/camel-quarkus.html
@@ -11,7 +11,7 @@ git checkout {{ .Params.version }}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this release</p>
-{{ $issues := getJSON "https://api.github.com/repos/apache/camel-quarkus/issues?state=closed&milestone=" (string .Params.milestone ) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI "/repos/apache/camel-quarkus/issues?state=closed&milestone=" (string .Params.milestone ) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/package.json b/package.json
index c52aab70..19344a3 100644
--- a/package.json
+++ b/package.json
@@ -5,9 +5,11 @@
   "scripts": {
     "build:antora": "antora --clean --fetch antora-playbook.yml --stacktrace --require ./patch-sitemap.js",
     "build:hugo": "hugo --cacheDir ${HUGO_CACHE_DIR:-$(pwd)/.hugo_data}",
+    "build:hugo-with-proxy": "run-s build:github-proxy build:hugo 'build:github-proxy stop'",
     "build:minify": "gulp minify",
+    "build:github-proxy": "node github-proxy.js",
     "build:sitemap": "gulp sitemap",
-    "build": "run-s build:antora build:hugo build:sitemap build:minify",
+    "build": "run-s build:antora build:hugo-with-proxy build:sitemap build:minify",
     "build-all": "yarn workspaces foreach --topological-dev run build",
     "clean": "gulp clean",
     "preview": "run-s preview:antora preview:hugo",
@@ -40,6 +42,7 @@
     "gulp-cheerio": "~1.0",
     "gulp-htmlmin": "~5.0",
     "html-validate": "~4",
+    "http-proxy": "~1.18",
     "hugo-extended": "^0.73.0",
     "netlify-cli": "^2.69.11",
     "npm-run-all": "~4",
diff --git a/yarn.lock b/yarn.lock
index 650d9b6..c95b099 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3219,6 +3219,7 @@ __metadata:
     gulp-cheerio: ~1.0
     gulp-htmlmin: ~5.0
     html-validate: ~4
+    http-proxy: ~1.18
     hugo-extended: ^0.73.0
     netlify-cli: ^2.69.11
     npm-run-all: ~4
@@ -9912,7 +9913,7 @@ fsevents@~2.1.2:
   languageName: node
   linkType: hard
 
-"http-proxy@npm:^1.18.0, http-proxy@npm:^1.18.1":
+"http-proxy@npm:^1.18.0, http-proxy@npm:^1.18.1, http-proxy@npm:~1.18":
   version: 1.18.1
   resolution: "http-proxy@npm:1.18.1"
   dependencies: