Problem/Motivation

My site's environment has a proxy that routes all traffic towards a forward proxy, and further to the internet.

However, when connecting to Elasticsearch, I do not want to route the traffic outside the network, because the Drupal and Elasticsearch servers are hosted on different servers on the same subnet.

Currently, the elasticsearch_connector module is unable to reach the Elasticsearch server to proxy settings.

From the console on the Drupal server, if I run curl --noproxy '*' http://es-search.dev.org.local:9200/, then I can see the cluster information printed to the console. However if i omit the --noproxy option (i.e.: curl http://es-search.dev.org.local:9200/ then curl can't connect.

How can I tell elasticsearch connector to connect to the elasticsearch using no_proxyalues?

Elasticsearch connector should be able to use Drupal's existing HTTP proxy settings to handle this situation.

The elasticsearch/elasticsearch library has a ClientBuilder::setHttpClientOptions() that accepts a \Psr\Http\Client\ClientInterface, i.e.: a PSR-7 HTTP Client.

Drupal core has a service with the machine name http_client, which conforms to the PSR-7 ClientInterface standard, and also knows about any proxies that are configured in settings.php for Drupal to use.

Proposed resolution

Pass Drupal's http_client to elasticsearch/elasticsearch's ClientBuilder::setHttpClientOptions, so it connects through that.

Remaining tasks

  1. Merge request for 8.0.x - merge request !96 created by @hexaki in #13
  2. Maintainer review and testing - done by @mparker17 by #21
  3. Merge request for 9.0.x - merge request !204 created by @murrow in #22
  4. Community review and testing - skipped by @mparker17 in #24
  5. Commit to 9.0.x - merged by @mparker17 in #26
  6. Commit to 8.0.x - merged by @mparker17 in #28
  7. Release 9.0.x - released in 9.0.0-alpha3 by @mparker17
  8. Release 8.0.x - released in 8.0.0-alpha7 by @mparker17

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

Added support for connecting to Elasticsearch through the proxy globally configured for Drupal in settings.php.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sabsbecool created an issue. See original summary.

murrow’s picture

In the current build (8.0.0-alpha2), I see no implementation that uses Drupal's HTTP proxy settings. This version of Elasticsearch Connector uses elasticsearch/elasticsearch 8, which should support proxies through the ClientBuilder's setHttpClientOptions() method, as described here: https://www.elastic.co/guide/en/elasticsearch/client/php-api/8.15/http-client.html.

Is it correct to say that proxy support is missing from this module?

murrow’s picture

Version: 8.x-7.0-alpha1 » 8.0.0-alpha2

Changing the version number

sokru’s picture

Title: elasticsearch connector unable to connect to cluster due to proxy dependency » Allow defining proxy settings for connection
Version: 8.0.0-alpha2 » 8.0.x-dev
Category: Support request » Task
Priority: Critical » Normal
Issue tags: +Needs issue summary update

@murrow, you're correct there's currently no support for proxy connection.

murrow’s picture

murrow’s picture

@sokru, I have updated the issue summary with a proposed solution. The dependency injection part will disturb the code a bit, but I don't think the other tasks will have much impact. What are your thoughts?

murrow’s picture

Issue summary: View changes

Further update to the summary problem section

murrow’s picture

Issue summary: View changes

Adding test task

hexaki made their first commit to this issue’s fork.

hexaki changed the visibility of the branch 3321300-allow-defining-proxy to hidden.

hexaki changed the visibility of the branch 3321300-allow-defining-proxy to hidden.

hexaki changed the visibility of the branch 3321300-allow-defining-proxy to active.

hexaki’s picture

Status: Active » Needs work

I pushed a solution using ClientBuilder::setHttpClient.
I also made it possible for Connector extending the StandardConnector to alter the Client without having to redefine it completely.

hexaki’s picture

Status: Needs work » Needs review

mparker17 made their first commit to this issue’s fork.

mparker17’s picture

Sorry for the noise; I haven't had a chance to review the change yet.

I rebased onto the latest 8.0.x, so the phpstan pipeline wouldn't warn us about a deprecation

mparker17’s picture

Category: Task » Feature request
Issue summary: View changes
Status: Needs review » Postponed (maintainer needs more info)
Issue tags: -proxy, -proxy variables +Needs tests

@murrow, @hexaki, thank you both very much for your contributions!

I've had a chance to look at the code in the merge request, and I have some feedback.

