-
Notifications
You must be signed in to change notification settings - Fork 54
/
config.go
154 lines (131 loc) · 5.05 KB
/
config.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
154
/********************************************************************************
* Copyright 2020 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package pkg
import (
"crypto/tls"
"fmt"
"net/url"
"reflect"
"strconv"
)
var TlsSchemes = []string{"tcps", "ssl", "tls", "redis"}
// X509KeyPairCreator defines the function signature for creating a tls.Certificate based on PEM encoding.
type X509KeyPairCreator func(certPEMBlock []byte, keyPEMBlock []byte) (tls.Certificate, error)
// X509KeyLoader defines a function signature for loading a tls.Certificate from cert and key files.
type X509KeyLoader func(certFile string, keyFile string) (tls.Certificate, error)
type TlsConfigurationOptions struct {
SkipCertVerify bool
CertFile string
KeyFile string
KeyPEMBlock string
CertPEMBlock string
}
func CreateDefaultTlsConfigurationOptions() TlsConfigurationOptions {
return TlsConfigurationOptions{
SkipCertVerify: false,
CertFile: "",
KeyFile: "",
KeyPEMBlock: "",
CertPEMBlock: "",
}
}
// generateTLSForClientClientOptions creates a tls.Config which can be used when configuring the underlying MQTT client.
// If TLS is not needed then nil will be returned which can be used to signal no TLS is needed to the MQTT client.
func GenerateTLSForClientClientOptions(
brokerURL string,
tlsConfigurationOptions TlsConfigurationOptions,
certCreator X509KeyPairCreator,
certLoader X509KeyLoader) (*tls.Config, error) {
// Nothing to do if the CertFile, KeyFile, CertPEMBlock, and KeyPEMBlock properties are not provided.
if len(tlsConfigurationOptions.CertFile) <= 0 && len(tlsConfigurationOptions.KeyFile) <= 0 &&
len(tlsConfigurationOptions.CertPEMBlock) <= 0 && len(tlsConfigurationOptions.KeyPEMBlock) <= 0 {
return nil, nil
}
parsedBrokerURL, err := url.Parse(brokerURL)
if err != nil {
return nil, NewBrokerURLErr(fmt.Sprintf("Failed to parse broker: %v", err))
}
for _, scheme := range TlsSchemes {
if parsedBrokerURL.Scheme != scheme {
continue
}
cert, err := generateCertificate(tlsConfigurationOptions, certCreator, certLoader)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
ClientCAs: nil,
InsecureSkipVerify: tlsConfigurationOptions.SkipCertVerify,
Certificates: []tls.Certificate{cert},
}
return tlsConfig, nil
}
// The scheme being used either does not require TLS or is not supported with this configuration setup.
return nil, nil
}
// generateCertificate creates a x509 certificate by either loading it from an existing cert and key files, or creates
// a cert and key from the provided PEM bytes.
func generateCertificate(
tlsConfigurationOptions TlsConfigurationOptions,
certCreator X509KeyPairCreator,
certLoader X509KeyLoader) (tls.Certificate, error) {
var cert tls.Certificate
var err error
if tlsConfigurationOptions.KeyPEMBlock != "" && tlsConfigurationOptions.CertPEMBlock != "" {
cert, err = certCreator([]byte(tlsConfigurationOptions.CertPEMBlock), []byte(tlsConfigurationOptions.KeyPEMBlock))
} else {
cert, err = certLoader(tlsConfigurationOptions.CertFile, tlsConfigurationOptions.KeyFile)
}
if err != nil {
return cert, NewCertificateErr(fmt.Sprintf("Failed loading x509 data: %v", err))
}
return cert, nil
}
// load by reflect to check map key and then fetch the value.
// This function ignores properties that have not been provided from the source. Therefore it is recommended to provide
// a destination struct with reasonable defaults.
//
// NOTE: This logic was borrowed from device-mqtt-go and some additional logic was added to accommodate more types.
// https://github.com/edgexfoundry/device-mqtt-go/blob/a0d50c6e03a7f7dcb28f133885c803ffad3ec502/internal/driver/config.go#L74-L101
func Load(config map[string]string, des interface{}) error {
val := reflect.ValueOf(des).Elem()
for i := 0; i < val.NumField(); i++ {
typeField := val.Type().Field(i)
valueField := val.Field(i)
val, ok := config[typeField.Name]
if !ok {
// Ignore the property if the value is not provided
continue
}
switch valueField.Kind() {
case reflect.Int:
intVal, err := strconv.Atoi(val)
if err != nil {
return err
}
valueField.SetInt(int64(intVal))
case reflect.String:
valueField.SetString(val)
case reflect.Bool:
boolVal, err := strconv.ParseBool(val)
if err != nil {
return err
}
valueField.SetBool(boolVal)
default:
return fmt.Errorf("none supported value type %v ,%v", valueField.Kind(), typeField.Name)
}
}
return nil
}