Skip to content

Commit acefb26

Browse files
committed
add cmd option of insecureSkipVerify to skip ssl verify
1 parent be61274 commit acefb26

12 files changed

+72
-22
lines changed

.golangci.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# https://golangci-lint.run/usage/configuration/
2+
run: {}
3+
output: {}
4+
linters-settings:
5+
govet:
6+
enable:
7+
- nilness
8+
linters: {}
9+
issues:
10+
max-issues-per-linter: 50
11+
max-same-issues: 50

Makefile

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
.PHONY: all elasticdump docker-build img clean
2-
31
DOCKER ?= docker
4-
GOLANG_VERSION ?= 1.21-alpine
2+
GOLANG_VERSION ?= 1.23-alpine
53
IMAGE ?= elasticdump
64
VERSION ?= latest
75
GOOS ?= linux
86
GOARCH ?= amd64
97
GOPROXY ?= $(shell printenv GOPROXY)
108
LDFLAGS ?= "-s -w"
119

10+
.PHONY: all
1211
all: elasticdump
1312

13+
.PHONY: elasticdump
1414
elasticdump:
1515
go build -v -o $@
1616

17+
.PHONY: docker-build
1718
docker-build:
1819
$(DOCKER) run --rm --name elasticdump-build -it \
1920
-v $(shell pwd):/build \
@@ -27,6 +28,7 @@ docker-build:
2728
golang:$(GOLANG_VERSION) \
2829
go build -v -ldflags=$(LDFLAGS) -buildvcs=false -o elasticdump-$(GOOS)-$(GOARCH)
2930

31+
.PHONY: img
3032
img:
3133
$(DOCKER) build \
3234
--build-arg GOLANG_VERSION=$(GOLANG_VERSION) \
@@ -36,5 +38,18 @@ img:
3638
--file docker/Dockerfile \
3739
.
3840

41+
.PHONY: tidy
42+
tidy:
43+
go mod tidy
44+
45+
.PHONY: lint
46+
lint:
47+
golangci-lint run -v --timeout 10m ./cmd/... ./pkg/...
48+
49+
.PHONY: test
50+
test:
51+
go test -count=1 ./cmd/... ./pkg/...
52+
53+
.PHONY: clean
3954
clean:
4055
rm -fr elasticdump*

cmd/base.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import (
1717
)
1818

1919
type BaseConfig struct {
20-
Host string `json:"host"`
21-
Index string `json:"index"`
20+
Host string `json:"host"`
21+
Index string `json:"index"`
22+
InsecureSkipVerify bool `json:"insecure_skip_verify"`
2223
}
2324

