-
Notifications
You must be signed in to change notification settings - Fork 7
/
mysql.go
165 lines (141 loc) · 4.58 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
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
// Copyright 2022 IBM Corp.
// SPDX-License-Identifier: Apache-2.0
package databasetypes
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/rs/zerolog"
models "fybrik.io/openmetadata-connector/datacatalog-go-models"
"fybrik.io/openmetadata-connector/pkg/utils"
"fybrik.io/openmetadata-connector/pkg/vault"
)
var standardFields = map[string]bool{
DatabaseSchema: true,
HostPort: true,
Password: true,
Scheme: true,
Username: true,
Table: true,
}
type mysql struct {
dataBase
vaultClientConfiguration map[interface{}]interface{}
}
func NewMysql(vaultClientConfiguration map[interface{}]interface{}, logger *zerolog.Logger) *mysql {
return &mysql{
dataBase: dataBase{name: Mysql, logger: logger},
vaultClientConfiguration: vaultClientConfiguration,
}
}
func (m *mysql) getCredentials(vaultClientConfiguration map[interface{}]interface{},
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{} {
ret := make(map[string]interface{})
if m.vaultClientConfiguration != nil && credentials != nil {
username, password, err := m.getCredentials(m.vaultClientConfiguration, credentials)
if err == nil && username != EmptyString && password != EmptyString {
ret[Username] = username
ret[Password] = password
}
}
host, ok1 := config[Host]
port, ok2 := config[Port]
if ok1 {
if !ok2 {
port = DefaultMySQLPort
}
ret[HostPort] = fmt.Sprintf("%s:%.0f", host, port)
}
if database, ok := config[Database]; ok {
ret[DatabaseSchema] = database
}
ret[Scheme] = "mysql+pymysql"
return ret
}
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{})
ret[Table] = tableName
for key, value := range config {
if _, ok := standardFields[key]; ok {
ret[key] = value
} else {
other[key] = value
}
}
ret[Other] = other
// remove sensitive information
delete(ret, Username)
delete(ret, Password)
if databaseSchema, ok := config[DatabaseSchema]; ok {
delete(ret, DatabaseSchema)
ret[Database] = databaseSchema
}
if hostPort, ok := config[HostPort]; ok {
delete(ret, HostPort)
s := strings.Split(hostPort.(string), ":")
ret[Host] = s[0]
if len(s) > 1 {
if port, err := strconv.Atoi(s[1]); err == nil {
ret[Port] = port
}
}
}
return ret, MysqlLowercase, 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[Database]
if found {
return databaseSchema.(string)
}
return EmptyString
}
func (m *mysql) TableName(createAssetRequest *models.CreateAssetRequest) (string, error) {
additionalProperties := createAssetRequest.Details.GetConnection().AdditionalProperties
mysqlAdditionalProperties, ok := additionalProperties[MysqlLowercase]
if !ok {
return EmptyString, fmt.Errorf(RequiredFieldMissing, MysqlLowercase)
}
connectionProperties, ok := utils.InterfaceToMap(mysqlAdditionalProperties, m.logger)
if !ok {
return EmptyString, fmt.Errorf(FailedToConvert, AdditionalProperties)
}
tableName, found := connectionProperties[Table]
if found {
return tableName.(string), nil
}
return *createAssetRequest.DestinationAssetID, nil
}