Skip to content

Instantly share code, notes, and snippets.

@kennwhite
kennwhite / merkel.json
Created Jan 16, 2023
Mock records for testing subdocuments and extended json EJSON-style dates (this is all fake data)
View merkel.json
[{
"name": "Angela Merkel",
"dob": {
"$date": {
"$numberLong": "-487900800000"
}
},
"phone": "+49 30 182722720",
"address": {
"street": "Willy-Brandt-Straße 1",
@kennwhite
kennwhite / ulimits_ubuntu_mongodb.sh
Last active Dec 21, 2022
Set Ubuntu 20.04 mongodb recommended ulimits
View ulimits_ubuntu_mongodb.sh
# ulimit -a
# sudo sysctl net.ipv4.tcp_fastopen=3
# sudo sysctl vm.max_map_count=524288
# note: "-l" (max locked memory) has no soft limit
# apt-get install numactl
sudo sh -c "sysctl -w net.ipv4.tcp_fastopen=3 ; \
sysctl -w vm.max_map_count=524288 ; \
ulimit -l unlimited ; \
@kennwhite
kennwhite / sublimeRegExp1.txt
Last active Dec 17, 2022
Sublime regexp for find & replace
View sublimeRegExp1.txt
Regular expression for Sublime to find & replace and add double quotes
"$numberLong": 706213333988
to
"$numberLong": "706213333988"
Use this (make sure the [.*] button on the left is pressed)
Find: \$numberLong": (.*)
@kennwhite
kennwhite / javascript_node_random_dob.js
Created Dec 9, 2022
Javascript/Node function to generate a random date set to UTC 00:00:00 timestamp
View javascript_node_random_dob.js
// Create a native Javascript Date object set to UTC 00:00:00 timestamp
// Here, the default parameters create a plausible DOB
function randomDate( start = new Date(1935, 0, 1), end = new Date(2004, 0, 1) ) {
var dt = new Date(+start + Math.random() * (end - start));
dt.setUTCHours(0,0,0,0);
return dt;
}
// Random DOB
@kennwhite
kennwhite / VS_Code_go_debugging.md
Last active Sep 7, 2022
Setting up VS Code and Golang with debug & build flag (-tags foo) support
View VS_Code_go_debugging.md

Setting up VS Code and Golang with debug & build flag (-tags foo) support

This is the setup that worked for me after half a day of hacking around and chasing rabbit holes down old forum posts and open & closed Github issues.

Problem: While Go integration with VS Code is pretty slick in general, I needed to pass compile-time build flags, e.g., -tags foo[^1] to go build and go run directives, and I wanted to be able to properly debug with breakpoints etc. While there are some promising tutorials out there like this on Digital Ocean and on Log Rocket it turned out that one of the first things they both say to do is add the Delve extension to VS Code,

@kennwhite
kennwhite / monterey_hidden_files.sh
Created Sep 4, 2022
Permanently show hidden files in MacOS Monterey including Sublime Text
View monterey_hidden_files.sh
# To change the default Finder behavior and actually show hidden files/directories
defaults write com.apple.finder AppleShowAllFiles -boolean true; killall Finder
# To make the Sublime 4 (Build 4126 at time of writing) File Open dialog boxes to also show hidden files
defaults write com.sublimetext.4 AppleShowAllFiles -bool true
@kennwhite
kennwhite / csfle_explicit_hello_world.go
Last active Sep 5, 2022
CSFLE explicit encryption golang Hello World example
View csfle_explicit_hello_world.go
/*
CSFLE explicit encryption golang Hello World example
brew install mongodb/brew/libmongocrypt
go get go.mongodb.org/mongo-driver/mongo
go get go.mongodb.org/mongo-driver/bson
go get go.mongodb.org/mongo-driver/mongo/options
go get go.mongodb.org/mongo-driver/mongo/readpref
@kennwhite
kennwhite / Dockerfile
Last active Apr 22, 2022
MongoDB .NET Alpine Dockerfile CSFLE example (MSFT's Alpine SDK image and Alpine's official image)
View Dockerfile
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine3.15
# FROM alpine:3.15
#
ENV MDB_CONN_STR="mongodb+srv://user:password@clusterX.XXX.mongodb.net/test?retryWrites=true&w=majority"
#
RUN apk update
RUN apk add git make cmake g++ libbson-static musl-dev libc-dev openssl openssl-dev py3-pip icu-dev bash nano coreutils
RUN mkdir -p /code/app
WORKDIR /code/app
@kennwhite
kennwhite / mongodb-csfle-example-kmip-or-local-key.md
Created Apr 18, 2022 — forked from pkdone/mongodb-csfle-example-kmip-or-local-key.md
MongoDB CSFLE example using mongosh with a master key sourced from KMIP or a local file
View mongodb-csfle-example-kmip-or-local-key.md

MongoDB Client-Side Field Level Encryption (CSFLE) Using KMIP or Local Master Key (with mongosh)

Assumptions

  • You have an accessible MongoDB deployment already running and accessible (self-managed or in Atlas)
  • You have the modern MongoDB Shell (mongosh) installed locally on your workstation
  • You have a KMIP Server running and accessible, if you don't intend to use a local keyfile (for an example of running and configuring a Hashicorp Vault development instance, see: Hashicorp Vault Configuration For MongoDB KMIP Use)

Configure Local Workstation Context Files

@kennwhite
kennwhite / alpine_password.sh
Last active Apr 6, 2022
Non-interactive user create & password change for Alpine Linux
View alpine_password.sh
#! /usr/bin/env sh
# Create unprivileged Alpine linux user. Run this script as root/sudo
# Don't prompt for password and make group same as username, default path & shell
adduser -D -g appuser appuser
# Set a decent random password (aiming for a 256 bit security level, but better than "monkey")
PW=$(head -c 32 /dev/urandom | base64) && echo -e "$PW\n$PW" | passwd appuser && unset PW