connecting.asciidoc 3.7 KB

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