Open Factory Flooropen source

Guides

The two sites

On this page

Open Factory Floor has two small websites, both built from this repository and served from AWS: files in a private S3 bucket, delivered by CloudFront.

  • openfactoryfloor.org is the open-source home: what the factory is, how it works, how to set it up yourself for free, every guide in this docs/ folder (each made into a page, so there is one copy to keep right), and how to contribute.
  • openfactoryfloor.com is the commercial side. For now it is one page saying hosted floors are coming, with a form to register interest, and a page saying exactly what happens to what is typed in it.

Both are plain HTML, one small stylesheet and, on the .com only, one small script for the form. Nothing is loaded from anybody else's servers, and there are no cookies and no trackers.

#What is where

PathWhat it is
site/build.pyBuilds both sites into site/out/org/ and site/out/com/ (not kept in git)
site/src/The words of each page (org-*.html, com-*.html), the stylesheet and the form's script
site/drawings.pyThe drawings, in the factory app's own style
site/markup.pyTurns the guides' Markdown into HTML
hosting/contact/handler.pyThe form's backend: a small AWS Lambda function that sends one email through Amazon SES
hosting/aws_site.pyMakes, publishes and removes the sites and the form's backend on AWS
.github/workflows/site.ymlBuilds, checks and publishes both sites when main changes
tests/test_site.py, tests/test_contact.py, tests/test_hosting_site.pyTheir tests

#Building and looking at them

python3 site/build.py
python3 -m http.server 8800 --bind 127.0.0.1 --directory site/out/org

Then open http://127.0.0.1:8800/. For the .com, serve site/out/com on another port. The builder needs only Python's standard library.

The builder stops, and says why, at a link that leads nowhere: to a guide that is not there, to a heading a guide does not have, to a file that is not in the repository, or between pages of the sites. A link from one guide to another becomes a link to that guide's page; a link to any other file becomes a link to it on GitHub. docs/EARLY_REVIEW_HISTORY.md is a record, not a guide, and is left off. The same files in always give the same bytes out.

A new guide in docs/ becomes a page by itself. It is listed on the guides page under the heading it is given in GROUPS in site/build.py, or under "More" until it is given one.

#How they are published

Once, by hand, with AWS credentials for the account (AWS_PROFILE, and AWS_REGION for where the buckets and the form live):

python3 hosting/aws_site.py create org --domain openfactoryfloor.org --zone ZONE_ID --www
python3 hosting/aws_site.py create com --domain openfactoryfloor.com --zone ZONE_ID --www
python3 hosting/aws_site.py publisher org --repo theaiengineers/factory
python3 hosting/aws_site.py publisher com --repo theaiengineers/factory

create makes a private bucket, a certificate proved by a DNS record in the Route 53 zone you name, a CloudFront distribution that alone may read the bucket, and the DNS records for the name and www.. It waits for the certificate (usually a few minutes); stopped anywhere, it carries on when run again. Add --plan to create, publish, contact, recipient or publisher to see every command a first run would send, without asking AWS anything.

The pages are served over HTTPS only (plain http is sent to https), TLS 1.2 or later, HTTP/2 and HTTP/3, compressed, with HSTS, nosniff, no framing, a referrer policy and a strict Content-Security-Policy that lets a page load only the site's own stylesheet, script and icon. /guides/setup/ is served from guides/setup/index.html, an address without its last slash is sent to the one with it, and www. is sent to the bare name.

publisher makes the role GitHub Actions takes to publish one site: it may be taken only by a workflow running on this repository's main branch, and may only list, put and delete files in that site's bucket and refresh that site's distribution. It needs the account's OpenID Connect provider for GitHub; if there is none it says the one command that adds it, or --make-provider adds it.

publisher prints the repository variables to set (none is secret): SITE_REGION, and for each site SITE_ORG_ROLE, SITE_ORG_BUCKET, SITE_ORG_DISTRIBUTION (and the same with COM). From then on every push to main that changes docs/, site/ or README.md builds both sites, runs their tests, and publishes each site whose variables are set. It can also be run by hand from the Actions tab. It never runs on a pull request.

To publish from your own machine instead: python3 site/build.py, then python3 hosting/aws_site.py publish org --from site/out/org. Only the files that changed are sent. Given --bucket, it writes only to the site's own bucket (factory-site-NAME-ACCOUNT, tagged as that site's), and refuses any other before anything is written or removed. Files whose names carry a fingerprint (the stylesheet and the script) may be cached for a year; everything else for five minutes.

#The interest form

The form on the .com posts what is typed, as JSON, to its own site: POST /api/contact. CloudFront sends that one path, uncached, to a small function (hosting/contact/handler.py) at a function URL, adding a header, x-origin-secret, that only CloudFront and the function know. The function checks what was typed and sends it as one plain-text email through Amazon SES to the recipient, with Reply-To set to the sender's address, so answering is just replying. Because the page and the form are on the same site, there is no cross-site request: the Content-Security-Policy allows connect-src 'self' and nothing more.

It is made, once the .com itself has been made, with:

python3 hosting/aws_site.py contact com --zone ZONE_ID

