↓Skip to main content

Week Notes 2026.03

Hugo Archetype for Week Notes #

I created a Hugo archetype for these posts. This is simply a file archetypes/week-notes.md containing:

---
title: "Week Notes {{ .TranslationBaseName }}"
date: {{ dateFormat "2006-01-02" .Date }}
summary: "Week notes for {{ replaceRE "\\..+" "" .TranslationBaseName }} week {{ replaceRE "[^.]+\\.0?" "" .TranslationBaseName }}"
categories: ["Week Notes", "Tech", "Programming"]
tags: []
draft: true
---

{{ <toc> }}

This allows me to create a stub post each week by running:

hugo new -k week-notes content/posts/week-notes/$(date +%G.%V).md

Scheme Databases #

Last week I mentioned my interest in SRFI 167: Ordered Key Value Store and SRFI 168: Generic Tuple Store Database. I updated the sample implementations for these to work with Guile Scheme and created Guix packages:

I’m still not sure where I’ll go with this, given the post finalization note on those SRFIs, but this is a starting point for some experimentation.

I had to do some background reading to understand the implementation of the OKVS memory store in SRFI 167:

Reading Scheme code can be quite tricky as it seems to be customary to import all the symbols from a module without any name-spacing, for example:

(use-modules (srfi srfi-125))

This makes all of the symbols exported by SRFI 125 available in the current module but, unless you’re very familiar with the SRFIs, you don’t know which symbols are pulled in and, when you see a function in the source code, you don’t know where it came from.

In the Clojure world, the equivalent would be:

