|
| 1 | +[[connecting]] |
| 2 | +== Connecting |
| 3 | + |
| 4 | +This page contains the information you need to connect and use the Client with |
| 5 | +{es}. |
| 6 | + |
| 7 | +**On this page** |
| 8 | + |
| 9 | +* <<auth-ec, Elastic Cloud>> |
| 10 | +* <<auth-http, Security by default (HTTPS)>> |
| 11 | + |
| 12 | +[discrete] |
| 13 | +[[auth-ec]] |
| 14 | +=== Elastic Cloud |
| 15 | + |
| 16 | +You can connect to https://www.elastic.co/cloud/[Elastic Cloud] using an **API key** |
| 17 | +and a **Cloud ID**: |
| 18 | + |
| 19 | +[source,php] |
| 20 | +---- |
| 21 | +$client = ClientBuilder::create() |
| 22 | + ->setElasticCloudId('<cloud-id>') |
| 23 | + ->setApiKey('<api-key>') |
| 24 | + ->build(); |
| 25 | +---- |
| 26 | + |
| 27 | +Where <cloud-id> and <api-key> can be retrieved using the Elastic Cloud web UI. |
| 28 | + |
| 29 | +You can get the `Cloud ID` from the `My deployment` page of your dashboard (see the red |
| 30 | +rectangle reported in the screenshot). |
| 31 | + |
| 32 | +image::images/cloud_id.png[alt="Elastic Cloud ID",align="center"] |
| 33 | + |
| 34 | +You can generate an `API key` in the `Management` page under the section `Security`. |
| 35 | + |
| 36 | +image::images/create_api_key.png[alt="Create API key",align="center"] |
| 37 | + |
| 38 | +When you click on `Create API key` button you can choose a name and set the other |
| 39 | +options (eg. restrict privileges, expire after time, etc). |
| 40 | + |
| 41 | +image::images/api_key_name.png[alt="Choose an API name",align="center"] |
| 42 | + |
| 43 | +After this step you will get the `API key`in the API keys page. |
| 44 | + |
| 45 | +image::images/cloud_api_key.png[alt="Cloud API key",align="center"] |
| 46 | + |
| 47 | +**IMPORTANT**: you need to copy and store the `API key`in a secure place, since you will not |
| 48 | +be able to view it again in Elastic Cloud. |
| 49 | + |
| 50 | + |
| 51 | +[discrete] |
| 52 | +[[auth-http]] |
| 53 | +=== Security by default (HTTPS) |
| 54 | + |
| 55 | +{es} 8.0 offers https://www.elastic.co/blog/introducing-simplified-elastic-stack-security[security by default], |
| 56 | +that means it uses https://en.wikipedia.org/wiki/Transport_Layer_Security[TLS] |
| 57 | +for protect the communication between client and server. |
| 58 | + |
| 59 | +In order to configure `elasticsearch-php` for connecting to {es} 8.0 we |
| 60 | +need to have the certificate authority file (CA). |
| 61 | + |
| 62 | +You can install {es} in different ways, for instance using https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html[Docker] |
| 63 | +you need to execute the followind command: |
| 64 | + |
| 65 | +[source,shell] |
| 66 | +-------------------------- |
| 67 | +docker pull docker.elastic.co/elasticsearch/elasticsearch:8.0.1 |
| 68 | +-------------------------- |
| 69 | + |
| 70 | +Once you have the docker image installed you can execute {es}, |
| 71 | +for instance using a single-node cluster configuration, as follows: |
| 72 | + |
| 73 | +[source,shell] |
| 74 | +-------------------------- |
| 75 | +docker network create elastic |
| 76 | +docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.0.1 |
| 77 | +-------------------------- |
| 78 | + |
| 79 | +This command creates an `elastic` Docker network and start {es} |
| 80 | +using the port `9200` (default). |
| 81 | + |
| 82 | +When you run the docker image a password is generated for the `elastic` user |
| 83 | +and it's printed to the terminal (you might need to scroll back a bit in the terminal |
| 84 | +to view it). You have to copy it since we will need to connect to {es}. |
| 85 | + |
| 86 | +Now that {es} is running we can get the `http_ca.crt` file certificate. |
| 87 | +We need to copy it from the docker instance, using the following command: |
| 88 | + |
| 89 | +[source,shell] |
| 90 | +-------------------------- |
| 91 | +docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt . |
| 92 | +-------------------------- |
| 93 | + |
| 94 | +Once we have the `http_ca.crt` certificate and the `password`, copied during the |
| 95 | +start of {es} , we can use it to connect with `elasticsearch-php` |
| 96 | +as follows: |
| 97 | + |
| 98 | +[source,php] |
| 99 | +-------------------------- |
| 100 | +$client = ClientBuilder::create() |
| 101 | + ->setHosts(['https://localhost:9200']) |
| 102 | + ->setBasicAuthentication('elastic', 'password copied during Elasticsearch start') |
| 103 | + ->setCABundle('path/to/http_ca.crt') |
| 104 | + ->build(); |
| 105 | +-------------------------- |
| 106 | + |
| 107 | +For more information about the Docker configuration of Elasticsearch you can |
| 108 | +read the official documentation https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html[here]. |
| 109 | + |
0 commit comments