setup-token.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. set -e
  3. echo 'Starting token creation process...'
  4. # Attempt to create token and capture both stdout and stderr
  5. TOKEN_OUTPUT=$(/hatchet/hatchet-admin token create --config /hatchet/config --tenant-id 707d0855-80ab-4e1f-a156-f1c4546cbf52 2>&1)
  6. # Extract the token (assuming it's the only part that looks like a JWT)
  7. TOKEN=$(echo "$TOKEN_OUTPUT" | grep -Eo 'eyJ[A-Za-z0-9_-]*\.eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*')
  8. if [ -z "$TOKEN" ]; then
  9. echo 'Error: Failed to extract token. Full command output:' >&2
  10. echo "$TOKEN_OUTPUT" >&2
  11. exit 1
  12. fi
  13. echo "$TOKEN" > /tmp/hatchet_api_key
  14. echo 'Token created and saved to /tmp/hatchet_api_key'
  15. # Copy token to final destination
  16. echo -n "$TOKEN" > /hatchet_api_key/api_key.txt
  17. echo 'Token copied to /hatchet_api_key/api_key.txt'
  18. # Verify token was copied correctly
  19. if [ "$(cat /tmp/hatchet_api_key)" != "$(cat /hatchet_api_key/api_key.txt)" ]; then
  20. echo 'Error: Token copy failed, files do not match' >&2
  21. echo 'Content of /tmp/hatchet_api_key:'
  22. cat /tmp/hatchet_api_key
  23. echo 'Content of /hatchet_api_key/api_key.txt:'
  24. cat /hatchet_api_key/api_key.txt
  25. exit 1
  26. fi
  27. echo 'Hatchet API key has been saved successfully'
  28. echo 'Token length:' ${#TOKEN}
  29. echo 'Token (first 20 chars):' ${TOKEN:0:20}
  30. echo 'Token structure:' $(echo $TOKEN | awk -F. '{print NF-1}') 'parts'
  31. # Check each part of the token
  32. for i in 1 2 3; do
  33. PART=$(echo $TOKEN | cut -d. -f$i)
  34. echo 'Part' $i 'length:' ${#PART}
  35. echo 'Part' $i 'base64 check:' $(echo $PART | base64 -d >/dev/null 2>&1 && echo 'Valid' || echo 'Invalid')
  36. done
  37. # Final validation attempt
  38. if ! echo $TOKEN | awk -F. '{print $2}' | base64 -d 2>/dev/null | jq . >/dev/null 2>&1; then
  39. echo 'Warning: Token payload is not valid JSON when base64 decoded' >&2
  40. else
  41. echo 'Token payload appears to be valid JSON'
  42. fi