column_anonymizer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.rspec_status +15 -0
- data/CHANGELOG.md +49 -0
- data/CUSTOM_GENERATORS_COMPLETE.md +507 -0
- data/CUSTOM_GENERATORS_GUIDE.md +515 -0
- data/CUSTOM_GENERATORS_IMPLEMENTATION.md +471 -0
- data/CUSTOM_GENERATORS_QUICK_REF.md +95 -0
- data/FEATURE_COMPLETE.md +287 -0
- data/GEMSPEC_FIX.md +90 -0
- data/IMPLEMENTATION_SUMMARY.md +205 -0
- data/QUICK_REFERENCE.md +92 -0
- data/RAKE_TASKS_GUIDE.md +469 -0
- data/RAKE_TASKS_IMPLEMENTATION.md +363 -0
- data/RAKE_TASKS_QUICK_REF.md +164 -0
- data/README.md +389 -0
- data/Rakefile +12 -0
- data/SCAN_GENERATOR_TEST.md +141 -0
- data/WORKFLOW_GUIDE.md +368 -0
- data/YAML_MIGRATION_GUIDE.md +284 -0
- data/lib/column_anonymizer/anonymizer.rb +103 -0
- data/lib/column_anonymizer/encryptable.rb +25 -0
- data/lib/column_anonymizer/railtie.rb +15 -0
- data/lib/column_anonymizer/schema_loader.rb +44 -0
- data/lib/column_anonymizer/version.rb +5 -0
- data/lib/column_anonymizer.rb +9 -0
- data/lib/generators/column_anonymizer/initializer/initializer_generator.rb +25 -0
- data/lib/generators/column_anonymizer/initializer/templates/column_anonymizer.rb +77 -0
- data/lib/generators/column_anonymizer/install/README +46 -0
- data/lib/generators/column_anonymizer/install/install_generator.rb +36 -0
- data/lib/generators/column_anonymizer/install/templates/encrypted_columns.yml +29 -0
- data/lib/generators/column_anonymizer/scan/scan_generator.rb +250 -0
- data/lib/tasks/column_anonymizer.rake +318 -0
- metadata +108 -0
data/WORKFLOW_GUIDE.md
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
# Column Anonymizer - Workflow Guide
|
|
2
|
+
|
|
3
|
+
## ๐ฏ Quick Reference
|
|
4
|
+
|
|
5
|
+
### Installation Options
|
|
6
|
+
|
|
7
|
+
#### Option 1: Install + Auto-Scan (Recommended) โญ
|
|
8
|
+
```bash
|
|
9
|
+
rails generate column_anonymizer:install --scan
|
|
10
|
+
```
|
|
11
|
+
**What happens:**
|
|
12
|
+
1. Creates `config/encrypted_columns.yml`
|
|
13
|
+
2. Scans all models for `encrypts` calls
|
|
14
|
+
3. Populates YAML with detected columns
|
|
15
|
+
4. Ready to use immediately!
|
|
16
|
+
|
|
17
|
+
#### Option 2: Install Then Scan Separately
|
|
18
|
+
```bash
|
|
19
|
+
rails generate column_anonymizer:install
|
|
20
|
+
rails generate column_anonymizer:scan
|
|
21
|
+
```
|
|
22
|
+
**What happens:**
|
|
23
|
+
1. Creates empty `config/encrypted_columns.yml` template
|
|
24
|
+
2. Then scans and populates it
|
|
25
|
+
3. Same result as Option 1
|
|
26
|
+
|
|
27
|
+
#### Option 3: Manual Configuration
|
|
28
|
+
```bash
|
|
29
|
+
rails generate column_anonymizer:install
|
|
30
|
+
# Edit config/encrypted_columns.yml manually
|
|
31
|
+
```
|
|
32
|
+
**What happens:**
|
|
33
|
+
1. Creates `config/encrypted_columns.yml` template
|
|
34
|
+
2. You manually add all models and columns
|
|
35
|
+
3. More control, more work
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## ๐ Workflow Diagrams
|
|
40
|
+
|
|
41
|
+
### Auto-Scan Workflow (Recommended)
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
45
|
+
โ rails generate column_anonymizer:install --scan โ
|
|
46
|
+
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
47
|
+
โ
|
|
48
|
+
โผ
|
|
49
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
50
|
+
โ Create config file โ
|
|
51
|
+
โ config/encrypted_ โ
|
|
52
|
+
โ columns.yml โ
|
|
53
|
+
โโโโโโโโโโฌโโโโโโโโโโโโโโโโ
|
|
54
|
+
โ
|
|
55
|
+
โผ
|
|
56
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
57
|
+
โ Scan app/models/ โ
|
|
58
|
+
โ directory โ
|
|
59
|
+
โโโโโโโโโโฌโโโโโโโโโโโโโโโโ
|
|
60
|
+
โ
|
|
61
|
+
โผ
|
|
62
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
63
|
+
โ Find all 'encrypts' โ
|
|
64
|
+
โ method calls โ
|
|
65
|
+
โโโโโโโโโโฌโโโโโโโโโโโโโโโโ
|
|
66
|
+
โ
|
|
67
|
+
โผ
|
|
68
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
69
|
+
โ Guess column types โ
|
|
70
|
+
โ based on names โ
|
|
71
|
+
โโโโโโโโโโฌโโโโโโโโโโโโโโโโ
|
|
72
|
+
โ
|
|
73
|
+
โผ
|
|
74
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
75
|
+
โ Update YAML config โ
|
|
76
|
+
โ with discovered โ
|
|
77
|
+
โ columns โ
|
|
78
|
+
โโโโโโโโโโฌโโโโโโโโโโโโโโโโ
|
|
79
|
+
โ
|
|
80
|
+
โผ
|
|
81
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
82
|
+
โ โ
Ready to anonymize! โ
|
|
83
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Manual Configuration Workflow
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
90
|
+
โ rails generate โ
|
|
91
|
+
โ column_anonymizer:install โ
|
|
92
|
+
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
93
|
+
โ
|
|
94
|
+
โผ
|
|
95
|
+
โโโโโโโโโโโโโโโโโโโโโโ
|
|
96
|
+
โ Create empty โ
|
|
97
|
+
โ config file โ
|
|
98
|
+
โโโโโโโโโโฌโโโโโโโโโโโโ
|
|
99
|
+
โ
|
|
100
|
+
โผ
|
|
101
|
+
โโโโโโโโโโโโโโโโโโโโโโ
|
|
102
|
+
โ Manually edit โ
|
|
103
|
+
โ YAML file โ
|
|
104
|
+
โ (tedious!) โ
|
|
105
|
+
โโโโโโโโโโฌโโโโโโโโโโโโ
|
|
106
|
+
โ
|
|
107
|
+
โผ
|
|
108
|
+
โโโโโโโโโโโโโโโโโโโโโโ
|
|
109
|
+
โ Add each model โ
|
|
110
|
+
โ and column โ
|
|
111
|
+
โ by hand โ
|
|
112
|
+
โโโโโโโโโโฌโโโโโโโโโโโโ
|
|
113
|
+
โ
|
|
114
|
+
โผ
|
|
115
|
+
โโโโโโโโโโโโโโโโโโโโโโ
|
|
116
|
+
โ Ready to use โ
|
|
117
|
+
โโโโโโโโโโโโโโโโโโโโโโ
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## ๐ Adding New Encrypted Columns
|
|
123
|
+
|
|
124
|
+
### When You Add New Models
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
# 1. Create a new model with encrypted attributes
|
|
128
|
+
class Patient < ApplicationRecord
|
|
129
|
+
encrypts :medical_record_number
|
|
130
|
+
encrypts :emergency_contact_phone
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# 2. Run the scanner
|
|
134
|
+
# rails generate column_anonymizer:scan
|
|
135
|
+
|
|
136
|
+
# 3. Output shows:
|
|
137
|
+
# ๐ Scanning models...
|
|
138
|
+
# โน๏ธ Skipping User.email (already configured)
|
|
139
|
+
# โ Adding Patient.medical_record_number as 'text'
|
|
140
|
+
# โ Adding Patient.emergency_contact_phone as 'phone'
|
|
141
|
+
# โ
Updated config/encrypted_columns.yml
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Smart Merging
|
|
145
|
+
|
|
146
|
+
The scanner **never overwrites** existing configuration:
|
|
147
|
+
|
|
148
|
+
```yaml
|
|
149
|
+
# Before scan:
|
|
150
|
+
User:
|
|
151
|
+
email: email # โ Stays as-is
|
|
152
|
+
|
|
153
|
+
# After adding new Patient model and running scan:
|
|
154
|
+
User:
|
|
155
|
+
email: email # โ Preserved!
|
|
156
|
+
|
|
157
|
+
Patient:
|
|
158
|
+
medical_record_number: text # โ Added
|
|
159
|
+
emergency_contact_phone: phone # โ Added
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## ๐ง Type Guessing Examples
|
|
165
|
+
|
|
166
|
+
### Common Column Names โ Auto-Detected Types
|
|
167
|
+
|
|
168
|
+
```ruby
|
|
169
|
+
class User < ApplicationRecord
|
|
170
|
+
encrypts :email # โ type: email
|
|
171
|
+
encrypts :phone_number # โ type: phone
|
|
172
|
+
encrypts :mobile # โ type: phone
|
|
173
|
+
encrypts :ssn # โ type: ssn
|
|
174
|
+
encrypts :social_security_number # โ type: ssn
|
|
175
|
+
encrypts :first_name # โ type: first_name
|
|
176
|
+
encrypts :last_name # โ type: last_name
|
|
177
|
+
encrypts :full_name # โ type: name
|
|
178
|
+
encrypts :home_address # โ type: address
|
|
179
|
+
encrypts :street_address # โ type: address
|
|
180
|
+
encrypts :credit_card_number # โ type: text
|
|
181
|
+
encrypts :api_token # โ type: text
|
|
182
|
+
encrypts :password_digest # โ type: text
|
|
183
|
+
encrypts :custom_field # โ type: text (default)
|
|
184
|
+
end
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Resulting YAML
|
|
188
|
+
|
|
189
|
+
```yaml
|
|
190
|
+
User:
|
|
191
|
+
email: email
|
|
192
|
+
phone_number: phone
|
|
193
|
+
mobile: phone
|
|
194
|
+
ssn: ssn
|
|
195
|
+
social_security_number: ssn
|
|
196
|
+
first_name: first_name
|
|
197
|
+
last_name: last_name
|
|
198
|
+
full_name: name
|
|
199
|
+
home_address: address
|
|
200
|
+
street_address: address
|
|
201
|
+
credit_card_number: text
|
|
202
|
+
api_token: text
|
|
203
|
+
password_digest: text
|
|
204
|
+
custom_field: text
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## ๐จ Customizing Auto-Detected Types
|
|
210
|
+
|
|
211
|
+
You can always override the auto-detected types:
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
# 1. Run scanner
|
|
215
|
+
rails generate column_anonymizer:scan
|
|
216
|
+
|
|
217
|
+
# 2. Review config/encrypted_columns.yml
|
|
218
|
+
|
|
219
|
+
# 3. Manually adjust any incorrect guesses
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
**Example:**
|
|
223
|
+
|
|
224
|
+
```yaml
|
|
225
|
+
# Auto-detected (might be wrong):
|
|
226
|
+
User:
|
|
227
|
+
contact_info: text # โ Guessed 'text'
|
|
228
|
+
|
|
229
|
+
# Manually corrected:
|
|
230
|
+
User:
|
|
231
|
+
contact_info: phone # โ You know it's a phone number
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## ๐ Complete Example
|
|
237
|
+
|
|
238
|
+
### Step-by-Step
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
# 1. Add gem to Gemfile
|
|
242
|
+
echo "gem 'column_anonymizer'" >> Gemfile
|
|
243
|
+
bundle install
|
|
244
|
+
|
|
245
|
+
# 2. Install with auto-scan
|
|
246
|
+
rails generate column_anonymizer:install --scan
|
|
247
|
+
|
|
248
|
+
# Output:
|
|
249
|
+
# โ
Created config/encrypted_columns.yml
|
|
250
|
+
# ๐ Scanning models for encrypted attributes...
|
|
251
|
+
# โ Adding User.email as 'email'
|
|
252
|
+
# โ Adding User.phone as 'phone'
|
|
253
|
+
# โ Adding User.ssn as 'ssn'
|
|
254
|
+
# โ
Scanned 1 model(s)
|
|
255
|
+
# ๐ Updated config/encrypted_columns.yml
|
|
256
|
+
|
|
257
|
+
# 3. Check the config
|
|
258
|
+
cat config/encrypted_columns.yml
|
|
259
|
+
|
|
260
|
+
# Output:
|
|
261
|
+
# ---
|
|
262
|
+
# User:
|
|
263
|
+
# email: email
|
|
264
|
+
# phone: phone
|
|
265
|
+
# ssn: ssn
|
|
266
|
+
|
|
267
|
+
# 4. Anonymize data
|
|
268
|
+
rails console
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
```ruby
|
|
272
|
+
# In console:
|
|
273
|
+
user = User.first
|
|
274
|
+
ColumnAnonymizer::Anonymizer.anonymize_model!(user)
|
|
275
|
+
user.reload
|
|
276
|
+
|
|
277
|
+
puts user.email # => "user_abc12345@example.com"
|
|
278
|
+
puts user.phone # => "+15551234567"
|
|
279
|
+
puts user.ssn # => "123-45-6789"
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
## ๐ Pro Tips
|
|
285
|
+
|
|
286
|
+
### Tip 1: Run Scan After Model Changes
|
|
287
|
+
```bash
|
|
288
|
+
# After adding encrypts to existing models
|
|
289
|
+
rails generate column_anonymizer:scan
|
|
290
|
+
# Only new columns are added
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Tip 2: Review Auto-Detections
|
|
294
|
+
```bash
|
|
295
|
+
# Always review the generated YAML
|
|
296
|
+
cat config/encrypted_columns.yml
|
|
297
|
+
# Adjust types if needed
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Tip 3: Use in CI/CD
|
|
301
|
+
```bash
|
|
302
|
+
# Add to your deployment script
|
|
303
|
+
rails generate column_anonymizer:scan --quiet
|
|
304
|
+
# Keeps config in sync with models
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Tip 4: Safe to Run Multiple Times
|
|
308
|
+
```bash
|
|
309
|
+
# No risk of data loss
|
|
310
|
+
rails generate column_anonymizer:scan
|
|
311
|
+
rails generate column_anonymizer:scan
|
|
312
|
+
rails generate column_anonymizer:scan
|
|
313
|
+
# Always safe - never overwrites!
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
## โก Quick Command Reference
|
|
319
|
+
|
|
320
|
+
| Command | What It Does | When to Use |
|
|
321
|
+
|---------|--------------|-------------|
|
|
322
|
+
| `rails g column_anonymizer:install --scan` | Install + auto-configure | First time setup โญ |
|
|
323
|
+
| `rails g column_anonymizer:scan` | Update config with new models | After adding encrypts |
|
|
324
|
+
| `rails g column_anonymizer:install` | Install empty config | Manual configuration |
|
|
325
|
+
| `ColumnAnonymizer::SchemaLoader.reload_schema!` | Reload in console | Development/testing |
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## ๐ฏ Decision Tree
|
|
330
|
+
|
|
331
|
+
```
|
|
332
|
+
Do you have models with 'encrypts' already?
|
|
333
|
+
โ
|
|
334
|
+
โโ YES โ Use: rails g column_anonymizer:install --scan โญ
|
|
335
|
+
โ (Fastest! Everything auto-configured)
|
|
336
|
+
โ
|
|
337
|
+
โโ NO โ Use: rails g column_anonymizer:install
|
|
338
|
+
Then add encrypts to models
|
|
339
|
+
Then run: rails g column_anonymizer:scan
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## ๐ Related Files
|
|
345
|
+
|
|
346
|
+
- `README.md` - Complete documentation
|
|
347
|
+
- `IMPLEMENTATION_SUMMARY.md` - Technical details
|
|
348
|
+
- `SCAN_GENERATOR_TEST.md` - Testing guide
|
|
349
|
+
- `YAML_MIGRATION_GUIDE.md` - Migration from old approach
|
|
350
|
+
- `CHANGELOG.md` - Version history
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
## ๐ Summary
|
|
355
|
+
|
|
356
|
+
**Before this feature:**
|
|
357
|
+
- Manual YAML editing required
|
|
358
|
+
- Prone to typos and errors
|
|
359
|
+
- Time-consuming setup
|
|
360
|
+
|
|
361
|
+
**After this feature:**
|
|
362
|
+
- One command: `rails g column_anonymizer:install --scan`
|
|
363
|
+
- Automatic discovery and configuration
|
|
364
|
+
- Intelligent type detection
|
|
365
|
+
- Safe to run multiple times
|
|
366
|
+
- Ready to use in seconds!
|
|
367
|
+
|
|
368
|
+
**The Power of Automation!** ๐
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# Column Encrypter Gem - YAML-Based Configuration
|
|
2
|
+
|
|
3
|
+
## โ
Gem Successfully Refactored!
|
|
4
|
+
|
|
5
|
+
The gem has been refactored from method-based configuration to YAML-based configuration.
|
|
6
|
+
|
|
7
|
+
## ๐ฏ Key Changes
|
|
8
|
+
|
|
9
|
+
### Before (Method-Based):
|
|
10
|
+
```ruby
|
|
11
|
+
class User < ApplicationRecord
|
|
12
|
+
encrypts_with_type :email, :email
|
|
13
|
+
encrypts_with_type :phone_number, :phone
|
|
14
|
+
end
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### After (YAML-Based):
|
|
18
|
+
```yaml
|
|
19
|
+
# config/encrypted_columns.yml
|
|
20
|
+
User:
|
|
21
|
+
email: email
|
|
22
|
+
phone_number: phone
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
class User < ApplicationRecord
|
|
27
|
+
encrypts :email
|
|
28
|
+
encrypts :phone_number
|
|
29
|
+
end
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## ๐ Gem Structure
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
column_anonymizer/
|
|
36
|
+
โโโ lib/
|
|
37
|
+
โ โโโ column_anonymizer.rb # Main entry point
|
|
38
|
+
โ โโโ column_anonymizer/
|
|
39
|
+
โ โ โโโ version.rb # Version: 0.1.0
|
|
40
|
+
โ โ โโโ schema_loader.rb # Loads YAML configuration
|
|
41
|
+
โ โ โโโ encryptable.rb # Reads types from YAML
|
|
42
|
+
โ โ โโโ anonymizer.rb # Data anonymization
|
|
43
|
+
โ โ โโโ railtie.rb # Rails integration
|
|
44
|
+
โ โโโ generators/
|
|
45
|
+
โ โโโ column_anonymizer/
|
|
46
|
+
โ โโโ install/
|
|
47
|
+
โ โ โโโ install_generator.rb # Rails generator
|
|
48
|
+
โ โ โโโ templates/
|
|
49
|
+
โ โ โ โโโ encrypted_columns.yml # YAML template
|
|
50
|
+
โ โ โโโ README # Installation instructions
|
|
51
|
+
โ โโโ scan/
|
|
52
|
+
โ โโโ scan_generator.rb # โญ Auto-discovery scanner
|
|
53
|
+
โโโ spec/
|
|
54
|
+
โ โโโ column_anonymizer_spec.rb # Tests
|
|
55
|
+
โ โโโ spec_helper.rb # Test configuration
|
|
56
|
+
โโโ column_anonymizer.gemspec # Gem specification
|
|
57
|
+
โโโ Gemfile # Dependencies
|
|
58
|
+
โโโ Rakefile # Rake tasks
|
|
59
|
+
โโโ README.md # Documentation
|
|
60
|
+
โโโ CHANGELOG.md # Version history
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## ๐ Installation & Usage
|
|
64
|
+
|
|
65
|
+
### 1. Install the Gem
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# Add to Gemfile
|
|
69
|
+
gem 'column_encrypter', path: '/Users/hkend/Documents/column_encrypter/column_encrypter'
|
|
70
|
+
|
|
71
|
+
# Or install locally
|
|
72
|
+
cd /Users/hkend/Documents/column_encrypter/column_encrypter
|
|
73
|
+
gem build column_encrypter.gemspec
|
|
74
|
+
gem install column_encrypter-0.1.0.gem
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 2. Generate Configuration File
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
rails generate column_anonymizer:install
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
This creates `config/encrypted_columns.yml`.
|
|
84
|
+
|
|
85
|
+
### 3. Define Your Schema
|
|
86
|
+
|
|
87
|
+
**Option A: Automatic Scanning (Recommended)**
|
|
88
|
+
|
|
89
|
+
The gem can automatically scan your models and populate the YAML file:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
# Scan existing models and update config
|
|
93
|
+
rails generate column_anonymizer:scan
|
|
94
|
+
|
|
95
|
+
# Or install and scan in one step
|
|
96
|
+
rails generate column_anonymizer:install --scan
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The scanner will:
|
|
100
|
+
- Find all models with `encrypts` calls
|
|
101
|
+
- Intelligently guess the data type based on column names
|
|
102
|
+
- Add new columns to your config without overwriting existing entries
|
|
103
|
+
- Show you what was discovered
|
|
104
|
+
|
|
105
|
+
**Option B: Manual Configuration**
|
|
106
|
+
|
|
107
|
+
Edit `config/encrypted_columns.yml`:
|
|
108
|
+
|
|
109
|
+
```yaml
|
|
110
|
+
User:
|
|
111
|
+
email: email
|
|
112
|
+
phone_number: phone
|
|
113
|
+
ssn: ssn
|
|
114
|
+
full_name: name
|
|
115
|
+
|
|
116
|
+
Account:
|
|
117
|
+
bank_account_number: text
|
|
118
|
+
routing_number: text
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 4. Use Standard Rails Encryption
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
class User < ApplicationRecord
|
|
125
|
+
encrypts :email
|
|
126
|
+
encrypts :phone_number
|
|
127
|
+
encrypts :ssn
|
|
128
|
+
encrypts :full_name
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 5. Anonymize Data
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
# In console or rake task
|
|
136
|
+
user = User.first
|
|
137
|
+
ColumnEncrypter::Anonymizer.anonymize_model!(user)
|
|
138
|
+
|
|
139
|
+
# email becomes: user_a1b2c3d4@example.com
|
|
140
|
+
# phone_number becomes: +15551234567
|
|
141
|
+
# ssn becomes: 123-45-6789
|
|
142
|
+
# full_name becomes: Anonymous User abc123
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## ๐จ Built-in Generators
|
|
146
|
+
|
|
147
|
+
| Type | Example Output |
|
|
148
|
+
|------|----------------|
|
|
149
|
+
| `:email` | `user_a1b2c3d4@example.com` |
|
|
150
|
+
| `:phone` | `+15551234567` |
|
|
151
|
+
| `:ssn` | `123-45-6789` |
|
|
152
|
+
| `:name` | `Anonymous User abc123` |
|
|
153
|
+
| `:first_name` | `John`, `Jane`, `Alex` |
|
|
154
|
+
| `:last_name` | `Smith`, `Johnson` |
|
|
155
|
+
| `:address` | `1234 Anonymous St, City, ST 12345` |
|
|
156
|
+
| `:text` | `Anonymized text a1b2c3d4` |
|
|
157
|
+
|
|
158
|
+
## ๐ง Intelligent Type Guessing
|
|
159
|
+
|
|
160
|
+
When you use `rails generate column_anonymizer:scan`, the generator intelligently guesses the data type based on column names:
|
|
161
|
+
|
|
162
|
+
| Column Name Pattern | Detected Type |
|
|
163
|
+
|---------------------|---------------|
|
|
164
|
+
| `email` | `:email` |
|
|
165
|
+
| `phone`, `mobile`, `cell`, `telephone` | `:phone` |
|
|
166
|
+
| `ssn`, `social_security` | `:ssn` |
|
|
167
|
+
| `first_name`, `fname` | `:first_name` |
|
|
168
|
+
| `last_name`, `lname`, `surname` | `:last_name` |
|
|
169
|
+
| `name`, `full_name`, `fullname` | `:name` |
|
|
170
|
+
| `address`, `street`, `addr` | `:address` |
|
|
171
|
+
| `card_number`, `credit_card`, `cvv`, `cvc` | `:text` |
|
|
172
|
+
| `password`, `token`, `secret` | `:text` |
|
|
173
|
+
| Everything else | `:text` |
|
|
174
|
+
|
|
175
|
+
You can always manually adjust the types in `config/encrypted_columns.yml` after scanning.
|
|
176
|
+
|
|
177
|
+
## โจ Benefits of YAML Approach
|
|
178
|
+
|
|
179
|
+
1. **Centralized Configuration**: All column types in one place
|
|
180
|
+
2. **No Code Changes**: Just use standard Rails `encrypts` method
|
|
181
|
+
3. **Easy Updates**: Modify schema without touching model files
|
|
182
|
+
4. **Automatic Discovery**: Scan feature finds encrypted columns automatically
|
|
183
|
+
5. **Smart Type Guessing**: Intelligently detects appropriate anonymization types
|
|
184
|
+
4. **Better Organization**: Clear overview of all encrypted columns
|
|
185
|
+
5. **Version Control Friendly**: YAML changes are easy to review
|
|
186
|
+
|
|
187
|
+
## ๐ Schema Reloading (Development)
|
|
188
|
+
|
|
189
|
+
```ruby
|
|
190
|
+
# In Rails console
|
|
191
|
+
ColumnEncrypter::SchemaLoader.reload_schema!
|
|
192
|
+
User.reload_encrypted_columns_metadata!
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## ๐ Example YAML Schema
|
|
196
|
+
|
|
197
|
+
```yaml
|
|
198
|
+
# config/encrypted_columns.yml
|
|
199
|
+
|
|
200
|
+
User:
|
|
201
|
+
email: email
|
|
202
|
+
phone_number: phone
|
|
203
|
+
ssn: ssn
|
|
204
|
+
first_name: first_name
|
|
205
|
+
last_name: last_name
|
|
206
|
+
home_address: address
|
|
207
|
+
date_of_birth: text
|
|
208
|
+
|
|
209
|
+
Patient:
|
|
210
|
+
medical_record_number: text
|
|
211
|
+
ssn: ssn
|
|
212
|
+
emergency_contact_phone: phone
|
|
213
|
+
email: email
|
|
214
|
+
|
|
215
|
+
CreditCard:
|
|
216
|
+
card_number: text
|
|
217
|
+
cvv: text
|
|
218
|
+
cardholder_name: name
|
|
219
|
+
billing_address: address
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## ๐งช Testing in Rails Console
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
# Add gem to your Rails app's Gemfile
|
|
226
|
+
gem 'column_encrypter', path: '/Users/hkend/Documents/column_encrypter/column_encrypter'
|
|
227
|
+
|
|
228
|
+
bundle install
|
|
229
|
+
rails generate column_encrypter:install
|
|
230
|
+
rails console
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Then in console:
|
|
234
|
+
|
|
235
|
+
```ruby
|
|
236
|
+
# Create the schema
|
|
237
|
+
File.write('config/encrypted_columns.yml', <<~YAML)
|
|
238
|
+
User:
|
|
239
|
+
email: email
|
|
240
|
+
phone: phone
|
|
241
|
+
YAML
|
|
242
|
+
|
|
243
|
+
# Reload schema
|
|
244
|
+
ColumnEncrypter::SchemaLoader.reload_schema!
|
|
245
|
+
|
|
246
|
+
# Check metadata
|
|
247
|
+
User.encrypted_columns_metadata
|
|
248
|
+
# => {:email=>:email, :phone=>:phone}
|
|
249
|
+
|
|
250
|
+
# Anonymize
|
|
251
|
+
user = User.first
|
|
252
|
+
ColumnEncrypter::Anonymizer.anonymize_model!(user)
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## ๐ฆ Files Created
|
|
256
|
+
|
|
257
|
+
- โ
`lib/column_encrypter.rb` - Main entry point
|
|
258
|
+
- โ
`lib/column_encrypter/version.rb` - Version
|
|
259
|
+
- โ
`lib/column_encrypter/schema_loader.rb` - YAML loader
|
|
260
|
+
- โ
`lib/column_encrypter/encryptable.rb` - ActiveRecord integration
|
|
261
|
+
- โ
`lib/column_encrypter/anonymizer.rb` - Data generators
|
|
262
|
+
- โ
`lib/column_encrypter/railtie.rb` - Rails auto-loading
|
|
263
|
+
- โ
`lib/generators/column_encrypter/install/install_generator.rb` - Generator
|
|
264
|
+
- โ
`lib/generators/column_encrypter/install/templates/encrypted_columns.yml` - Template
|
|
265
|
+
- โ
`lib/generators/column_encrypter/install/README` - Install instructions
|
|
266
|
+
- โ
`spec/column_encrypter_spec.rb` - Tests
|
|
267
|
+
- โ
`spec/spec_helper.rb` - Test configuration
|
|
268
|
+
- โ
`column_encrypter.gemspec` - Gem specification
|
|
269
|
+
- โ
`Gemfile` - Dependencies
|
|
270
|
+
- โ
`Rakefile` - Rake tasks
|
|
271
|
+
- โ
`README.md` - Complete documentation
|
|
272
|
+
- โ
`CHANGELOG.md` - Version history
|
|
273
|
+
|
|
274
|
+
## ๐ Ready to Use!
|
|
275
|
+
|
|
276
|
+
The gem is complete with YAML-based configuration. You can now:
|
|
277
|
+
|
|
278
|
+
1. Install it in your Rails app
|
|
279
|
+
2. Run `rails generate column_encrypter:install`
|
|
280
|
+
3. Define your schema in `config/encrypted_columns.yml`
|
|
281
|
+
4. Use standard Rails `encrypts` method
|
|
282
|
+
5. Anonymize data with `ColumnEncrypter::Anonymizer.anonymize_model!(record)`
|
|
283
|
+
|
|
284
|
+
**Location:** `/Users/hkend/Documents/column_encrypter/column_encrypter/`
|