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(impersonated): add impersonated credentials auth #1207

Merged
merged 31 commits into from Jul 29, 2021
Merged

Conversation

bcoe
Copy link
Contributor

@bcoe bcoe commented Jul 7, 2021

Adds ImpersonatedCredentials. This credential type basically takes one source credential and exchanges it for another, different service account.

Fixes #535
Refs #779


@salrashid123 I've gone through the original #779 and made some edits to make the work more idiomatic for Node.js.

I'm leaving some review comments on this PR that would get it ready to land.

@bcoe bcoe requested a review from a team as a code owner July 7, 2021 18:18
@google-cla
Copy link

google-cla bot commented Jul 7, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no This human has *not* signed the Contributor License Agreement. label Jul 7, 2021
Copy link
Contributor Author

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

@salrashid123 sorry for screwing up your branch, I left some refactoring and recommendations in this PR, to act as a baseline.

One additional thought, we should probably add a blurb in the README about how to use the impersonated client?

src/auth/googleauth.ts Show resolved Hide resolved
src/auth/impersonated.ts Outdated Show resolved Hide resolved
src/auth/impersonated.ts Outdated Show resolved Hide resolved
} catch (err) {
let helpfulMessage = null;
if (err.status === 403) {
helpfulMessage =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Likewise, perhaps we can get away with checking for just the 403 and 404 case?

test/test.impersonated.ts Outdated Show resolved Hide resolved
test/test.impersonated.ts Outdated Show resolved Hide resolved
test/test.impersonated.ts Outdated Show resolved Hide resolved
@salrashid123
Copy link
Contributor

thanks, i retested the code from this PR and it worked fine with the snippet below

1,2 wokred fine but ...how do you supply the impersonated client to a GCP library like @google-cloud/logging?

i do know in the original PR i showed 3 working below but ...i must've just had that as a place holder or somthing of a wishlist

is it possible to supply Impersonated to any nodejs library?

i looked at but those credentials and what not are something else like a keyfile
https://googleapis.dev/nodejs/logging/latest/global.html#ClientConfig
https://googleapis.dev/nodejs/google-auth-library/latest/interfaces/GoogleAuthOptions.html

const {Logging} = require('@google-cloud/logging');
const { GoogleAuth, JWT, Impersonated  } = require('google-auth-library');
const { Storage } = require('@google-cloud/storage');

var svcAccountFile = '/home/srashid/gcp_misc/certs/mineral-minutia-820-e9a7c8665867.json';
var project_id = 'fabled-ray-104117';
const keys = require(svcAccountFile);

// create a sourceCredential
let saclient = new JWT(
  keys.client_email,
  null,
  keys.private_key,
  ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/iam'],
);

// Use that to impersonate the targetPrincipal
let targetClient = new Impersonated({
  sourceClient: saclient,
  targetPrincipal: "impersonated-account@fabled-ray-104117.iam.gserviceaccount.com",
  lifetime: 30,
  delegates: [],
  targetScopes: ["https://www.googleapis.com/auth/cloud-platform"]
});


// At this point you can use the client as any other:

// 1. Acquire Headers
const authHeaders = await targetClient.getRequestHeaders();
console.log(authHeaders);

// 2. Use client in authorized session 
targetClient.getAccessToken().then(res => {
  let url = 'https://www.googleapis.com/storage/v1/b?project=' + project_id
  targetClient.requestAsync({ url }).then(resp => {
    console.log(resp.data.items[0]);
  }).catch(function (error) {
    console.error('Unable to list buckets: ' + error);
  });
});

// 3. Use client with GCP Service 

const logging = new Logging({projectId: project_id});
const log = logging.log('mylog');
const text = 'Hello, world!';
const metadata = {
  resource: {type: 'global'},
};
const entry = log.entry(metadata, text);
await log.write(entry);
console.log(`Logged: ${text}`);

@google-cla
Copy link

google-cla bot commented Jul 16, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@salrashid123
Copy link
Contributor

thanks. verified the last push works. here's a suggestion for the README.md

https://gist.github.com/salrashid123/6e3c2eaa0575d6c18632931cbf8cb496

@google-cla
Copy link

google-cla bot commented Jul 21, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@generated-files-bot
Copy link

Warning: This pull request is touching the following templated files:

@google-cla
Copy link

google-cla bot commented Jul 21, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@bcoe bcoe added the cla: yes This human has signed the Contributor License Agreement. label Jul 21, 2021
@bcoe bcoe added the cla: yes This human has signed the Contributor License Agreement. label Jul 27, 2021
@google-cla
Copy link

google-cla bot commented Jul 27, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no This human has *not* signed the Contributor License Agreement. and removed cla: yes This human has signed the Contributor License Agreement. labels Jul 27, 2021
@bcoe bcoe requested a review from bojeil-google July 27, 2021 17:10
@bcoe bcoe added cla: yes This human has signed the Contributor License Agreement. and removed cla: no This human has *not* signed the Contributor License Agreement. labels Jul 27, 2021
@bojeil-google
Copy link
Contributor

Hey @bcoe thanks for addressing the comments. I noticed some comments may have been accidentally missed. Perhaps you forgot to commit those changes?
This includes: adding space between tests, 2021 copyright, using AuthClient instead of OAuth2Client for source AuthClient, updating ImpersonatedOptions param description and adding a test for Impersonated in test.index.ts.

This may be relevant but when I try to "show changes since my last review", I get an error message:

We went looking everywhere, but couldn’t find those commits.
Sometimes commits can disappear after a force-push. Head back to the latest changes here.

@bcoe
Copy link
Contributor Author

bcoe commented Jul 28, 2021

@bojeil-google the push had just failed.

@google-cla
Copy link

google-cla bot commented Jul 28, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no This human has *not* signed the Contributor License Agreement. and removed cla: yes This human has signed the Contributor License Agreement. labels Jul 28, 2021
@google-cla
Copy link

google-cla bot commented Jul 28, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

1 similar comment
@google-cla
Copy link

google-cla bot commented Jul 28, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

* the "Service Account Token Creator" IAM role.
*
* @param {object} options - The configuration object.
* @param {object} [options.credentials] the service account email address.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this exists in ImpersonatedOptions

@@ -0,0 +1,351 @@
/**
* Copyright 2019 Google LLC
Copy link
Contributor

Choose a reason for hiding this comment

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

Missed this one: 2021

@google-cla
Copy link

google-cla bot commented Jul 29, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@bcoe bcoe added cla: yes This human has signed the Contributor License Agreement. and removed cla: no This human has *not* signed the Contributor License Agreement. labels Jul 29, 2021
@bcoe
Copy link
Contributor Author

bcoe commented Jul 29, 2021

@bojeil-google thank you for the thorough review, this is much better than it would have been if landed earlier (also it identified a bug with refreshing token logic).

@bcoe bcoe merged commit ab1cd31 into master Jul 29, 2021
@bcoe bcoe deleted the impersonated branch July 29, 2021 14:00
xil222 pushed a commit to xil222/google-auth-library-nodejs that referenced this pull request Jul 29, 2021
xil222 pushed a commit to xil222/google-auth-library-nodejs that referenced this pull request Jul 29, 2021
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.

Implement ImpersonatedCredentials
6 participants