Before I give my feedback, note that I am assuming that, because you moved the Issue Status to "Needs review", you have finished implementing the full solution that you want. If that's not the case (i.e.: you still have work to do), please move it back to "Needs work" until you're done! Thank you! (it's hard for me to tell because I don't have the context that you do — I've never needed to connect to Elasticsearch through a proxy!)

Without further ado, here is my feedback...

  1. The Issue Title says "Allow defining proxy settings for connection", so I was expecting to see some configuration and/or UI in the Merge Request to do that.
    (note that I may have misunderstood the Issue Title, and my expectation may be wrong!)
    1. The code in the Merge Request seems to change the specifics of how the Elasticsearch Client object gets built. Has the Issue Title become out-of-sync with the solution? If so, can you update the Issue Title?
      • For example, If you're using some other module (e.g.: cURL HTTP Request) or service (e.g.: Guzzle) to change the proxy settings, please explain what you're doing, so that I understand! (this will also document a solution for other community members who need to do the same thing that you are doing but don't know how!)
  2. In the Issue Summary, under Proposed resolution, it says "We should be able to use the elasticsearch/elasticsearch ClientBuilder::setHttpClientOptions method to add any Drupal HTTP proxy settings into the call if we have Guzzle and Curl as our transports." so I was expecting to see a call to setHttpClientOptions() in the merge request, but I don't see anything like that.
    (note that I may have misunderstood the issue summary and my expectation may be wrong)
    1. If this is no longer the solution we're going for, can you update the issue summary to explain what the new solution is?
  3. Would you be able to update the Remaining tasks in the Issue Summary to explain what else needs to be done? @sokru added the Needs issue summary update tag, please remove when you're done updating the issue summary.
  4. The merge request doesn't have any automated tests. Automated tests ultimately benefit you, because they ensure that future changes to the module contributed by other people won't break the functionality that your site depends on. If you need help writing tests, please ask us! I've added the Needs tests tag, please remove it when you've added some tests.

Since I have some questions for you, I'm changing the status to Postponed (maintainer needs more info). When you've answered the questions, please move the issue back to Needs review if you're done, or Needs work if you have more work to do!


Some other notes...

I fixed a spelling mistake in a docblock in the code.

I've updated the Issue Summary a bit based on my understanding of the problem and your solution. Please review it, and fix it if I've made a mistake.

I removed the proxy and proxy variables issue tags, as those are non-standard. See this list of standard tags.

I changed this to a "Feature request" to reflect that you're asking for a feature that doesn't currently exist in the module. See also @sokru's comment in #4.

hexaki’s picture

Status: Postponed (maintainer needs more info) » Needs work

Hello, thanks @mparker17 for your feedback.

1. My understanding was that the proxy is usually an environment configuration and Drupal already has the means to configure it via the settings.php. So I didn't see the need for the proxy to be redefined in the server configuration.
I'm not used to contrib modules, and I have access to the entire site configuration (config and settings), so it seemed the easiest solution.

Do you rather like a full config solution ?

2. My propsed solution is : Use Drupal's "HttpClient" as in the \Elastic\Elasticsearch\ClientBuilder. This way the proxy is defined by $settings['http_client_config']['proxy']

3. Next step is validating this solution and writing tests.

4. I could use help writing a test for this solution (any guideline is fine).

sweetchuck’s picture

MR96 at commit c0ce23116b39467d4c2bb857d7a75a5d3037c0ef works for me.

settings.php

<?php
$settings['http_client_config'] = [
  'proxy' => [
    'http' => getenv('HTTP_PROXY') ?: NULL,
    'https' => getenv('HTTPS_PROXY') ?: NULL,
    'no' => array_filter(explode(',', getenv('NO_PROXY') ?: '')),
  ],
];
mparker17’s picture

Component: Elasticsearch Connector » Code
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -Needs tests

I rebased onto 8.0.x. I added the ability to use Drupal core's http_client service (which knows about the proxy) from the newer ElasticCloudEndpointConnector and ElasticCloudIdConnector connectors.

I was on the fence about adding a getClientBuilder() method to the Standard connector (or presumably, all connector plugins), but I ultimately decided that, to keep it simple, I wouldn't add it (which adds a bit of duplication). I think it's worth exploring if it would be worth adding a connector base class, but I daresay that's out-of-scope for this issue.

This merge request also changes nearly the same part of the code as #3529647: Fix logging in BasicAuthConnector, meaning there's a chance those two changes will conflict... so I set the HTTP Client before the Hosts: hopefully Git can still figure out how to auto-merge them. If not, it should be relatively simple to resolve a merge conflict if that happens anyway.

I'm not sure how we can test this so I'm removing the "Needs tests" tag. Happily, it seems like pipeline 801875 passed, so that tells me we didn't break anything, although our current CI environment doesn't have a proxy configured.

I would appreciate it if @sabsbecool, @murrow, @hexaki, @sweetchuck, and/or @sokru could test out the updated changes and let me know if it still works.

mparker17’s picture

Version: 8.0.x-dev » 9.0.x-dev
Issue summary: View changes
Issue tags: +needs backport to 8.0.x

Created merge request !204 for the 9.0.x branch; updated this issue's version, and added the "Needs backport" tag so I don't forget to merge the 8.0.x MR.

mparker17’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

I haven't heard back from anyone in 6 days, but I want to make this issue part of the next release, so I'm going to merge this one now.

mparker17’s picture

Had to rebase and fix a merge conflict; then made a follow-up change to make merges easier in the future.

D10 an D11 tests pass, so I'm going to merge.

  • mparker17 committed d2c6181f on 9.0.x
    feat: #3321300 Allow defining proxy settings for connection
    
    By: murrow...
mparker17’s picture

Version: 9.0.x-dev » 8.0.x-dev
Issue summary: View changes
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: -needs backport to 8.0.x

Merged to 9.0.x; merging to 8.0.x next.

  • mparker17 committed e907c101 on 8.0.x authored by hexaki
    feat: #3321300 Allow defining proxy settings for connection
    
    By: murrow...
mparker17’s picture

Issue summary: View changes
Status: Patch (to be ported) » Fixed

Merged to 8.0.x. I will update this when I make a the next release.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

mparker17’s picture

Issue summary: View changes

The changes in this issue were released in elasticsearch_connector-9.0.0-alpha3, and elasticsearch_connector-8.0.0-alpha7

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.