Skip to content

Instantly share code, notes, and snippets.

@kennwhite
kennwhite / ulimits_ubuntu_mongodb.sh
Last active December 21, 2022 23:17
Set Ubuntu 20.04 mongodb recommended ulimits
# 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 December 17, 2022 04:15
Sublime regexp for find & replace
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 December 9, 2022 04:04
Javascript/Node function to generate a random date set to UTC 00:00:00 timestamp
// 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 February 9, 2024 21:29
Setting up VS Code and Golang with debug & build flag (-tags foo) support

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 September 4, 2022 02:33
Permanently show hidden files in MacOS Monterey including Sublime Text
# 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 September 5, 2022 21:24
CSFLE explicit encryption golang Hello World example
/*
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 October 4, 2023 09:04
MongoDB .NET Alpine Dockerfile CSFLE example (MSFT's Alpine SDK image and Alpine's official image)
# 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 April 18, 2022 20:51 — 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

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 July 12, 2023 15:53
Non-interactive user create & password change for Alpine Linux
#! /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
@kennwhite
kennwhite / alpine_libmongocrypt_build.sh
Last active March 23, 2022 13:01
Simple MongoDB client-side field level encryption (libmongocrypt) demo on Alpine Linux with .NET
# WARNING: This is a demonstration only, not any kind of official build - use at your own risk
# Launched standard Alpine Linux AMI on an t2.micro instance configured w/ 8GB:
# alpine-3.15.1-x86_64-bios-cloudinit-r0 - ami-0421638898b821bff
#
# ssh -i mykey.pem alpine@[instance address]
# This demo runs as default non-root "alpine" user