Skip to content

Commit ca536d9

Browse files
committed
Add a basic Docker build
Signed-off-by: Brian Warner <brian@bdwarner.com>
1 parent 126c18c commit ca536d9

File tree

5 files changed

+168
-3
lines changed

5 files changed

+168
-3
lines changed

Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM nginx:alpine
2+
3+
RUN apk add vim
4+
COPY cfg/vimrc /etc/vim/vimrc
5+
6+
7+
ARG CDN_ACCESS_KEY=''
8+
COPY cfg/default.conf /etc/nginx/conf.d/default.conf
9+
10+
# If the CDN_ACCESS_KEY environment variable is *not* set, operate in "break glass" mode where the
11+
# container responds to all requests. Otherwise, look for the secret header the CDN adds to origin
12+
# pulls and only allow responses to those requests, and 301 the rest back to the CDN.
13+
14+
RUN if [ -n "$CDN_ACCESS_KEY" ]; then \
15+
sed -i s/##PLACEHOLDER-cdn_header_detection-DO_NOT_CHANGE##/"map \$http_cdn_access \$reroute_to_cdn { default '1'; $CDN_ACCESS_KEY '0'; }"/g /etc/nginx/conf.d/default.conf && \
16+
sed -i s/##PLACEHOLDER-cdn_reroute-DO_NOT_CHANGE##/"if (\$reroute_to_cdn) { return 301 \$scheme:\/\/code.jquery.com\$uri; }"/g /etc/nginx/conf.d/default.conf; \
17+
fi
18+
19+
COPY cdn/* /usr/share/nginx/html/
20+
COPY git/* /usr/share/nginx/html/
21+
22+
EXPOSE 80
23+

README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
1-
codeorigin.jquery.com
1+
# Official project releases
22
=====================
33

4-
### Build
4+
This repo is used to build a Docker container that serves the codeorigin site for jQuery and related projects. It is designed to deploy easily, and includes a "break glass in case of emergency" minimal config mode should codeorigin need to be redeployed urgently.
55

6-
To build and deploy your changes for previewing in a [`jquery-wp-content`](https://github.com/jquery/jquery-wp-content) instance, follow the [workflow instructions](http://contribute.jquery.org/web-sites/#workflow) from our documentation on [contributing to jQuery Foundation web sites](http://contribute.jquery.org/web-sites/).
6+
## Build a local copy
7+
8+
To build a local container (defaults to "break glass" mode):
9+
10+
1. Install Docker
11+
1. Clone this repo, and `cd` into it
12+
1. Build the image: `docker build -t releases ./`
13+
1. Run the container, exposing port 80: `docker run -p 127.0.0.1:80:80/tcp releases`
14+
1. To exit the container, press `ctrl+c`
15+
16+
To build a local container in deployment mode (redirecting any requests without the magic header that indicates an origin pull), build the container with the header value in an environment variable:
17+
18+
1. Install Docker
19+
1. Clone this repo, and `cd` into it
20+
1. Generate a random string for the environment variable: ``CDN_ACCESS_KEY=`openssl rand -hex 16` ``
21+
1. Build the image: `docker build -t prod-releases --build-arg CDN_ACCESS_KEY=$CDN_ACCESS_KEY ./`
22+
1. Run the container, exposing port 80: `docker run -p 127.0.0.1:80:80/tcp prod-releases`
23+
1. To exit the container, press `ctrl+c`
24+
25+
Note that you will need to keep track of `$CDN_ACCESS_KEY` and add it to the headers sent for origin pulls. To test whether this is working correctly, you can use `curl`:
26+
27+
* This should always redirect to `code.jquery.org`: `curl -i localhost/jquery-3.1.1.js`
28+
* This should always deliver a copy of the file (don't forget to set the environment variable in your current shell): `curl -i -H "cdn-access: ${CDN_ACCESS_KEY}" localhost/jquery-3.1.1.js`
29+
30+
## Build the production site
31+
32+
To deploy, first generate the CDN access key. Next, you'll need to configure the container host to build from the Dockerfile in this repository, and use the CDN access key as a build argument. Finally, you'll configure the CDN to send both the Host header and the access key during origin pulls.
33+
34+
1. Generate the access key: ``CDN_ACCESS_KEY=`openssl rand -hex 16` ``
35+
1. Configure the container host to build from this repo, and set this build variable: `CDN_ACCESS_KEY=(Insert the value of $CDN_ACCESS_KEY here)`
36+
1. Create the magic header and the host header at the CDN: `cdn-access: (Insert the value of $CDN_ACCESS_KEY here)|Host: (insert URL to app container)`
37+
38+
## In case of emergency
39+
40+
If you need to deploy a codeorigin container immediately, or if there are origin pull failures and you're not sure why, deploy without setting the `CDN_ACCESS_KEY` environment variable. The codeorigin server will respond to all requests without redirecting non-origin pulls to the CDN, so this should be only used in case of emergencies.
41+
42+
## Add or update project release files
43+
44+
To add a new release or update an existing one, simply commit the new file to the `cdn` directory and merge to the `main` branch. The container will rebuild automatically.

cfg/default.conf

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#
2+
# This file is responsible for the site which serves the main releases from codeorigin. By default,
3+
# it generates a site that operates in "break glass" emergency mode. All requests are served,
4+
# regardless of where they come from.
5+
#
6+
# In production, the CDN should add a private header to origin fetches. All requests which do not
7+
# include the correct header should be bounced via 301 redirect back to the CDN. This ensures that
8+
# even if a client attempts to link to codeorigin, it will still be served from the CDN. The end
9+
# result should be that codeorigin is only reachable for origin pulls from the CDN, decreasing its
10+
# load and reducing the attack surface for DDOSs.
11+
#
12+
# How to enable production mode:
13+
#
14+
# **VERY IMPORTANT**: You must first configure the CDN to send the header **BEFORE** you configure
15+
# codeorigin to ignore requests that omit the header. If you do not preserve this order you will
16+
# create a 301 redirect loop and **break a substantial portion of the internet.**
17+
#
18+
# 1. Add a header to origin pulls named "cdn-access". Set the value to something long and random. An
19+
# md5 hash of some random letters is probably good enough.
20+
#
21+
# 2. Rebuild this container and set the environment variable CDN_ACCESS_KEY to the value in step 1.
22+
#
23+
# 3. Test the container:
24+
# * First, check that requests with the correct header will not be redirected. This should always
25+
# return the actual file contents of the 3.5.1 release.
26+
#
27+
# * `curl -i -H 'cdn-access: {value from step 1}' https://CONTAINER_URL/jquery-3.5.1.js`
28+
#
29+
# * Next, check that requests without the header are redirected to code.jquery.com. This should
30+
# always return "HTTP/1.1 301 Moved Permanently"
31+
#
32+
# * `curl -i https://CONTAINER_URL/jquery-3.5.1.js`
33+
#
34+
# 4. Update the DNS to point the codeorigin CNAME to https://CONTAINER_URL
35+
#
36+
#
37+
# How to enable "Break glass" mode:
38+
#
39+
# If everything has melted down and you need to go into "break glass" mode, deploy this container
40+
# without a CDN_ACCESS_KEY environment variable. It will serve all requests without redirecting to
41+
# the CDN, regardless of the presence or absence of the header.
42+
#
43+
# What this means in real terms is that requests to https://code.jquery.com will still go through
44+
# the CDN, but requests to https://codeorigin.jquery.com will hit this container directly. This is a
45+
# recipe for a DDOS (innocent or intentional), so this should be a last resort while other issues
46+
# are worked out.
47+
#
48+
49+
# Do not change the following line. The Dockerfile will add CDN header detection at build time if
50+
# the correct environment variable is set.
51+
##PLACEHOLDER-cdn_header_detection-DO_NOT_CHANGE##
52+
53+
54+
# CDN_ACCESS_KEY "0";
55+
server {
56+
listen 80;
57+
listen [::]:80;
58+
server_name localhost;
59+
60+
access_log /var/log/nginx/host.access.log main;
61+
62+
location / {
63+
root /usr/share/nginx/html;
64+
index index.html index.htm;
65+
66+
# Do not change the following line. The Dockerfile will add the reroute logic at build time if
67+
# the correct environment variable is set.
68+
##PLACEHOLDER-cdn_reroute-DO_NOT_CHANGE##
69+
70+
}
71+
72+
#error_page 404 /404.html;
73+
74+
# redirect server error pages to the static page /50x.html
75+
#
76+
error_page 500 502 503 504 /50x.html;
77+
location = /50x.html {
78+
root /usr/share/nginx/html;
79+
}
80+
}
81+
82+
# vim: ts=2 sw=2 et

cfg/vimrc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
set nocompatible " Use Vim defaults (much better!)
2+
set bs=2 " Allow backspacing over everything in insert mode
3+
set ai " Always set auto-indenting on
4+
set history=50 " keep 50 lines of command history
5+
set ruler " Show the cursor position all the time
6+
7+
" Don't use Ex mode, use Q for formatting
8+
map Q gq
9+
10+
" When doing tab completion, give the following files lower priority.
11+
set suffixes+=.info,.aux,.log,.dvi,.bbl,.out,.o,.lo
12+
13+
set modeline
14+
syntax on
15+
autocmd BufRead APKBUILD set filetype=sh
16+

git/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Release snapshots
2+
3+
This directory is for git snapshots only. They should be considered extremely
4+
unstable and entirely unsuitable for production deployments, and should only be
5+
used in active develompent.
6+

0 commit comments

Comments
 (0)