(use 'clojure.string)

but you almost never see that in modern production code: instead, you would write

(require '[clojure.string :as str])

to make the symbols available with a prefix, or

(require '[clojure.string :refer [lower-case]])

to import only the specified symbol(s). Guile supports both of these concepts too: you could write:

(use-modules ((srfi srfi-125) #:prefix srfi-125:)

to import the module with a prefix, or

(use-modules ((srfi srfi-125) #:select (make-hash-table)))

to import specific symbols, but this seems to be much less common in the Scheme code I’ve read.

Caddy OAuth2 Proxy Setup #

Continuing with the task I started last week to configure a Caddy server with OAuth2 proxy authentication, I followed these instructions to configure an OAuth client for Google Workspace. Then I created a configuration file for the OAuth2 Proxy based on the sample configuration.

Stripped of comments and blank lines, the configuration boils down to:

http_address = ":4180"
reverse_proxy = true
redirect_url = "https://docs.example.org/oauth2/callback"
email_domains = [
  "example.org"
]
whitelist_domains = [
  "docs.example.org"
]
client_id = "${client_id}"
client_secret = "${client_secret}"
cookie_name = "_oauth2_proxy"
cookie_secret = "${cookie_secret}"
cookie_domains = "docs.example.org"
cookie_expire = "168h"
cookie_refresh = "2h"
cookie_secure = true
cookie_httponly = true

The ${} placeholders are terraform template variables. I don’t want to store the secrets in the git repository, so created some encrypted parameters in AWS SSM Parameter Store:

aws ssm put-parameter --name /terraform/docsite/oauth-client-id --type SecureString --value $CLIENT_ID
aws ssm put-parameter --name /terraform/docsite/oauth-client-secret --type SecureString --value $CLIENT_SECRET
aws ssm put-parameter --name /terraform/docsite/cookie-secret --type SecureString --value $COOKIE_SECRET

…then defined data sources in the terraform manifest to look them up, and created the S3 object from the template:

data "aws_ssm_parameter" "oauth_client_id" {
  name = "/terraform/docsite/oauth-client-id"
}

data "aws_ssm_parameter" "oauth_client_secret" {
  name = "/terraform/docsite/oauth-client-secret"
}

data "aws_ssm_parameter" "cookie_secret" {
  name = "/terraform/docsite/cookie-secret"
}

resource "aws_s3_object" "oauth2_proxy_config" {
  bucket = module.ignition_files.s3_bucket_id
  key    = "caddy-oauth-server/oauth2-proxy.cfg"
  content = templatefile("${path.module}/ignition/oauth2-proxy.cfg", {
    client_id     = data.aws_ssm_parameter.oauth_client_id.value
    client_secret = data.aws_ssm_parameter.oauth_client_secret.value
    cookie_secret = data.aws_ssm_parameter.cookie_secret.value
  })
}

The ignition configuration is only applied on first boot and the test cycle would be too slow if I had to provision a new instance to test every change, so I created an instance with some initial configuration then modified files in place until I got everything working.

During this initial development, it’s useful to be able to download and install this proxy configuration from S3. CoreOS is an immutable system and the AWS CLI is not installed, but we can use podman to run it from a container:

podman run --rm -it public.ecr.aws/aws-cli/aws-cli s3 cp s3://ignition-bucket/caddy-oauth-server/oauth2-proxy.cfg - > /etc/oauth2-proxy/oauth2-proxy.cfg

Terraform lifecycle rules #

I learned about a new terraform feature, the replace_triggered_by lifecycle rule. This allows you to force a resource to be replaced if another resource changes. In this case, I want the EC2 instance running the Caddy OAuth2 proxy to be replaced if its ignition file or OAuth2 configuration file changes. We can do this by adding a lifecycle rule to the aws_instance resource:

lifecycle {
  replace_triggered_by = [
    aws_s3_object.ignition_file,
    aws_s3_object.oauth2_proxy_config
  ]
}

Getting EC2 instance public IP address #

Another handy JMESPath query for the AWS CLI, this one to get the public IP address of an EC2 instance:

aws ec2 describe-instances --query 'Reservations[0].Instances[0].NetworkInterfaces[0].Association.PublicIp'

Resetting RDS master password #

Somebody lost the master password for one of our RDS databases! Turns out you can reset it using the AWS CLI, it’s as simple as:

aws rds modify-db-instance --db-instance-identifier example-db-id --apply-immediately --master-user-password example-new-password

Distributed Locks with DynamoDB #

One project I’m working on has a Lambda function that is invoked by S3 and AWS Batch events and, to avoid duplicating work, it needs to take out a lock for each job it’s handling. I think last time I needed to do this I was working with Clojure so could use the DynamoDB Lock client from AWS, which is implemented as a Java library.

Now I’m working in Go, and I found a couple of people have implemented similar libraries:

The dynamodb-lock-go module only supports tables with a partition key, not a sort key, and it does not protect against clock skew (the risk of clock skew is, if you store absolute times in DynamoDB, a system whose clock is fast might assume a lock has expired when another system still thinks it has a valid lock).

The dynamolock/v2 library supports tables with both partition key and sort key, and implements the same method as the original Java library to protect against clock skew.

My table has both partition key and sort key and I don’t want to create a dedicated table for the locks, so I can either use dynamolock/v2 or, if I’m not worried about clock skew, implement similar logic to dynamo-lock-go but support compound keys.

Setting the File Association for xdg-open #

A minor annoyance with my desktop, when I run xdg-open some-file.docx it opens in Calibre. I’m running KDE, where everything is configurable, and this setting can be found in System Settings > Apps & Windows > Default Applications > File Associations. Fixed!

S3 Bucket Metrics Discrepancy #

Looking in the S3 console, the metrics tab for one of our S3 buckets shows it has grown to more than 18TB data and 150,000 objects. This is a surprise, as I had set a lifecycle rule to delete objects after two days and we don’t generate that volume of data in 2 days. Listing the bucket, I see fewer than 2,000 objects and less than 1TB total data, and (as expected) nothing more than two days old.

I started to raise a support query with AWS to explain this, and their “answer bot” suggested several solutions:

  • Creating lifecycle policies to delete old versions
  • Manually deleting unnecessary object versions
  • Cleaning up incomplete multipart uploads using lifecycle rules

The bucket is not versioned so old/unnecessary object versions cannot be the cause, but what about incomplete multipart uploads?

aws s3api list-multipart-uploads --bucket $BUCKET_NAME | jq -r '.Uploads[].UploadId' | wc -l

Hmm, this shows 12,786 incomplete multipart uploads: could that account for the 17TB difference in data volume?

We can configure the additional lifecycle rule by updating our CDK stack, replacing:

bucket.add_lifecycle_rule(expiration=Duration.days(2))

with:

bucket.add_lifecycle_rule(
  abort_incomplete_multipart_upload_after=Duration.days(1),
  expiration=Duration.days(2),
)

We’ll have to wait (at least) 24 hours to find out if this solves the problem.

Listening #