-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleset.py
614 lines (527 loc) · 21.3 KB
/
ruleset.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
from enum import Enum
from propset import TransProps, OrgType
from cbpr import is_cbpr
class Region(Enum):
''' Different jurisdiction with different laws '''
EEA = 1
CHINA = 2
INDIA = 3
RUSSIA = 4
SINGAPORE= 5
VIETNAM = 6
JAPAN = 7
KOREA = 8
HONGKONG = 9
UK = 10
USA = 11
CANADA = 12
MACRO = 14
UNKNOWN = 100
def find_region(desc :str) -> Region:
''' Find the matched jurisdiction '''
descl = str.lower(desc)
res = Region.UNKNOWN
match descl:
case "austria" | "belgium" | "bulgaria" | "croatia" | "republic of cyprus":
res = Region.EEA
case "czech republic" | "denmark" | "estonia" | "finland":
res = Region.EEA
case "france" | "germany" | "greece" | "hungary" | "ireland":
res = Region.EEA
case "italy" | "latvia" | "lithuania" | "luxembourg" | "malta":
res = Region.EEA
case "netherlands" | "poland" | "portugal" | "romania" | "slovakia":
res = Region.EEA
case "slovenia" | "spain" | "sweden":
res = Region.EEA
case "iceland" | "liechtenstein" | "norway":
# Not in EU, but in EEA
res = Region.EEA
case "china":
res = Region.CHINA
case "india":
res = Region.INDIA
case "russia":
res = Region.RUSSIA
case "singapore":
res = Region.SINGAPORE
case "vietnam":
res = Region.VIETNAM
case "japan":
res = Region.JAPAN
case "canada":
res = Region.CANADA
case "united kingdom" | "uk" | "the united kingdom":
res = Region.UK
case "the united states" | "united states" | "usa":
res = Region.USA
case "korea" | "republic of korea":
res = Region.KOREA
case "hongkong" | "hong kong":
res = Region.HONGKONG
case "macro" | "aomen":
res = Region.MACRO
case _:
res = Region.UNKNOWN
#print(f"DBG: Region: {desc}, result: {res}")
return res
class Decision(Enum):
''' Decision on trasfer request '''
GO = 1
REJECT = 2
RISK = 3
TBD = 4
UNKNOWN = 100
class Rule:
def check(self, req: TransProps) -> Decision:
return Decision.TBD
class AllowRule(Rule):
' If matches then allow '
class DenyRule(Rule):
' If matches then deny '
class RiskRule(Rule):
' Not forbidded by law, but with concerns '
class Rules:
allow_rules = []
deny_rules = []
risk_rules = []
def add_allow(self, rl: Rule):
self.allow_rules.append(rl)
def add_deny(self, rl: Rule):
self.deny_rules.append(rl)
def add_risk(self, rl: Rule):
self.risk_rules.append(rl)
def judge(self, req: TransProps) -> Decision:
' Give a decision based on rules '
for rl in self.deny_rules:
res = rl.check(req)
if res == Decision.REJECT:
return Decision.REJECT
for rl in self.allow_rules:
res = rl.check(req)
if res == Decision.GO:
return Decision.GO
if res == Decision.REJECT:
print(f"Fobidden in an allow rule: {req.trans_props}")
return Decision.REJECT
for rl in self.risk_rules:
res = rl.check(req)
if res == Decision.RISK:
return Decision.RISK
if res == Decision.REJECT:
print(f"Fobidden in a risk rule: {req.trans_props}")
return Decision.REJECT
if res == Decision.GO:
print(f"Allowed in a risk rule: {req.trans_props}")
return Decision.GO
print(f"Ticket {req.trans_props.ticketId} cannot be decided automatically!")
return Decision.TBD
class RuleChecker:
''' Check a transborder request and give a decision '''
def judge(self, req: TransProps) -> Decision:
''' Check a transborder request and give a decision. True means allow. '''
if req.trans_props.check_expiration() is False:
return Decision.REJECT
req.trans_props.print_info()
des = Decision.TBD
orig_region = find_region(req.trans_props.origArea)
match orig_region:
case Region.EEA:
gdpr = GdprRules()
des = gdpr.judge(req)
case Region.UK:
gdpr = UkGdprRules()
des = gdpr.judge(req)
case Region.CANADA:
rl = CanadaRules()
des = rl.judge(req)
case Region.USA:
rl = UsaRules()
des = rl.judge(req)
case Region.VIETNAM:
rl = VietnamRules()
des = rl.judge(req)
case Region.INDIA:
rl = IndianRules()
des = rl.judge(req)
case Region.CHINA:
rl = ChinaRules()
des = rl.judge(req)
case Region.JAPAN:
rl = JapanRules()
des = rl.judge(req)
case Region.SINGAPORE:
rl = SingaporeRules()
des = rl.judge(req)
case Region.MACRO:
rl = MacroRules()
des = rl.judge(req)
case _:
# TODO implementation
print(f"INFO: Cannot find checker for region: {req.trans_props.origArea}")
print("---------------------")
return des
class RuleSameRegion(AllowRule):
def check(self, req: TransProps) -> Decision:
dest_region = find_region(req.trans_props.destArea)
orig_region = find_region(req.trans_props.origArea)
if dest_region == orig_region:
return Decision.GO
return Decision.TBD
class RuleGdprAdequacy(AllowRule):
def check(self, req: TransProps) -> Decision:
''' GDPR Article 45 '''
descl = str.lower(req.trans_props.destArea)
match descl:
case "andorra" | "argentina" | "faroe islands" | "guernsey" | "israel" | "isle of man" | "japan":
print(f"Ticket has valid adequacy: {req.trans_props.ticketId}")
return Decision.GO
case "jersey" | "new zealand" | "republic of korea" | "switzerland " | "the united kingdom" | "uruguay":
print(f"Ticket has valid adequacy: {req.trans_props.ticketId}")
return Decision.GO
case "united kingdom":
# alias
print(f"Ticket has valid adequacy: {req.trans_props.ticketId}")
return Decision.GO
case "canada":
return eu_to_canada(req)
case "the united states" | "united states" | "usa":
return eu_to_us(req)
case _:
print(f"Ticket out of adequacy: {req.trans_props.ticketId}")
return Decision.TBD
class RuleGdprBcrs(AllowRule):
def check(self, req: TransProps) -> Decision:
''' Binding corporate rules (Article 46) '''
reasonl = req.trans_props.reason.lower()
if reasonl.find("bcrs") != -1:
print(f"Ticket has valid BCRs: {req.trans_props.ticketId}")
return Decision.GO
if reasonl.find("binding corporate rules") != -1:
print(f"Ticket has valid BCRs: {req.trans_props.ticketId}")
return Decision.GO
return Decision.TBD
class RuleGdprSCCs(AllowRule):
def check(self, req: TransProps) -> Decision:
''' standard contractual clauses (Article 46) '''
reasonl = req.trans_props.reason.lower()
if reasonl.find("sccs") != -1:
print(f"Ticket has valid SCCs: {req.trans_props.ticketId}")
return Decision.GO
if reasonl.find("standard contractual clauses") != -1:
print(f"Ticket has valid SCCs: {req.trans_props.ticketId}")
return Decision.GO
return Decision.TBD
class RuleGdprCoC(AllowRule):
def check(self, req: TransProps) -> Decision:
''' an approved code of conduct pursuant to Article 40 '''
reasonl = req.trans_props.reason.lower()
if reasonl.find("code of conduct") != -1:
print(f"Ticket has valid CoC: {req.trans_props.ticketId}")
return Decision.GO
return Decision.TBD
class GdprRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleGdprAdequacy())
self.add_allow(RuleGdprBcrs())
self.add_allow(RuleGdprSCCs())
self.add_allow(RuleGdprCoC())
def eu_to_canada(req: TransProps) -> Decision:
''' To Canada transborder '''
print("EU to CA: Checking the type of organizations...")
if hasattr(req, "sender_prop") is False:
return Decision.TBD
if hasattr(req, "receiver_prop") is False:
return Decision.TBD
org1 = find_org(req.sender_prop.org_type)
org2 = find_org(req.receiver_prop.org_type)
if org1 != OrgType.COMMERCIAL:
return Decision.TBD
if org2 != OrgType.COMMERCIAL:
return Decision.TBD
print(f"Go! Ticket within two commercial organizations: {req.trans_props.ticketId}")
return Decision.GO
def eu_to_us(req: TransProps) -> Decision:
''' To US transborder '''
print("EU to US: Checking the type of organizations...")
if hasattr(req, "sender_prop") is False:
return Decision.TBD
if hasattr(req, "receiver_prop") is False:
return Decision.TBD
org1 = find_org(req.sender_prop.org_type)
org2 = find_org(req.receiver_prop.org_type)
if org1 != OrgType.COMMERCIAL:
return Decision.TBD
if org2 != OrgType.COMMERCIAL:
return Decision.TBD
reasonl = req.trans_props.reason.lower()
if reasonl.find("safe harbor") != -1:
# invalid on 2015 by EU court
print(f"Ticket has an invalid reason (Safe Harbor): {req.trans_props.ticketId}")
return Decision.TBD
if reasonl.find("privacy shield") != -1:
# invalid on 2020 by EU court
print(f"Ticket has an invalid reason (privacy shield): {req.trans_props.ticketId}")
return Decision.TBD
if reasonl.find("data privacy framework") != -1:
# Enable on 2023-07 by EU and USA
print(f"Ticket has a valid reason (DPF): {req.trans_props.ticketId}")
return Decision.GO
return Decision.TBD
class UkGdprRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleUkGdprAdequacy())
self.add_allow(RuleUkGdprBcrs())
self.add_allow(RuleUkGdprSCCs())
self.add_allow(RuleUkGdprCoC())
class RuleUkGdprAdequacy(AllowRule):
eugdpr = RuleGdprAdequacy()
def check(self, req: TransProps) -> Decision:
dest = find_region(req.trans_props.destArea)
if dest == Region.EEA:
# UK and EEA
return Decision.GO
res = self.eugdpr.check(req)
if res == Decision.GO:
return Decision.GO
descl = str.lower(req.trans_props.destArea)
match descl:
case "Gibraltar" | "korea":
print(f"Ticket has valid adequacy: {req.trans_props.ticketId}")
return Decision.GO
case "canada":
return uk_to_canada(req)
case "the united states" | "united states" | "usa":
return uk_to_us(req)
case _:
print(f"Ticket out of adequacy: {req.trans_props.ticketId}")
return Decision.TBD
def uk_to_canada(req: TransProps) -> Decision:
return eu_to_canada(req)
def uk_to_us(req: TransProps) -> Decision:
return eu_to_us(req)
class RuleUkGdprBcrs(RuleGdprBcrs):
' Same as in EU '
class RuleUkGdprSCCs(AllowRule):
def check(self, req: TransProps) -> Decision:
''' standard contractual clauses '''
reasonl = req.trans_props.reason.lower()
if reasonl.find("sccs") != -1:
print(f"Ticket has valid SCCs: {req.trans_props.ticketId}")
return Decision.GO
if reasonl.find("standard contractual clauses") != -1:
print(f"Ticket has valid SCCs: {req.trans_props.ticketId}")
return Decision.GO
if reasonl.find("international data transfer agreement") != -1:
print(f"Ticket has valid IDTAs: {req.trans_props.ticketId}")
return Decision.GO
return Decision.TBD
class RuleUkGdprCoC(RuleGdprCoC):
' Same as in EU '
class CanadaRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_risk(RuleCaOPC())
class RuleCaOPC(RiskRule):
' Rules by OPC (Canada) '
concern_regions = [Region.CHINA, Region.RUSSIA] # Only for DEMO
def check(self, req: TransProps) -> Decision:
dest = find_region(req.trans_props.destArea)
if dest in self.concern_regions:
# UK and EEA
return Decision.RISK
class UsaRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_risk(RuleUsaFtc())
class RuleUsaFtc(RiskRule):
' Rules by FTC '
concern_regions = [Region.CHINA, Region.RUSSIA] # Only for DEMO
def check(self, req: TransProps) -> Decision:
dest = find_region(req.trans_props.destArea)
if dest in self.concern_regions:
# UK and EEA
return Decision.RISK
class VietnamRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleVnLocalStorage())
class RuleLocalStorage(AllowRule):
def check(self, req: TransProps) -> Decision:
''' Several contries require local storage of personal data.
If fulfilled, transborder action can be permitted. '''
reasonl = req.trans_props.reason.lower()
if reasonl.find("local storage") != -1:
return Decision.GO
return Decision.RISK
class RuleVnLocalStorage(RuleLocalStorage):
''' 53/2022/ND‑CP (local storage) '''
class RuleInLocalStorage(RuleLocalStorage):
''' Digital Personal Data Protection Act, 2023 (Article 40: local storage) '''
class IndianRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleInLocalStorage())
self.add_deny(RuleInFinancial())
self.add_deny(RuleInBlacklist())
class RuleInFinancial(DenyRule):
def check(self, req: TransProps) -> Decision:
desc = req.trans_props.dataType.lower()
if desc.find("financial") != -1:
return Decision.REJECT
if desc.find("payment") != -1:
return Decision.REJECT
class RuleInBlacklist(DenyRule):
'Article 16 clause 1 of DPDP Act'
def check(self, req: TransProps) -> Decision:
'Article 16 clause 1'
blacklist = [] # The banned regions' name
if req.trans_props.destArea in blacklist:
return Decision.REJECT
return Decision.TBD
class SingaporeRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleSgInTransit())
# Personal Data Protection Regulations, 2021, Article 26
self.add_allow(RuleUserConcent())
self.add_allow(RuleSgDpas())
self.add_allow(RuleSgCoC())
self.add_allow(RuleSgCerts())
class RuleInTransit(AllowRule):
def check(self, req: TransProps) -> Decision:
' From outside and to outside, no additional processing '
reasonl = req.trans_props.reason.lower()
if reasonl.find("only transit") != -1:
return Decision.GO
return Decision.RISK
class RuleSgInTransit(RuleInTransit):
' Personal Data Protection Regulations, 2021, Article 10 '
class RuleUserConcent(AllowRule):
def check(self, req: TransProps) -> Decision:
' Several regions allow user concent as a valid reason'
reasonl = req.trans_props.reason.lower()
if reasonl.find("user concent") != -1:
return Decision.GO
return Decision.RISK
class RuleSgDpas(AllowRule):
def check(self, req: TransProps) -> Decision:
' Personal Data Protection Regulations, 2021, Article 26 '
reasonl = req.trans_props.reason.lower()
if reasonl.find("dpa") != -1:
return Decision.GO
if reasonl.find("data processing agreement") != -1:
return Decision.GO
return Decision.RISK
class RuleSgCoC(RuleGdprCoC):
' Same as in EU '
class RuleSgCerts(AllowRule):
' Personal Data Protection Regulations, 2021, Article 26. Should be changed to constraints '
def check(self, req: TransProps) -> Decision:
reasonl = req.trans_props.reason.lower()
if reasonl.find("apec cbpr") != -1:
return Decision.GO
if reasonl.find("apec prp") != -1:
return Decision.GO
return Decision.RISK
class ChinaRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleCnInTransit())
self.add_allow(RuleCnFTZs())
self.add_allow(RuleCnBusinessNeeds())
self.add_allow(RuleCnNoPIIs())
self.add_risk(RuleCnImportant())
self.add_risk(RuleCnCIIs())
self.add_risk(RuleCnNoCIIs())
class RuleCnInTransit(RuleInTransit):
' Facilitate and regulate regulations for cross-border data flows, March 2024, Article 4 '
class RuleCnNoPIIs(AllowRule):
' Facilitate and regulate regulations for cross-border data flows, March 2024, Article 3 '
def check(self, req: TransProps) -> Decision:
if is_pii(req.trans_props.dataType):
return Decision.RISK
if is_cn_important(req.trans_props.dataType):
return Decision.RISK
return Decision.GO
class RuleCnBusinessNeeds(AllowRule):
' Facilitate and regulate regulations for cross-border data flows, March 2024, Article 5 '
def check(self, req: TransProps) -> Decision:
reasonl = req.trans_props.reason.lower()
if reasonl.find("contract") != -1:
return Decision.GO
if reasonl.find("hr") != -1:
return Decision.GO
if reasonl.find("human resource") != -1:
return Decision.GO
if reasonl.find("emergency") != -1:
return Decision.GO
return Decision.TBD
class RuleCnFTZs(AllowRule):
' Facilitate and regulate regulations for cross-border data flows, March 2024, Article 6 '
def check(self, req: TransProps) -> Decision:
reasonl = req.trans_props.reason.lower()
if reasonl.find("free trade zone") != -1:
return Decision.GO
if reasonl.find("ftz") != -1:
return Decision.GO
return Decision.TBD
class RuleCnImportant(RiskRule):
' Facilitate and regulate regulations for cross-border data flows, March 2024, Article 2 '
def check(self, req: TransProps) -> Decision:
if is_cn_important(req.trans_props.dataType):
return Decision.RISK
return Decision.TBD
class RuleCnCIIs(RiskRule):
' Facilitate and regulate regulations for cross-border data flows, March 2024, Article 7 '
def check(self, req: TransProps) -> Decision:
if hasattr(req, "receiver_org") is False:
return Decision.TBD
if req.receiver_org.org_type != OrgType.CII:
return Decision.TBD
if is_pii(req.trans_props.dataType) or is_cn_important(req.trans_props.dataType):
if reason_security_assessment(req.trans_props.reason) is False:
return Decision.REJECT
else:
# SHOULD review the assessment report
return Decision.TBD
return Decision.TBD
class RuleCnNoCIIs(RiskRule):
' Facilitate and regulate regulations for cross-border data flows, March 2024, Article 7 '
' Should be reviewed manually. '
def is_pii(datadesc: str) -> bool:
desc = datadesc.lower()
if desc.find("pii") != -1:
return True
if desc.find("personal") != -1:
return True
return False
def is_cn_important(datadesc: str) -> bool:
desc = datadesc.lower()
if desc.find("important") != -1:
return True
return False
def reason_security_assessment(reason: str) -> bool:
reasonl = reason.lower()
return reasonl.find("security assessment") != -1
class JapanRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleCBPR())
class RuleCBPR(AllowRule):
' Global Cross-Border Privacy Rules Declaration, Apr. 2022 '
def check(self, req: TransProps) -> Decision:
res = is_cbpr(req.trans_props.destArea)
if res is True:
return Decision.GO
return Decision.TBD
class MacroRules(Rules):
def __init__(self):
self.add_allow(RuleSameRegion())
self.add_allow(RuleGreaterBayArea())
class RuleGreaterBayArea(AllowRule):
def check(self, req: TransProps) -> Decision:
# TODO Agreements of Great Bay Area (Guangdong, Hong Kong and Macro)
return Decision.TBD