Skip to content

Commit b31698a

Browse files
authored
Merge pull request #43 from hsshss/insecure_tls
Insecure TLS connection
2 parents 3587bc2 + e3c2363 commit b31698a

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ func realMain() int {
5656

5757
var tlshandler *redisdump.TlsHandler = nil
5858
if c.Tls == true {
59-
tlshandler = redisdump.NewTlsHandler(c.CaCert, c.Cert, c.Key)
59+
tlshandler, err = redisdump.NewTlsHandler(c.CaCert, c.Cert, c.Key, c.Insecure)
60+
if err != nil {
61+
fmt.Fprintln(os.Stderr, err.Error())
62+
return 1
63+
}
6064
}
6165

6266
var serializer func([]string) string

pkg/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
Output string
2020
Silent bool
2121
Tls bool
22+
Insecure bool
2223
CaCert string
2324
Cert string
2425
Key string
@@ -54,6 +55,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
5455
flags.StringVar(&c.Output, "output", "resp", "Output type - can be resp or commands")
5556
flags.BoolVar(&c.Silent, "s", false, "Silent mode (disable logging of progress / stats)")
5657
flags.BoolVar(&c.Tls, "tls", false, "Establish a secure TLS connection")
58+
flags.BoolVar(&c.Insecure, "insecure", false, "Allow insecure TLS connection by skipping cert validation")
5759
flags.StringVar(&c.CaCert, "cacert", "", "CA Certificate file to verify with")
5860
flags.StringVar(&c.Cert, "cert", "", "Private key file to authenticate with")
5961
flags.StringVar(&c.Key, "key", "", "SSL private key file path")

pkg/config/config_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestFromFlags(t *testing.T) {
2121
NWorkers: 10,
2222
WithTTL: true,
2323
Output: "resp",
24+
Insecure: false,
2425
},
2526
},
2627
{
@@ -34,6 +35,7 @@ func TestFromFlags(t *testing.T) {
3435
NWorkers: 10,
3536
WithTTL: true,
3637
Output: "resp",
38+
Insecure: false,
3739
},
3840
},
3941
{
@@ -47,6 +49,7 @@ func TestFromFlags(t *testing.T) {
4749
NWorkers: 10,
4850
WithTTL: false,
4951
Output: "resp",
52+
Insecure: false,
5053
},
5154
},
5255
{
@@ -60,6 +63,22 @@ func TestFromFlags(t *testing.T) {
6063
NWorkers: 5,
6164
WithTTL: true,
6265
Output: "commands",
66+
Insecure: false,
67+
},
68+
},
69+
{
70+
[]string{"-host", "redis", "-port", "1234", "-batchSize", "10", "-user", "test", "-insecure"},
71+
Config{
72+
Db: -1,
73+
Host: "redis",
74+
Port: 1234,
75+
Filter: "*",
76+
BatchSize: 10,
77+
NWorkers: 10,
78+
WithTTL: true,
79+
Output: "resp",
80+
Username: "test",
81+
Insecure: true,
6382
},
6483
},
6584
{
@@ -87,6 +106,7 @@ func TestFromFlags(t *testing.T) {
87106
NWorkers: 10,
88107
WithTTL: true,
89108
Output: "resp",
109+
Insecure: false,
90110
},
91111
},
92112
{
@@ -101,6 +121,7 @@ func TestFromFlags(t *testing.T) {
101121
WithTTL: true,
102122
Output: "resp",
103123
Help: true,
124+
Insecure: false,
104125
},
105126
},
106127
}

pkg/redisdump/tlsutils.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,48 @@ package redisdump
33
import (
44
"crypto/tls"
55
"crypto/x509"
6+
"errors"
67
"fmt"
78
"io/ioutil"
89
)
910

1011
type TlsHandler struct {
12+
skipVerify bool
1113
caCertPath string
1214
certPath string
1315
keyPath string
1416
}
1517

16-
func NewTlsHandler(caCertPath, certPath, keyPath string) *TlsHandler {
18+
func NewTlsHandler(caCertPath, certPath, keyPath string, insecure bool) (*TlsHandler, error) {
1719
if caCertPath == "" && certPath == "" && keyPath == "" {
18-
return nil
20+
if insecure {
21+
return &TlsHandler{
22+
skipVerify: true,
23+
}, nil
24+
} else {
25+
return nil, errors.New("no cert is set. if skip cert validation to set -insecure option")
26+
}
1927
}
2028

2129
return &TlsHandler{
30+
skipVerify: false,
2231
caCertPath: caCertPath,
2332
certPath: certPath,
2433
keyPath: keyPath,
25-
}
34+
}, nil
2635
}
2736

2837
func tlsConfig(tlsHandler *TlsHandler) (*tls.Config, error) {
2938
if tlsHandler == nil {
3039
return nil, nil
3140
}
3241

42+
if tlsHandler.skipVerify {
43+
return &tls.Config{
44+
InsecureSkipVerify: true,
45+
}, nil
46+
}
47+
3348
certPool := x509.NewCertPool()
3449
// ca cert is optional
3550
if tlsHandler.caCertPath != "" {

0 commit comments

Comments
 (0)