-
Notifications
You must be signed in to change notification settings - Fork 7
/
s3.go
213 lines (190 loc) · 6.9 KB
/
s3.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
// Copyright 2022 IBM Corp.
// SPDX-License-Identifier: Apache-2.0
package databasetypes
import (
"fmt"
"reflect"
"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"
)
type s3 struct {
dataBase
vaultClientConfiguration map[interface{}]interface{}
}
var translate = map[string]string{
Region: AwsRegion,
Endpoint: EndPointURL,
AccessKeyID: AwsAccessKeyID,
SecretAccessID: AwsSecretAccessKey,
SessionToken: AwsSessionToken,
}
var translateInv = map[string]string{
AwsRegion: Region,
EndPointURL: Endpoint,
AwsAccessKeyID: AccessKeyID,
AwsSecretAccessKey: SecretAccessID,
AwsSessionToken: SessionToken,
}
func NewS3(vaultClientConfiguration map[interface{}]interface{}, logger *zerolog.Logger) *s3 {
return &s3{dataBase: dataBase{name: Datalake, logger: logger},
vaultClientConfiguration: vaultClientConfiguration}
}
func (s *s3) getCredentials(vaultClientConfiguration map[interface{}]interface{},
credentialsPath *string) (string, string, string, error) {
client := vault.NewVaultClient(vaultClientConfiguration, s.logger, utils.HTTPClient)
secrets, err := client.GetSecretMap(credentialsPath)
if err != nil {
s.logger.Warn().Msg("S3 credentials extraction failed")
return EmptyString, EmptyString, EmptyString, err
}
requiredFields := []string{AccessKey, SecretKey}
secretStrings := utils.InterfaceMapToStringMap(secrets, requiredFields, s.logger)
if secretStrings == nil {
s.logger.Warn().Msg(fmt.Sprintf(SomeRequiredFieldsMissing, requiredFields))
return EmptyString, EmptyString, EmptyString, fmt.Errorf(SomeRequiredFieldsMissing, requiredFields)
}
sessionToken := EmptyString
val, ok := secrets[SessionToken].(string)
if ok {
sessionToken = val
}
return secretStrings[AccessKey], secretStrings[SecretKey], sessionToken, nil
}
func (s *s3) TranslateFybrikConfigToOpenMetadataConfig(config map[string]interface{},
connectionType string, credentialsPath *string) map[string]interface{} {
ret := make(map[string]interface{})
configSourceMap := make(map[string]interface{})
ret[Type] = s.OMTypeName()
bucketName, found := config[Bucket]
if found {
ret[BucketName] = bucketName
}
securityMap := make(map[string]interface{})
securityMap[AwsRegion] = "eu-de" // awsRegion field is mandatory, although it is presumably ignored if endpoint is provided
for key, value := range config {
translation, found := translate[key]
if found {
securityMap[translation] = value
}
}
if s.vaultClientConfiguration != nil && credentialsPath != nil {
awsAccessKeyID, awsSecretAccessKey, awsSessionToken, err := s.getCredentials(s.vaultClientConfiguration, credentialsPath)
if err == nil && awsAccessKeyID != EmptyString && awsSecretAccessKey != EmptyString {
securityMap[AwsAccessKeyID] = awsAccessKeyID
securityMap[AwsSecretAccessKey] = awsSecretAccessKey
if awsSessionToken != EmptyString {
securityMap[AwsSessionToken] = awsSessionToken
}
}
}
configSourceMap[SecurityConfig] = securityMap
ret[ConfigSource] = configSourceMap
return ret
}
func (s *s3) TranslateOpenMetadataConfigToFybrikConfig(tableName string,
config map[string]interface{}) (map[string]interface{}, string, error) {
ret := make(map[string]interface{})
ret[ObjectKey] = tableName
configSource, ok := utils.InterfaceToMap(config[ConfigSource], s.logger)
if !ok {
return nil, S3, fmt.Errorf(FailedToConvert, ConfigSource)
}
securityConfig, ok := utils.InterfaceToMap(configSource[SecurityConfig], s.logger)
if !ok {
return nil, S3, fmt.Errorf(FailedToConvert, SecurityConfig)
}
for key, value := range securityConfig {
if translation, found := translateInv[key]; found {
ret[translation] = value
}
}
if value, found := config[BucketName]; found {
ret[Bucket] = value
}
delete(ret, AccessKeyID)
delete(ret, SecretAccessID)
delete(ret, SessionToken)
return ret, S3, nil
}
// Check whether the fields in the 'configSource' section are equivalent.
// They don't have to be identical. We allow additional fields (e.g. 'aws_token')
// in the OM configuration, but we require that all fields in the request configuration
// also appear in the OM configuration, and that the values be identical
func (s *s3) equivalentConfigSource(fromService, fromRequest map[string]interface{}) bool {
// ignore some fields, such as 'aws_token' which would appear only serviceSecurityConfig
serviceSecurityConfig, ok := utils.InterfaceToMap(fromService[SecurityConfig], s.logger)
if !ok {
s.logger.Warn().Msg(fmt.Sprintf(FailedToConvert, "OM "+SecurityConfig))
return false
}
requestSecurityConfig, ok := utils.InterfaceToMap(fromRequest[SecurityConfig], s.logger)
if !ok {
s.logger.Warn().Msg(fmt.Sprintf(FailedToConvert, "Request "+SecurityConfig))
return false
}
for property, value := range requestSecurityConfig {
if !reflect.DeepEqual(serviceSecurityConfig[property], value) {
return false
}
}
return true
}
func (s *s3) EquivalentServiceConfigurations(requestConfig, serviceConfig map[string]interface{}) bool {
for property, value := range requestConfig {
if property == ConfigSource {
servicePropertyMap, ok := utils.InterfaceToMap(serviceConfig[property], s.logger)
if !ok {
s.logger.Warn().Msg(fmt.Sprintf(FailedToConvert, property))
return false
}
valueMap, ok := utils.InterfaceToMap(value, s.logger)
if !ok {
s.logger.Warn().Msg(fmt.Sprintf(FailedToConvert, Value))
return false
}
if !s.equivalentConfigSource(servicePropertyMap, valueMap) {
return false
}
} else if !reflect.DeepEqual(serviceConfig[property], value) {
return false
}
}
return true
}
func (s *s3) DatabaseName(createAssetRequest *models.CreateAssetRequest) string {
return Default
}
func (s *s3) DatabaseSchemaName(createAssetRequest *models.CreateAssetRequest) string {
connectionProperties, ok := utils.InterfaceToMap(createAssetRequest.Details.GetConnection().
AdditionalProperties[S3], s.logger)
if !ok {
s.logger.Warn().Msg(fmt.Sprintf(FailedToConvert, AdditionalProperties))
return EmptyString
}
bucket, found := connectionProperties[Bucket]
if found {
return bucket.(string)
}
assetID := *createAssetRequest.DestinationAssetID
split := strings.Split(assetID, ".")
if len(split) > 1 {
return split[len(split)-2]
}
s.logger.Warn().Msg("Could not determine the name of the DatabaseSchema (bucket)")
return EmptyString
}
func (s *s3) TableName(createAssetRequest *models.CreateAssetRequest) (string, error) {
connectionProperties, ok := utils.InterfaceToMap(createAssetRequest.Details.GetConnection().AdditionalProperties[S3], s.logger)
if !ok {
return EmptyString, fmt.Errorf(FailedToConvert, AdditionalProperties)
}
objectKey, found := connectionProperties[ObjectKey]
if found {
return objectKey.(string), nil
}
split := strings.Split(*createAssetRequest.DestinationAssetID, ".")
return split[len(split)-1], nil
}