/
random_faker.go
336 lines (303 loc) · 9.9 KB
/
random_faker.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2023 Greenmask
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transformers
import (
"context"
"fmt"
"github.com/go-faker/faker/v4"
"github.com/go-faker/faker/v4/pkg/options"
"github.com/greenmaskio/greenmask/internal/db/postgres/transformers/utils"
"github.com/greenmaskio/greenmask/pkg/toolkit"
)
type FakerFunc func(opts ...options.OptionFunc) string
type FakerTransformerDef struct {
SupportedTypes []string
Description string
Generator FakerFunc
}
var FakerTransformersDes = map[string]*FakerTransformerDef{
// Faker geo
"RandomLatitude": {
Generator: func(opts ...options.OptionFunc) string {
return fmt.Sprintf("%f", faker.Latitude())
},
SupportedTypes: []string{"float4", "float8", "numeric"},
Description: "Generates a random latitude value.",
},
"RandomLongitude": {
Generator: func(opts ...options.OptionFunc) string {
return fmt.Sprintf("%f", faker.Longitude())
},
SupportedTypes: []string{"float4", "float8", "numeric"},
Description: "Generates a random longitude value.",
},
// Faker Datetime
"RandomUnixTime": {
Generator: func(opts ...options.OptionFunc) string {
return fmt.Sprintf("%d", faker.UnixTime())
},
SupportedTypes: []string{"int4", "int8", "numeric"},
Description: "Generates a random Unix timestamp.",
},
"RandomMonthName": {
Generator: faker.MonthName,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates the name of a random month.",
},
"RandomYearString": {
Generator: faker.YearString,
SupportedTypes: []string{"text", "varchar", "int2", "int4", "int8", "numeric"},
Description: "Generates a random year as a string.",
},
"RandomDayOfWeek": {
Generator: faker.DayOfWeek,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random day of the week.",
},
"RandomDayOfMonth": {
Generator: faker.DayOfMonth,
SupportedTypes: []string{"text", "varchar", "int2", "int4", "int8", "numeric"},
Description: "Generates a random day of the month.",
},
"RandomCentury": {
Generator: faker.Century,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random century.",
},
"RandomTimezone": {
Generator: faker.Timezone,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random timezone.",
},
// Faker Internet
"RandomEmail": {
Generator: faker.Email,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random email address.",
},
"RandomMacAddress": {
Generator: faker.MacAddress,
SupportedTypes: []string{"text", "varchar", "macaddr", "macaddr8"},
Description: "Generates a random MAC address.",
},
"RandomDomainName": {
Generator: faker.DomainName,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random domain name.",
},
"RandomURL": {
Generator: faker.URL,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random URL.",
},
"RandomUsername": {
Generator: faker.Username,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random username.",
},
"RandomIPv4": {
Generator: faker.IPv4,
SupportedTypes: []string{"text", "varchar", "inet"},
Description: "Generates a random IPv4 address.",
},
"RandomIPv6": {
Generator: faker.IPv6,
SupportedTypes: []string{"text", "varchar", "inet"},
Description: "Generates a random IPv6 address.",
},
"RandomPassword": {
Generator: faker.Password,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random password.",
},
// Faker words and Sentences
"RandomWord": {
Generator: faker.Word,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random word.",
},
"RandomSentence": {
Generator: faker.Sentence,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random sentence.",
},
"RandomParagraph": {
Generator: faker.Paragraph,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random paragraph.",
},
// Faker Payment
"RandomCCType": {
Generator: faker.CCType,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random credit card type.",
},
"RandomCCNumber": {
Generator: faker.CCNumber,
SupportedTypes: []string{"text", "varchar", "int4", "int8", "numeric"},
Description: "Generates a random credit card number.",
},
"RandomCurrency": {
Generator: faker.Currency,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random currency code.",
},
"RandomAmountWithCurrency": {
Generator: faker.AmountWithCurrency,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random monetary amount with currency.",
},
// Faker Person
"RandomTitleMale": {
Generator: faker.TitleMale,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random title for males.",
},
"RandomTitleFemale": {
Generator: faker.TitleFemale,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random title for females.",
},
"RandomFirstName": {
Generator: faker.FirstName,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random first name.",
},
"RandomFirstNameMale": {
Generator: faker.FirstNameMale,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random male first name.",
},
"RandomFirstNameFemale": {
Generator: faker.FirstNameFemale,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random female first name.",
},
"RandomLastName": {
Generator: faker.LastName,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random last name.",
},
"RandomName": {
Generator: faker.Name,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a full random name.",
},
// Faker Phone
"RandomPhoneNumber": {
Generator: faker.Phonenumber,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random phone number.",
},
"RandomTollFreePhoneNumber": {
Generator: faker.TollFreePhoneNumber,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random toll-free phone number.",
},
"RandomE164PhoneNumber": {
Generator: faker.E164PhoneNumber,
SupportedTypes: []string{"text", "varchar"},
Description: "Generates a random phone number in E.164 format.",
},
}
func generateFakerTransformers(registry *utils.TransformerRegistry) {
for name, def := range FakerTransformersDes {
td := utils.NewDefinition(
utils.NewTransformerProperties(
name,
def.Description,
),
MakeNewFakeTransformerFunction(def.Generator),
toolkit.MustNewParameter(
"column",
"column name",
).SetIsColumn(toolkit.NewColumnProperties().
SetAffected(true).
SetAllowedColumnTypes(def.SupportedTypes...),
).SetRequired(true),
toolkit.MustNewParameter(
"keep_null",
"indicates that NULL values must not be replaced with transformed values",
).SetDefaultValue(
toolkit.ParamsValue("true"),
),
)
utils.DefaultTransformerRegistry.MustRegister(td)
}
}
type FakeTransformer struct {
columnName string
keepNull bool
columnIdx int
affectedColumns map[int]string
generate FakerFunc
}
func MakeNewFakeTransformerFunction(generator FakerFunc) utils.NewTransformerFunc {
return func(ctx context.Context, driver *toolkit.Driver, parameters map[string]*toolkit.Parameter) (utils.Transformer, toolkit.ValidationWarnings, error) {
return NewFakeTransformer(ctx, driver, parameters, generator)
}
}
func NewFakeTransformer(
ctx context.Context, driver *toolkit.Driver, parameters map[string]*toolkit.Parameter, generator FakerFunc,
) (utils.Transformer, toolkit.ValidationWarnings, error) {
p := parameters["column"]
var columnName string
var keepNull bool
if _, err := p.Scan(&columnName); err != nil {
return nil, nil, fmt.Errorf("unable to parse column param: %w", err)
}
idx, _, ok := driver.GetColumnByName(columnName)
if !ok {
return nil, nil, fmt.Errorf("column with name %s is not found", columnName)
}
p = parameters["keep_null"]
if _, err := p.Scan(&keepNull); err != nil {
return nil, nil, fmt.Errorf(`unable to scan "keep_null" param: %w`, err)
}
affectedColumns := make(map[int]string)
affectedColumns[idx] = columnName
return &FakeTransformer{
columnName: columnName,
keepNull: keepNull,
columnIdx: idx,
affectedColumns: affectedColumns,
generate: generator,
}, nil, nil
}
func (fts *FakeTransformer) GetAffectedColumns() map[int]string {
return fts.affectedColumns
}
func (fts *FakeTransformer) Init(ctx context.Context) error {
return nil
}
func (fts *FakeTransformer) Done(ctx context.Context) error {
return nil
}
func (fts *FakeTransformer) Transform(ctx context.Context, r *toolkit.Record) (*toolkit.Record, error) {
valAny, err := r.GetRawColumnValueByIdx(fts.columnIdx)
if err != nil {
return nil, fmt.Errorf("unable to scan value: %w", err)
}
if valAny.IsNull && fts.keepNull {
return r, nil
}
newValue := toolkit.NewRawValue([]byte(fts.generate()), false)
if err := r.SetRawColumnValueByIdx(fts.columnIdx, newValue); err != nil {
return nil, fmt.Errorf("unable to set new value: %w", err)
}
return r, nil
}
func init() {
generateFakerTransformers(utils.DefaultTransformerRegistry)
}