From 4343fa831fed5b1bfeaa0d9824db99c78a5e261d Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Thu, 23 Apr 2020 11:47:45 -0700 Subject: [PATCH] [chore] Release 3.12.1 (#364) * chore: Removed Travis CI integration (#326) * chore: Added Actions-based release workflow (#331) * chore: Added Actions-based release workflow * Set GOPATH * Fixed working directory for tests * Decrypting credentials into the testdata directory * Added preflight and post check scripts * chore: Running CI workflow on pull_request (#338) * fix(fcm): correct the iidEndpoint endpoints used for topic management (#335) According to the document https://developers.google.com/instance-id/reference/server, the endpoints should be: https://iid.googleapis.com/iid/v1:batchAdd https://iid.googleapis.com/iid/v1:batchRemove NOT: https://iid.googleapis.com/iid/v1/:batchAdd https://iid.googleapis.com/iid/v1/:batchRemove * fix(fcm): Fix documents of FCM batch request limit (#347) Co-authored-by: Hiranya Jayathilaka * fix: Deferring credential loading until required (#361) * Bumped version to 3.12.1 (#363) * chore: Specifying correct working directory for staging command (#365) Co-authored-by: Rueian Co-authored-by: 178inaba <178inaba.git@gmail.com> --- .../resources/integ-service-account.json.gpg | Bin 0 -> 1730 bytes .github/scripts/generate_changelog.sh | 79 +++++++++++++ .github/scripts/publish_post_check.sh | 105 ++++++++++++++++++ .github/scripts/publish_preflight_check.sh | 97 ++++++++++++++++ .github/scripts/run_all_tests.sh | 25 +++++ .github/workflows/ci.yml | 8 +- .github/workflows/publish.yml | 64 +++++++++++ .github/workflows/stage.yml | 75 +++++++++++++ .travis.gofmt.sh | 6 - .travis.yml | 27 ----- README.md | 4 +- auth/auth.go | 8 +- auth/auth_test.go | 63 ++++++++++- firebase.go | 41 ++++--- firebase_test.go | 41 ++----- internal/internal.go | 2 - messaging/messaging_batch.go | 10 +- messaging/topic_mgt.go | 6 +- messaging/topic_mgt_test.go | 8 +- 19 files changed, 558 insertions(+), 111 deletions(-) create mode 100644 .github/resources/integ-service-account.json.gpg create mode 100755 .github/scripts/generate_changelog.sh create mode 100755 .github/scripts/publish_post_check.sh create mode 100755 .github/scripts/publish_preflight_check.sh create mode 100755 .github/scripts/run_all_tests.sh create mode 100644 .github/workflows/publish.yml create mode 100644 .github/workflows/stage.yml delete mode 100755 .travis.gofmt.sh delete mode 100644 .travis.yml diff --git a/.github/resources/integ-service-account.json.gpg b/.github/resources/integ-service-account.json.gpg new file mode 100644 index 0000000000000000000000000000000000000000..145389e0e839675ef92e2712638f3d39dc030fb0 GIT binary patch literal 1730 zcmV;z20i(V4Fm}T0?6uq1v?nDCI8au0gw$8IMvg~=Xcz9UM=*NZ5RCsFH>eO?p z`a^EFF$25U3A=Oe?N{hlAYsHIn1<-8dZs!_SjOD zNc0><*irbMG5XMAmkxEg7Vq=0eMf__RhX2*c>b%81@)u`1gZMPQ zGKW`slEt)@cq-#;Eka6FiLO2c17RPvwoe_$O7(KK55Ff^Y`gYK`IqN1^#GzsAzAJ= z2iDr$?bAYi^{Gd`W)N9aE82E}*GEJlD6Ne!1RN^h|GY+|I?_TzBrmGNd3k|BCt=0+ zlq$1BYyQT)E`HaX^j&P|2@!^L|L&AB=Lf3#hL**Mv%2jw}VoJ1h*g?JA6|73=1pI1!mf43=2 zb%CDko(ScSX&(QBwwJ#&0r2Z3yYpYQ#-@G3e0UUcdCF!@XBNWXu60L$0p+B zKGg9onju-)=jcEDq)n52gLbLRwjRV214Vkq>K)yt_%R!86j(%WjUkm}Z zkld=xUJ}5pRVi0DDM65R?RzmS0v4PdgPj2M!b?qkZeH2kblF4TE5-> z+wx^D+a8D9wMjvx4~3%w==`~DNy)e&VRq>gnoi~d3iv0n9CQwklD^ngCt&HclPN;sYPq-!;(NBWa-ANlB1|rWZQxe7e9&%C!t66F|w!x@^9{lBd zm@=95Fb+n~I{Gu3ck*o*973|Z*l`yJ&93|4`QmqDBP}U6XB)cr6-2&7LXU_`bGFyk z$Y)%4T^<`}!M7))ymrZhN^Q57bN)#fV)M1RDq1%H?Fta$qSF9MyS!B5`8nVdJ51rQPEAZ^O?zsM}O zPhp<9H7#qk*1~nY?Kwyy63(aXTuneshSsSuy&}{cmv3>jcURN+wzjMPjZ|=n$dPnu z3pt(#iEFy1Xy0d@+7OFlqfp{;Fb4x(T2gG= zxu=dWI=75#Cn`d2=HA$qYRp{goK~aRQ`g;(YjSYZc(^(j8n&X~78N1#$D!&gS(uVy zTk&c$0>XvD4+B$~g!xb<3yDfSCiPguMZ9Q}3jwv`{5kx)_{JLg1FHR6&o*QSe6RzF z4GnVbVJgjce+fXr>&#Wqkv)y#R2IFNldAdS%NJm;I{0nCTqEAhTe=%Tt z<4JG}+xXl(>w*ZIV`zr}LJNUbi)+LMRpFZE@Qk0%|Ag_xvgt)NC z%K0pcz3{!a01i+|RU%UUOua$O*?k5)SMr^Ww2)NKENK7-4bml?WYi~(Wm#GVOSk*k z?|?mdMfkR3z-u?0Ep@}-*86k*+PM7w71x@zPV)}3(x|hmy#3B z;;&>t_G6>4eDTRLb+rsozx@J<8Y`Etwhw&i0PZz#$84|6_7N*%xz*%dvH$=8 literal 0 HcmV?d00001 diff --git a/.github/scripts/generate_changelog.sh b/.github/scripts/generate_changelog.sh new file mode 100755 index 00000000..e393f40e --- /dev/null +++ b/.github/scripts/generate_changelog.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e +set -u + +function printChangelog() { + local TITLE=$1 + shift + # Skip the sentinel value. + local ENTRIES=("${@:2}") + if [ ${#ENTRIES[@]} -ne 0 ]; then + echo "### ${TITLE}" + echo "" + for ((i = 0; i < ${#ENTRIES[@]}; i++)) + do + echo "* ${ENTRIES[$i]}" + done + echo "" + fi +} + +if [[ -z "${GITHUB_SHA}" ]]; then + GITHUB_SHA="HEAD" +fi + +LAST_TAG=`git describe --tags $(git rev-list --tags --max-count=1) 2> /dev/null` || true +if [[ -z "${LAST_TAG}" ]]; then + echo "[INFO] No tags found. Including all commits up to ${GITHUB_SHA}." + VERSION_RANGE="${GITHUB_SHA}" +else + echo "[INFO] Last release tag: ${LAST_TAG}." + COMMIT_SHA=`git show-ref -s ${LAST_TAG}` + echo "[INFO] Last release commit: ${COMMIT_SHA}." + VERSION_RANGE="${COMMIT_SHA}..${GITHUB_SHA}" + echo "[INFO] Including all commits in the range ${VERSION_RANGE}." +fi + +echo "" + +# Older versions of Bash (< 4.4) treat empty arrays as unbound variables, which triggers +# errors when referencing them. Therefore we initialize each of these arrays with an empty +# sentinel value, and later skip them. +CHANGES=("") +FIXES=("") +FEATS=("") +MISC=("") + +while read -r line +do + COMMIT_MSG=`echo ${line} | cut -d ' ' -f 2-` + if [[ $COMMIT_MSG =~ ^change(\(.*\))?: ]]; then + CHANGES+=("$COMMIT_MSG") + elif [[ $COMMIT_MSG =~ ^fix(\(.*\))?: ]]; then + FIXES+=("$COMMIT_MSG") + elif [[ $COMMIT_MSG =~ ^feat(\(.*\))?: ]]; then + FEATS+=("$COMMIT_MSG") + else + MISC+=("${COMMIT_MSG}") + fi +done < <(git log ${VERSION_RANGE} --oneline) + +printChangelog "Breaking Changes" "${CHANGES[@]}" +printChangelog "New Features" "${FEATS[@]}" +printChangelog "Bug Fixes" "${FIXES[@]}" +printChangelog "Miscellaneous" "${MISC[@]}" diff --git a/.github/scripts/publish_post_check.sh b/.github/scripts/publish_post_check.sh new file mode 100755 index 00000000..8311ec25 --- /dev/null +++ b/.github/scripts/publish_post_check.sh @@ -0,0 +1,105 @@ + +#!/bin/bash + +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +###################################### Outputs ##################################### + +# 1. version: The version of this release including the 'v' prefix (e.g. v1.2.3). +# 2. changelog: Formatted changelog text for this release. + +#################################################################################### + +set -e +set -u + +function echo_info() { + local MESSAGE=$1 + echo "[INFO] ${MESSAGE}" +} + +function echo_warn() { + local MESSAGE=$1 + echo "[WARN] ${MESSAGE}" +} + +function terminate() { + echo "" + echo_warn "--------------------------------------------" + echo_warn "POST CHECK FAILED" + echo_warn "--------------------------------------------" + exit 1 +} + + +echo_info "Starting publish post check..." +echo_info "Git revision : ${GITHUB_SHA}" +echo_info "Git ref : ${GITHUB_REF}" +echo_info "Workflow triggered by : ${GITHUB_ACTOR}" +echo_info "GitHub event : ${GITHUB_EVENT_NAME}" + + +echo_info "" +echo_info "--------------------------------------------" +echo_info "Extracting release version" +echo_info "--------------------------------------------" +echo_info "" + +echo_info "Loading version from: firebase.go" + +readonly RELEASE_VERSION=`grep "const Version" firebase.go | awk '{print $4}' | tr -d \"` || true +if [[ -z "${RELEASE_VERSION}" ]]; then + echo_warn "Failed to extract release version from: firebase.go" + terminate +fi + +if [[ ! "${RELEASE_VERSION}" =~ ^([0-9]*)\.([0-9]*)\.([0-9]*)$ ]]; then + echo_warn "Malformed release version string: ${RELEASE_VERSION}. Exiting." + terminate +fi + +echo_info "Extracted release version: ${RELEASE_VERSION}" +echo "::set-output name=version::v${RELEASE_VERSION}" + + +echo_info "" +echo_info "--------------------------------------------" +echo_info "Generating changelog" +echo_info "--------------------------------------------" +echo_info "" + +echo_info "---< git fetch origin master --prune --unshallow >---" +git fetch origin master --prune --unshallow +echo "" + +echo_info "Generating changelog from history..." +readonly CURRENT_DIR=$(dirname "$0") +readonly CHANGELOG=`${CURRENT_DIR}/generate_changelog.sh` +echo "$CHANGELOG" + +# Parse and preformat the text to handle multi-line output. +# See https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/td-p/37870 +FILTERED_CHANGELOG=`echo "$CHANGELOG" | grep -v "\\[INFO\\]"` +FILTERED_CHANGELOG="${FILTERED_CHANGELOG//'%'/'%25'}" +FILTERED_CHANGELOG="${FILTERED_CHANGELOG//$'\n'/'%0A'}" +FILTERED_CHANGELOG="${FILTERED_CHANGELOG//$'\r'/'%0D'}" +echo "::set-output name=changelog::${FILTERED_CHANGELOG}" + + +echo "" +echo_info "--------------------------------------------" +echo_info "POST CHECK SUCCESSFUL" +echo_info "--------------------------------------------" diff --git a/.github/scripts/publish_preflight_check.sh b/.github/scripts/publish_preflight_check.sh new file mode 100755 index 00000000..9dad71bb --- /dev/null +++ b/.github/scripts/publish_preflight_check.sh @@ -0,0 +1,97 @@ +#!/bin/bash + +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +set -e +set -u + +function echo_info() { + local MESSAGE=$1 + echo "[INFO] ${MESSAGE}" +} + +function echo_warn() { + local MESSAGE=$1 + echo "[WARN] ${MESSAGE}" +} + +function terminate() { + echo "" + echo_warn "--------------------------------------------" + echo_warn "PREFLIGHT FAILED" + echo_warn "--------------------------------------------" + exit 1 +} + + +echo_info "Starting publish preflight check..." +echo_info "Git revision : ${GITHUB_SHA}" +echo_info "Git ref : ${GITHUB_REF}" +echo_info "Workflow triggered by : ${GITHUB_ACTOR}" +echo_info "GitHub event : ${GITHUB_EVENT_NAME}" + + +echo_info "" +echo_info "--------------------------------------------" +echo_info "Extracting release version" +echo_info "--------------------------------------------" +echo_info "" + +echo_info "Loading version from: firebase.go" + +readonly RELEASE_VERSION=`grep "const Version" firebase.go | awk '{print $4}' | tr -d \"` || true +if [[ -z "${RELEASE_VERSION}" ]]; then + echo_warn "Failed to extract release version from: firebase.go" + terminate +fi + +if [[ ! "${RELEASE_VERSION}" =~ ^([0-9]*)\.([0-9]*)\.([0-9]*)$ ]]; then + echo_warn "Malformed release version string: ${RELEASE_VERSION}. Exiting." + terminate +fi + +echo_info "Extracted release version: ${RELEASE_VERSION}" + + +echo_info "" +echo_info "--------------------------------------------" +echo_info "Checking release tag" +echo_info "--------------------------------------------" +echo_info "" + +echo_info "---< git fetch --depth=1 origin +refs/tags/*:refs/tags/* >---" +git fetch --depth=1 origin +refs/tags/*:refs/tags/* +echo "" + +readonly EXISTING_TAG=`git rev-parse -q --verify "refs/tags/v${RELEASE_VERSION}"` || true +if [[ -n "${EXISTING_TAG}" ]]; then + echo_warn "Tag v${RELEASE_VERSION} already exists. Exiting." + echo_warn "If the tag was created in a previous unsuccessful attempt, delete it and try again." + echo_warn " $ git tag -d v${RELEASE_VERSION}" + echo_warn " $ git push --delete origin v${RELEASE_VERSION}" + + readonly RELEASE_URL="https://github.com/firebase/firebase-admin-go/releases/tag/v${RELEASE_VERSION}" + echo_warn "Delete any corresponding releases at ${RELEASE_URL}." + terminate +fi + +echo_info "Tag v${RELEASE_VERSION} does not exist." + + +echo "" +echo_info "--------------------------------------------" +echo_info "PREFLIGHT SUCCESSFUL" +echo_info "--------------------------------------------" diff --git a/.github/scripts/run_all_tests.sh b/.github/scripts/run_all_tests.sh new file mode 100755 index 00000000..b52b283c --- /dev/null +++ b/.github/scripts/run_all_tests.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e +set -u + +gpg --quiet --batch --yes --decrypt --passphrase="${FIREBASE_SERVICE_ACCT_KEY}" \ + --output testdata/integration_cert.json .github/resources/integ-service-account.json.gpg + +echo "${FIREBASE_API_KEY}" > testdata/integration_apikey.txt + +go test -v -race firebase.google.com/go/... diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 37149aec..fb8f93ba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,5 @@ -name: Go Continuous Integration -on: [push, pull_request] +name: Continuous Integration +on: pull_request jobs: build: @@ -22,7 +22,7 @@ jobs: uses: actions/checkout@v2 with: path: go/src/firebase.google.com/go - + - name: Get dependencies run: go get -t -v $(go list ./... | grep -v integration) @@ -43,7 +43,7 @@ jobs: echo "Go code is not formatted:" gofmt -d -s . exit 1 - fi + fi - name: Run Static Analyzer run: go vet -v firebase.google.com/go/... diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..32f6dbe6 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,64 @@ +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Publish Release + +on: + # Only run the workflow when a PR is merged to the master branch. + pull_request: + branches: master + types: closed + +jobs: + publish_release: + if: github.event.pull_request.merged + + runs-on: ubuntu-latest + + steps: + - name: Checkout source + uses: actions/checkout@v2 + + - name: Publish post check + id: postcheck + run: ./.github/scripts/publish_post_check.sh + + # We pull this action from a custom fork of a contributor until + # https://github.com/actions/create-release/pull/32 is merged. Also note that v1 of + # this action does not support the "body" parameter. + - name: Create release tag + uses: fleskesvor/create-release@1a72e235c178bf2ae6c51a8ae36febc24568c5fe + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ steps.postcheck.outputs.version }} + release_name: Firebase Admin Go SDK ${{ steps.postcheck.outputs.version }} + body: ${{ steps.postcheck.outputs.changelog }} + draft: false + prerelease: false + + # Post to Twitter if explicitly opted-in by adding the label 'release:tweet'. + - name: Post to Twitter + if: success() && + contains(github.event.pull_request.labels.*.name, 'release:tweet') + uses: firebase/firebase-admin-node/.github/actions/send-tweet@master + with: + status: > + ${{ steps.postcheck.outputs.version }} of @Firebase Admin Go SDK is available. + https://github.com/firebase/firebase-admin-go/releases/tag/${{ steps.postcheck.outputs.version }} + consumer-key: ${{ secrets.FIREBASE_TWITTER_CONSUMER_KEY }} + consumer-secret: ${{ secrets.FIREBASE_TWITTER_CONSUMER_SECRET }} + access-token: ${{ secrets.FIREBASE_TWITTER_ACCESS_TOKEN }} + access-token-secret: ${{ secrets.FIREBASE_TWITTER_ACCESS_TOKEN_SECRET }} + continue-on-error: true diff --git a/.github/workflows/stage.yml b/.github/workflows/stage.yml new file mode 100644 index 00000000..540d9508 --- /dev/null +++ b/.github/workflows/stage.yml @@ -0,0 +1,75 @@ +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Stage Release + +on: + # Only run the workflow when a PR is updated or when a developer explicitly requests + # a build by sending a 'firebase_build' event. + pull_request: + types: [opened, synchronize] + + repository_dispatch: + types: + - firebase_build + +jobs: + stage_release: + # To stage a release without publishing it, send a 'firebase_build' event or apply + # the 'release:stage' label to a PR. PRs targetting the master branch are always + # staged. + if: github.event.action == 'firebase_build' || + contains(github.event.pull_request.labels.*.name, 'release:stage') || + github.event.pull_request.base.ref == 'master' + + runs-on: ubuntu-latest + + env: + GOPATH: ${{ github.workspace }}/go + + # When manually triggering the build, the requester can specify a target branch or a tag + # via the 'ref' client parameter. + steps: + - name: Check out code into GOPATH + uses: actions/checkout@v2 + with: + path: go/src/firebase.google.com/go + ref: ${{ github.event.client_payload.ref || github.ref }} + + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: 1.11 + + - name: Get dependencies + run: go get -t -v $(go list ./... | grep -v integration) + + - name: Run Linter + run: | + echo + go get golang.org/x/lint/golint + $GOPATH/bin/golint -set_exit_status firebase.google.com/go/... + + - name: Run Tests + working-directory: ./go/src/firebase.google.com/go + run: ./.github/scripts/run_all_tests.sh + env: + FIREBASE_SERVICE_ACCT_KEY: ${{ secrets.FIREBASE_SERVICE_ACCT_KEY }} + FIREBASE_API_KEY: ${{ secrets.FIREBASE_API_KEY }} + + # If triggered by a PR against the master branch, run additional checks. + - name: Publish preflight check + if: github.event.pull_request.base.ref == 'master' + working-directory: ./go/src/firebase.google.com/go + run: ./.github/scripts/publish_preflight_check.sh diff --git a/.travis.gofmt.sh b/.travis.gofmt.sh deleted file mode 100755 index e33451d7..00000000 --- a/.travis.gofmt.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -if [[ ! -z "$(gofmt -l -s .)" ]]; then - echo "Go code is not formatted:" - gofmt -d -s . - exit 1 -fi diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a4290f13..00000000 --- a/.travis.yml +++ /dev/null @@ -1,27 +0,0 @@ -language: go - -go: - - "1.11.x" - - "1.12.x" - - "1.13.x" - - master - -matrix: - # Build OK if fails on unstable development versions of Go. - allow_failures: - - go: master - # Don't wait for tests to finish on allow_failures. - # Mark the build finished if tests pass on other versions of Go. - fast_finish: true - -go_import_path: firebase.google.com/go - -install: - - go get golang.org/x/lint/golint - - go get -t -v $(go list ./... | grep -v integration) - -script: - - golint -set_exit_status $(go list ./...) - - if [[ "$TRAVIS_GO_VERSION" =~ ^1\.11\.([0-9]+|x)$ ]]; then ./.travis.gofmt.sh; fi - - go test -v -race -test.short ./... # Run tests with the race detector. - - go vet -v ./... # Run Go static analyzer. diff --git a/README.md b/README.md index 4b0e87b8..899a88d4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/firebase/firebase-admin-go.svg?branch=master)](https://travis-ci.org/firebase/firebase-admin-go) +[![Build Status](https://github.com/firebase/firebase-admin-go/workflows/Continuous%20Integration/badge.svg)](https://github.com/firebase/firebase-admin-go/actions) [![GoDoc](https://godoc.org/firebase.google.com/go?status.svg)](https://godoc.org/firebase.google.com/go) [![Go Report Card](https://goreportcard.com/badge/github.com/firebase/firebase-admin-go)](https://goreportcard.com/report/github.com/firebase/firebase-admin-go) @@ -42,7 +42,7 @@ requests, code review feedback, and also pull requests. ## Supported Go Versions We support Go v1.11 and higher. -[Continuous integration](https://travis-ci.org/firebase/firebase-admin-go) system +[Continuous integration](https://github.com/firebase/firebase-admin-go/actions) system tests the code on Go v1.11 through v1.13. ## Documentation diff --git a/auth/auth.go b/auth/auth.go index c12a82ed..b5088806 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -57,14 +57,18 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error) signer cryptoSigner err error ) + + creds, _ := transport.Creds(ctx, conf.Opts...) + // Initialize a signer by following the go/firebase-admin-sign protocol. - if conf.Creds != nil && len(conf.Creds.JSON) > 0 { + if creds != nil && len(creds.JSON) > 0 { // If the SDK was initialized with a service account, use it to sign bytes. - signer, err = signerFromCreds(conf.Creds.JSON) + signer, err = signerFromCreds(creds.JSON) if err != nil && err != errNotAServiceAcct { return nil, err } } + if signer == nil { if conf.ServiceAccountID != "" { // If the SDK was initialized with a service account email, use it with the IAM service diff --git a/auth/auth_test.go b/auth/auth_test.go index 72ad19d9..1e8e1098 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -34,6 +34,7 @@ import ( ) const ( + credEnvVar = "GOOGLE_APPLICATION_CREDENTIALS" testProjectID = "mock-project-id" testVersion = "test-version" ) @@ -82,7 +83,6 @@ func TestNewClientWithServiceAccountCredentials(t *testing.T) { t.Fatal(err) } client, err := NewClient(context.Background(), &internal.AuthConfig{ - Creds: creds, Opts: optsWithServiceAcct, ProjectID: creds.ProjectID, Version: testVersion, @@ -176,7 +176,6 @@ func TestNewClientWithUserCredentials(t *testing.T) { }`), } conf := &internal.AuthConfig{ - Creds: creds, Opts: []option.ClientOption{option.WithCredentials(creds)}, Version: testVersion, } @@ -206,7 +205,11 @@ func TestNewClientWithMalformedCredentials(t *testing.T) { creds := &google.DefaultCredentials{ JSON: []byte("not json"), } - conf := &internal.AuthConfig{Creds: creds} + conf := &internal.AuthConfig{ + Opts: []option.ClientOption{ + option.WithCredentials(creds), + }, + } if c, err := NewClient(context.Background(), conf); c != nil || err == nil { t.Errorf("NewClient() = (%v,%v); want = (nil, error)", c, err) } @@ -222,12 +225,61 @@ func TestNewClientWithInvalidPrivateKey(t *testing.T) { t.Fatal(err) } creds := &google.DefaultCredentials{JSON: b} - conf := &internal.AuthConfig{Creds: creds} + conf := &internal.AuthConfig{ + Opts: []option.ClientOption{ + option.WithCredentials(creds), + }, + } if c, err := NewClient(context.Background(), conf); c != nil || err == nil { t.Errorf("NewClient() = (%v,%v); want = (nil, error)", c, err) } } +func TestNewClientAppDefaultCredentialsWithInvalidFile(t *testing.T) { + current := os.Getenv(credEnvVar) + + if err := os.Setenv(credEnvVar, "../testdata/non_existing.json"); err != nil { + t.Fatal(err) + } + defer os.Setenv(credEnvVar, current) + + conf := &internal.AuthConfig{} + if c, err := NewClient(context.Background(), conf); c != nil || err == nil { + t.Errorf("Auth() = (%v, %v); want (nil, error)", c, err) + } +} + +func TestNewClientInvalidCredentialFile(t *testing.T) { + invalidFiles := []string{ + "testdata", + "testdata/plain_text.txt", + } + + ctx := context.Background() + for _, tc := range invalidFiles { + conf := &internal.AuthConfig{ + Opts: []option.ClientOption{ + option.WithCredentialsFile(tc), + }, + } + if c, err := NewClient(ctx, conf); c != nil || err == nil { + t.Errorf("Auth() = (%v, %v); want (nil, error)", c, err) + } + } +} + +func TestNewClientExplicitNoAuth(t *testing.T) { + ctx := context.Background() + conf := &internal.AuthConfig{ + Opts: []option.ClientOption{ + option.WithoutAuthentication(), + }, + } + if c, err := NewClient(ctx, conf); c == nil || err != nil { + t.Errorf("Auth() = (%v, %v); want (auth, nil)", c, err) + } +} + func TestCustomToken(t *testing.T) { client := &Client{ signer: testSigner, @@ -298,8 +350,7 @@ func TestCustomTokenError(t *testing.T) { func TestCustomTokenInvalidCredential(t *testing.T) { ctx := context.Background() conf := &internal.AuthConfig{ - Creds: nil, - Opts: optsWithTokenSource, + Opts: optsWithTokenSource, } s, err := NewClient(ctx, conf) if err != nil { diff --git a/firebase.go b/firebase.go index d66b1fb0..0343d15b 100644 --- a/firebase.go +++ b/firebase.go @@ -31,7 +31,6 @@ import ( "firebase.google.com/go/internal" "firebase.google.com/go/messaging" "firebase.google.com/go/storage" - "golang.org/x/oauth2/google" "google.golang.org/api/option" "google.golang.org/api/transport" ) @@ -39,7 +38,7 @@ import ( var defaultAuthOverrides = make(map[string]interface{}) // Version of the Firebase Go Admin SDK. -const Version = "3.12.0" +const Version = "3.12.1" // firebaseEnvName is the name of the environment variable with the Config. const firebaseEnvName = "FIREBASE_CONFIG" @@ -47,7 +46,6 @@ const firebaseEnvName = "FIREBASE_CONFIG" // An App holds configuration and state common to all Firebase services that are exposed from the SDK. type App struct { authOverride map[string]interface{} - creds *google.DefaultCredentials dbURL string projectID string serviceAccountID string @@ -67,7 +65,6 @@ type Config struct { // Auth returns an instance of auth.Client. func (a *App) Auth(ctx context.Context) (*auth.Client, error) { conf := &internal.AuthConfig{ - Creds: a.creds, ProjectID: a.projectID, Opts: a.opts, ServiceAccountID: a.serviceAccountID, @@ -142,28 +139,14 @@ func (a *App) Messaging(ctx context.Context) (*messaging.Client, error) { func NewApp(ctx context.Context, config *Config, opts ...option.ClientOption) (*App, error) { o := []option.ClientOption{option.WithScopes(internal.FirebaseScopes...)} o = append(o, opts...) - creds, err := transport.Creds(ctx, o...) - if err != nil { - return nil, err - } if config == nil { + var err error if config, err = getConfigDefaults(); err != nil { return nil, err } } - var pid string - if config.ProjectID != "" { - pid = config.ProjectID - } else if creds.ProjectID != "" { - pid = creds.ProjectID - } else { - pid = os.Getenv("GOOGLE_CLOUD_PROJECT") - if pid == "" { - pid = os.Getenv("GCLOUD_PROJECT") - } - } - + pid := getProjectID(ctx, config, o...) ao := defaultAuthOverrides if config.AuthOverride != nil { ao = *config.AuthOverride @@ -171,7 +154,6 @@ func NewApp(ctx context.Context, config *Config, opts ...option.ClientOption) (* return &App{ authOverride: ao, - creds: creds, dbURL: config.DatabaseURL, projectID: pid, serviceAccountID: config.ServiceAccountID, @@ -213,3 +195,20 @@ func getConfigDefaults() (*Config, error) { } return fbc, nil } + +func getProjectID(ctx context.Context, config *Config, opts ...option.ClientOption) string { + if config.ProjectID != "" { + return config.ProjectID + } + + creds, _ := transport.Creds(ctx, opts...) + if creds != nil && creds.ProjectID != "" { + return creds.ProjectID + } + + if pid := os.Getenv("GOOGLE_CLOUD_PROJECT"); pid != "" { + return pid + } + + return os.Getenv("GCLOUD_PROJECT") +} diff --git a/firebase_test.go b/firebase_test.go index 831712f4..1b367f1c 100644 --- a/firebase_test.go +++ b/firebase_test.go @@ -58,11 +58,6 @@ func TestServiceAcctFile(t *testing.T) { if len(app.opts) != 2 { t.Errorf("Client opts: %d; want: 2", len(app.opts)) } - if app.creds == nil { - t.Error("Credentials: nil; want creds") - } else if len(app.creds.JSON) == 0 { - t.Error("JSON: empty; want; non-empty") - } } func TestClientOptions(t *testing.T) { @@ -116,11 +111,6 @@ func TestRefreshTokenFile(t *testing.T) { if len(app.opts) != 2 { t.Errorf("Client opts: %d; want: 2", len(app.opts)) } - if app.creds == nil { - t.Error("Credentials: nil; want creds") - } else if len(app.creds.JSON) == 0 { - t.Error("JSON: empty; want; non-empty") - } } func TestRefreshTokenFileWithConfig(t *testing.T) { @@ -135,11 +125,6 @@ func TestRefreshTokenFileWithConfig(t *testing.T) { if len(app.opts) != 2 { t.Errorf("Client opts: %d; want: 2", len(app.opts)) } - if app.creds == nil { - t.Error("Credentials: nil; want creds") - } else if len(app.creds.JSON) == 0 { - t.Error("JSON: empty; want; non-empty") - } } func TestRefreshTokenWithEnvVar(t *testing.T) { @@ -158,11 +143,6 @@ func TestRefreshTokenWithEnvVar(t *testing.T) { if app.projectID != "mock-project-id" { t.Errorf("[env=%s] Project ID: %q; want: mock-project-id", varName, app.projectID) } - if app.creds == nil { - t.Errorf("[env=%s] Credentials: nil; want creds", varName) - } else if len(app.creds.JSON) == 0 { - t.Errorf("[env=%s] JSON: empty; want; non-empty", varName) - } } for _, varName := range []string{"GCLOUD_PROJECT", "GOOGLE_CLOUD_PROJECT"} { verify(varName) @@ -185,11 +165,6 @@ func TestAppDefault(t *testing.T) { if len(app.opts) != 1 { t.Errorf("Client opts: %d; want: 1", len(app.opts)) } - if app.creds == nil { - t.Error("Credentials: nil; want creds") - } else if len(app.creds.JSON) == 0 { - t.Error("JSON: empty; want; non-empty") - } } func TestAppDefaultWithInvalidFile(t *testing.T) { @@ -201,8 +176,8 @@ func TestAppDefaultWithInvalidFile(t *testing.T) { defer os.Setenv(credEnvVar, current) app, err := NewApp(context.Background(), nil) - if app != nil || err == nil { - t.Errorf("NewApp() = (%v, %v); want: (nil, error)", app, err) + if app == nil || err != nil { + t.Fatalf("NewApp() = (%v, %v); want = (app, nil)", app, err) } } @@ -215,12 +190,20 @@ func TestInvalidCredentialFile(t *testing.T) { ctx := context.Background() for _, tc := range invalidFiles { app, err := NewApp(ctx, nil, option.WithCredentialsFile(tc)) - if app != nil || err == nil { - t.Errorf("NewApp(%q) = (%v, %v); want: (nil, error)", tc, app, err) + if app == nil || err != nil { + t.Fatalf("NewApp() = (%v, %v); want = (app, nil)", app, err) } } } +func TestExplicitNoAuth(t *testing.T) { + ctx := context.Background() + app, err := NewApp(ctx, nil, option.WithoutAuthentication()) + if app == nil || err != nil { + t.Fatalf("NewApp() = (%v, %v); want = (app, nil)", app, err) + } +} + func TestAuth(t *testing.T) { ctx := context.Background() app, err := NewApp(ctx, nil, option.WithCredentialsFile("testdata/service_account.json")) diff --git a/internal/internal.go b/internal/internal.go index d5164e74..8edc7e4d 100644 --- a/internal/internal.go +++ b/internal/internal.go @@ -20,7 +20,6 @@ import ( "time" "golang.org/x/oauth2" - "golang.org/x/oauth2/google" "google.golang.org/api/option" ) @@ -40,7 +39,6 @@ var SystemClock = &systemClock{} // AuthConfig represents the configuration of Firebase Auth service. type AuthConfig struct { Opts []option.ClientOption - Creds *google.DefaultCredentials ProjectID string ServiceAccountID string Version string diff --git a/messaging/messaging_batch.go b/messaging/messaging_batch.go index 9ada0f0e..cb4e848a 100644 --- a/messaging/messaging_batch.go +++ b/messaging/messaging_batch.go @@ -38,7 +38,7 @@ const multipartBoundary = "__END_OF_PART__" // Messaging (FCM). // // It contains payload information as well as the list of device registration tokens to which the -// message should be sent. A single MulticastMessage may contain up to 100 registration tokens. +// message should be sent. A single MulticastMessage may contain up to 500 registration tokens. type MulticastMessage struct { Tokens []string Data map[string]string @@ -89,7 +89,7 @@ type BatchResponse struct { // SendAll sends the messages in the given array via Firebase Cloud Messaging. // -// The messages array may contain up to 100 messages. SendAll employs batching to send the entire +// The messages array may contain up to 500 messages. SendAll employs batching to send the entire // array of mssages as a single RPC call. Compared to the `Send()` function, // this is a significantly more efficient way to send multiple messages. The responses list // obtained from the return value corresponds to the order of the input messages. An error from @@ -105,7 +105,7 @@ func (c *fcmClient) SendAll(ctx context.Context, messages []*Message) (*BatchRes // This function does not actually deliver any messages to target devices. Instead, it performs all // the SDK-level and backend validations on the messages, and emulates the send operation. // -// The messages array may contain up to 100 messages. SendAllDryRun employs batching to send the +// The messages array may contain up to 500 messages. SendAllDryRun employs batching to send the // entire array of mssages as a single RPC call. Compared to the `SendDryRun()` function, this // is a significantly more efficient way to validate sending multiple messages. The responses list // obtained from the return value corresponds to the order of the input messages. An error from @@ -117,7 +117,7 @@ func (c *fcmClient) SendAllDryRun(ctx context.Context, messages []*Message) (*Ba // SendMulticast sends the given multicast message to all the FCM registration tokens specified. // -// The tokens array in MulticastMessage may contain up to 100 tokens. SendMulticast uses the +// The tokens array in MulticastMessage may contain up to 500 tokens. SendMulticast uses the // `SendAll()` function to send the given message to all the target recipients. The // responses list obtained from the return value corresponds to the order of the input tokens. An // error from SendMulticast indicates a total failure -- i.e. the message could not be sent to any @@ -137,7 +137,7 @@ func (c *fcmClient) SendMulticast(ctx context.Context, message *MulticastMessage // This function does not actually deliver any messages to target devices. Instead, it performs all // the SDK-level and backend validations on the messages, and emulates the send operation. // -// The tokens array in MulticastMessage may contain up to 100 tokens. SendMulticastDryRun uses the +// The tokens array in MulticastMessage may contain up to 500 tokens. SendMulticastDryRun uses the // `SendAllDryRun()` function to send the given message. The responses list obtained from // the return value corresponds to the order of the input tokens. An error from SendMulticastDryRun // indicates a total failure -- i.e. none of the messages were sent to FCM for validation. Partial diff --git a/messaging/topic_mgt.go b/messaging/topic_mgt.go index 8ae170a5..6252cdb5 100644 --- a/messaging/topic_mgt.go +++ b/messaging/topic_mgt.go @@ -26,8 +26,8 @@ import ( const ( iidEndpoint = "https://iid.googleapis.com/iid/v1" - iidSubscribe = ":batchAdd" - iidUnsubscribe = ":batchRemove" + iidSubscribe = "batchAdd" + iidUnsubscribe = "batchRemove" ) var iidErrorCodes = map[string]struct{ Code, Msg string }{ @@ -164,7 +164,7 @@ func (c *iidClient) makeTopicManagementRequest(ctx context.Context, req *iidRequ request := &internal.Request{ Method: http.MethodPost, - URL: fmt.Sprintf("%s/%s", c.iidEndpoint, req.op), + URL: fmt.Sprintf("%s:%s", c.iidEndpoint, req.op), Body: internal.NewJSONEntity(req), } var result iidResponse diff --git a/messaging/topic_mgt_test.go b/messaging/topic_mgt_test.go index 00be6755..68a82a2d 100644 --- a/messaging/topic_mgt_test.go +++ b/messaging/topic_mgt_test.go @@ -41,7 +41,7 @@ func TestSubscribe(t *testing.T) { if err != nil { t.Fatal(err) } - client.iidEndpoint = ts.URL + client.iidEndpoint = ts.URL + "/v1" resp, err := client.SubscribeToTopic(ctx, []string{"id1", "id2"}, "test-topic") if err != nil { @@ -84,7 +84,7 @@ func TestUnsubscribe(t *testing.T) { if err != nil { t.Fatal(err) } - client.iidEndpoint = ts.URL + client.iidEndpoint = ts.URL + "/v1" resp, err := client.UnsubscribeFromTopic(ctx, []string{"id1", "id2"}, "test-topic") if err != nil { @@ -125,7 +125,7 @@ func TestTopicManagementError(t *testing.T) { if err != nil { t.Fatal(err) } - client.iidEndpoint = ts.URL + client.iidEndpoint = ts.URL + "/v1" client.iidClient.httpClient.RetryConfig = nil cases := []struct { @@ -185,7 +185,7 @@ func checkIIDRequest(t *testing.T, b []byte, tr *http.Request, op string) { if tr.Method != http.MethodPost { t.Errorf("Method = %q; want = %q", tr.Method, http.MethodPost) } - wantOp := "/" + op + wantOp := "/v1:" + op if tr.URL.Path != wantOp { t.Errorf("Path = %q; want = %q", tr.URL.Path, wantOp) }