Skip to content

Instantly share code, notes, and snippets.

View Xophmeister's full-sized avatar

Christopher Harrison Xophmeister

View GitHub Profile
@Xophmeister
Xophmeister / kms.sh
Last active December 21, 2023 12:54
Demonstration of asymmetric encryption using Amazon KMS
#!/usr/bin/env bash
# Demonstration of asymmetric cryptography, using Amazon KMS as the
# asymmetric key store and OpenSSL for client-side symmetric encryption.
#
# THIS SHOULD NOT BE CONSIDERED A SECURE IMPLEMENTATION.
set -euo pipefail
get-public-key() {
@Xophmeister
Xophmeister / __main__.py
Last active January 25, 2023 08:02
Python metaclass to enforce "good behaviour" from simulated sum type class hierarchies
from math import pi
from .shape import Shape, Circle, Rectangle
def area(shape: Shape) -> float:
"""
Calculate the area of the given shape
"""
match shape:
@Xophmeister
Xophmeister / wtfp.sh
Created October 31, 2021 21:10
Functional programming...in Bash
#!/usr/bin/env bash
map() {
local fn="$1"
local input
while read -r input; do
"${fn}" "${input}"
done
}
@Xophmeister
Xophmeister / optional_option.py
Created September 21, 2021 15:59
argparse.Action like "store", but with an optional modifier
import argparse
import sys
from enum import Enum
class Viewport(Enum):
All = "all"
WorkingDirectory = "here"
OwnedByMe = "mine"
# NOTE The property decorator doesn't work on default and choices,
@Xophmeister
Xophmeister / buildContainer.nix
Last active August 21, 2021 21:35
Proof-of-concept Docker image building of multiple packages, with Nix
{ pkgs ? import <nixpkgs> {}, name, packages ? {}}:
with pkgs;
let
# We need bash and coreutils to display the container "run" message;
# dockerTools.buildImage expects a list of derivations, not a set
required = { inherit bash coreutils; };
contents = builtins.attrValues (required // packages);
prettyPrint = import ./prettyPrint.nix {};
@Xophmeister
Xophmeister / letter-boxed.sh
Last active August 26, 2021 14:12
Find valid words (not full solutions) to the New York Times' Letter Boxed game
#!/usr/bin/env bash
set -euo pipefail
readonly WORDS="${WORDS-/usr/share/dict/words}"
readonly MINIMUM_LENGTH="${MINIMUM_LENGTH-3}"
limit-alphabet() {
local alphabet="$1"
grep -E "^[${alphabet}]{${MINIMUM_LENGTH},}$"
@Xophmeister
Xophmeister / README.md
Last active January 14, 2021 14:08
Demonstration of using a thread pool to launch background tasks in a web server

Start the Server

python toy.py

Create a Job

curl -X PUT localhost:8080/[name]

Job Status

@Xophmeister
Xophmeister / network.py
Created December 4, 2020 11:32
Create an externally routed OpenStack network
import os
from keystoneauth1 import identity, session
from neutronclient.v2_0 import client
def _neutron():
creds = {
"auth_url": os.environ["OS_AUTH_URL"] + "/v3",
"username": os.environ["OS_USERNAME"],
"password": os.environ["OS_PASSWORD"],
"project_name": os.environ["OS_PROJECT_NAME"],
@Xophmeister
Xophmeister / q.rkt
Created September 29, 2020 16:23
Objects are a poor man's closure. Closures are a poor man's object.
#lang racket/base
(require racket/contract
racket/match
racket/math)
(define numerator/c exact-integer?)
(define denominator/c (and/c exact-integer? (not/c zero?)))
(define Q/c (->i ((msg (symbols 'p 'q '->exact)))
@Xophmeister
Xophmeister / hello-world.sh
Last active January 21, 2020 13:20
Create a group of homogeneous machines, with SSH access, instance metadata and user data, in OpenStack
#!/usr/bin/env bash
declare INDEX="$(cloud-init query ds.meta_data.meta.index)"
declare TOTAL="$(cloud-init query ds.meta_data.meta.total)"
echo "Hello, World! This is instance $(( INDEX + 1 )) of ${TOTAL}." \
| tee /root/hello.txt