Skip to content
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

Merged
merged 27 commits into from
Nov 20, 2017
Merged

feat: add getCredentials method #180

merged 27 commits into from
Nov 20, 2017

Conversation

bantini
Copy link
Contributor

@bantini bantini commented Nov 9, 2017

Solves #169
Created getCredentials method to get credentials from the metadata server.

The details from JSON file is left for the user to implement.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Nov 9, 2017
@coveralls
Copy link

coveralls commented Nov 9, 2017

Coverage Status

Coverage decreased (-0.4%) to 93.179% when pulling d04e52c on bantini:169-create-get-credentials into b39c16f on google:master.

@bantini bantini requested a review from ofrobots November 9, 2017 05:57
@bantini
Copy link
Contributor Author

bantini commented Nov 9, 2017

@stephenplusplus Does it address your requirements?
@saicheems FYI.

@stephenplusplus
Copy link
Contributor

Yes, thank you, this LGTM!

@stephenplusplus
Copy link
Contributor

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?

@bantini
Copy link
Contributor Author

bantini commented Nov 9, 2017

@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:

        {
        "default":
        {
          "aliases":["default"],
          "email":"default@test-creds.iam.gserviceaccount.com",
          "scopes":["https://www.googleapis.com/auth/cloud-platform"]
        },
        "test-creds@test-creds.iam.gserviceaccount.com":
        {
          "aliases":["test-creds"],
          "email":"test-creds@test-creds.iam.gserviceaccount.com",
          "scopes":["https://www.googleapis.com/auth/cloud-platform"]}
        }

@stephenplusplus
Copy link
Contributor

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:

if GCP
  - call metadata server
  - detect email from response
else
  - read JSON file
  - parse out client_email and private_key

They only have to write:

authClient.getCredentials(function(err, credentials) {
  console.log(credentials.client_email)
})

@lukesneeringer
Copy link

@bantini Would that work for you?

@jasonpolites
Copy link

jasonpolites commented Nov 14, 2017

@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 signBlob API.

@danoscarmike

@stephenplusplus
Copy link
Contributor

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).

@bantini
Copy link
Contributor Author

bantini commented Nov 14, 2017

Test case for checking the environment variable and returning an error if it is not set is failing due to a time-out.

@saicheems
Copy link
Contributor

saicheems commented Nov 16, 2017

Nilayan, I experimented in another PR to fix the build:

  1. From Cannot read property '0' of undefined npm/npm#17858 (comment)

    Run

    npx npmc install
    rm package-lock.json
    npx npmc install
    

    Recommit package-lock.json.

  2. You'll have to do one of the following:

    In .travis.yml delete the cache item.

    -cache:
    -  directories:
    -  - node_modules/
    -
    

    ^ the cached modules are corrupted and causing npm install to fail.

    The alternative requires admin access to Travis. You'd have to clear the build cache for this PR. I think that's an option accessible from More options in the Travis interface.

@bantini
Copy link
Contributor Author

bantini commented Nov 16, 2017

@saicheems The changes worked. Thanks!

@lukesneeringer
Copy link

@bantini Can this be merged?

@bantini
Copy link
Contributor Author

bantini commented Nov 16, 2017

@lukesneeringer Yes. The tests are all passing. And it seems @stephenplusplus got it working in his code.

@stephenplusplus
Copy link
Contributor

@bantini can you merge, or do we need someone else?

@bantini
Copy link
Contributor Author

bantini commented Nov 17, 2017

@stephenplusplus I don't have permission to merge.

@stephenplusplus
Copy link
Contributor

@ofrobots ?

// 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.

} 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.

}
}
});
} else if (this.jsonContent) {

This comment was marked as spam.

This comment was marked as spam.

} 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.

}
} else {
if (callback) {
callback(new Error('Could not find credential file.'), undefined);

This comment was marked as spam.

This comment was marked as spam.

if (callback) {
callback(null, credential);
}
} else if (err) {

This comment was marked as spam.

This comment was marked as spam.

@ofrobots ofrobots changed the title 169 create get credentials feat: add getCredentials method Nov 17, 2017
Copy link
Contributor

@ofrobots ofrobots left a 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.

if (callback) {
callback(err, undefined);
}
} else if (gce) {

This comment was marked as spam.

This comment was marked as spam.



/**
* Returns the contents of the metadata of GC instance

This comment was marked as spam.

This comment was marked as spam.

},
(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.

@JustinBeckwith JustinBeckwith added this to the 1.0 milestone Nov 19, 2017
Copy link
Contributor

@ofrobots ofrobots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there.

* 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.

Copy link
Contributor

@ofrobots ofrobots left a 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.

if (callback) {
callback(err, undefined);
}
callback(err, undefined);

This comment was marked as spam.

if (callback) {
callback(null, credential);
}
callback(new Error('Unknown error.'), undefined);

This comment was marked as spam.

@danoscarmike
Copy link

@bantini is this one good to merge? And this fixes #169 still, correct?

@bantini bantini merged commit 8eddbc9 into googleapis:master Nov 20, 2017
@bantini
Copy link
Contributor Author

bantini commented Nov 20, 2017

@danoscarmike Yes. It fixes #169.

@bantini bantini deleted the 169-create-get-credentials branch November 20, 2017 18:17
@stephenplusplus
Copy link
Contributor

Thanks for this @bantini! Would you let me know when the release is out?

@bantini
Copy link
Contributor Author

bantini commented Nov 20, 2017

@stephenplusplus I have no information about the release process. But I will surely let you know if I hear something.

@stephenplusplus
Copy link
Contributor

@ofrobots do you know of someone we could ping to send a release?

@ofrobots
Copy link
Contributor

I can do it. Need a bit of time to move #173 off to a separate branch for 1.0.

@stephenplusplus
Copy link
Contributor

Cool, thanks!

@ofrobots
Copy link
Contributor

google-auth-library 0.12.0 published 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants