jolicode / elastically

🔍 JoliCode's Elastica wrapper to bootstrap Elasticsearch PHP integrations
248 stars 36 forks source link

Document how to connect with authentication #154

Open tacman opened 1 year ago

tacman commented 1 year ago

I've installed the docker version of OpenSearch, which appears to require authentication.

https://opensearch.org/docs/1.2/#docker-quickstart

So this works as expected:

curl -XGET --insecure -u 'admin:admin' 'https://localhost:9200'

I've followed the instruction at https://github.com/jolicode/elastically#configuration-1 to configure the bundle (though I wish it had a recipe to add the bundle and such), but the sample code fails with an error 52, I assume because it's not authenticated

How do I configure authentication in the .env file? I tried this, but it doesn't work.

# .env
ELASTICSEARCH_HOST=https://admin:admin@localhost:9200

I've also tried tweaking the .yaml

# config/packages/elastically.yaml
elastically:
  connections:
    default:
      client:
        host:                '%env(ELASTICSEARCH_HOST)%'
        username: admin
        password: admin

This is probably related to #112

Korbeil commented 1 year ago

Hey @tacman and thanks for your feedback !

Since elastically is based upon ruflin/elastica library, we do support the same configuration schemes. For a username/password authentication, you can use the same as Elastica: https://github.com/ruflin/Elastica/pull/1030

Sadly, in your case, I do not see them implementing this in the DSN configuration section. So will you have to declare the configuration in separate env vars, here is an example:

ENV:

ELASTICSEARCH_HOST=https://admin:admin@localhost:9200
ELASTICSEARCH_USERNAME=admin
ELASTICSEARCH_PASSWORD=admin

YAML;

elastically:
  connections:
    default:
      client:
        host: %env(ELASTICSEARCH_HOST)%
        username: %env(ELASTICSEARCH_USERNAME)%
        password: %env(ELASTICSEARCH_PASSWORD)%

Also, if you are using the https://github.com/jolicode/elastically/blob/master/src/Transport/HttpClientTransport.php transport we provide, we do not support this configuration keys. You will have to add authentication headers in your declared Symfony HttpClient.