summaryrefslogtreecommitdiff
path: root/docs/code
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2020-01-28 20:49:17 -0500
committerOwen Jacobson <owen@grimoire.ca>2020-01-28 23:23:18 -0500
commit0d6f58c54a7af6c8b4e6cd98663eb36ec4e3accc (patch)
treea2af4dc93f09a920b0ca375c1adde6d8f64eb6be /docs/code
parentacf6f5d3bfa748e2f8810ab0fe807f82efcf3eb6 (diff)
Editorial pass & migration to mkdocs.
There's a lot in grimoire.ca that I either no longer stand behind or feel pretty weird about having out there.
Diffstat (limited to 'docs/code')
-rw-r--r--docs/code/commit-messages.md13
-rw-r--r--docs/code/configuring-browser-apps.md98
-rw-r--r--docs/code/index.md11
-rw-r--r--docs/code/users-rolegraph-privs.md88
4 files changed, 210 insertions, 0 deletions
diff --git a/docs/code/commit-messages.md b/docs/code/commit-messages.md
new file mode 100644
index 0000000..532b2c6
--- /dev/null
+++ b/docs/code/commit-messages.md
@@ -0,0 +1,13 @@
+# Writing Good Commit Messages
+
+Rule zero: “good” is defined by the standards of the project you're on. Have a look at what the existing messages look like, and try to emulate that first before doing anything else.
+
+Having said that, here are some principles I've found helpful and broadly applicable.
+
+* Treat the first line of the message as a one-sentence summary. Most SCM systems have an “overview” command that shows shortened commit messages in bulk, so making the very beginning of the message meaningful helps make those modes more useful for finding specific commits. _It's okay for this to be a “what” description_ if the rest of the message is a “why” description.
+
+* Fill out the rest of the message with prose outlining why you made the change. Don't reiterate the contents of the change in great detail if you can avoid it: anyone who needs that can read the diff themselves, or reach out to ask for help understanding the change. A good rationale sets context for the problem being solved and addresses the ways the proposed change alters that context.
+
+* If you use an issue tracker (and you should), include whatever issue-linking notes it supports right at the start of the message, where it'll be visible even in summarized commit logs. If your tracker has absurdly long issue-linking syntax, or doesn't support issue links in commits at all, include a short issue identifier at the front of the message and put the long part somewhere out of the way, such as on a line of its own at the end of the message.
+
+* If you need rich commit messages (links, lists, and so on), pick one markup language and stick with it. It'll be easier to write useful commit formatters if you only have to deal with one syntax, rather than four. Personally, I use Markdown when I can, or a reduced subset of Markdown, as it's something most developers I interact with will be at least passing familiar with.
diff --git a/docs/code/configuring-browser-apps.md b/docs/code/configuring-browser-apps.md
new file mode 100644
index 0000000..8869a61
--- /dev/null
+++ b/docs/code/configuring-browser-apps.md
@@ -0,0 +1,98 @@
+# Configuring Browser Apps
+
+I've found myself in he unexpected situation of having to write a lot of
+browser apps/single page apps this year. I have some thoughts on configuration.
+
+## Why Bother
+
+* Centralize environment-dependent facts to simplify management & testing
+* Make it easy to manage app secrets.
+
+ [@wlonk](https://twitter.com/wlonk) adds:
+
+ > “Secrets”? What this means in a browser app is a bit different.
+
+ Which is unpleasantly true. In a freestanding browser app, a “secret” is only as secret as your users and their network connections choose to make it, i.e., not very secret at all. Maybe that should read “make it easy to manage app _tokens_ and _identities_,” instead.
+
+* Keep config data & API tokens out of app's source control
+* Integration point for external config sources (Aerobatic, Heroku, etc)
+* The forces described in [12 Factor App: Dependencies](http://12factor.net/dependencies) and, to a lesser extent, [12 Factor App: Configuration](http://12factor.net/config) apply just as well to web client apps as they do to freestanding services.
+
+## What Gets Configured
+
+Yes:
+
+* Base URLs of backend services
+* Tokens and client IDs for various APIs
+
+No:
+
+* “Environments” (sorry, Ember folks - I know Ember thought this through carefully, but whole-env configs make it easy to miss settings in prod or test, and encourage patterns like “all devs use the same backends”)
+
+## Delivering Configuration
+
+There are a few ways to get configuration into the app.
+
+### Globals
+
+```html
+<head>
+ <script>window.appConfig = {
+ "FOO_URL": "https://foo.example.com/",
+ "FOO_TOKEN": "my-super-secret-token"
+ };</script>
+ <script src="/your/app.js"></script>
+</head>
+```
+
+* Easy to consume: it's just globals, so `window.appConfig.foo` will read them.
+ * This requires some discipline to use well.
+* Have to generate a script to set them.
+ * This can be a `<script>window.appConfig = {some json}</script>` tag or a standalone config script loaded with `<script src="/config.js">`
+ * Generating config scripts sets a minimum level of complexity for the deployment process: you either need a server to generate the script at request time, or a preprocessing step at deployment time.
+ * It's code generation, which is easy to do badly. I had originally proposed using `JSON.stringify` to generate a Javascript object literal, but this fails for any config values with `</script>` in them. That may be an unlikely edge case, but that only makes it a nastier trap for administrators.
+
+ [There are more edge cases](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). I strongly suspect that a hazard-free implementation requires a full-blown JS source generator. I had a look at building something out of [escodegen](https://github.com/estools/escodegen) and [estemplate](https://github.com/estools/estemplate), but
+
+ 1. `escodegen`'s node version [doesn't generate browser-safe code](https://github.com/estools/escodegen/issues/298), so string literals with `</script>` or `</head>` in them still break the page, and
+
+ 2. converting javascript values into parse trees to feed to `estemplate` is some seriously tedious code.
+
+### Data Attributes and Link Elements
+
+```html
+<head>
+ <link rel="foo-url" href="https://foo.example.com/">
+ <script src="/your/app.js" data-foo-token="my-super-secret-token"></script>
+</head>
+```
+
+* Flat values only. This is probably a good thing in the grand, since flat configurations are easier to reason about and much easier to document, but it makes namespacing trickier than it needs to be for groups of related config values (URL + token for a single service, for example).
+* Have to generate the DOM to set them.
+ * This is only practical given server-side templates or DOM rendering. You can't do this with bare nginx, unless you pre-generate pages at deployment time.
+
+### Config API Endpoint
+
+```js
+fetch('/config') /* {"FOO_URL": …, "FOO_TOKEN": …} */
+ .then(response => response.json())
+ .then(json => someConfigurableService);
+```
+
+* Works even with “dumb” servers (nginx, CloudFront) as the endpoint can be a generated JSON file on disk. If you can generate files, you can generate a JSON endpoint.
+* Requires an additional request to fetch the configuration, and logic for injecting config data into all the relevant configurable places in the code.
+ * This request can't happen until all the app code has loaded.
+ * It's very tempting to write the config to a global. This produces some hilarious race conditions.
+
+### Cookies
+
+See for example [clientconfig](https://github.com/henrikjoreteg/clientconfig):
+
+```js
+var config = require('clientconfig');
+```
+
+* Easy to consume given the right tools; tricky to do right from scratch.
+* Requires server-side support to send the correct cookie. Some servers will allow you to generate the right cookie once and store it in a config file; others will need custom logic, which means (effectively) you need an app server.
+* Cookies persist and get re-sent on subsequent requests, even if the server stops delivering config cookies. Client code has to manage the cookie lifecycle carefully (clientconfig does this automatically)
+* Size limits constrain how much configuration you can do.
diff --git a/docs/code/index.md b/docs/code/index.md
new file mode 100644
index 0000000..a2f1564
--- /dev/null
+++ b/docs/code/index.md
@@ -0,0 +1,11 @@
+# Code
+
+Pieces of code and code-adjacent work, with or without exposition, that don't quite fit into the library ecosystem, but which I enjoyed writing.
+
+* [A Users, Roles & Privileges Scheme Using Graphs](users-rolegraph-privs.md) — An SQL schema and associated queries for handling permissions when roles can nest arbitrarily.
+
+* [Configuring Browser Apps](configuring-browser-apps.md) — Notes on the available techniques for delivering runtime configuration to code running in a user's browser, and the tradeoffs involved.
+
+* [Writing Good Commit Messages](commit-messages.md) — A style guide.
+
+I also maintain a [Github account](https://github.com/ojacobson/) for more substantial projects.
diff --git a/docs/code/users-rolegraph-privs.md b/docs/code/users-rolegraph-privs.md
new file mode 100644
index 0000000..d36f430
--- /dev/null
+++ b/docs/code/users-rolegraph-privs.md
@@ -0,0 +1,88 @@
+# A Users, Roles & Privileges Scheme Using Graphs
+
+The basic elements:
+
+* Every agent that can interact with a system is represented by a **user**.
+* Every capability the system has is authorized by a distinct **privilege**.
+* Each user has a list of zero or more **roles**.
+ * Roles can **imply** further roles. This relationship is transitive: if role A implies role B, then a member of role A is a member of role B; if role B also implies role C, then a member of role A is also a member of role C. It helps if the resulting role graph is acyclic, but it's not necessary.
+ * Roles can **grant** privileges.
+
+A user's privileges are the union of the privileges granted by the transitive closure of their roles.
+
+```sql
+create table "user" (
+ username varchar
+ primary key
+ -- credentials &c
+);
+
+create table role (
+ name varchar
+ primary key
+);
+
+create table role_member (
+ role varchar
+ not null
+ references role,
+ member varchar
+ not null
+ references "user",
+ primary key (role, member)
+);
+
+create table role_implies (
+ role varchar
+ not null
+ references role,
+ implied_role varchar
+ not null
+);
+
+create table privilege (
+ privilege varchar
+ primary key
+);
+
+create table role_grants (
+ role varchar
+ not null
+ references role,
+ privilege varchar
+ not null
+ references privilege,
+ primary key (role, privilege)
+);
+```
+
+If your database supports recursive CTEs, this schema can be queried in one shot, since we can have the database do all the graph-walking along roles:
+
+```sql
+with recursive user_roles (role) AS (
+ select
+ role
+ from
+ role_member
+ where
+ member = 'SOME USERNAME'
+ union
+ select
+ implied_role as role
+ from
+ user_roles
+ join role_implies on
+ user_roles.role = role_implies.role
+)
+select distinct
+ role_grants.privilege as privilege
+from
+ user_roles
+ join role_grants on
+ user_roles.role = role_grants.role
+order by privilege;
+```
+
+If not, you'll need to pull the entire graph into memory and manipulate it there: this schema doesn't give you any easy handles to identify only the roles transitively included in the role of interest, and repeatedly querying for each step of the graph requires an IO roundtrip at each step, burning whole milliseconds along the way.
+
+Realistic use cases should have fairly simple graphs: elemental privileges are grouped into concrete roles, which are in turn grouped into abstracted roles (by department, for example), which are in turn granted to users. If the average user is in tens of roles and has hundreds of privileges, the entire dataset fits in memory, and PostgreSQL performs well. In PostgreSQL, the above schema handles ~10k privileges and ~10k roles with randomly-generated graph relationships in around 100ms on my laptop, which is pretty slow but not intolerable. Perverse cases (interconnected total subgraphs, deeply-nested linear graphs) can take absurd time but do not reflect any likely permissions scheme.