GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for detecting project ID. Fixes #97 by jmdobry · Pull Request #98 · googleapis/google-auth-library-nodejs

This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Conversation

@jmdobry

@jmdobry jmdobry commented Oct 1, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Fixes #97

  • - Add support for detecting default project ID
    • - Detect from environment variables
    • - Detect from project_id field in GOOGLE_APPLICATION_CREDENTIALS key file
    • - Detect from Google Cloud SDK default config core/project setting
    • - Detect Compute Engine project ID
  • - Write tests

Before

auth.getApplicationDefault(err, authClient) {
  // ... 
});

After

auth.getApplicationDefault(err, authClient, projectId) {
  // ... 
});

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

stephenplusplus reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 1, 2016
@stephenplusplus

stephenplusplus commented Oct 1, 2016

Copy link
Copy Markdown
Contributor

What do you think about putting the project ID as a property on the authClient / a getter? Since users of the library are likely caching the authClient, it should be easier for them to just grab the projectId off of that object as opposed to additionally caching the projectId returned when they create the client.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmdobry

jmdobry commented Oct 1, 2016

Copy link
Copy Markdown
Contributor Author

They are related but still separate concerns. One is a set of credentials (a json key file), the other an identifier for a project. The auth object itself actually will have both of them cached. Today you can already access the cached credentials. My PR caches the project ID too. e.g.

// Get just the project ID
auth.getDefaultProjectId(function (err, projectId) {
  // The project ID has now been cached
  console.log(auth._cachedProjectId === projectId); // true
});
// Retrieve credentials and the project id
auth.getApplicationDefault(function (err, credentials, projectId) {
  // Both are cached
  console.log(auth._cachedCredential === credentials); // true
  console.log(auth._cachedProjectId === projectId); // true
});

This is how python is implementing it:

import google.auth

credentials, project_id = google.auth.default()
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus

stephenplusplus commented Oct 1, 2016

Copy link
Copy Markdown
Contributor

I think the functionality of auto-detection of a project ID is quite helpful, and I think users will appreciate it, too. My thought is we should make it as easy as possible for them to get by putting it on the authClient instance so they don't have to "catch it before it's gone" (in the callback of the auth client fetcher).

But either way, google-auto-auth wraps this library, so I'll put it on the authClient that library returns, which should help us in our client libs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmdobry

jmdobry commented Oct 1, 2016

Copy link
Copy Markdown
Contributor Author

catch it before it's gone

It won't be gone, it's cached on the auth object, just like the credentials. Once that callback is called they can access the cached project id on the auth instance they used to call getApplicationDefault, e.g. auth._cachedProjectId. Also, if the credentials object is actually a user's Service Account, it will already have a project_id field. I don't see anywhere in google-auth-library-nodejs where it actually modifies the default application credentials. It just loads them and caches them. I'm not sure one would want to attach the default project id to the loaded credentials, hence I cached them separate from each other.

But either way, google-auto-auth wraps this library, so I'll put it on the authClient that library returns, which should help us in our client libs.

👍, though personally I would have google-auto-auth do as I've done here and cache the project id on the google-auto-auth Auth instance, not the authClient value (which is actually the parsed key file). e.g. (from google-auto-auth):

function addScope(err, authClient, projectId) {
  if (err) {
    callback(err);
    return;
  }

  if (authClient.createScopedRequired && authClient.createScopedRequired()) {
    // ...
  }

  self.authClient = authClient;
  self.projectId = projectId;
  callback(null, authClient, projectId);
}

