Files
query-orchestration/cmd/auth_related/user.creation.tool/list.users.by.env.sh
T
Jay Brown c668485e6f Merged in feature/cognito-shared-environments (pull request #211)
cognito and permit shared prod environment support

* code complete

* user creattion tool

test harness
2026-02-26 12:33:35 +00:00

122 lines
4.1 KiB
Bash
Executable File

#!/bin/sh
# list.users.by.env.sh - Lists Cognito users filtered by custom:environment_id attribute.
#
# Usage: ./list.users.by.env.sh <user-pool-id> [environment-id]
#
# Arguments:
# user-pool-id - AWS Cognito User Pool ID (required)
# environment-id - Environment ID to filter by (optional, falls back to COGNITO_ENVIRONMENT_ID env var)
#
# If no environment-id is provided (via argument or env var), lists all users with their
# environment_id attribute value.
#
# Requires: aws cli, jq
#
# Examples:
# ./list.users.by.env.sh us-east-2_abc123 acmehealth
# COGNITO_ENVIRONMENT_ID=acmehealth ./list.users.by.env.sh us-east-2_abc123
# ./list.users.by.env.sh us-east-2_abc123 # lists all users with env info
set -e
USER_POOL_ID="$1"
ENV_ID="${2:-$COGNITO_ENVIRONMENT_ID}"
if [ -z "$USER_POOL_ID" ]; then
echo "Usage: $0 <user-pool-id> [environment-id]" >&2
echo "" >&2
echo "Arguments:" >&2
echo " user-pool-id AWS Cognito User Pool ID (required)" >&2
echo " environment-id Environment ID to filter by (optional)" >&2
echo "" >&2
echo "Falls back to COGNITO_ENVIRONMENT_ID env var if environment-id not provided." >&2
echo "If neither is set, lists all users with their environment_id value." >&2
exit 1
fi
# Check for required tools
if ! command -v aws >/dev/null 2>&1; then
echo "Error: aws cli is required but not installed." >&2
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq is required but not installed." >&2
exit 1
fi
# Fetch all users with pagination
ALL_USERS="[]"
PAGINATION_TOKEN=""
while true; do
if [ -z "$PAGINATION_TOKEN" ]; then
RESPONSE=$(aws cognito-idp list-users \
--user-pool-id "$USER_POOL_ID" \
--max-results 60 \
--output json 2>&1)
else
RESPONSE=$(aws cognito-idp list-users \
--user-pool-id "$USER_POOL_ID" \
--max-results 60 \
--pagination-token "$PAGINATION_TOKEN" \
--output json 2>&1)
fi
if [ $? -ne 0 ]; then
echo "Error fetching users: $RESPONSE" >&2
exit 1
fi
PAGE_USERS=$(echo "$RESPONSE" | jq '.Users')
ALL_USERS=$(echo "$ALL_USERS $PAGE_USERS" | jq -s '.[0] + .[1]')
PAGINATION_TOKEN=$(echo "$RESPONSE" | jq -r '.PaginationToken // empty')
if [ -z "$PAGINATION_TOKEN" ]; then
break
fi
done
TOTAL=$(echo "$ALL_USERS" | jq 'length')
if [ -n "$ENV_ID" ]; then
# Filter: show users matching this environment_id OR users with no environment_id (untagged)
FILTERED=$(echo "$ALL_USERS" | jq --arg env "$ENV_ID" '
[.[] | select(
(.Attributes // [] | map(select(.Name == "custom:environment_id")) | length == 0) or
(.Attributes // [] | map(select(.Name == "custom:environment_id" and .Value == $env)) | length > 0)
)]
')
FILTERED_COUNT=$(echo "$FILTERED" | jq 'length')
echo "Users for environment: $ENV_ID"
echo "Showing $FILTERED_COUNT of $TOTAL total users (matching + untagged)"
echo "============================================="
echo ""
echo "$FILTERED" | jq -r '
.[] |
"Email: " + (.Attributes // [] | map(select(.Name == "email")) | .[0].Value // "N/A") +
"\nUsername: " + .Username +
"\nStatus: " + .UserStatus +
"\nEnabled: " + (.Enabled | tostring) +
"\nEnvironment: " + (.Attributes // [] | map(select(.Name == "custom:environment_id")) | .[0].Value // "(untagged)") +
"\n---"
'
else
# No filter: list all users with their environment_id
echo "All users (no environment filter)"
echo "Total: $TOTAL users"
echo "============================================="
echo ""
echo "$ALL_USERS" | jq -r '
.[] |
"Email: " + (.Attributes // [] | map(select(.Name == "email")) | .[0].Value // "N/A") +
"\nUsername: " + .Username +
"\nStatus: " + .UserStatus +
"\nEnabled: " + (.Enabled | tostring) +
"\nEnvironment: " + (.Attributes // [] | map(select(.Name == "custom:environment_id")) | .[0].Value // "(untagged)") +
"\n---"
'
fi