Skip to content

Instantly share code, notes, and snippets.

View DinoChiesa's full-sized avatar

Dino Chiesa DinoChiesa

View GitHub Profile
@DinoChiesa
DinoChiesa / weightedRandomSelector2.js
Created December 2, 2021 21:15
perform a weighted random selection in JS
// weightedRandomSelector.js
//
// This is a simple weighted random selector, works in Rhino, can use within an
// Apigee JavaScript step.
//
// last saved: <2021-December-02 13:11:16>
//
// --------------------------------------------------------
(function (){
@DinoChiesa
DinoChiesa / convert.Java
Created October 8, 2021 17:16
Java: convert between ASN.1 and P1363 Encoding of Signature
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
import java.math.BigInteger;
private static byte[] toP1363(byte[] asn1EncodedSignature) throws Exception {
ASN1Sequence seq = ASN1Sequence.getInstance(asn1EncodedSignature);
BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getValue();
BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getValue();
@DinoChiesa
DinoChiesa / json-Stringify.js
Created September 29, 2021 19:45
diagnose JSON.stringify failure
/* jshint esversion:6, node:false, strict:implied */
/* global print, context */
function createPartialCopy(obj, partialPropertyList) {
// create new "partial copy" with only a subset of properties
return partialPropertyList.reduce(function(a, prop) {
a[prop] = obj[prop];
return a;
}, {});
}
@DinoChiesa
DinoChiesa / showcertssubjectandissuer
Created August 13, 2021 19:20
bash script, depends on openssl and perl, to show cert chain subject and issuer for a host. Runs on OS X
#!/bin/bash
# uncomment to debug
# set -x
usage() {
printf "%s: display subject and issuer of certificates in the chain that a host presents\n" $0
printf "usage:\n"
printf " %s HOSTNAME\n" $0
printf " show cert subject and issuer\n\n"
@DinoChiesa
DinoChiesa / JS-ShredJSON.xml
Last active June 10, 2021 17:15
Shred JSON into context variables
<Javascript name='JS-ShredJSON' timeLimit='200' >
<Properties>
<Property name='output-prefix'>private</Property>
<Property name='source'>ContextVariableContainingJSON</Property>
</Properties>
<ResourceURL>jsc://extractJsonToContextVars.js</ResourceURL>
</Javascript>
@DinoChiesa
DinoChiesa / getTokenWithServiceAccount.js
Last active April 16, 2021 00:18
get a GCP token using a Service Account .json file
// getTokenWithServiceAccount.js
// ------------------------------------------------------------------
// uses only builtin modules, no external dependencies.
/* jshint esversion:9, node:true, strict:implied */
/* global process, console, Buffer */
const crypto = require('crypto'),
util = require('util'),
// invokeCallsWithLimit.js
// ------------------------------------------------------------------
//
/* jshint esversion:6, node:false, strict:implied */
/* global properties, httpClient, context, Request */
// Specify the limit of concurrent outbound requests
const ASYNC_LIMIT = 3;
/* tweak-expiry-display.js */
/* jshint esversion:9, browser:true, strict:implied */
/* global window, navigator, console, fetch, Buffer */
/*
Instructions
Add this as a "custom script" in the "Settings" dialog for the portal.
Be sure to bracket this source with open-and-close script tags.
@DinoChiesa
DinoChiesa / 0-gcloud commands
Last active January 28, 2021 00:32
gcloud commands for interacting with Apigee, and also nodejs code to sign a JWT, for use in a request-for-token sent to Google APIs
(First create a service account and create+download a keyfile for it)
KEY_FILE=~/Downloads/name-of-the-downloaded-keyfile.json
(download and install gcloud if you don't have it)
gcloud components update
gcloud auth list
// weightedRandomSelector.js
//
// simple weighted random selector.
//
// Copyright © 2013, 2014 Dino Chiesa and Apigee Corp
// All rights reserved.
//
// created: Fri, 26 Jul 2013 11:14
// last saved: <2019-June-24 06:13:20>
//