forked from hashicorp/consul-template
/
ssl.go
153 lines (127 loc) · 2.82 KB
/
ssl.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package config
import "fmt"
const (
// DefaultSSLVerify is the default value for SSL verification.
DefaultSSLVerify = true
)
// SSLConfig is the configuration for SSL.
type SSLConfig struct {
CaCert *string `mapstructure:"ca_cert"`
CaPath *string `mapstructure:"ca_path"`
Cert *string `mapstructure:"cert"`
Enabled *bool `mapstructure:"enabled"`
Key *string `mapstructure:"key"`
ServerName *string `mapstructure:"server_name"`
Verify *bool `mapstructure:"verify"`
}
// DefaultSSLConfig returns a configuration that is populated with the
// default values.
func DefaultSSLConfig() *SSLConfig {
return &SSLConfig{}
}
// Copy returns a deep copy of this configuration.
func (c *SSLConfig) Copy() *SSLConfig {
if c == nil {
return nil
}
var o SSLConfig
o.CaCert = c.CaCert
o.CaPath = c.CaPath
o.Cert = c.Cert
o.Enabled = c.Enabled
o.Key = c.Key
o.ServerName = c.ServerName
o.Verify = c.Verify
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *SSLConfig) Merge(o *SSLConfig) *SSLConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.Cert != nil {
r.Cert = o.Cert
}
if o.CaCert != nil {
r.CaCert = o.CaCert
}
if o.CaPath != nil {
r.CaPath = o.CaPath
}
if o.Enabled != nil {
r.Enabled = o.Enabled
}
if o.Key != nil {
r.Key = o.Key
}
if o.ServerName != nil {
r.ServerName = o.ServerName
}
if o.Verify != nil {
r.Verify = o.Verify
}
return r
}
// Finalize ensures there no nil pointers.
func (c *SSLConfig) Finalize() {
if c.Enabled == nil {
c.Enabled = Bool(false ||
StringPresent(c.Cert) ||
StringPresent(c.CaCert) ||
StringPresent(c.CaPath) ||
StringPresent(c.Key) ||
StringPresent(c.ServerName) ||
BoolPresent(c.Verify))
}
if c.Cert == nil {
c.Cert = String("")
}
if c.CaCert == nil {
c.CaCert = String("")
}
if c.CaPath == nil {
c.CaPath = String("")
}
if c.Key == nil {
c.Key = String("")
}
if c.ServerName == nil {
c.ServerName = String("")
}
if c.Verify == nil {
c.Verify = Bool(DefaultSSLVerify)
}
}
// GoString defines the printable version of this struct.
func (c *SSLConfig) GoString() string {
if c == nil {
return "(*SSLConfig)(nil)"
}
return fmt.Sprintf("&SSLConfig{"+
"CaCert:%s, "+
"CaPath:%s, "+
"Cert:%s, "+
"Enabled:%s, "+
"Key:%s, "+
"ServerName:%s, "+
"Verify:%s"+
"}",
StringGoString(c.CaCert),
StringGoString(c.CaPath),
StringGoString(c.Cert),
BoolGoString(c.Enabled),
StringGoString(c.Key),
StringGoString(c.ServerName),
BoolGoString(c.Verify),
)
}