Skip to content

Commit

Permalink
pdsadmin: style nits, hopefully nothing broken
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob2161 committed Feb 21, 2024
1 parent 78da261 commit 307e235
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 43 deletions.
102 changes: 68 additions & 34 deletions pdsadmin/account.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,92 @@ set -o pipefail
PDS_ENV_FILE="/pds/pds.env"
source "${PDS_ENV_FILE}"

curl_cmd() {
# curl a URL and fail if the request fails.
function curl_cmd_get {
curl --fail --silent --show-error "$@"
}

curl_cmd_post() {
# curl a URL and fail if the request fails.
function curl_cmd_post {
curl --fail --silent --show-error --request POST --header "Content-Type: application/json" "$@"
}

curl_cmd_post_nofail() {
# curl a URL but do not fail if the request fails.
function curl_cmd_post_nofail {
curl --silent --show-error --request POST --header "Content-Type: application/json" "$@"
}

# The subcommand to run.
SUBCOMMAND="${1:-}"

#
# account list
#
if [[ "${SUBCOMMAND}" == "list" ]]; then
DIDS=$(curl_cmd \
DIDS="$(curl_cmd \
"https://${PDS_HOSTNAME}/xrpc/com.atproto.sync.listRepos?limit=100" | jq --raw-output '.repos[].did'
)
)"
OUTPUT='[{"handle":"Handle","email":"Email","did":"DID"}'
for did in $DIDS; do
ITEM=$(curl_cmd \
for did in ${DIDS}; do
ITEM="$(curl_cmd \
--user "admin:${PDS_ADMIN_PASSWORD}" \
"https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.getAccountInfo?did=$did"
)
"https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.getAccountInfo?did=${did}"
)"
OUTPUT="${OUTPUT},${ITEM}"
done
OUTPUT="${OUTPUT}]"
echo $OUTPUT | jq --raw-output '.[] | [.handle, .email, .did] | @tsv' | column -t
echo "${OUTPUT}" | jq --raw-output '.[] | [.handle, .email, .did] | @tsv' | column --table

#
# account create
#
elif [[ "${SUBCOMMAND}" == "create" ]]; then
EMAIL="${2:-}"
HANDLE="${3:-}"

if [[ "${EMAIL}" == "" ]]; then
read -p "Enter an email address (e.g. alice@${PDS_HOSTNAME}): " EMAIL
fi
if [[ "${HANDLE}" == "" ]]; then
read -p "Enter a handle (e.g. alice.${PDS_HOSTNAME}): " HANDLE
fi

if [[ "${EMAIL}" == "" || "${HANDLE}" == "" ]]; then
echo "ERROR: missing EMAIL and/or HANDLE parameters." >/dev/stderr
echo "Usage: $0 ${SUBCOMMAND} <EMAIL> <HANDLE>" >/dev/stderr
exit 1
fi

PASSWORD=$(openssl rand -base64 30 | tr -d "=+/" | cut -c1-24)
INVITE_CODE=$(curl_cmd_post \
PASSWORD="$(openssl rand -base64 30 | tr -d "=+/" | cut -c1-24)"
INVITE_CODE="$(curl_cmd_post \
--user "admin:${PDS_ADMIN_PASSWORD}" \
--data '{"useCount": 1}' \
https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode | jq --raw-output '.code'
)
RESULT=$(curl_cmd_post_nofail \
"https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode" | jq --raw-output '.code'
)"
RESULT="$(curl_cmd_post_nofail \
--data "{\"email\":\"${EMAIL}\", \"handle\":\"${HANDLE}\", \"password\":\"${PASSWORD}\", \"inviteCode\":\"${INVITE_CODE}\"}" \
https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createAccount
)
"https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createAccount"
)"

DID=$(echo $RESULT | jq --raw-output '.did')
DID="$(echo $RESULT | jq --raw-output '.did')"
if [[ "${DID}" != did:* ]]; then
ERR=$(echo $RESULT | jq --raw-output '.message')
ERR="$(echo ${RESULT} | jq --raw-output '.message')"
echo "ERROR: ${ERR}" >/dev/stderr
echo "Usage: $0 ${SUBCOMMAND} <EMAIL> <HANDLE>" >/dev/stderr
exit 1
fi

echo "Account created for ${HANDLE}.\nYour password is below, which we'll only show you once.\n"
echo "DID: ${DID}"
echo "Password: ${PASSWORD}"
echo "Account created successfully!"
echo "-----------------------------"
echo "Handle : ${HANDLE}"
echo "DID : ${DID}"
echo "Password : ${PASSWORD}"
echo "-----------------------------"
echo "Save this password, it will not be displayed again."

