MCPcopy
hub / github.com/elastic/go-elasticsearch / main

Function main

_examples/security/tls_with_ca.go:33–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

31)
32
33func main() {
34 log.SetFlags(0)
35
36 var (
37 err error
38
39 // --> Configure the path to the certificate authority and the password
40 //
41 cacert = flag.String("cacert", "certificates/ca/ca.crt", "Path to the file with certificate authority")
42 password = flag.String("password", "elastic", "Elasticsearch password")
43 )
44 flag.Parse()
45
46 // --> Read the certificate from file
47 //
48 cert, err := ioutil.ReadFile(*cacert)
49 if err != nil {
50 log.Fatalf("ERROR: Unable to read CA from %q: %s", *cacert, err)
51 }
52
53 es, err := elasticsearch.New(
54 elasticsearch.WithAddresses("https://localhost:9200"),
55 elasticsearch.WithBasicAuth("elastic", *password),
56 // --> Pass the certificate to the client
57 //
58 elasticsearch.WithCACert(cert),
59 )
60 if err != nil {
61 log.Fatalf("ERROR: Unable to create client: %s", err)
62 }
63 defer func() {
64 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
65 defer cancel()
66 if err := es.Close(ctx); err != nil {
67 log.Fatalf("Error closing the client: %s\n", err)
68 }
69 }()
70
71 res, err := es.Info()
72 if err != nil {
73 log.Fatalf("ERROR: Unable to get response: %s", err)
74 }
75 defer res.Body.Close()
76
77 log.Println(res)
78}

Callers

nothing calls this directly

Calls 4

CloseMethod · 0.65
StringMethod · 0.45
WithTimeoutMethod · 0.45
InfoMethod · 0.45

Tested by

no test coverage detected