2425
func addBaseConfigFlags(flagSet *flag.FlagSet, cfg *BaseConfig) {
2526
flagSet.StringVar(&cfg.Host, "host", cfg.Host, "elasticsearch host: http://<user>:<password>@<host>:<port>")
2627
flagSet.StringVar(&cfg.Index, "index", cfg.Index, "elasticsearch index name")
28+
flagSet.BoolVarP(&cfg.InsecureSkipVerify, "insecure-skip-verify", "k", cfg.InsecureSkipVerify, "skip verify tls certificate")
2729

2830
klogSet := gflag.NewFlagSet(os.Args[0], gflag.ContinueOnError)
2931
klog.InitFlags(klogSet)
@@ -33,8 +35,9 @@ func addBaseConfigFlags(flagSet *flag.FlagSet, cfg *BaseConfig) {
3335

3436
func newBaseConfig() *BaseConfig {
3537
return &BaseConfig{
36-
Host: "http://localhost:9200",
37-
Index: "elasticdumptest",
38+
Host: "http://localhost:9200",
39+
Index: "elasticdumptest",
40+
InsecureSkipVerify: false,
3841
}
3942
}
4043

cmd/delete_index.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"k8s.io/klog"
2020
)
2121

22-
func newCmdDeleteIndex(out io.Writer) *cobra.Command {
22+
func newCmdDeleteIndex(_ io.Writer) *cobra.Command {
2323
cfg := newBaseConfig()
2424
cmd := &cobra.Command{
2525
Use: "index",
@@ -37,7 +37,7 @@ func newCmdDeleteIndex(out io.Writer) *cobra.Command {
3737
index := cfg.Index
3838
klog.V(5).Infof("deleting index: %s\n", index)
3939
startTime := time.Now()
40-
client, err := helpers.NewElasticSearchClient(cfg.Host)
40+
client, err := helpers.NewElasticSearchClient(cfg.Host, cfg.InsecureSkipVerify)
4141
if err != nil {
4242
return err
4343
}

cmd/dump_data.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"k8s.io/klog"
2121
)
2222

23-
func newCmdDumpData(out io.Writer) *cobra.Command {
23+
func newCmdDumpData(_ io.Writer) *cobra.Command {
2424
type extraOption struct {
2525
OutputFile string
2626
Batch int
@@ -51,7 +51,7 @@ func newCmdDumpData(out io.Writer) *cobra.Command {
5151
},
5252
RunE: func(cmd *cobra.Command, args []string) error {
5353
startTime := time.Now()
54-
client, err := helpers.NewElasticSearchClient(cfg.Host)
54+
client, err := helpers.NewElasticSearchClient(cfg.Host, cfg.InsecureSkipVerify)
5555
if err != nil {
5656
return err
5757
}

cmd/dump_mapping.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"k8s.io/klog"
2020
)
2121

22-
func newCmdDumpMapping(out io.Writer) *cobra.Command {
22+
func newCmdDumpMapping(_ io.Writer) *cobra.Command {
2323
type extraOption struct {
2424
OutputFile string
2525
}
@@ -42,7 +42,7 @@ func newCmdDumpMapping(out io.Writer) *cobra.Command {
4242
},
4343
RunE: func(cmd *cobra.Command, args []string) error {
4444
startTime := time.Now()
45-
client, err := helpers.NewElasticSearchClient(helpers.PathJoin(cfg.Host, cfg.Index))
45+
client, err := helpers.NewElasticSearchClient(helpers.PathJoin(cfg.Host, cfg.Index), cfg.InsecureSkipVerify)
4646
if err != nil {
4747
return err
4848
}

cmd/gen_test_data.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"k8s.io/klog"
1717
)
1818

19-
func newCmdTestGenData(out io.Writer) *cobra.Command {
19+
func newCmdTestGenData(_ io.Writer) *cobra.Command {
2020
type extraOption struct {
2121
Epoch int
2222
Batch int
@@ -41,7 +41,7 @@ func newCmdTestGenData(out io.Writer) *cobra.Command {
4141
return nil
4242
},
4343
RunE: func(cmd *cobra.Command, args []string) error {
44-
client, err := helpers.NewElasticSearchClient(cfg.Host)
44+
client, err := helpers.NewElasticSearchClient(cfg.Host, cfg.InsecureSkipVerify)
4545
if err != nil {
4646
return err
4747
}

cmd/load_data.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"k8s.io/klog"
1919
)
2020

21-
func newCmdLoadData(out io.Writer) *cobra.Command {
21+
func newCmdLoadData(_ io.Writer) *cobra.Command {
2222
type extraOption struct {
2323
InputFile string
2424
Batch int
@@ -50,7 +50,7 @@ func newCmdLoadData(out io.Writer) *cobra.Command {
5050
return nil
5151
},
5252
RunE: func(cmd *cobra.Command, args []string) error {
53-
client, err := helpers.NewElasticSearchClient(cfg.Host)
53+
client, err := helpers.NewElasticSearchClient(cfg.Host, cfg.InsecureSkipVerify)
5454
if err != nil {
5555
return err
5656
}

cmd/load_mapping.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"k8s.io/klog"
2121
)
2222

23-
func newCmdLoadMapping(out io.Writer) *cobra.Command {
23+
func newCmdLoadMapping(_ io.Writer) *cobra.Command {
2424
type extraOption struct {
2525
InputFile string
2626
Delete bool `json:"delete"`
@@ -46,7 +46,7 @@ func newCmdLoadMapping(out io.Writer) *cobra.Command {
4646
return nil
4747
},
4848
RunE: func(cmd *cobra.Command, args []string) error {
49-
client, err := helpers.NewElasticSearchClient(cfg.Host)
49+
client, err := helpers.NewElasticSearchClient(cfg.Host, cfg.InsecureSkipVerify)
5050
if err != nil {
5151
return err
5252
}

docker/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Licensed under the MIT License.
44

5-
ARG GOLANG_VERSION=1.21-alpine
5+
ARG GOLANG_VERSION=1.23-alpine
66
FROM golang:$GOLANG_VERSION as build
77

88
COPY ./ /build

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/shinexia/elasticdump
22

3-
go 1.21
3+
go 1.23
44

55
require (
66
github.com/elastic/go-elasticsearch/v7 v7.17.10

pkg/helpers/elastic.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ Licensed under the MIT License.
77
package helpers
88

99
import (
10+
"crypto/tls"
11+
"net"
12+
"net/http"
1013
"os"
1114
"strings"
15+
"time"
1216

1317
"github.com/elastic/go-elasticsearch/v7"
1418
"github.com/elastic/go-elasticsearch/v7/estransport"
@@ -17,13 +21,30 @@ import (
1721
)
1822

1923
// NewElasticSearchClient create elasticsearch.Client
20-
func NewElasticSearchClient(host string) (*elasticsearch.Client, error) {
24+
func NewElasticSearchClient(host string, insecureSkipVerify bool) (*elasticsearch.Client, error) {
2125
conf := elasticsearch.Config{
2226
Addresses: []string{host},
2327
EnableMetrics: true,
2428
EnableDebugLogger: bool(klog.V(4)),
2529
Logger: nil,
2630
}
31+
if insecureSkipVerify {
32+
conf.Transport = &http.Transport{
33+
Proxy: http.ProxyFromEnvironment,
34+
DialContext: (&net.Dialer{
35+
Timeout: 30 * time.Second,
36+
KeepAlive: 30 * time.Second,
37+
}).DialContext,
38+
ForceAttemptHTTP2: true,
39+
MaxIdleConns: 100,
40+
IdleConnTimeout: 90 * time.Second,
41+
TLSHandshakeTimeout: 10 * time.Second,
42+
ExpectContinueTimeout: 1 * time.Second,
43+
TLSClientConfig: &tls.Config{
44+
InsecureSkipVerify: true,
45+
},
46+
}
47+
}
2748
if klog.V(4) {
2849
conf.Logger = &estransport.TextLogger{
2950
Output: os.Stdout,

0 commit comments

Comments
 (0)