Skip to content

Instantly share code, notes, and snippets.

View sebnyberg's full-sized avatar
🇸🇪

Sebastian Nyberg sebnyberg

🇸🇪
View GitHub Profile
@sebnyberg
sebnyberg / example.md
Last active March 25, 2022 15:58
Unintuitive Bash behavior for variables

Setting a local variable then running a command does not provide the variable to the child process:

$ GREETING=hi
$ python3 -c 'import os; print(os.environ.get("GREETING", "unknown"))'
unknown

However, setting a local variable in the same statement as the command does:

@sebnyberg
sebnyberg / ipow.c
Created February 6, 2022 16:21 — forked from orlp/ipow.c
int64_t ipow(int64_t base, uint8_t exp) {
static const uint8_t highest_bit_set[] = {
0, 1, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 255, // anything past 63 is a guaranteed overflow with base > 1
@sebnyberg
sebnyberg / main.go
Created January 17, 2022 15:17
concurrent_errors.go
package main
import (
"context"
"fmt"
"math/rand"
"strings"
"sync"
"time"
@sebnyberg
sebnyberg / grpc_proto_request_details.py
Created December 16, 2021 18:20
Unsafe parsing of error details (trailing metadata) from client-side in GRPC Python
def maybe_parse_details(e: grpc.RpcError) -> any:
if "_state" not in e.__dict__ or "trailing_metadata" not in e._state.__dict__:
return None
md = dict(e._state.trailing_metadata)
if "grpc-status-details-bin" not in md:
return None
val = md["grpc-status-details-bin"]
status = status_pb2.Status.FromString(val)
@sebnyberg
sebnyberg / example.cs
Created December 7, 2021 20:51
Example Function App with mTLS + GRPC
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Security.Cryptography.X509Certificates;
@sebnyberg
sebnyberg / settings.json
Created December 6, 2021 14:43
VSCode settings
{
"[markdown]": {
"editor.quickSuggestions": true
},
"[python]": {
"editor.formatOnSave": true
},
"[go]": {
"editor.suggest.snippetsPreventQuickSuggestions": true
},
@sebnyberg
sebnyberg / .vimrc
Created December 6, 2021 14:41
Dotfile
set nocompatible
filetype plugin indent on
set encoding=utf-8
set fileencoding=utf-8
scriptencoding utf-8
set history=256
set timeoutlen=250
set clipboard+=unnamed " copy to clipboard
@sebnyberg
sebnyberg / main.go
Last active October 20, 2021 13:03
Basic http server example
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
@sebnyberg
sebnyberg / example.py
Created October 6, 2021 14:34
Doccano API client using Azure KeyVault + basic auth + admin user
import sys
import os
from pprint import pprint
from azure.keyvault.secrets import SecretClient
from azure.identity import DefaultAzureCredential
import requests
api_base_url = os.environ.get("API_BASE_URL", "http://doccano.example.com")
@sebnyberg
sebnyberg / cert.sh
Created September 21, 2021 21:15
Create Kubernetes certs using openssl
#!/local/bin/env bash
#
# Create certs
#
set -eux
# Create Root CA cert
ca="rootca"
cat > $ca.conf << EOM
[ req ]