/
api_server.go
294 lines (221 loc) · 9.63 KB
/
api_server.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"context"
"crypto/tls"
"database/sql"
"errors"
"fmt"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/RedHatInsights/cloud-connector/internal/config"
"github.com/RedHatInsights/cloud-connector/internal/connection_repository"
"github.com/RedHatInsights/cloud-connector/internal/controller/api"
"github.com/RedHatInsights/cloud-connector/internal/mqtt"
"github.com/RedHatInsights/cloud-connector/internal/platform/db"
"github.com/RedHatInsights/cloud-connector/internal/platform/logger"
"github.com/RedHatInsights/cloud-connector/internal/platform/utils"
"github.com/RedHatInsights/cloud-connector/internal/platform/utils/jwt_utils"
"github.com/RedHatInsights/cloud-connector/internal/platform/utils/tls_utils"
"github.com/RedHatInsights/tenant-utils/pkg/tenantid"
"github.com/redhatinsights/platform-go-middlewares/request_id"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
func buildJwtGenerator(cfg *config.Config, mqttClientId string) (jwt_utils.JwtGenerator, error) {
if cfg.MqttBrokerJwtGeneratorImpl == jwt_utils.RsaTokenGenerator {
return jwt_utils.NewRSABasedJwtGenerator(cfg.JwtPrivateKeyFile, mqttClientId, cfg.JwtTokenExpiry)
} else if cfg.MqttBrokerJwtGeneratorImpl == jwt_utils.FileTokenGenerator {
return jwt_utils.NewFileBasedJwtGenerator(cfg.MqttBrokerJwtFile)
} else {
errorMsg := "Invalid JWT generator configured for the MQTT connection"
logger.Log.Error(errorMsg)
return nil, errors.New(errorMsg)
}
}
func buildMqttClientId(cfg *config.Config) (string, error) {
if cfg.MqttUseHostnameAsClientId == true {
hostname, err := os.Hostname()
if err != nil {
logger.Log.WithFields(logrus.Fields{"error": err}).Error("Unable to determine hostname to use as client_id for MQTT connection")
return "", err
}
return hostname, nil
} else if cfg.MqttClientId != "" {
return cfg.MqttClientId, nil
} else {
errorMsg := "Unable to determine what to use as the client_id for MQTT connection"
logger.Log.Error(errorMsg)
return "", errors.New(errorMsg)
}
}
func buildDefaultMqttBrokerConfigFuncList(brokerUrl string, tlsConfig *tls.Config, cfg *config.Config) ([]mqtt.MqttClientOptionsFunc, error) {
u, err := url.Parse(brokerUrl)
if err != nil {
logger.Log.WithFields(logrus.Fields{"error": err}).Error("Unable to determine protocol for the MQTT connection")
return nil, err
}
brokerConfigFuncs := []mqtt.MqttClientOptionsFunc{}
if tlsConfig != nil {
brokerConfigFuncs = append(brokerConfigFuncs, mqtt.WithTlsConfig(tlsConfig))
}
mqttClientId, err := buildMqttClientId(cfg)
if err != nil {
return nil, err
}
brokerConfigFuncs = append(brokerConfigFuncs, mqtt.WithClientID(mqttClientId))
if u.Scheme == "wss" { //Rethink this check - jwt also works over TLS
jwtGenerator, err := buildJwtGenerator(cfg, mqttClientId)
if err != nil {
logger.Log.WithFields(logrus.Fields{"error": err}).Error("Unable to instantiate a JWT generator for the MQTT connection")
return nil, err
}
brokerConfigFuncs = append(brokerConfigFuncs, mqtt.WithJwtAsHttpHeader(jwtGenerator))
brokerConfigFuncs = append(brokerConfigFuncs, mqtt.WithJwtReconnectingHandler(jwtGenerator))
}
brokerConfigFuncs = append(brokerConfigFuncs, mqtt.WithProtocolVersion(4))
brokerConfigFuncs = append(brokerConfigFuncs, mqtt.WithConnectionLostHandler(logMqttConnectionLostHandler))
return brokerConfigFuncs, nil
}
func startCloudConnectorApiServer(mgmtAddr string) {
logger.Log.Info("Starting Cloud-Connector service")
cfg := config.GetConfig()
logger.Log.Info("Cloud-Connector configuration:\n", cfg)
database, err := db.InitializeDatabaseConnection(cfg)
if err != nil {
logger.LogFatalError("Unable to connect to database: ", err)
}
tlsConfigFuncs, err := buildBrokerTlsConfigFuncList(cfg)
if err != nil {
logger.LogFatalError("TLS configuration error for MQTT Broker connection", err)
}
tlsConfig, err := tls_utils.NewTlsConfig(tlsConfigFuncs...)
if err != nil {
logger.LogFatalError("Unable to configure TLS for MQTT Broker connection", err)
}
brokerOptions, err := buildDefaultMqttBrokerConfigFuncList(cfg.MqttBrokerAddress, tlsConfig, cfg)
if err != nil {
logger.LogFatalError("Unable to configure MQTT Broker connection", err)
}
connectedChan := make(chan struct{})
brokerOptions = append(brokerOptions, mqtt.WithOnConnectHandler(notifyOnIntialMqttConnection(connectedChan)))
mqttConnectionFailedChan := make(chan error)
brokerOptions = buildOnConnectionLostMqttOptions(cfg, mqttConnectionFailedChan, brokerOptions)
mqttClient, err := mqtt.CreateBrokerConnection(cfg.MqttBrokerAddress, brokerOptions...)
if err != nil {
logger.LogFatalError("Unable to establish MQTT Broker connection", err)
}
select {
case <-connectedChan:
break
case <-time.After(2 * time.Second):
logger.Log.Fatal("Failed to connect")
}
mqttTopicBuilder := mqtt.NewTopicBuilder(cfg.MqttTopicPrefix)
proxyFactory, err := mqtt.NewConnectorClientMQTTProxyFactory(cfg, mqttClient, mqttTopicBuilder)
if err != nil {
logger.LogFatalError("Unable to create proxy factory", err)
}
tenantTranslator, err := buildTenantTranslatorInstance(cfg)
if err != nil {
logger.LogFatalError("Unable to create tenant translator", err)
}
managementGetConnectionByOrgID, err := connection_repository.NewSqlGetConnectionByClientID(cfg, database)
if err != nil {
logger.LogFatalError("Unable to create getConnectionByClientID impl", err)
}
apiMux := mux.NewRouter()
apiMux.Use(request_id.ConfiguredRequestID("x-rh-insights-request-id"))
apiSpecServer := api.NewApiSpecServer(apiMux, cfg.UrlBasePath, cfg.OpenApiSpecFilePath)
apiSpecServer.Routes()
monitoringServer := api.NewMonitoringServer(apiMux, cfg)
monitoringServer.Routes()
var getConnectionFunction connection_repository.GetConnectionByClientID
getConnectionFunction = buildConnectionLookupInstances(cfg, database)
jr := api.NewMessageReceiver(getConnectionFunction, tenantTranslator, proxyFactory, apiMux, cfg.UrlBasePath, cfg)
jr.Routes()
getConnectionListByOrgIDFunction, err := connection_repository.NewSqlGetConnectionsByOrgID(cfg, database)
if err != nil {
logger.LogFatalError("Unable to create connection_repository.GetConnectionsByOrgID() function", err)
}
getAllConnections, err := connection_repository.NewGetAllConnections(cfg, database)
if err != nil {
logger.LogFatalError("Unable to create connection_repository.GetAllConnections() function", err)
}
mgmtServer := api.NewManagementServer(managementGetConnectionByOrgID, getConnectionListByOrgIDFunction, getAllConnections, tenantTranslator, proxyFactory, apiMux, cfg.UrlBasePath, cfg)
mgmtServer.Routes()
connectionMediator := api.NewConnectionMediatorV2(getConnectionFunction, getConnectionListByOrgIDFunction, proxyFactory, apiMux, cfg.UrlBasePath, cfg)
connectionMediator.Routes()
apiSrv := utils.StartHTTPServer(mgmtAddr, "management", apiMux)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-signalChan:
logger.Log.Info("Received signal to shutdown: ", sig)
case err = <-mqttConnectionFailedChan:
logger.Log.Info("MQTT connection dropped: ", err)
}
ctx, cancel := context.WithTimeout(context.Background(), cfg.HttpShutdownTimeout)
defer cancel()
utils.ShutdownHTTPServer(ctx, "management", apiSrv)
mqttClient.Disconnect(cfg.MqttDisconnectQuiesceTime)
logger.Log.Info("Cloud-Connector shutting down")
}
func buildConnectionLookupInstances(cfg *config.Config, database *sql.DB) connection_repository.GetConnectionByClientID {
var getConnectionFunction connection_repository.GetConnectionByClientID
var err error
if cfg.ApiServerConnectionLookupImpl == "relaxed" {
logger.Log.Info("Using \"relaxed\" connection lookup mechanism")
getConnectionFunction, err = connection_repository.NewPermittedTenantSqlGetConnectionByClientID(cfg, database)
if err != nil {
logger.LogFatalError("Unable to create connection_repository.GetConnection() function", err)
}
} else {
logger.Log.Info("Using \"strict\" connection lookup mechanism")
getConnectionFunction, err = connection_repository.NewSqlGetConnectionByClientID(cfg, database)
if err != nil {
logger.LogFatalError("Unable to create connection_repository.GetConnection() function", err)
}
}
return getConnectionFunction
}
func buildTenantTranslatorInstance(cfg *config.Config) (tenantid.Translator, error) {
logger.Log.Infof("Using \"%s\" tenant translator impl", cfg.TenantTranslatorImpl)
if cfg.TenantTranslatorImpl == "translator-service" {
return tenantid.NewTranslator(cfg.TenantTranslatorURL, tenantid.WithTimeout(cfg.TenantTranslatorTimeout)), nil
}
if cfg.TenantTranslatorImpl == "mock" {
mapping := buildTenantTranslatorMockMapping(cfg.TenantTranslatorMockMapping)
return tenantid.NewTranslatorMockWithMapping(mapping), nil
}
logger.Log.Fatalf("Invalid tenant translator impl - %s", cfg.TenantTranslatorImpl)
return nil, fmt.Errorf("Invalid tenant translator impl")
}
func buildTenantTranslatorMockMapping(mappingFromConfig map[string]interface{}) map[string]*string {
mapping := make(map[string]*string)
for k, v := range mappingFromConfig {
value := v.(string)
if value == "" {
mapping[k] = nil
} else {
mapping[k] = &value
}
}
return mapping
}
func notifyOnIntialMqttConnection(connectedChan chan struct{}) func(MQTT.Client) {
// The onConnect handler function below will be called repeatedly
// if AutoReconnect is enabled. Use the sync.Once to only notify
// the caller on the initial connection.
var initialConnection sync.Once
return func(MQTT.Client) {
logger.Log.Info("Connected to MQTT broker")
initialConnection.Do(func() {
connectedChan <- struct{}{}
})
}
}