Then google-cloud-node or gax-nodejs can cache the credentials and projectId separately as they see fit. The two values are used for separate things. One for making an authorization header to pass with requests, the second to be a path parameter in requests (especially in the case of gax-nodejs.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus

stephenplusplus commented Oct 1, 2016

Copy link
Copy Markdown
Contributor

it's cached on the auth object, just like the credentials.

I guess all I'm really saying is it shouldn't be "private". In JS, the leading _ tells developers not to rely on this value. And it tells contributors to this library that they can change the purpose of that property or remove it without being concerned it would break users' code.

  self.authClient = authClient;
  self.projectId = projectId;

Yep, that's exactly how I would do it :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmdobry

jmdobry commented Oct 1, 2016

Copy link
Copy Markdown
Contributor Author

So perhaps we should also add synchronous getters to google-auth-library-nodejs for the credentials and projectId

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus

stephenplusplus commented Oct 3, 2016

Copy link
Copy Markdown
Contributor

Can we also read the project_id from a service account keyfile?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus stephenplusplus mentioned this pull request Oct 3, 2016
streamline auth - normalize project_id to projectId googleapis/google-cloud-node#1661
Closed
@jmdobry

jmdobry commented Oct 3, 2016

Copy link
Copy Markdown
Contributor Author

Yep: https://github.com/google/google-auth-library-nodejs/pull/98/files#diff-74a29c6f6e51bc579dce9754d2d4eeb0R185

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus

stephenplusplus commented Oct 3, 2016

Copy link
Copy Markdown
Contributor

Is that in the case I don't have gcloud SDK installed at all, and I just provide a JSON key file?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmdobry

jmdobry commented Oct 3, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Yeah, newer Service Account key files have a project_id field. This PR looks for a projectId in following places (in order of precedence):

  • GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT env var
  • project_id field of Service Account key file pointed to by GOOGLE_APPLICATION_CREDENTIALS
  • core/project field of default config file of Google Cloud SDK
  • GCE metadata server
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus

stephenplusplus commented Oct 3, 2016

Copy link
Copy Markdown
Contributor

Does this library accept a key file path provided directly as a string, though? This is how I do it in google-auto-auth, but I'm not sure if I'm doing it in a way that this library intended to support.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmdobry

jmdobry commented Oct 3, 2016

Copy link
Copy Markdown
Contributor Author

Passing keyFile works as a consequence of google-auth-library-nodejs using gtoken under the hood. google-auth-library-nodejs itself does not actually do anything with keyFile.

getApplicationDefault and getDefaultProjectId don't look at keyFile at all.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus stephenplusplus mentioned this pull request Oct 3, 2016
core: read projectId from authClient googleapis/google-cloud-node#1656
Merged
9 tasks done
@jmdobry jmdobry changed the title [DO NOT MERGE] Add support for detecting default project ID Add support for detecting project ID. Fixes #97 Oct 3, 2016
@jmdobry
Add support for detecting project ID. Fixes #97
6921be6
@coveralls

coveralls commented Oct 3, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.3%) to 92.673% when pulling 6921be6 on jmdobry:default-project into 02f5874 on google:master.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmdobry jmdobry mentioned this pull request Oct 3, 2016
New quickstarts. GoogleCloudPlatform/nodejs-docs-samples#226
Merged
@stephenplusplus

stephenplusplus commented Oct 14, 2016

Copy link
Copy Markdown
Contributor

@jmdobry what's the process for getting this shipped?

Splaktar reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stephenplusplus

stephenplusplus commented Oct 14, 2016

Copy link
Copy Markdown
Contributor

// @tbetbetbe @jasonall

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmdobry

jmdobry commented Oct 14, 2016

Copy link
Copy Markdown
Contributor Author

I'm not sure. Wishing I had write access.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@murgatroid99
murgatroid99 merged commit 1c2d853 into googleapis:master Oct 14, 2016
@murgatroid99 murgatroid99 mentioned this pull request Oct 14, 2016
Prepare for 0.9.9 release #96
Merged
@Splaktar Splaktar mentioned this pull request Oct 14, 2016
Auto-detect a project ID googleapis/google-cloud-node#1653
Closed
@stephenplusplus stephenplusplus mentioned this pull request Nov 21, 2016
RFC: Project ID Detection googleapis/google-cloud-php#245
Closed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@jmdobry @stephenplusplus @coveralls @murgatroid99 @googlebot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.