Skip to content

Instantly share code, notes, and snippets.

View mlafeldt's full-sized avatar

Mathias Lafeldt mlafeldt

View GitHub Profile
@mlafeldt
mlafeldt / drizzle.config.local.ts
Created September 12, 2024 14:01
Drizzle config for local development with wrangler
import { defineConfig } from 'drizzle-kit'
import crypto from 'node:crypto'
import { mkdirSync } from 'node:fs'
import path from 'node:path'
// Based on https://github.com/cloudflare/workers-sdk/blob/64710904ad4055054bea09ebb23ededab140aa79/packages/miniflare/src/plugins/shared/index.ts#L194
function idFromName(uniqueKey: string, name: string) {
const key = new Uint8Array(crypto.createHash('sha256').update(uniqueKey).digest())
const nameHmac = new Uint8Array(crypto.createHmac('sha256', key).update(name).digest().subarray(0, 16))
const hmac = new Uint8Array(crypto.createHmac('sha256', key).update(nameHmac).digest().subarray(0, 16))

EMAILENGINE LICENSE AGREEMENT

Version 2.1, 17 October 2023

PLEASE CAREFULLY READ THIS EMAILENGINE LICENSE AGREEMENT ("AGREEMENT"). THIS AGREEMENT CONSTITUTES A LEGALLY BINDING AGREEMENT BETWEEN YOU AND POSTAL SYSTEMS OÜ AND GOVERNS YOUR USE OF THE SOFTWARE (DEFINED BELOW). IF YOU DO NOT AGREE WITH THIS AGREEMENT, YOU MAY NOT USE THE SOFTWARE. IF YOU ARE USING THE SOFTWARE ON BEHALF OF A LEGAL ENTITY, YOU REPRESENT AND WARRANT THAT YOU HAVE AUTHORITY TO AGREE TO THIS AGREEMENT ON BEHALF OF SUCH ENTITY. IF YOU DO NOT HAVE SUCH AUTHORITY, DO NOT USE THE SOFTWARE IN ANY MANNER.

This Agreement is entered into by and between Postal Systems OÜ and you, or the legal entity on behalf of whom you are acting (as applicable, "You" or "Your").

1. DEFINITIONS

@mlafeldt
mlafeldt / clerk.ts
Created August 25, 2022 09:51
Talking to Clerk's backend API from Deno
#!/usr/bin/env -S deno run --allow-env=CLERK_API_KEY --allow-net=api.clerk.dev --no-check
import {
ClerkAPIResponseError,
ClerkBackendAPI,
User,
} from "https://cdn.skypack.dev/@clerk/backend-core?dts";
const ClerkAPI = new ClerkBackendAPI({
apiClient: {
@mlafeldt
mlafeldt / app.py
Created August 12, 2022 08:41
Extracted from https://eventbridge-inbound-webhook-templates-prod-eu-central-1/lambda-templates/github-lambdasrc.zip
"""Webhook implementation for Github"""
import os
import json
import urllib.parse
import base64
import hmac
import hashlib
from cgi import parse_header
import boto3
@mlafeldt
mlafeldt / useAuth0WithToken.ts
Last active May 14, 2021 12:37
Like useAuth but also returns the access token
import { useState, useEffect } from 'react'
import { useAuth0 } from '@auth0/auth0-react'
export const useAuth0WithToken = () => {
const auth0 = useAuth0()
const [token, setToken] = useState('')
const [error, setError] = useState()
useEffect(() => {
if (!auth0.isAuthenticated) return
@mlafeldt
mlafeldt / run.sh
Last active February 25, 2021 10:59
Use K8s pod to extract session credentials and region from EC2 metadata service
# kubectl run my-shell --rm -it --restart=Never --image alpine --command sh
apk add curl jq
BASEURL=http://169.254.169.254/latest/meta-data
ROLE=$(curl -s $BASEURL/iam/security-credentials)
CREDS=$(curl -s $BASEURL/iam/security-credentials/$ROLE)
AZ=$(curl -s $BASEURL/placement/availability-zone)
echo "export AWS_REGION=${AZ::-1}"; echo "$CREDS" | jq -r '{AWS_ACCESS_KEY_ID: .AccessKeyId, AWS_SECRET_ACCESS_KEY: .SecretAccessKey, AWS_SESSION_TOKEN: .Token} | to_entries | .[] | "export " + .key + "=" + .value'
# Paste the result into any shell and run `aws sts get-caller-identity` etc.
# What you can do about it:
# https://docs.aws.amazon.com/eks/latest/userguide/best-practices-security.html#restrict-ec2-credential-access
@mlafeldt
mlafeldt / bug.md
Last active January 5, 2021 17:59
AWS Landing Zone v2.4.1 broken after pykwalify update

Bug Report

AWS Landing Zone version 2.4.1 is currently broken. More specifically, validation of manifest.yaml has been broken since Dec 30 when version 1.8.0 of https://pypi.org/project/pykwalify was released. This release requires schemas to be written in YAML 1.2. However, Landing Zone's current schema and manifest file adhere to YAML 1.1 (where Yes|yes still means true). The result is that the Build step of AWS-Landing-Zone-CodePipeline currently fails during schema validation.

Hotfix

Update the buildspec of AWS-Landing-Zone-CodeBuild like this:

- - pip install --upgrade pykwalify
@mlafeldt
mlafeldt / nginx.yaml
Created April 12, 2019 14:45
HPA based on CPU
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: ae-ops-testing
labels:
app: nginx
spec:
# replicas: 1
selector:
@mlafeldt
mlafeldt / main.go
Created March 1, 2019 22:43
Lambda handlertrace example
func main() {
lambda.StartHandler(middleware(handler))
}
type handlerFunc func(ctx context.Context, payload []byte) ([]byte, error)
func (h handlerFunc) Invoke(ctx context.Context, input []byte) ([]byte, error) {
log.Printf("[DEBUG] input = %s", string(input))
output, err := h(ctx, input)
log.Printf("[DEBUG] output = %s", string(output))
@mlafeldt
mlafeldt / spec.yaml
Created October 26, 2018 09:46
K8s JSON Logger Container
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: test
name: test
spec:
replicas: 1