Skip to content

Commit 860e974

Browse files
authored
Merge pull request #8 from Trendyol/feature/improve-es-ping
Add control for elasticsearch unauthorized
2 parents db06835 + 500b396 commit 860e974

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

elasticsearch/client/client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"github.com/Trendyol/go-pq-cdc-elasticsearch/config"
55
"github.com/elastic/go-elasticsearch/v7"
6+
"github.com/go-playground/errors"
67
)
78

89
func NewClient(config *config.Config) (*elasticsearch.Client, error) {
@@ -21,9 +22,14 @@ func NewClient(config *config.Config) (*elasticsearch.Client, error) {
2122
return nil, err
2223
}
2324

24-
if _, err = client.Ping(); err != nil {
25+
r, err := client.Ping()
26+
if err != nil {
2527
return nil, err
2628
}
2729

30+
if r.StatusCode == 401 {
31+
return nil, errors.New("unauthorized")
32+
}
33+
2834
return client, nil
2935
}

example/simple/docker-compose.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
POSTGRES_DB: "es_cdc_db"
1111
POSTGRES_HOST_AUTH_METHOD: trust
1212
network_mode: "host"
13+
1314
es01:
1415
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
1516
labels:
@@ -26,7 +27,7 @@ services:
2627
- discovery.type=single-node
2728
- ELASTIC_PASSWORD=es_cdc_es_pass
2829
- bootstrap.memory_lock=true
29-
- xpack.security.enabled=false
30+
- xpack.security.enabled=true
3031
- xpack.security.http.ssl.enabled=false
3132
- xpack.security.transport.ssl.enabled=false
3233
ulimits:
@@ -37,11 +38,12 @@ services:
3738
test:
3839
[
3940
"CMD-SHELL",
40-
"curl -I -o /dev/null http://localhost:9200 -w '%{http_code}' | grep -q '200'",
41+
"curl -u elastic:es_cdc_es_pass -I -o /dev/null http://localhost:9200 -w '%{http_code}' | grep -q '200'",
4142
]
4243
interval: 10s
4344
timeout: 10s
4445
retries: 120
46+
4547
kibana:
4648
depends_on:
4749
es01:
@@ -58,8 +60,8 @@ services:
5860
environment:
5961
- SERVERNAME=kibana
6062
- ELASTICSEARCH_HOSTS=http://es01:9200
61-
- ELASTICSEARCH_USERNAME=kibana_system
62-
- ELASTICSEARCH_PASSWORD=cdc_pass
63+
- ELASTICSEARCH_USERNAME=elastic
64+
- ELASTICSEARCH_PASSWORD=es_cdc_es_pass
6365
healthcheck:
6466
test:
6567
[
@@ -74,6 +76,7 @@ volumes:
7476
postgres_data: null
7577
kibana1_data: null
7678
es01_data: null
79+
7780
networks:
7881
cdc_net:
79-
driver: bridge
82+
driver: bridge

example/simple/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func main() {
8383
},
8484
},
8585
Elasticsearch: config.Elasticsearch{
86+
Username: "elastic",
87+
Password: "es_cdc_es_pass",
8688
BatchSizeLimit: 10000,
8789
BatchTickerDuration: time.Millisecond * 100,
8890
TableIndexMapping: map[string]string{

0 commit comments

Comments
 (0)