/
athenaconfiguration.go
236 lines (196 loc) · 5.4 KB
/
athenaconfiguration.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
package aws
import (
"fmt"
"github.com/tkennes/openghg/pkg/cloud/config"
"github.com/tkennes/openghg/pkg/util/json"
)
// AthenaConfiguration
type AthenaConfiguration struct {
Bucket string `json:"bucket"`
Region string `json:"region"`
Database string `json:"database"`
Catalog string `json:"catalog""`
Table string `json:"table"`
Workgroup string `json:"workgroup"`
Account string `json:"account"`
Authorizer Authorizer `json:"authorizer"`
}
func (ac *AthenaConfiguration) Validate() error {
// Validate Authorizer
if ac.Authorizer == nil {
return fmt.Errorf("AthenaConfiguration: missing Authorizer")
}
err := ac.Authorizer.Validate()
if err != nil {
return fmt.Errorf("AthenaConfiguration: %s", err)
}
// Validate base properties
if ac.Bucket == "" {
return fmt.Errorf("AthenaConfiguration: missing bucket")
}
if ac.Region == "" {
return fmt.Errorf("AthenaConfiguration: missing region")
}
if ac.Database == "" {
return fmt.Errorf("AthenaConfiguration: missing database")
}
if ac.Table == "" {
return fmt.Errorf("AthenaConfiguration: missing table")
}
if ac.Account == "" {
return fmt.Errorf("AthenaConfiguration: missing account")
}
return nil
}
func (ac *AthenaConfiguration) Equals(config config.Config) bool {
if config == nil {
return false
}
thatConfig, ok := config.(*AthenaConfiguration)
if !ok {
return false
}
if ac.Authorizer != nil {
if !ac.Authorizer.Equals(thatConfig.Authorizer) {
return false
}
} else {
if thatConfig.Authorizer != nil {
return false
}
}
if ac.Bucket != thatConfig.Bucket {
return false
}
if ac.Region != thatConfig.Region {
return false
}
if ac.Database != thatConfig.Database {
return false
}
if ac.Catalog != thatConfig.Catalog {
return false
}
if ac.Table != thatConfig.Table {
return false
}
if ac.Workgroup != thatConfig.Workgroup {
return false
}
if ac.Account != thatConfig.Account {
return false
}
return true
}
func (ac *AthenaConfiguration) Sanitize() config.Config {
return &AthenaConfiguration{
Bucket: ac.Bucket,
Region: ac.Region,
Database: ac.Database,
Catalog: ac.Catalog,
Table: ac.Table,
Workgroup: ac.Workgroup,
Account: ac.Account,
Authorizer: ac.Authorizer.Sanitize().(Authorizer),
}
}
func (ac *AthenaConfiguration) Key() string {
return fmt.Sprintf("%s/%s", ac.Account, ac.Bucket)
}
func (ac *AthenaConfiguration) UnmarshalJSON(b []byte) error {
var f interface{}
err := json.Unmarshal(b, &f)
if err != nil {
return err
}
fmap := f.(map[string]interface{})
bucket, err := config.GetInterfaceValue[string](fmap, "bucket")
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Bucket = bucket
region, err := config.GetInterfaceValue[string](fmap, "region")
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Region = region
database, err := config.GetInterfaceValue[string](fmap, "database")
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Database = database
catalog, err := config.GetInterfaceValue[string](fmap, "catalog")
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Catalog = catalog
table, err := config.GetInterfaceValue[string](fmap, "table")
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Table = table
workgroup, err := config.GetInterfaceValue[string](fmap, "workgroup")
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Workgroup = workgroup
account, err := config.GetInterfaceValue[string](fmap, "account")
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Account = account
authAny, ok := fmap["authorizer"]
if !ok {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: missing authorizer")
}
authorizer, err := config.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
if err != nil {
return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
}
ac.Authorizer = authorizer
return nil
}
// ConvertAwsAthenaInfoToConfig takes a legacy config and generates a Config based on the presence of properties to match
// legacy behavior
func ConvertAwsAthenaInfoToConfig(aai AwsAthenaInfo) config.KeyedConfig {
if aai.IsEmpty() {
return nil
}
var authorizer Authorizer
if aai.ServiceKeyName == "" && aai.ServiceKeySecret == "" {
authorizer = &ServiceAccount{}
} else {
authorizer = &AccessKey{
ID: aai.ServiceKeyName,
Secret: aai.ServiceKeySecret,
}
}
// Wrap Authorizer with AssumeRole if MasterPayerArn is set
if aai.MasterPayerARN != "" {
authorizer = &AssumeRole{
Authorizer: authorizer,
RoleARN: aai.MasterPayerARN,
}
}
var config config.KeyedConfig
if aai.AthenaTable != "" || aai.AthenaDatabase != "" {
config = &AthenaConfiguration{
Bucket: aai.AthenaBucketName,
Region: aai.AthenaRegion,
Catalog: aai.AthenaCatalog,
Database: aai.AthenaDatabase,
Table: aai.AthenaTable,
Workgroup: aai.AthenaWorkgroup,
Account: aai.AccountID,
Authorizer: authorizer,
}
} else {
config = &S3Configuration{
Bucket: aai.AthenaBucketName,
Region: aai.AthenaRegion,
Account: aai.AccountID,
Authorizer: authorizer,
}
}
return config
}