peburrows/goth

Look for ADC credentials at ~/.config/gcloud/application_default_credentials.json

eamontaaffe opened this issue · 7 comments

When you run the command gcloud auth application-default-credentials login the credentials are deposited at ~/.config/gcloud/application_default_credentials.json.

This behavior doesn't seem to be documented in the Application Default Credentials documentation. However, many of the google supported libraries seem to identify credentials at this location (such as the python api).

It would be great if this library would also support his credentials location as part of the ADC lookup procedure.

goth/lib/goth/config.ex

Lines 131 to 136 in 7eb2068

defp from_creds_file(_config) do
case System.get_env("GOOGLE_APPLICATION_CREDENTIALS") do
nil -> nil
filename -> filename |> File.read!() |> decode_json()
end
end

It looks like we could just update this function slightly.

Maybe like this. Ill wrap this up into a PR later today.

 defp fetch_creds_filename do
   default_path = Path.expand("~/.config/gcloud/application_default_credentials.json")
   case {System.fetch_env("GOOGLE_APPLICATION_CREDENTIALS"), File.regular?(default_path)} do
     {{:ok, filename}, _} -> {:ok, filename}     
     {_, true} -> {:ok, default_path}
   end 
 end

 defp from_creds_file(_config) do 
   case fetch_creds_filename do
     {:ok, filename} -> filename |> File.read!() |> decode_json()
     {:error, _} -> nil
   end 
 end 

All of the Google auth clients support ADC, is there no way to get Goth to without a full redesign?

We hope the redesigned API is more flexible so adding ADC support will hopefully not require a 'full redesign'. Feel free to send a PR with ADC support, it could even be the default :source.

Apologies, it wasn't clear that the redesign you write about above was already done as and released as an RC. I will give adding ADC support a shot!

No worries, thanks for looking into it!