Skip to content

Instantly share code, notes, and snippets.

@hsiam261
hsiam261 / get-creds-from-imds.sh
Created September 22, 2024 16:14
get creds from imds
# Set them as needed
USER=''
IP=''
# IMDS v1
creds=$(ssh $USER@$IP -- 'curl http://169.254.169.254/latest/meta-data/iam/security-credentials/$(curl http://169.254.169.254/latest/meta-data/iam/security-credentials/)')
export AWS_ACCESS_KEY_ID=$(echo $creds | jq -r .AccessKeyId)
export AWS_SECRET_ACCESS_KEY=$(echo $creds | jq -r .SecretAccessKey)
export AWS_SESSION_TOKEN=$(echo $creds | jq -r .Token)
@hsiam261
hsiam261 / explanation.md
Last active May 29, 2024 06:21
Why does phrase_prefix_match return less hits than it should in quickwit (and also elasticsearch)?

Phrase prefix in quickwit works like how it functions in elastic search.

While easy to set up, using the match_phrase_prefix query for search autocompletion can sometimes produce confusing results.
For example, consider the query string quick brown f. This query works by creating a phrase query out of quick and brown (i.e. the term quick must exist and must be followed by the term brown). Then it looks at the sorted term dictionary to find the first 50 terms that begin with f, and adds these terms to the phrase query.
The problem is that the first 50 terms may not include the term fox so the phrase quick brown fox will not be found. This usually isn’t a problem as the user will continue to type more letters until the word they are looking for appears.

This means that while searching for "quick brown f" elastic search will first search for all words starting with the letter f and pick the first 50 words (can be overridden with max_expansions) from the alphabetical order i.e fab, fable, fabrication.

@hsiam261
hsiam261 / aws-kms-encrypt-decrypt
Last active May 28, 2025 07:04
How to encrypt and decrypt using aws kms and gcloud kms without any files only using bash
# encrypt a password with a key using bash without any files
# the fileb:// is an awscli specific term that tells the cli that the file is a binary blob file
# <(echo -n "$password") we are using process substitution so that we don't have to use files
# the -n is required in echo. Otherwise echo will append a newline at the end of the password
# output will be base64 encoded
enc_password=$(aws kms encrypt --key-id "$KEY_ID" --plaintext fileb://<(echo -n "$password") --output text --query CiphertextBlob)
# To decrypt the encrypted_password, we must first decode using base64 -d
# and then pass it to aws kms decrypt
# output will be base64 encoded and will need additional decoding
import botocore
import botocore.session
import botocore.config
import webbrowser
start_url='https://xxxxxxx.awsapps.com/start/'
region = 'us-east-1'
role_name = 'xxxxxxx'
sso_account_id = 'xxxxxxx'
@hsiam261
hsiam261 / composelist.sh
Last active November 5, 2023 19:24 — forked from mortenson/composelist.sh
List all Docker Compose projects currently running
#!/bin/bash
#--filter can filter with labels with either label=key or label=key=value queries
docker ps --filter "label=com.docker.compose.project" -q \
| xargs docker inspect --format='{{index .Config.Labels "com.docker.compose.project"}}' \
| sort | uniq
@hsiam261
hsiam261 / EC2 serial Connect
Last active November 3, 2023 17:59
EC2 Serial Connect
# EC2 Serial Connect
# Can connect to an EC2 machine with no networking
# No sg inbound rules required
# No ssh key required
# Need Nitro Powered Machines
# Just add the following bash script in user data when launching and then you can login
# IAM permission must allow user to do serial connects
# This one uses Ubuntu AMI and apt package manager
# doesn't work without the shebang :(
# find all prs that conatin commit hash
gh pr list --search $commit-hash --state merged
#first commit in a pr given a commit-hash in the pr
gh pr list --search $commit-hash --state merged | awk '{print $1}'|xargs gh pr view --json commits | jq -r '.commits[-1].oid'
# get all commits given a PR
gh pr view $PR_NUMBER --json commits