3

In order not to call every time:

gcloud auth login

which would interrupt a script for user input

I would like to see if a user is already authenticated (any user) and only if no user, then to call gcloud auth login.

I would love a more elegant/reliable solution than scraping the output of gcloud auth list. Something like a ping that fails or succeeds if there is an account already AND the account has the right to do the thing. Maybe a --dry-run option?

Stephen Weinberg
  • 51,320
  • 14
  • 134
  • 113
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
  • What are you trying to do? Google Compute Engine VMs [are already authenticated](https://cloud.google.com/docs/authentication#service_running_in_compute_engine) with credentials, which the [client libraries](https://cloud.google.com/sdk/cloud-client-libraries) automatically use (via [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials)) when making api calls. – jerjou Feb 12 '16 at 02:16

1 Answers1

5

Being logged in means having account property set. You can extract it via

gcloud config list --format='value(core.account)' 2>/dev/null

You can also use

gcloud auth list --format='value(active_account)' 2>/dev/null
cherba
  • 8,681
  • 3
  • 27
  • 34