class Google::Auth::GCECredentials

Patches GCECredentials to lookup the project id using the metadata service when on compute engine.

Public Instance Methods

project_id() click to toggle source

The compute engine project id

# File lib/firebase/admin/gce.rb, line 14
def project_id
  @project_id ||= fetch_project_id
end

Private Instance Methods

fetch_project_id() click to toggle source

Fetches the project id using the compute engine metadata service.

# File lib/firebase/admin/gce.rb, line 21
def fetch_project_id
  connection = build_default_connection || Faraday.default_connection
  retry_with_error do
    uri = Firebase::Admin::GCE::PROJECT_ID_URI
    resp = connection.get(uri) do |req|
      req.headers["Metadata-Flavor"] = "Google"
    end
    case resp.status
    when 200
      resp.body
    when 404
      raise Signet::AuthorizationError, NO_METADATA_SERVER_ERROR
    else
      msg = "Unexpected error code #{resp.status} #{UNEXPECTED_ERROR_SUFFIX}"
      raise Signet::AuthorizationError, msg
    end
  end
end