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

fix: If withProxy(apiKey) is set the client does not set custom headers #191

Merged
merged 5 commits into from
Jan 10, 2024

Conversation

chriswk
Copy link
Member

@chriswk chriswk commented Dec 19, 2023

Since our README explicitly states that using withProxy($apiKey) is in lieu of using custom headers for the Authorization header, our SDK should make sure to check if the proxyApiKey is set, and if so, use it to set the Authorization header also for client registration and client metrics.

Description

We had clients complaining that when they used the PHP client in proxy mode (using the withProxy method), they struggled with 403 responses when trying to register/post metrics. After diving into the code I found that we were not using the key as an authorization header for any other request but the request to /api/frontend.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

  • Unit tests
  • Spec Tests
  • Integration tests / Manual Tests

Checklist:

  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Since our README explicitly states that using withProxy($apiKey) is in
lieu of using custom headers for the Authorization header, our SDK
should make sure to check if the proxyApiKey is set, and if so, use it
to set the Authorization header also for client registration and client
metrics.
foreach ($this->configuration->getHeaders() as $name => $value) {
$request = $request->withHeader($name, $value);
}

try {
$this->httpClient->sendRequest($request);
$response = $this->httpClient->sendRequest($request);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the variable assignment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, thanks for the spot, because, I needed to see the status code, it wasn't supposed to be part of the PR, reverting :)

Comment on lines 61 to 63
if ($this->configuration->getProxyKey() !== null) {
$request = $request->withHeader('Authorization', $this->configuration->getProxyKey());
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, this is rather unfortunate solution, but sadly I don't think we have any other quick fix. The proxy key should be directly tied to the headers in configuration.

Can you add a comment with TODO: refactor proxy key into headers or something similar, so that I'll pick it up next time I'm doing clean-up?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can have another stab before converting this to a proper PR.

Copy link
Member Author

@chriswk chriswk Dec 19, 2023

Choose a reason for hiding this comment

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

Totally agree that this should be moved to the headers list when we build the configuration.

Comment on lines 34 to 36
if ($this->configuration->getProxyKey() !== null) {
$request = $request->withHeader('Authorization', $this->configuration->getProxyKey());
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add the same todo comment here, please.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to have one more stab at the builder before converting this to a proper PR. Thanks for the feedback 👍

@chriswk chriswk marked this pull request as ready for review December 19, 2023 12:07
@chriswk chriswk requested a review from RikudouSage December 19, 2023 12:31
@chriswk
Copy link
Member Author

chriswk commented Dec 21, 2023

Hey @RikudouSage - could I get a stop or a go-ahead here, I added the api key to the headers in the builder's build method instead, which meant I could remove the unfortunate if checks in our request handling.

Copy link
Collaborator

@RikudouSage RikudouSage left a comment

Choose a reason for hiding this comment

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

Looks good to me! Do you want to do any further changes or shall I merge?

@chriswk
Copy link
Member Author

chriswk commented Jan 4, 2024

Please merge, sorry, I see I owed you this answer.

@chriswk chriswk changed the title fix: If apiKey is set people don't set custom headers fix: If withProxy(apiKey) is set. The client does not set custom headers Jan 4, 2024
@chriswk chriswk changed the title fix: If withProxy(apiKey) is set. The client does not set custom headers fix: If withProxy(apiKey) is set the client does not set custom headers Jan 4, 2024
@chriswk chriswk merged commit 22fb05e into main Jan 10, 2024
11 checks passed
@chriswk chriswk deleted the fix/addApiKeyToMetricsPosting branch January 10, 2024 07:53
RikudouSage pushed a commit that referenced this pull request Jan 12, 2024
…rs (#191)

Since our README explicitly states that using withProxy($apiKey) is in
lieu of using custom headers for the Authorization header, our SDK
now makes sure to check if the proxyApiKey is set, and if so, uses it
to set the Authorization header also for client registration and client
metrics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants