()
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | log.SetFlags(0) |
| 38 | |
| 39 | var ( |
| 40 | err error |
| 41 | |
| 42 | // --> Configure the path to the certificate authority and the password |
| 43 | // |
| 44 | cacert = flag.String("cacert", "certificates/ca/ca.crt", "Path to the file with certificate authority") |
| 45 | password = flag.String("password", "elastic", "Elasticsearch password") |
| 46 | ) |
| 47 | flag.Parse() |
| 48 | |
| 49 | ca, err := ioutil.ReadFile(*cacert) |
| 50 | if err != nil { |
| 51 | log.Fatalf("ERROR: Unable to read CA from %q: %s", *cacert, err) |
| 52 | } |
| 53 | |
| 54 | // --> Clone the default HTTP transport |
| 55 | // |
| 56 | tp := http.DefaultTransport.(*http.Transport).Clone() |
| 57 | |
| 58 | // --> Initialize the set of root certificate authorities |
| 59 | // |
| 60 | if tp.TLSClientConfig.RootCAs, err = x509.SystemCertPool(); err != nil { |
| 61 | log.Fatalf("ERROR: Problem adding system CA: %s", err) |
| 62 | } |
| 63 | |
| 64 | // --> Add the custom certificate authority |
| 65 | // |
| 66 | if ok := tp.TLSClientConfig.RootCAs.AppendCertsFromPEM(ca); !ok { |
| 67 | log.Fatalf("ERROR: Problem adding CA from file %q", *cacert) |
| 68 | } |
| 69 | |
| 70 | es, err := elasticsearch.New( |
| 71 | elasticsearch.WithAddresses("https://localhost:9200"), |
| 72 | elasticsearch.WithBasicAuth("elastic", *password), |
| 73 | // --> Pass the transport to the client |
| 74 | // |
| 75 | elasticsearch.WithTransportOptions(elastictransport.WithTransport(tp)), |
| 76 | ) |
| 77 | if err != nil { |
| 78 | log.Fatalf("ERROR: Unable to create client: %s", err) |
| 79 | } |
| 80 | defer func() { |
| 81 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 82 | defer cancel() |
| 83 | if err := es.Close(ctx); err != nil { |
| 84 | log.Fatalf("Error closing the client: %s\n", err) |
| 85 | } |
| 86 | }() |
| 87 | |
| 88 | res, err := es.Info() |
| 89 | if err != nil { |
| 90 | log.Fatalf("ERROR: Unable to get response: %s", err) |
| 91 | } |
| 92 | defer res.Body.Close() |
| 93 |
nothing calls this directly
no test coverage detected