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: workforce credentials #485

Merged
merged 23 commits into from
Feb 1, 2024
Merged

feat: workforce credentials #485

merged 23 commits into from
Feb 1, 2024

Conversation

bshaffer
Copy link
Contributor

@bshaffer bshaffer commented Oct 5, 2023

addresses #483

(note: relevant testing requested in phpspec/prophecy#611)

@bshaffer
Copy link
Contributor Author

bshaffer commented Jan 17, 2024

@aeitzman I'd love to get your review on this one as well!

One thing I'd like to get clarity on - in the python implementation, there's a check that bypasses setting the "additionalOptions" when a client_id exists (https://github.com/googleapis/google-auth-library-python/pull/868/files#diff-002ea4a611abe1254cc4abc68bbf57f139a0ca17ff5389ab287e9a732bf44267R328). Is this something I need to add? It seems a bit strange, as I can't really tell where that Client ID would be set or passed in.

@bshaffer bshaffer marked this pull request as ready for review January 17, 2024 22:57
@bshaffer bshaffer requested a review from a team as a code owner January 17, 2024 22:57
// using the IAM API. This saves a call to fetch an access token when a
// cached token exists.
if ($this->fetcher instanceof Credentials\GCECredentials
|| $this->fetcher instanceof Credentials\ImpersonatedServiceAccountCredentials

Choose a reason for hiding this comment

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

Is this related to this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, no. I am not sure why this would be in here. I probably just realized that it was something that needed fixing, when adding similar logic below for getProjectId.

// Pass the access token from cache for credentials that require an
// access token to fetch the project ID. This saves a call to fetch an
// access token when a cached token exists.
if ($this->fetcher instanceof Credentials\ExternalAccountCredentials) {

Choose a reason for hiding this comment

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

Just double checking since I don't fully understand the caching flow in this library, this call is only used if getProjectId is explicitly called by a user/some other library right? Just want to make sure that the default flow for getting an access token doesn't break if cloudresourcemanager is down.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that's correct - this is ONLY for calls to $credentials->getProjectId()

Copy link
Contributor

@yash30201 yash30201 left a comment

Choose a reason for hiding this comment

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

Just left few nits and comments to double check my understanding. Looks good implementation wise.

Comment on lines +395 to +409
$httpHandler = function (RequestInterface $request) {
$this->assertEquals(
'https://cloudresourcemanager.googleapis.com/v1/projects/1234',
(string) $request->getUri()
);
$this->assertEquals('Bearer some-token', $request->getHeaderLine('authorization'));
$body = $this->prophesize(StreamInterface::class);
$body->__toString()->willReturn(json_encode(['projectId' => 'test-project-id']));

$response = $this->prophesize(ResponseInterface::class);
$response->getBody()->willReturn($body->reveal());
$response->hasHeader('Content-Type')->willReturn(false);

return $response->reveal();
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you help me understand why are we defining this httphandler if the token and eventually the project id would be fetched from the mocked cache?

Copy link
Contributor Author

@bshaffer bshaffer Jan 31, 2024

Choose a reason for hiding this comment

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

Sure. This test is testing the getProjectId method, which makes an API call using the project number in order to retrieve the project ID. It's also testing that a cached access token is used when that API call is made, instead of retrieving a new one.

So the cache is for the access token, which is needed for the API request to get the project ID. The httpHandler is the HTTP request made when getProjectId is called, which is not cached.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, now this makes sense.
I feel adding a small explanation there in comment would go a long way to quickly understand it.

@bshaffer bshaffer enabled auto-merge (squash) February 1, 2024 17:19
@bshaffer bshaffer merged commit c1b240f into main Feb 1, 2024
13 checks passed
@bshaffer bshaffer deleted the workforce-credentials branch February 1, 2024 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants