Skip to content

Upgrade Guzzle to v6#6581

Merged
techanvil merged 17 commits intodevelopfrom
enhance/1146-guzzle6
Mar 10, 2023
Merged

Upgrade Guzzle to v6#6581
techanvil merged 17 commits intodevelopfrom
enhance/1146-guzzle6

Conversation

@aaemnnosttv
Copy link
Copy Markdown
Collaborator

@aaemnnosttv aaemnnosttv commented Feb 11, 2023

Summary

Addresses issue:

Relevant technical choices

Relevant docs

PR Author Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 5.2 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have added a QA Brief on the issue linked above.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

Do not alter or remove anything below. The following sections will be managed by moderators only.

Code Reviewer Checklist

  • Run the code.
  • Ensure the acceptance criteria are satisfied.
  • Reassess the implementation with the IB.
  • Ensure no unrelated changes are included.
  • Ensure CI checks pass.
  • Check Storybook where applicable.
  • Ensure there is a QA Brief.

Merge Reviewer Checklist

  • Ensure the PR has the correct target branch.
  • Double-check that the PR is okay to be merged.
  • Ensure the corresponding issue has a ZenHub release assigned.
  • Add a changelog message to the issue.

"true/punycode": "^2.0 <2.1.1"
},
"replace": {
"paragonie/random_compat": ">=2",
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

random_compat is bundled in WordPress so this avoids loading via Composer, potentially loading an older version.

The version here is significant as any version below version 2 cannot be installed due to roave/security-advisories, so the constraint must only target versions which could actually be installed. This configuration causes the package not to be installed.

$client->setPrompt( 'consent' );
$client->setRedirectUri( $args['redirect_uri'] );
$client->setScopes( (array) $args['required_scopes'] );
$client->prepareScopes();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This method didn't actually do anything meaningful as it has no side-effects, it only returns the list of requested scopes as a single string.

scoper.inc.php Outdated

// This dependency is a global function which should remain global.
'vendor/ralouphie/getallheaders/src/getallheaders.php',
'vendor/symfony/polyfill-intl-normalizer/bootstrap.php',
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There may be others that are needed here, this raised a failure without it so it was the only one added. See the generated third-party/vendor/autoload_files.php for other possible entries here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm interested to know where the failure was raised, as looking at the resulting third-party/symfony/polyfill-intl-normalizer/bootstrap.php, the generated normalizer_is_normalized() / normalizer_normalize() functions don't work properly as they refer to Symfony\Polyfill\Intl\Normalizer, whereas as can be seen examining third-party/symfony/polyfill-intl-normalizer/Normalizer.php, this Normalizer class is now defined within the Google\Site_Kit_Dependencies\Symfony\Polyfill\Intl\Normalizer namespace.

I think it would be a more correct fix to instead use Scoper to modify the resulting bootstrap.php file to simply avoid adding a namespace statement. I dug into this a bit and was able to test the current generated normalizer_is_normalized() function, which threw a PHP Fatal error: Uncaught Error: Class 'Symfony\\Polyfill\\Intl\\Normalizer\\Normalizer' not found error, whereas a modded version of bootstrap.php as generated without being in the files-whitelist array, but with the namespace line removed, resulted in a working normalizer_is_normalized()...

Given that the normalizer_is_normalized() / normalizer_normalize() functions are called in the WP core remove_accents() function which is in turn called by Tag_Manager::sanitize_container_name(), we need to be a bit careful here...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good call, this was the part I think was most likely to introduce problems, but I think removing the namespace from the global bootstrap.php files for polyfills should fix it 👍

I wasn't able to reproduce the initial error that caused me to add the one path to the whitelist initially, but it shouldn't be needed anymore anyways with the more robust solution.

* @return \Google\Site_Kit_Dependencies\GuzzleHttp\Message\ResponseInterface
* @return ResponseInterface
*/
public function send( RequestInterface $request ) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This class is no longer useful for its intended purpose and needs to be replaced.

$fake_http_client->set_request_handler(
function ( Request $request ) use ( $activity_metrics ) {
if ( 0 !== strpos( $request->getUrl(), 'https://oauth2.googleapis.com/token' ) ) {
if ( 0 !== strpos( $request->getUri(), 'https://oauth2.googleapis.com/token' ) ) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Naming difference in the new interface.

Comment on lines +501 to +502
$this->authentication->get_oauth_client()->get_client()->setHttpClient( $http_client );
$this->analytics->get_owner_oauth_client()->get_client()->setHttpClient( $http_client );
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This looks like it was missing initially but may have been working by coincidence. This will need to change going forward as using an alternate HTTP client won't work for this with Guzzle 6.

@aaemnnosttv aaemnnosttv mentioned this pull request Feb 11, 2023
@aaemnnosttv aaemnnosttv marked this pull request as ready for review March 3, 2023 18:58
Copy link
Copy Markdown
Collaborator Author

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Note to the reviewer: the diff will be easiest to review with whitespace ignored/hidden as indentation has changed in some chunks but otherwise most of the content has been preserved with minimal changes.


use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Modules\Module;
use Google\Site_Kit\Core\Modules\Module_With_Service_Entity;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There are a few instances like this where unused imports were cleaned up as well.

@aaemnnosttv aaemnnosttv requested a review from techanvil March 3, 2023 19:17
Copy link
Copy Markdown
Collaborator

@techanvil techanvil left a comment

Choose a reason for hiding this comment

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

Nice work, @aaemnnosttv. I have left a couple of comments, a suggestion and a couple of questions, please take a look.

scoper.inc.php Outdated

// This dependency is a global function which should remain global.
'vendor/ralouphie/getallheaders/src/getallheaders.php',
'vendor/symfony/polyfill-intl-normalizer/bootstrap.php',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm interested to know where the failure was raised, as looking at the resulting third-party/symfony/polyfill-intl-normalizer/bootstrap.php, the generated normalizer_is_normalized() / normalizer_normalize() functions don't work properly as they refer to Symfony\Polyfill\Intl\Normalizer, whereas as can be seen examining third-party/symfony/polyfill-intl-normalizer/Normalizer.php, this Normalizer class is now defined within the Google\Site_Kit_Dependencies\Symfony\Polyfill\Intl\Normalizer namespace.

I think it would be a more correct fix to instead use Scoper to modify the resulting bootstrap.php file to simply avoid adding a namespace statement. I dug into this a bit and was able to test the current generated normalizer_is_normalized() function, which threw a PHP Fatal error: Uncaught Error: Class 'Symfony\\Polyfill\\Intl\\Normalizer\\Normalizer' not found error, whereas a modded version of bootstrap.php as generated without being in the files-whitelist array, but with the namespace line removed, resulted in a working normalizer_is_normalized()...

Given that the normalizer_is_normalized() / normalizer_normalize() functions are called in the WP core remove_accents() function which is in turn called by Tag_Manager::sanitize_container_name(), we need to be a bit careful here...

Comment on lines +524 to +531
FakeHttp::fake_google_http_handler(
$this->authentication->get_oauth_client()->get_client(),
$fake_http_handler
);
FakeHttp::fake_google_http_handler(
$this->analytics->get_owner_oauth_client()->get_client(),
$fake_http_handler
);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a slight departure from the existing logic, where just setting the handler for $this->analytics->get_client() as below would be a direct mapping of it. I seem to recall you mentioned something about this, though, where we should have been updating the authentication client as well as the Analytics client. Therefore, I'm pretty confident your update here will be the right thing to do, but would you mind clarifying it for my understanding?

		FakeHttp::fake_google_http_handler(
			$this->analytics->get_client(),
			$fake_http_handler
		);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, maybe it was this comment I was thinking of, which I didn't spot at the time of writing the above...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I mentioned this above in https://github.com/google/site-kit-wp/pull/6581/files/b58274c240118142014bca49d98ad01c00d754a8#r1103512495.

I thought it was missing the handler set on the module owners client instance since the test uses the data_access_token data provider, which includes the empty token, so I probably assumed the test was intended to run in a sharing context as well, but this one is not so it's all good. I've restored the original form 👍

Copy link
Copy Markdown
Collaborator

@techanvil techanvil left a comment

Choose a reason for hiding this comment

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

Looks like this needs a bit more attention, as the E2E tests are failing... Please see my separate comment with a bit of detail on this.

Copy link
Copy Markdown
Collaborator

@techanvil techanvil left a comment

Choose a reason for hiding this comment

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

LGTM, nice one @aaemnnosttv!

@techanvil techanvil merged commit 61404a7 into develop Mar 10, 2023
@techanvil techanvil deleted the enhance/1146-guzzle6 branch March 10, 2023 13:16
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.

2 participants