-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add getCredentials method #180
feat: add getCredentials method #180
Conversation
-Added metadata return for GCP
-Added test case
…e-auth-library-nodejs into 169-create-get-credentials
@stephenplusplus Does it address your requirements? |
Yes, thank you, this LGTM! |
Maybe it's possible to have this method be more generic, so that it would return the JSON key file contents, if they exist, otherwise the metadata from GCP? Also, do you know what the response from GCP looks like that this new method would return? |
@stephenplusplus The reason I have not added the functionality to return the contents from the JSON is that the user can retrieve the content from the JSON file directly if they are not on GCP. The response from the server looks like this:
|
The point of the method that I'm looking for is basically: "Get the user's client_email and private_key". If the private key isn't available, just the email is fine. If they are both unavailable, that would be an error. That way, instead of each application writing:
They only have to write:
|
@bantini Would that work for you? |
@stephenplusplus Are we sure we want to make the PK accessible? For use cases like generating a signed URL you don't actually need the keyfile as you can defer to the |
Either way is fine with me. I would lean towards returning as much as possible, otherwise we are basically just creating a "getClientEmail()" method (which is also fine with me). |
Test case for checking the environment variable and returning an error if it is not set is failing due to a time-out. |
Nilayan, I experimented in another PR to fix the build:
|
@saicheems The changes worked. Thanks! |
@bantini Can this be merged? |
@lukesneeringer Yes. The tests are all passing. And it seems @stephenplusplus got it working in his code. |
@bantini can you merge, or do we need someone else? |
@stephenplusplus I don't have permission to merge. |
src/auth/googleauth.ts
Outdated
// To save the contents of the JSON credential file | ||
private _jsonContent: JWTInput; | ||
|
||
get jsonContent(): JWTInput { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
} else { | ||
// Callback with the body | ||
const credential: | ||
CredentialBody = {client_email: body['default']['email']}; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
} | ||
} | ||
}); | ||
} else if (this.jsonContent) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
} else if (this.jsonContent) { | ||
const credential: CredentialBody = { | ||
client_email: this.jsonContent.client_email, | ||
private_key: this.jsonContent.private_key |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
} | ||
} else { | ||
if (callback) { | ||
callback(new Error('Could not find credential file.'), undefined); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
if (callback) { | ||
callback(null, credential); | ||
} | ||
} else if (err) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Left a few more comments.
src/auth/googleauth.ts
Outdated
if (callback) { | ||
callback(err, undefined); | ||
} | ||
} else if (gce) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
|
||
|
||
/** | ||
* Returns the contents of the metadata of GC instance |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
}, | ||
(err, body, res) => { | ||
if (err || !res || res.statusCode !== 200 || !body || | ||
!body.default || !body.default.email) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there.
src/auth/googleauth.ts
Outdated
* If it doesn't exist, and the environment is on GCE, it gets the | ||
* client_email from the cloud metadata server. | ||
* @param {function} callback Callback. | ||
* @return object representation of the service account JSON |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, go ahead and land once you have addressed the two minor nits.
Note that I changed the PR title to follow conventionalcommits.org. That helps figuring out the semver-ness of commits going forward.
src/auth/googleauth.ts
Outdated
if (callback) { | ||
callback(err, undefined); | ||
} | ||
callback(err, undefined); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/auth/googleauth.ts
Outdated
if (callback) { | ||
callback(null, credential); | ||
} | ||
callback(new Error('Unknown error.'), undefined); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@danoscarmike Yes. It fixes #169. |
Thanks for this @bantini! Would you let me know when the release is out? |
@stephenplusplus I have no information about the release process. But I will surely let you know if I hear something. |
@ofrobots do you know of someone we could ping to send a release? |
I can do it. Need a bit of time to move #173 off to a separate branch for 1.0. |
Cool, thanks! |
google-auth-library 0.12.0 published 🎉 |
Solves #169
Created getCredentials method to get credentials from the metadata server.
The details from JSON file is left for the user to implement.