I am checking the operation with Logger to see how to specify the Host header, but the output differs depending on the Logger. Also, I am not sure if I am specifying the Host header correctly. SourceCode
package main
import (
"os"
"log"
"net/http"
"crypto/tls"
elasticsearch "github.com/elastic/go-elasticsearch/v7"
estransport "github.com/elastic/go-elasticsearch/v7/estransport"
)
func main() {
cfg := elasticsearch.Config{
Addresses: []string{"https://ssh-tunnel:19200/"},
Username: "elastic",
Password: "password",
Header: http.Header{"Host": []string{"somewhere.es.asia-northeast1.gcp.cloud.es.io"}},
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Logger: &estransport.TextLogger{
//Logger: &estransport.JsonLogger{
//Logger: &estransport.CurlLogger{
Output: os.Stdout,
EnableRequestBody: true,
EnableResponseBody: true,
},
EnableDebugLogger: true,
}
es, _ := elasticsearch.NewClient(cfg)
log.Println(es.Info())
}
Output of TextLogger
golang@c8b920b337fb:~/some_backend/src$ go run main.go
2022-03-18T05:48:12Z GET https://ssh-tunnel:19200/ [status:404 request:182ms]
< {"ok":false,"message":"Unknown resource."}
2022/03/18 14:48:12 [0 <nil>] cannot retrieve informations from Elasticsearch
Output of JSONLogger
golang@c8b920b337fb:~/some_backend/src$ go run main.go
{"@timestamp":"2022-03-18T05:48:58Z","event":{"duration":332604100},"url":{"scheme":"https","domain":"ssh-tunnel","port":19200,"path":"/","query":""},"http":{"request":{"method":"GET"},"response":{"status_code":404,"body":"{\"ok\":false,\"message\":\"Unknown resource.\"}\n"}}}
2022/03/18 14:48:58 [0 <nil>] cannot retrieve informations from Elasticsearch
Output of CurlLogger
golang@c8b920b337fb:~/some_backend/src$ go run main.go
curl -X GET -H 'Host: somewhere.es.asia-northeast1.gcp.cloud.es.io' -H 'X-Elastic-Client-Meta: es=7.17.0,go=1.17.6,t=7.17.0,hc=1.17.6' 'http://localhost:9200/?pretty'
# => 2022-03-18T05:46:51Z [404 Not Found] 224ms
# {
# "ok": false,
# "message": "Unknown resource."
# }
2022/03/18 14:46:51 [0 <nil>] cannot retrieve informations from Elasticsearch
When using curlLogger, https://ssh-tunnel:19200/ has been replaced by http://localhost:9200 For the connection without go-elasticSearch, we are getting the expected 200 OK results. The source code and response results at that time are as follows SourceCode
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
"crypto/tls"
)
func main() {
req, err := http.NewRequest(http.MethodGet, "https://ssh-tunnel:19200/", nil)
if err != nil {
log.Fatal(err)
}
req.Host = "somewhere.es.asia-northeast1.gcp.cloud.es.io"
req.SetBasicAuth("elastic", "password")
client := &http.Client{}
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
dump, _ := httputil.DumpResponse(res, true)
fmt.Printf("%q", dump)
}
Output
golang@c8b920b337fb:~/some_backend/src$ go run main.go
"HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\nDate: Fri, 18 Mar 2022 05:59:29 GMT\r\nX-Cloud-Request-Id: AU_0RjIkSXeNDrC2mMNGUw\r\nX-Elastic-Product: Elasticsearch\r\nX-Found-Handling-Cluster: 7dc5c050851046e69a4a97184c94f5fc\r\nX-Found-Handling-Instance: instance-0000000000\r\n\r\n{\n \"name\" : \"instance-0000000000\",\n \"cluster_name\" : \"7dc5c050851046e69a4a97184c94f5fc\",\n \"cluster_uuid\" : \"_FHW7jXNQkGXsL7oTyCdlg\",\n \"version\" : {\n \"number\" : \"7.16.3\",\n \"build_flavor\" : \"default\",\n \"build_type\" : \"docker\",\n \"build_hash\" : \"4e6e4eab2297e949ec994e688dad46290d018022\",\n \"build_date\" : \"2022-01-06T23:43:02.825887787Z\",\n \"build_snapshot\" : false,\n \"lucene_version\" : \"8.10.1\",\n \"minimum_wire_compatibility_version\" : \"6.8.0\",\n \"minimum_index_compatibility_version\" : \"6.0.0-beta1\"\n },\n \"tagline\" : \"You Know, for Search\"\n}\n"