generate_access_token.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. # Copyright 2020 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script generates access tokens that are needed to make admin API
  16. # calls to the Firebase Console.
  17. #
  18. # The script takes three arguments:
  19. # - GHA_SECRET: The password for decrypting GitHub secrets.
  20. # - SERVICE_ACCOUNT: The path to the encrypted service account secret.
  21. # - OUTPUT: The path to file where generated access token will be stored.
  22. #
  23. # This script uses Google's Swift Auth Client Library.
  24. # - https://github.com/googleapis/google-auth-library-swift
  25. #
  26. # Generated tokens are `JSON` in the form:
  27. # {
  28. # "token_type":"Bearer",
  29. # "expires_in":3599,
  30. # "access_token":"1234567890ABCDEFG"
  31. # }
  32. GHA_SECRET="$1" # Pass in `local_dev` if the SERVICE_ACCOUNT does not to be decrypted.
  33. SERVICE_ACCOUNT="$2"
  34. OUTPUT="$3"
  35. echo "GHA_SECRET: ***"
  36. echo "SERVICE_ACCOUNT: ${SERVICE_ACCOUNT}"
  37. echo "OUTPUT: ${OUTPUT}"
  38. if [[ ! -f $SERVICE_ACCOUNT ]]; then
  39. echo ERROR: Cannot find encrypted secret at $SERVICE_ACCOUNT, aborting.
  40. exit 1
  41. fi
  42. if [[ ! -f $OUTPUT ]]; then
  43. echo ERROR: Cannot find $OUTPUT, aborting.
  44. exit 1
  45. fi
  46. # The access token is generated using a downloaded Service Account JSON file from a
  47. # Firebase Project. This can be downloaded from Firebase console under 'Project Settings'.
  48. #
  49. # The following stores the decrypted service account JSON file in `$HOME/.credentials/` and points
  50. # the GOOGLE_APPLICATION_CREDENTIALS env var to it.
  51. SERVICE_ACCOUNT_FILE=$(basename $SERVICE_ACCOUNT .gpg)
  52. echo "Creating ~/.credentials/ directory"
  53. mkdir -p ~/.credentials/
  54. if [[ $GHA_SECRET == "local_dev" ]]; then
  55. echo "Local Development Mode"
  56. echo "Copying ${SERVICE_ACCOUNT_FILE} to ~/.credentials/"
  57. cp $SERVICE_ACCOUNT ~/.credentials/
  58. else
  59. echo "Decrypting ${SERVICE_ACCOUNT_FILE}.gpg"
  60. scripts/decrypt_GHA_SECRET.sh $SERVICE_ACCOUNT ~/.credentials/$SERVICE_ACCOUNT_FILE "$plist_secret"
  61. fi
  62. echo "::set-env name=GOOGLE_APPLICATION_CREDENTIALS::${HOME}/.credentials/${SERVICE_ACCOUNT_FILE}"
  63. export GOOGLE_APPLICATION_CREDENTIALS="${HOME}/.credentials/${SERVICE_ACCOUNT_FILE}"
  64. # Clone Google's Swift Auth Client Library and use it to generate a token.
  65. # The generated token is piped to the specified OUTPUT file.
  66. git clone https://github.com/googleapis/google-auth-library-swift.git
  67. cd google-auth-library-swift
  68. make -f Makefile
  69. # Prepend OUTPUT path with ../ since we cd'd into `google-auth-library-swift`.
  70. swift run TokenSource > ../$OUTPUT
  71. # Remove cloned Swift Auth Client Library.
  72. cd ..
  73. rm -rf google-auth-library-swift
  74. if grep -q "access_token" $OUTPUT; then
  75. echo "Token successfully generated and placed at ${OUTPUT}"
  76. else
  77. echo "ERROR: $(cat $OUTPUT)"
  78. exit 1
  79. fi