#
# account delete
#
elif [[ "${SUBCOMMAND}" == "delete" ]]; then
DID="${2:-}"

Expand All @@ -83,16 +109,20 @@ elif [[ "${SUBCOMMAND}" == "delete" ]]; then

echo "This action is permanent."
read -r -p "Are you sure you'd like to delete ${DID}? [y/N] " response
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
if [[ ! "${response}" =~ ^([yY][eE][sS]|[yY])$ ]]; then
exit 0
fi

curl_cmd_post \
--user "admin:${PDS_ADMIN_PASSWORD}" \
--data "{\"did\": \"${DID}\"}" \
https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.deleteAccount >/dev/null
--user "admin:${PDS_ADMIN_PASSWORD}" \
--data "{\"did\": \"${DID}\"}" \
"https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.deleteAccount" >/dev/null

echo "${DID} deleted"

#
# account takedown
#
elif [[ "${SUBCOMMAND}" == "takedown" ]]; then
DID="${2:-}"
TAKEDOWN_REF="$(date +%s)"
Expand All @@ -109,7 +139,7 @@ elif [[ "${SUBCOMMAND}" == "takedown" ]]; then
exit 1
fi

PAYLOAD=$(cat <<EOF
PAYLOAD="$(cat <<EOF
{
"subject": {
"\$type": "com.atproto.admin.defs#repoRef",
Expand All @@ -121,14 +151,18 @@ elif [[ "${SUBCOMMAND}" == "takedown" ]]; then
}
}
EOF
)
)"

curl_cmd_post \
--user "admin:${PDS_ADMIN_PASSWORD}" \
--data "${PAYLOAD}" \
https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.updateSubjectStatus >/dev/null
--user "admin:${PDS_ADMIN_PASSWORD}" \
--data "${PAYLOAD}" \
"https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.updateSubjectStatus" >/dev/null

echo "${DID} taken down"

#
# account untakedown
#
elif [[ "${SUBCOMMAND}" == "untakedown" ]]; then
DID="${2:-}"

Expand Down Expand Up @@ -158,9 +192,9 @@ EOF
)

curl_cmd_post \
--user "admin:${PDS_ADMIN_PASSWORD}" \
--data "${PAYLOAD}" \
https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.updateSubjectStatus >/dev/null
--user "admin:${PDS_ADMIN_PASSWORD}" \
--data "${PAYLOAD}" \
"https://${PDS_HOSTNAME}/xrpc/com.atproto.admin.updateSubjectStatus" >/dev/null

echo "${DID} untaken down"
else
Expand Down
2 changes: 1 addition & 1 deletion pdsadmin/create-invite-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ curl \
--user "admin:${PDS_ADMIN_PASSWORD}" \
--header "Content-Type: application/json" \
--data '{"useCount": 1}' \
https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode | jq --raw-output '.code'
"https://${PDS_HOSTNAME}/xrpc/com.atproto.server.createInviteCode" | jq --raw-output '.code'
2 changes: 1 addition & 1 deletion pdsadmin/request-crawl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ for host in ${RELAY_HOSTS//,/ }; do
--user "admin:${PDS_ADMIN_PASSWORD}" \
--header "Content-Type: application/json" \
--data "{\"hostname\": \"${PDS_HOSTNAME}\"}" \
https://${host}/xrpc/com.atproto.sync.requestCrawl >/dev/null
"https://${host}/xrpc/com.atproto.sync.requestCrawl" >/dev/null
done

echo "done"
14 changes: 7 additions & 7 deletions pdsadmin/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ COMPOSE_TEMP_FILE="${COMPOSE_FILE}.tmp"

echo "* Downloading PDS compose file"
curl \
--silent \
--show-error \
--fail \
--output "${COMPOSE_TEMP_FILE}" \
"${COMPOSE_URL}"
--silent \
--show-error \
--fail \
--output "${COMPOSE_TEMP_FILE}" \
"${COMPOSE_URL}"

if cmp -s "${COMPOSE_FILE}" "${COMPOSE_TEMP_FILE}"; then
if cmp --quiet "${COMPOSE_FILE}" "${COMPOSE_TEMP_FILE}"; then
echo "PDS is already up to date"
rm "${COMPOSE_TEMP_FILE}"
rm --force "${COMPOSE_TEMP_FILE}"
exit 0
fi

Expand Down

0 comments on commit 307e235

Please sign in to comment.