~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
76 lines · 5.9 KB Raw History
#!/usr/bin/env bash
# On the staging VM, as stg: the vouches prod has, so staging runs prod's market policies (never `open`, plan 0047).
#   trust.sh accounts <run>   before the bidders join: the throwaway accounts (accounts.json, creds-<account>), and
#                             `DIDS <did>,<did>`, which the bidders take as `run bidder --bidder-vouch` (each bidder vouches them);
#                             the names and roles are accounts.sh's (README.md "Accounts"): the drivers (alice1 to alice4);
#                             the gateway's operator (alice5; gateway.sh, plan 0040 step 8d): the account the gateway's
#                             localRepo requester is the agent of, as prod's is alice's: the bidders vouch it and it
#                             vouches them, as the drivers; not an account of the target; the ssh gateway's
#                             operator (bob1; step 8e, --ssh-operator-did): not in DIDS, not an account of the target
#   trust.sh vouch            once the bidders joined (bidders.json): each account vouches every bidder and its operator
#                             (applyRequesterVouches' record), so an RFP's requester-tangled-vouch admits them; the operator
#                             vouches the drivers instead, so the ssh gateway's sign-in (bidder-tangled-vouch, self the
#                             operator) admits them
#   trust.sh associate <did>  once the gateway's requester has its DID (gateway.sh up): the gateway account (alice5), its
#                             operator, writes the ident.link pair's operator side at rkey <did>: the grant
#                             (com.publicdomainrelay.temp.market.association) and the endorsement (link.ident.endorsement),
#                             what scripts/associate-bidder.ts writes from alice on prod
set -euo pipefail
. "$(dirname "$0")/accounts.sh"
S=${SWC:-/home/stg/stg/socialweb-computer-linux-amd64} ST=${SWC_STATE:-/home/stg/stg/s} I=${SWC_INSTANCE:-stg} C=${SWC_CREDS:-/home/stg}
D=${SWC_DIR:-/home/stg/stg}
st=$($S dev status --instance "$I" --state-dir "$ST" 2>/dev/null)
pds=$(jq -r '.network.status.bidderPdsUrl' <<<"$st") plc=$(jq -r '.network.status.network.plcDirectoryUrl' <<<"$st")
case "${1:-}" in
  accounts)
    run=$2 accounts='{}'
    for a in $STG_DRIVERS $STG_GATEWAY $STG_OPERATOR; do
      h="$a-${run: -4}$(date +%s | tail -c 5).bob.test" pw=$(head -c 24 /dev/urandom | base64 | tr -dc A-Za-z0-9) c=$C/creds-$a
      did=$(curl -fsS -H 'content-type: application/json' "$pds/xrpc/com.atproto.server.createAccount" \
        -d "$(jq -nc --arg h "$h" --arg p "$pw" '{handle: $h, password: $p, email: ($h + "@staging.invalid")}')" | jq -r .did)
      P=$(tr a-z A-Z <<<"$a")
      (umask 077 && printf 'ATPROTO_PASSWORD=%s\n%s_ATPROTO_HANDLE=%s\n%s_ATPROTO_PASSWORD=%s\n' "$pw" "$P" "$h" "$P" "$pw" > "$c")
      accounts=$(jq -c --arg a "$a" --arg h "$h" --arg d "$did" --arg c "$c" --arg p "$P" '.[$a] = {handle: $h, did: $d, creds: $c, oauthAccount: $p, throwaway: true}' <<<"$accounts")
    done
    (umask 077 && echo "$accounts" > "$D/accounts.json")
    echo "DIDS $(jq -r --arg o "$STG_OPERATOR" '[to_entries[] | select(.key != $o) | .value.did] | join(",")' <<<"$accounts")"
    ;;
  vouch)
    now=$(date -u +%Y-%m-%dT%H:%M:%SZ) n=0
    for a in $(jq -r 'keys[]' "$D/accounts.json"); do
      h=$(jq -r --arg a "$a" '.[$a].handle' "$D/accounts.json") did=$(jq -r --arg a "$a" '.[$a].did' "$D/accounts.json")
      jwt=$(curl -fsS -H 'content-type: application/json' "$pds/xrpc/com.atproto.server.createSession" \
        -d "$(jq -nc --arg h "$h" --arg p "$(sed -n 's/^ATPROTO_PASSWORD=//p' "$(jq -r --arg a "$a" '.[$a].creds' "$D/accounts.json")")" '{identifier: $h, password: $p}')" | jq -r .accessJwt)
      if [ "$a" = "$STG_OPERATOR" ]; then
        subjects=$(jq -r --arg d "$STG_DRIVERS" "$STG_PICK_DRIVERS | .[].did" "$D/accounts.json")
      else
        subjects=
        for b in $(jq -r '.[].did' "$D/bidders.json"); do
          op=$(curl -fsS "$plc/$b" | jq -r '[(.alsoKnownAs // [])[] | select(startswith("https://ident.link/agent-of/"))][0] // "" | ltrimstr("https://ident.link/agent-of/")')
          subjects="$subjects $b $op"
        done
      fi
      for subject in $subjects; do
        curl -fsS -o /dev/null -H "Authorization: Bearer $jwt" -H 'content-type: application/json' "$pds/xrpc/com.atproto.repo.putRecord" \
          -d "$(jq -nc --arg r "$did" --arg s "$subject" --arg t "$now" '{repo: $r, collection: "sh.tangled.graph.vouch", rkey: $s, record: {"$type": "sh.tangled.graph.vouch", subject: $s, createdAt: $t}}')"
        n=$((n + 1))
      done
    done
    echo "trust.sh: account vouches $n"
    ;;
  associate)
    agent=${2:-}
    [ "${agent#did:}" != "$agent" ] || { echo "usage: trust.sh associate <did>" >&2; exit 2; }
    h=$(jq -r --arg a "$STG_GATEWAY" '.[$a].handle' "$D/accounts.json") did=$(jq -r --arg a "$STG_GATEWAY" '.[$a].did' "$D/accounts.json")
    jwt=$(curl -fsS -H 'content-type: application/json' "$pds/xrpc/com.atproto.server.createSession" \
      -d "$(jq -nc --arg h "$h" --arg p "$(sed -n 's/^ATPROTO_PASSWORD=//p' "$(jq -r --arg a "$STG_GATEWAY" '.[$a].creds' "$D/accounts.json")")" '{identifier: $h, password: $p}')" | jq -r .accessJwt)
    now=$(date -u +%Y-%m-%dT%H:%M:%SZ)
    for record in "$(jq -nc --arg a "$agent" --arg t "$now" '{"$type": "com.publicdomainrelay.temp.market.association", agent: $a, createdAt: $t}')" \
      "$(jq -nc --arg a "$agent" '{"$type": "link.ident.endorsement", agent: $a}')"; do
      curl -fsS -o /dev/null -H "Authorization: Bearer $jwt" -H 'content-type: application/json' "$pds/xrpc/com.atproto.repo.putRecord" \
        -d "$(jq -nc --arg r "$did" --arg a "$agent" --argjson v "$record" '{repo: $r, collection: $v["$type"], rkey: $a, record: $v}')"
    done
    echo "trust.sh: $STG_GATEWAY ($did) endorses and grants its agent $agent"
    ;;
  *) echo "usage: trust.sh accounts <run> | vouch | associate <did>" >&2; exit 2 ;;
esac