magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.59k stars 9.32k forks source link

Elasticsearch8 does not work when Server has Basic authentication #37356

Closed lory-to closed 1 year ago

lory-to commented 1 year ago

Preconditions and environment

Steps to reproduce

  1. Deploy elastic-cloud with Elasticsearch 8.X
  2. Configure Magento to use elasticsearch8, see https://experienceleague.adobe.com/docs/commerce-operations/upgrade-guide/prepare/prerequisites.html?lang=en#upgrade-elasticsearch
  3. Configure Catalog > Catalog > Catalog Search as follows
    string(75) "https://*****.*****.azure.elastic-cloud.com"
    ["port"]=>
    NULL
    ["index"]=>
    string(8) "magento2"
    ["enableAuth"]=>
    string(1) "1"
    ["username"]=>
    string(7) "*****"
    ["password"]=>
    string(24) "*****"
    ["timeout"]=>
    string(2) "15"
  4. Run bin/magento setup:upgrade
  5. Run bin/magento indexer:reindex

Expected result

bin/magento setup:upgrade ends without errors bin/magento indexer:reindex ends without errors

Actual result

Additional information

The versions required for elastic/transport and elasticsearch/elasticsearch ignore the settings for username and password The Transport then does not have such credentials and according to the code they are not recorvered from the available nodes. (the nodes have the credentials as the URL is created prepending username:password@ but this URL is not used. \Elastic\Elasticsearch\Client::sendRequest is then sent without credentials.

Suggested change \Magento\Elasticsearch8\Model\Client\Elasticsearch::buildESConfig

add the following

$options['basicAuthentication'] = [$options['username'], $options['password']];

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @lory-to. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

m2-assistant[bot] commented 1 year ago

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Bravo commented 1 year ago

Hi @lory-to,

Thank you for reporting and collaboration.

Verified the issue on Magento 2.4.6 instance and the issue is not reproducible.Kindly refer the screenshots.

We have Configured Magento with elasticsearch8 using https://experienceleague.adobe.com/docs/commerce-operations/upgrade-guide/prepare/prerequisites.html?lang=en#upgrade-elasticsearch.

Screenshot 2023-04-28 at 12 56 46 PM Screenshot 2023-04-28 at 12 59 09 PM

Both the bin/magento setup:upgrade and bin/magento indexer:reindex ends without errors.

Kindly recheck the behavior on Magento 2.4.6 instance and elaborate steps to reproduce if the issue is still reproducible.

Thanks.

lory-to commented 1 year ago

Hi @lory-to, We have Configured Magento with elasticsearch8 using https://experienceleague.adobe.com/docs/commerce-operations/upgrade-guide/prepare/prerequisites.html?lang=en#upgrade-elasticsearch.

Both the bin/magento setup:upgrade and bin/magento indexer:reindex ends without errors.

Kindly recheck the behavior on Magento 2.4.6 instance and elaborate steps to reproduce if the issue is still reproducible.

Thanks.

Good morning @engcom-Bravo Does your Elasticsearch instance have Authentication enabled?

This is happening only when I'm required to imput username and password.

engcom-Bravo commented 1 year ago

Hi @lory-to,

Thanks for your update.

Verified the issue on Magento 2.4.6 instance and the issue is not reproducible.Kindly refer the screenshots.

Please try to install elasticsearch with setup : install command.Kindly check this document https://experienceleague.adobe.com/docs/commerce-operations/installation-guide/advanced.html

bin/magento setup:install --base-url=[http://magento246.local](http://magento2.local/) --db-host=localhost --db-name=magento246 --db-user=global --db-password=Global@123 --admin-firstname="admin" --admin-lastname=admin --admin-email=admin@admin.com --admin-user=admin --admin-password=admin123 --language=en_US --currency="USD" --timezone=America/Chicago --use-rewrites=1 --backend-frontname=admin --search-engine=elasticsearch8 --elasticsearch-host=localhost --elasticsearch-port=9200 --elasticsearch-username=magento --elasticsearch-password=guest

We have configured Magento instance with Elasticsearch8 using this document https://experienceleague.adobe.com/docs/commerce-operations/upgrade-guide/prepare/prerequisites.html?lang=en#upgrade-elasticsearch

Screenshot 2023-05-02 at 1 36 53 PM Screenshot 2023-05-02 at 1 46 48 PM

https://user-images.githubusercontent.com/51680745/235614003-9b47f854-09c4-4553-914e-3dbf5bcce37e.mov

Both the bin/magento setup:upgrade and bin/magento indexer:reindex ends without errors.

Kindly recheck the behavior on Magento 2.4.6 instance and elaborate steps to reproduce if the issue is still reproducible.

Please let us know if you are still facing any issue.

Thanks.

engcom-Bravo commented 1 year ago

Hi @lory-tom,

We have noticed that this issue has not been updated for a period of 14 Days.
Hence we assume that this issue is fixed now, so we are closing it. Please raise a fresh ticket or reopen this ticket if you need more assistance on this.

Thanks.

jtolhurst-trellis commented 1 year ago

I have also ran into this issue on 2.4.6 and found @lory-to 's proposed solution to work. This seem to happen with external hosts. I also am using an externally hosted instance and not one hosted on localhost. It is possible this could play a role in how you are testing.

In either case, it would be beneficial for Magento to use the Elasticsearch library correctly. Here is a demonstration without Magento that shows how Magento's config array that it builds does not work, but adding the correct configurations allows a connection.

composer require elasticsearch/elasticsearch

Create a file test.php

<?php

include 'vendor/autoload.php';

use Elastic\Elasticsearch\ClientBuilder;

// How Magento would build an options array
$options1 = [
    'hostname' => 'https://my-instance.es.us-east-1.aws.found.io',
    'port' => '443',
    'index' => 'myindex',
    'enableAuth' => '1',
    'username' => 'username',
    'password' => 'password',
    'timeout' => '15',
    'hosts' => [
        'https://username:password@my-instance.es.us-east-1.aws.found.io:443'
    ]
];

$client1 = ClientBuilder::fromConfig($options1, true);

// Adding in `basicAuthentication` to the config
$options2 = [
    'hostname' => 'https://my-instance.es.us-east-1.aws.found.io',
    'port' => '443',
    'index' => 'myindex',
    'enableAuth' => '1',
    'username' => 'username',
    'password' => 'password',
    'timeout' => '15',
    'basicAuthentication' => [
        'username',
        'password',
    ],
    'hosts' => [
        'https://username:password@my-instance.es.us-east-1.aws.found.io:443'
    ]
];

$client2 = ClientBuilder::fromConfig($options2, true);

// Info API
$response1 = $client1->ping()->asBool();
$response2 = $client2->ping()->asBool();

Execute the file

php test.php

And you will get the following output

bool(false)
bool(true)

Where the first ping() fails because of a 401 Unauthorized response from the server, but the second will succeed because it properly authenticates. So it seems that Magento could do better to correctly configure the Elasticsearch client to ensure a connection to all clients.

As an additional note, the current array that Magento passes is largely discarded. If you do not silence the errors, fromConfig($options, false), the Elasticsearch library will emit an error showing that it discards all but the hosts key from the options because they are not valid configurations.