diff --git a/README.md b/README.md index a248b99..3e4d7c9 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,13 @@ $ curl https://raw.githubusercontent.com/jharshman/fwsync/master/install.sh | sh ## Usage ### Authentication -Communication to the GoogleAPIs requires setting the environment variable `GOOGLE_APPLICATION_CREDENTIALS`. -This variable should point to the path of your downloaded service account json. For example, `export GOOGLE_APPLICATION_CREDENTIALS=/Users/bob/bobserviceaccount.json`. +Communication to the GoogleAPIs requires either setting the environment variable `GOOGLE_APPLICATION_CREDENTIALS` or +alternatively running `gcloud auth application-default login`. If using the `GOOGLE_APPLICATION_CREDENTIALS` environment +variable, it must point to the full path to your downloaded service account json. + +For example: + +`$ export GOOGLE_APPLICATION_CREDENTIALS=/Users/bob/bobserviceaccount.json` ### Init After installing, you can invoke the CLI by typing `fwsync` in your terminal. diff --git a/internal/auth/auth.go b/internal/auth/auth.go index fb8ee83..97ed799 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -2,13 +2,10 @@ package auth import ( "context" - "errors" - "os" "sync" "github.com/spf13/cobra" "google.golang.org/api/compute/v1" - "google.golang.org/api/option" ) var ( @@ -16,20 +13,19 @@ var ( once sync.Once ) +// Auth returns the authentication function. func Auth() func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - serviceAccountJSONPath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") - if serviceAccountJSONPath == "" { - return errors.New("missing environment variable GOOGLE_APPLICATION_CREDENTIALS") - } - return auth(serviceAccountJSONPath) + return auth() } } -func auth(svcAcctFile string) (authErr error) { +// auth will authenticate to the Google APIs using default methods. +// https://cloud.google.com/docs/authentication/application-default-credentials#personal +func auth() (authErr error) { once.Do(func() { var cc *compute.Service - cc, authErr = compute.NewService(context.Background(), option.WithCredentialsFile(svcAcctFile)) + cc, authErr = compute.NewService(context.Background()) GoogleCloudAuthorizedClient = cc }) return