This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
/
deid_test.py
287 lines (217 loc) · 7.83 KB
/
deid_test.py
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
# Copyright 2017 Google Inc.
#
# 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.
import os
import shutil
import tempfile
import google.cloud.dlp_v2
import pytest
import deid
HARMFUL_STRING = "My SSN is 372819127"
HARMLESS_STRING = "My favorite color is blue"
GCLOUD_PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT")
UNWRAPPED_KEY = "YWJjZGVmZ2hpamtsbW5vcA=="
WRAPPED_KEY = (
"CiQAz0hX4+go8fJwn80Fr8pVImwx+tmZdqU7JL+7TN/S5JxBU9gSSQDhFHpFVy"
"uzJps0YH9ls480mU+JLG7jI/0lL04i6XJRWqmI6gUSZRUtECYcLH5gXK4SXHlL"
"rotx7Chxz/4z7SIpXFOBY61z0/U="
)
KEY_NAME = (
"projects/python-docs-samples-tests/locations/global/keyRings/"
"dlp-test/cryptoKeys/dlp-test"
)
SURROGATE_TYPE = "SSN_TOKEN"
CSV_FILE = os.path.join(os.path.dirname(__file__), "resources/dates.csv")
DATE_SHIFTED_AMOUNT = 30
DATE_FIELDS = ["birth_date", "register_date"]
CSV_CONTEXT_FIELD = "name"
@pytest.fixture(scope="module")
def tempdir():
tempdir = tempfile.mkdtemp()
yield tempdir
shutil.rmtree(tempdir)
def test_deidentify_with_mask(capsys):
deid.deidentify_with_mask(
GCLOUD_PROJECT, HARMFUL_STRING, ["US_SOCIAL_SECURITY_NUMBER"]
)
out, _ = capsys.readouterr()
assert "My SSN is *********" in out
def test_deidentify_with_mask_ignore_insensitive_data(capsys):
deid.deidentify_with_mask(
GCLOUD_PROJECT, HARMLESS_STRING, ["US_SOCIAL_SECURITY_NUMBER"]
)
out, _ = capsys.readouterr()
assert HARMLESS_STRING in out
def test_deidentify_with_mask_masking_character_specified(capsys):
deid.deidentify_with_mask(
GCLOUD_PROJECT,
HARMFUL_STRING,
["US_SOCIAL_SECURITY_NUMBER"],
masking_character="#",
)
out, _ = capsys.readouterr()
assert "My SSN is #########" in out
def test_deidentify_with_mask_masking_number_specified(capsys):
deid.deidentify_with_mask(
GCLOUD_PROJECT, HARMFUL_STRING, ["US_SOCIAL_SECURITY_NUMBER"], number_to_mask=7,
)
out, _ = capsys.readouterr()
assert "My SSN is *******27" in out
def test_deidentify_with_redact(capsys):
deid.deidentify_with_redact(
GCLOUD_PROJECT, HARMFUL_STRING + "!", ["US_SOCIAL_SECURITY_NUMBER"]
)
out, _ = capsys.readouterr()
assert "My SSN is !" in out
def test_deidentify_with_replace(capsys):
deid.deidentify_with_replace(
GCLOUD_PROJECT,
HARMFUL_STRING,
["US_SOCIAL_SECURITY_NUMBER"],
replacement_str="REPLACEMENT_STR",
)
out, _ = capsys.readouterr()
assert "My SSN is REPLACEMENT_STR" in out
def test_deidentify_with_fpe(capsys):
deid.deidentify_with_fpe(
GCLOUD_PROJECT,
HARMFUL_STRING,
["US_SOCIAL_SECURITY_NUMBER"],
alphabet=google.cloud.dlp_v2.CharsToIgnore.CommonCharsToIgnore.NUMERIC,
wrapped_key=WRAPPED_KEY,
key_name=KEY_NAME,
)
out, _ = capsys.readouterr()
assert "My SSN is" in out
assert "372819127" not in out
def test_deidentify_with_deterministic(capsys):
deid.deidentify_with_deterministic(
GCLOUD_PROJECT,
HARMFUL_STRING,
["US_SOCIAL_SECURITY_NUMBER"],
surrogate_type=SURROGATE_TYPE,
key_name=KEY_NAME,
wrapped_key=WRAPPED_KEY,
)
out, _ = capsys.readouterr()
assert "My SSN is" in out
assert "372819127" not in out
def test_deidentify_with_fpe_uses_surrogate_info_types(capsys):
deid.deidentify_with_fpe(
GCLOUD_PROJECT,
HARMFUL_STRING,
["US_SOCIAL_SECURITY_NUMBER"],
alphabet=google.cloud.dlp_v2.CharsToIgnore.CommonCharsToIgnore.NUMERIC,
wrapped_key=WRAPPED_KEY,
key_name=KEY_NAME,
surrogate_type=SURROGATE_TYPE,
)
out, _ = capsys.readouterr()
assert "My SSN is SSN_TOKEN" in out
assert "372819127" not in out
def test_deidentify_with_fpe_ignores_insensitive_data(capsys):
deid.deidentify_with_fpe(
GCLOUD_PROJECT,
HARMLESS_STRING,
["US_SOCIAL_SECURITY_NUMBER"],
alphabet=google.cloud.dlp_v2.CharsToIgnore.CommonCharsToIgnore.NUMERIC,
wrapped_key=WRAPPED_KEY,
key_name=KEY_NAME,
)
out, _ = capsys.readouterr()
assert HARMLESS_STRING in out
def test_deidentify_with_date_shift(tempdir, capsys):
output_filepath = os.path.join(tempdir, "dates-shifted.csv")
deid.deidentify_with_date_shift(
GCLOUD_PROJECT,
input_csv_file=CSV_FILE,
output_csv_file=output_filepath,
lower_bound_days=DATE_SHIFTED_AMOUNT,
upper_bound_days=DATE_SHIFTED_AMOUNT,
date_fields=DATE_FIELDS,
)
out, _ = capsys.readouterr()
assert "Successful" in out
def test_deidentify_with_date_shift_using_context_field(tempdir, capsys):
output_filepath = os.path.join(tempdir, "dates-shifted.csv")
deid.deidentify_with_date_shift(
GCLOUD_PROJECT,
input_csv_file=CSV_FILE,
output_csv_file=output_filepath,
lower_bound_days=DATE_SHIFTED_AMOUNT,
upper_bound_days=DATE_SHIFTED_AMOUNT,
date_fields=DATE_FIELDS,
context_field_id=CSV_CONTEXT_FIELD,
wrapped_key=WRAPPED_KEY,
key_name=KEY_NAME,
)
out, _ = capsys.readouterr()
assert "Successful" in out
def test_reidentify_with_fpe(capsys):
labeled_fpe_string = "My SSN is SSN_TOKEN(9):731997681"
deid.reidentify_with_fpe(
GCLOUD_PROJECT,
labeled_fpe_string,
surrogate_type=SURROGATE_TYPE,
wrapped_key=WRAPPED_KEY,
key_name=KEY_NAME,
alphabet=google.cloud.dlp_v2.CharsToIgnore.CommonCharsToIgnore.NUMERIC,
)
out, _ = capsys.readouterr()
assert "731997681" not in out
def test_reidentify_with_deterministic(capsys):
labeled_fpe_string = "My SSN is SSN_TOKEN(36):ATeRUd3WWnAHHFtjtl1bv+CT09FZ7hyqNas="
deid.reidentify_with_deterministic(
GCLOUD_PROJECT,
labeled_fpe_string,
surrogate_type=SURROGATE_TYPE,
key_name=KEY_NAME,
wrapped_key=WRAPPED_KEY,
)
out, _ = capsys.readouterr()
assert "SSN_TOKEN(" not in out
def test_deidentify_free_text_with_fpe_using_surrogate(capsys):
labeled_fpe_string = "My phone number is 4359916732"
deid.deidentify_free_text_with_fpe_using_surrogate(
GCLOUD_PROJECT,
labeled_fpe_string,
info_type="PHONE_NUMBER",
surrogate_type="PHONE_TOKEN",
unwrapped_key=UNWRAPPED_KEY,
alphabet=google.cloud.dlp_v2.CharsToIgnore.CommonCharsToIgnore.NUMERIC,
)
out, _ = capsys.readouterr()
assert "PHONE_TOKEN" in out
assert "My phone number is" in out
assert "4359916732" not in out
def test_reidentify_free_text_with_fpe_using_surrogate(capsys):
labeled_fpe_string = "My phone number is PHONE_TOKEN(10):9617256398"
deid.reidentify_free_text_with_fpe_using_surrogate(
GCLOUD_PROJECT,
labeled_fpe_string,
surrogate_type="PHONE_TOKEN",
unwrapped_key=UNWRAPPED_KEY,
alphabet=google.cloud.dlp_v2.CharsToIgnore.CommonCharsToIgnore.NUMERIC,
)
out, _ = capsys.readouterr()
assert "PHONE_TOKEN" not in out
assert "9617256398" not in out
assert "My phone number is" in out
def test_deidentify_with_replace_infotype(capsys):
url_to_redact = "https://cloud.google.com"
deid.deidentify_with_replace_infotype(
GCLOUD_PROJECT, "My favorite site is " + url_to_redact, ["URL"],
)
out, _ = capsys.readouterr()
assert url_to_redact not in out
assert "My favorite site is [URL]" in out