Skip to content

Instantly share code, notes, and snippets.

@sphr2k
sphr2k / secretmanager.tf
Created September 19, 2025 10:42
GCP Secret Manager write-only secrets with automatic versioning
locals {
secrets = yamldecode(file("${path.module}/secrets/secrets-staging.plain.yml"))
# Derive numeric versions from the first 15 characters of the SHA-256 hash of the secret values
# to trigger replacement when the secret value changes.
secret_data_wo_versions = {
for key, value in local.secrets : key =>
parseint(substr(sha256(tostring(value)), 0, 15), 16)
}
@sphr2k
sphr2k / gcpsqlh.fish
Last active January 8, 2025 17:20
GCP Cloud SQL Proxy Helper
function gcpsqlh --description "Run cloud-sql-proxy with current GCP project, optionally impersonating a service account"
# Get the current GCP project
set -l current_project (gcloud config get-value project)
if test -z "$current_project"
echo "No GCP project is currently set. Please set a project using 'gcloud config set project PROJECT_ID'"
return 1
end
# Get the instance connection name
@sphr2k
sphr2k / sualio-tag-factory-reset.md
Last active December 12, 2024 12:46
Sualio Tag Factory Reset

Sualio Tag Factory Reset

Press the function button four times rapidly and then hold it a fifth time until you hear a ringing chime.

Sualio Tag
@sphr2k
sphr2k / bitbucket-pipelines.yml
Created November 8, 2024 13:29
Bitbucket & Google Cloud Workload ID Federation
---
image: google/cloud-sdk:latest
steps:
- step: &test-id-fed
name: Test
services:
- docker
oidc: true
script:
@sphr2k
sphr2k / gcp-metrics-cli.py
Created October 15, 2024 10:02
GCP Metrics CLI
import argparse
import datetime
import logging
from google.api import metric_pb2
from google.cloud import monitoring_v3
from google.protobuf import timestamp_pb2
import google.auth
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
@sphr2k
sphr2k / gcp-prom-proxy.py
Last active October 11, 2024 11:57
GCP Prometheus Auth Proxy
from flask import Flask, request, Response
from flask_cors import CORS
import requests
import json
import subprocess
import google.auth
import google.auth.exceptions
from google.auth.transport.requests import Request
import logging
@sphr2k
sphr2k / gcp-backend-logging.py
Last active July 19, 2024 10:10
Google Cloud Backend Service Logging Manager
from google.cloud import compute_v1
import google.auth
from dataclasses import dataclass
from typing import List
import questionary
from questionary import Choice
@dataclass
class LogConfig:
@sphr2k
sphr2k / iap.sh
Created June 20, 2024 16:36
Access Google IAP-protected services using service account impersonation
OAUTH_CLIENT_ID=1234567890-j9onig1ofcgle7iogv8fceu04v8hriuv.apps.googleusercontent.com
[email protected]
URL=https://your-iap-protected-endpoint
ID_TOKEN=$(
gcloud auth print-identity-token \
--audiences $OAUTH_CLIENT_ID \
--include-email \
--impersonate-service-account $AUTHORIZED_SA
)
@sphr2k
sphr2k / gcpswitch.fish
Created June 20, 2024 11:15
gcpswitch: Switch GCP project (Fish)
function gcpswitch -d "Switch GCP project"
# Retrieve all GCP projects in JSON format
set -l projects_json (gcloud projects list --format=json)
# Parse JSON to extract project IDs
set -l projects (echo $projects_json | jq -r '.[].projectId')
# Define the regex patterns to filter projects
set -l patterns "hadv-" "hadvimpexp-" "alpen-" "alen-"
@sphr2k
sphr2k / link-extractor.user.js
Created May 26, 2024 12:43
Link Extractor User Script