That makes the SES identity for the site's domain (Easy DKIM: its three DKIM records are written to the zone, and it waits a while for SES to see them), a small DynamoDB table of counters, the function, its role, its log and its function URL, and adds the origin and the /api/contact behaviour to the site's distribution. The behaviour sends on every header the browser sent except Host (AWS's managed origin request policy "AllViewerExceptHostHeader"), so the function sees the page's Origin and Content-Type; a function URL refuses a Host that is not its own. The email is sent from contact@ the site's domain, in the region you run it in (the account is expected to be out of the SES sandbox there; the tool checks, and says what to do if it is not, since in the sandbox mail to an unverified address is refused).

It does not set who receives the messages. It ends by printing the one command that does:

python3 hosting/aws_site.py recipient com

which asks for the address and reads it from standard input, never from an argument (so it is never in your shell's history), checks it looks like an address, adds it to the function's settings keeping all the others, and says only "recipient set". python3 hosting/aws_site.py list says whether each site's form has a recipient, yes or no, and never who. The recipient's address lives only in the function's settings. It is not in this repository or either site, and nobody else is ever told it. Until it is set the form answers that it cannot take messages, and the page says the message could not be sent.

The cap. The form sends at most 20 emails an hour and 100 a day, whoever sends them. contact com --zone ZONE_ID --per-hour N --per-day N changes that, and a later contact without them keeps what was set. Before sending, the function adds one to this hour's and this day's counter in its table, each only while it is under its cap; when either is full it answers 429 ("Too many messages just now. Please try again later.") and sends nothing. Only a request that came through the site, is well formed and passed the hidden field and the timer is counted, so rubbish does not use up the allowance; one that is counted and then fails to send stays counted. If DynamoDB cannot be asked, the form fails closed: 503, and nothing is sent. The counters lapse by themselves (DynamoDB's time to live). So the most the form can put in the mailbox, or take from the account's SES allowance, is the cap. What it cannot do is tell a flood from real people: a flood that uses up the hour's allowance means real people are told to try later until the hour (or the day) has passed.

The origin secret. The tool makes a long random value (secrets.token_urlsafe(48)) and puts it in two places only: the function's settings and the distribution's origin header. It never prints it, never writes it to a file, and never shows it in --plan; a later run reads it back from the function so that the two stay the same. Anybody who may read the function's or the distribution's configuration in the AWS account can read it. It only stops people going round CloudFront to the function's own address; it is not a password to anything else. A stronger way is known and left for later: CloudFront can sign its requests to the function URL with an origin access control, and the URL can then be closed to everybody else.

What the function does:

  • It refuses, with 403, anything without the right x-origin-secret (compared in constant time) or with an Origin that is not the site's own (https://DOMAIN or https://www.DOMAIN), with 415 anything not sent as Content-Type: application/json, and takes POST only, a JSON object only, at most 20,000 bytes.
  • A hidden field a person never sees (website, which programs fill in), or less than two and a half seconds on the page, gets the answer "sent" and nothing is sent.
  • Name (120 characters), email address (200, of a plausible shape) and message (5,000) are needed; project or organisation (200) is not. Line breaks and tabs are taken out of every one-line field, so nothing typed can reach an email header. The subject is "Website enquiry: NAME".
  • It answers in small JSON of its own words, never cached, and never repeats what was sent. Its log says only that a message of so many bytes was sent, or why one was not: never a message, never an address.
  • It sends no more than the cap (above). At most three copies run at once, for at most ten seconds each (256 MB), which bounds what the function itself can cost. Its log is kept 90 days.

Its role may write its own log, update items in its own table of counters (dynamodb:UpdateItem on that one table), and send email from the domain's SES identity as contact@ the site's domain (ses:SendEmail on arn:aws:ses:REGION:ACCOUNT:identity/DOMAIN, with the condition ses:FromAddress), and nothing else.

#What it costs

In plain words, for two small sites like these:

  • S3 and CloudFront: pennies a month. A few hundred kilobytes stored, and CloudFront's charges for requests and data, which at this size come to a few pence. The first thousand invalidations a month are free, and a publish makes one.
  • The certificates: nothing. Certificates from AWS Certificate Manager used with CloudFront are free.
  • DNS: the Route 53 zone you already have. Lookups of alias records that point at CloudFront are not charged for.
  • The form: nothing while nobody uses it. A Lambda function and an on-demand DynamoDB table cost nothing standing still, and each message costs a tiny fraction of a penny in Lambda, DynamoDB and SES.
  • GitHub Actions: a minute or two of a runner for each change to the guides.

Check AWS's prices for your region; none of this is a quote.

#Undoing each thing

To undoDo this
A publishRevert the change on main (the workflow publishes the reverted pages), or build an older commit and publish it by hand. The bucket also keeps older copies of every file for 30 days
The workflow publishingDelete the site's three repository variables: its step is then skipped. Or disable the workflow in the Actions tab
Who receives the form's messagesRun recipient com again with another address. To stop all mail, remove the RECIPIENT setting from the function in the Lambda console: the form then answers that it cannot take messages
A whole sitepython3 hosting/aws_site.py delete org says what it would remove, and removes nothing. delete org --yes --zone ZONE_ID removes the DNS records that point at it, the distribution (switched off first, which takes CloudFront a while: run it again if it says so), the CloudFront function, header policy and origin access control, the certificate, the bucket and every file in it, the form's function, role and table of counters, and the publishing role. It refuses unless every part is its own
What delete leavesThe form's log group (aws logs delete-log-group --log-group-name /aws/lambda/factory-site-NAME-contact), the SES identity for the domain and its three DKIM records (other mail from the domain may rely on them: aws sesv2 delete-email-identity --email-identity DOMAIN), the DNS record that proved the domain for the certificate (a CNAME whose name starts with an underscore), and GitHub's OpenID Connect provider, which other roles in the account may trust

Everything the tool makes is named factory-site-NAME… and tagged project=factory and factory-site=NAME (or, where AWS allows no tags, says so in its description), so it can always be found and told apart from anything else in the account.

This guide is docs/SITES.md in the repository, and this page is made from it. Something wrong or unclear? Open an issue or send a pull request.