forked from project-flogo/core
/
client.go
120 lines (103 loc) · 2.43 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package ssl
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"github.com/VSBLTY-Inc/core/data/coerce"
"github.com/VSBLTY-Inc/core/support/log"
)
const ConfigSchema = `
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"caFile": {
"type": "string"
},
"certFile": {
"type": "string"
},
"keyFile": {
"type": "string"
},
"skipVerify": {
"type": "boolean"
},
"useSystemCert": {
"type": "boolean"
}
}
}`
type Config struct {
CAFile string `json:"caFile"`
CertFile string `json:"certFile"`
KeyFile string `json:"keyFile"`
SkipVerify bool `json:"skipVerify"`
UseSystemCert bool `json:"useSystemCert"`
}
func (i *Config) ToMap() map[string]interface{} {
return map[string]interface{}{
"caFile": i.CAFile,
"certFile": i.CertFile,
"keyFile": i.KeyFile,
"skipVerify": i.SkipVerify,
"useSystemCert": i.UseSystemCert,
}
}
func (i *Config) FromMap(values map[string]interface{}) error {
var err error
i.CAFile, err = coerce.ToString(values["caFile"])
if err != nil {
return err
}
i.CertFile, err = coerce.ToString(values["certFile"])
if err != nil {
return err
}
i.KeyFile, err = coerce.ToString(values["keyFile"])
if err != nil {
return err
}
i.SkipVerify, err = coerce.ToBool(values["skipVerify"])
if err != nil {
return err
}
i.UseSystemCert, err = coerce.ToBool(values["useSystemCert"])
if err != nil {
return err
}
return nil
}
func NewClientTLSConfig(config *Config) (*tls.Config, error) {
tlsConfig := &tls.Config{
//MinVersion: tls.VersionTLS12,
InsecureSkipVerify: config.SkipVerify,
}
var caCertPool *x509.CertPool
if config.UseSystemCert {
caCertPool, _ = x509.SystemCertPool()
if caCertPool == nil {
log.RootLogger().Warnf("unable to get system cert pool, using empty pool")
}
}
if caCertPool == nil {
caCertPool = x509.NewCertPool()
}
if config.CAFile != "" {
caCert, err := ioutil.ReadFile(config.CAFile)
if err != nil {
return nil, fmt.Errorf("unable to read CAfile '%s' : %v", config.CAFile, err)
}
caCertPool.AppendCertsFromPEM(caCert)
}
tlsConfig.RootCAs = caCertPool
if config.CertFile != "" && config.KeyFile != "" {
cert, err := tls.LoadX509KeyPair(config.CertFile, config.CertFile)
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
return tlsConfig, nil
}