Skip to content

Instantly share code, notes, and snippets.

@mahdilamb
mahdilamb / okta-auth.py
Created March 29, 2026 13:42
Okta Auth (requires SPA + PKCE on app)
"""Fetch an Okta access token via authorization code + PKCE flow.
Caches the token (and refresh token) in ~/.okta-token/credentials.json.
Refreshes automatically when expired. Opens a browser for consent when needed.
Configuration is stored in ~/.okta-token/config.json. On first run, the user
is prompted to fill in the details interactively.
Usage:
# Print a valid access token (refreshing or re-authing as needed):
@mahdilamb
mahdilamb / opensearch_age_decay.py
Last active February 6, 2026 20:27
Numpy implementation of Opensearch Age decay
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "matplotlib>=3.10.8",
# "numpy>=2.4.2",
# ]
#
# ///
@mahdilamb
mahdilamb / export_env.sh
Created January 15, 2026 20:13
Export specific values
#!/usr/bin/env bash
ALLOWED_VALUES=("$@")
while IFS='=' read -r key value; do
[[ -z "$key" || "$key" =~ ^# ]] && { echo "$key"; continue; }
if [ "${#ALLOWED_VALUES[@]}" -eq 0 ]; then
echo "$key=$value"
continue
@mahdilamb
mahdilamb / Dockerfile
Last active January 13, 2026 23:16
Dockerfile for routing https traffic to http
FROM alpine:latest AS cert-init
RUN apk add openssl
WORKDIR /root/ssl
RUN openssl req -x509 -nodes -newkey rsa:2048 \
-keyout key.pem \
-out cert.pem \
-days 365 \
-sha256 \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
"""Utility script to export the OpenAPI spec from a FastAPI service.
Usage:
python3 fastapi2openapi path.to.module:app -C root/of/package --file openapi.yaml
Where the unnamed argument is the location of the app (the module:app_variable). -C is the working directory to import from. And --file is the file to output.
Note that the output file will be relative to the current working directory and not the location in -C.
"""
@mahdilamb
mahdilamb / compose.yaml
Created September 25, 2024 14:40
compose with mkcert certificates
# Docker compose file for testing the build process locally
services:
frontend:
container_name: frontend
build:
context: .
args:
- NEXT_PUBLIC_LOCAL=http://localhost:8080
ports:
- "3000:3000"
@mahdilamb
mahdilamb / gcs-pubsub-fastapi.py
Created August 18, 2024 12:53
Script for creating a FastAPI endpoint for google cloud storage pub/sub
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pydantic",
# "fastapi",
# "uvicorn[standard]",
# ]
# ///
import datetime
"""
Live utility for converting `.model_validate({...})` calls into
model constructor calls with snake_case keyword arguments.
The module watches itself for changes, reloads on save, transforms
the code in `SAMPLE` and copies it to the clipboard.
"""
import importlib.util
import os
def _show_commands():
"""Print the help for each of the functions."""
import inspect
import tokenize
import scripts as module
def get_docs(fns: dict[str, str]) -> dict[str, str]:
result = {}
@mahdilamb
mahdilamb / hibernate-after.ps1
Created June 10, 2024 17:04
Hibernate the computer after a number of seconds. Format: `mm:ss`
param($DURATION)
$_POS = $DURATION.IndexOf(":")
$MINS = $DURATION.SubString(0, $_POS)
$SECS = $DURATION.SubString($_POS + 1, $DURATION.Length - [Int64]$_POS - 1)
$TOTAL_SECS = (([Int64]$MINS) * 60) + $SECS
$REMAINING_SECS = $TOTAL_SECS
while ($REMAINING_SECS -gt 0) {
Write-Host -NoNewLine "`rSleeping after $REMAINING_SECS seconds"
Start-Sleep -s 1
$REMAINING_SECS--