forked from fybrik/openmetadata-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
115 lines (99 loc) · 3.46 KB
/
mysql.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
// Copyright 2022 IBM Corp.
// SPDX-License-Identifier: Apache-2.0
package databasetypes
import (
"fmt"
"reflect"
"github.com/rs/zerolog"
models "fybrik.io/openmetadata-connector/datacatalog-go-models"
"fybrik.io/openmetadata-connector/pkg/utils"
"fybrik.io/openmetadata-connector/pkg/vault"
)
type mysql struct {
dataBase
vaultClientConfiguration map[interface{}]interface{}
standardFields map[string]bool
}
func NewMysql(vaultClientConfiguration map[interface{}]interface{}, logger *zerolog.Logger) *mysql {
standardFields := map[string]bool{
DatabaseSchema: true,
HostPort: true,
Password: true,
Scheme: true,
Username: true,
}
return &mysql{
standardFields: standardFields,
dataBase: dataBase{name: Mysql, logger: logger},
vaultClientConfiguration: vaultClientConfiguration,
}
}
func (m *mysql) getCredentials(vaultClientConfiguration map[interface{}]interface{}, //nolint:dupl
credentialsPath *string) (string, string, error) {
client := vault.NewVaultClient(vaultClientConfiguration, m.logger, utils.HTTPClient)
secrets, err := client.GetSecretMap(credentialsPath)
if err != nil {
m.logger.Warn().Msg("MySQL credentials extraction failed")
return EmptyString, EmptyString, err
}
requiredFields := []string{Username, Password}
secretStrings := utils.InterfaceMapToStringMap(secrets, requiredFields, m.logger)
if secretStrings == nil {
m.logger.Warn().Msg(fmt.Sprintf(SomeRequiredFieldsMissing, requiredFields))
return EmptyString, EmptyString, fmt.Errorf(SomeRequiredFieldsMissing, requiredFields)
}
return secretStrings[Username], secretStrings[Password], nil
}
func (m *mysql) TranslateFybrikConfigToOpenMetadataConfig(config map[string]interface{},
connectionType string, credentials *string) map[string]interface{} {
if m.vaultClientConfiguration != nil && credentials != nil {
username, password, err := m.getCredentials(m.vaultClientConfiguration, credentials)
if err == nil && username != EmptyString && password != EmptyString {
config[Username] = username
config[Password] = password
}
}
return config
}
func (m *mysql) TranslateOpenMetadataConfigToFybrikConfig(tableName string,
config map[string]interface{}) (map[string]interface{}, string, error) {
other := make(map[string]interface{})
ret := make(map[string]interface{})
for key, value := range config {
if _, ok := m.standardFields[key]; ok {
ret[key] = value
} else {
other[key] = value
}
}
ret[Other] = other
// remove sensitive information
delete(ret, Username)
delete(ret, Password)
return ret, Mysql, nil
}
func (m *mysql) EquivalentServiceConfigurations(requestConfig, serviceConfig map[string]interface{}) bool {
for property, value := range requestConfig {
if !reflect.DeepEqual(serviceConfig[property], value) {
return false
}
}
return true
}
func (m *mysql) DatabaseName(createAssetRequest *models.CreateAssetRequest) string {
return Default
}
func (m *mysql) DatabaseSchemaName(createAssetRequest *models.CreateAssetRequest) string {
connectionProperties, ok := utils.InterfaceToMap(createAssetRequest.Details.GetConnection().AdditionalProperties[MysqlLowercase], m.logger)
if !ok {
return EmptyString
}
databaseSchema, found := connectionProperties[DatabaseSchema]
if found {
return databaseSchema.(string)
}
return EmptyString
}
func (m *mysql) TableName(createAssetRequest *models.CreateAssetRequest) (string, error) {
return *createAssetRequest.DestinationAssetID, nil
}