From 60eb7e1dc94d2abcf0f3767272d324b4e71e1c43 Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Thu, 14 Nov 2024 11:50:42 -0600 Subject: [PATCH] admin registration script Signed-off-by: eternal-flame-AD --- yume-mods/admin-scripts/.gitignore | 1 + yume-mods/admin-scripts/batch_register.fish | 79 +++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 yume-mods/admin-scripts/.gitignore create mode 100755 yume-mods/admin-scripts/batch_register.fish diff --git a/yume-mods/admin-scripts/.gitignore b/yume-mods/admin-scripts/.gitignore new file mode 100644 index 0000000000..956d4725fc --- /dev/null +++ b/yume-mods/admin-scripts/.gitignore @@ -0,0 +1 @@ +/secrets diff --git a/yume-mods/admin-scripts/batch_register.fish b/yume-mods/admin-scripts/batch_register.fish new file mode 100755 index 0000000000..1112fa9d7a --- /dev/null +++ b/yume-mods/admin-scripts/batch_register.fish @@ -0,0 +1,79 @@ +#!/usr/bin/env fish + +argparse --name batch_register 'n/number=' 'h/host=' 'm/mount=' -- $argv + +set -l dir_name (dirname (realpath (status -f))) + +echo "Running in $dir_name" + +if [ -f $dir_name/.env ] + source $dir_name/.env +end + +if [ -f $dir_name/secrets/.env ] + source $dir_name/secrets/.env +end + +if ! set -q MISSKEY_TOKEN + echo "Please set MISSKEY_TOKEN in .env file." + exit 1 +end + +if ! set -q VAULT_MOUNT + echo "Please set VAULT_MOUNT in .env file." + exit 1 +end + +if set -q _flag_host + set HOST $_flag_host +else + set HOST "https://test0.mi.yumechi.jp" +end + +if set -q _flag_number + set NUMBER $_flag_number +else + set NUMBER 1 +end + +if ! set -q ACCOUNT_PREFIX + set ACCOUNT_PREFIX test_automated_ +end + +for i in (seq 1 $NUMBER) + set -l USERNAME "$ACCOUNT_PREFIX"(openssl rand 8 | base64 | tr -d /+=) + set -l PASSWORD (openssl rand 16 | base64) + + echo "[$i/$NUMBER]" \ + "Registering $USERNAME" + + echo "$PASSWORD" | vault kv put -mount=$VAULT_MOUNT $USERNAME username=$USERNAME password=- + or begin + echo "Failed to save password for $USERNAME" + exit 3 + end + + set -l start (date +%s%N) + jq -nr --arg username $USERNAME --arg password $PASSWORD '{ + username: $username, + password: $password + }' | curl -f --tlsv1.2 -d@- \ + -H "Accept: application/json" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $MISSKEY_TOKEN" \ + "$HOST/api/admin/accounts/create" | \ + jq --arg password $PASSWORD \ + '{"id": .id, "username": .username, "password": $password, "success": true, "token": .token}' | \ + tee /dev/stderr | \ + vault kv put -mount=$VAULT_MOUNT $USERNAME @/dev/stdin + or begin + echo "Failed to register $USERNAME" + vault kv destroy -mount=$VAULT_MOUNT -versions=1 $USERNAME + exit 2 + end + + echo "[$i/$NUMBER]" \ + "Registered $USERNAME" (math \((date +%s%N) - $start\) / 1000000) ms +end + +echo "Done."