index.adoc 72.8 KB
Newer Older
Raj Shah's avatar
Raj Shah committed
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
= IND^2^UCE Policy Language Documentation
Fraunhofer IESE
:revnumber: {project-version}
:doctype: book
:icons: font
:source-highlighter: highlightjs
:toc: left
:toclevels: 2
:sectlinks:
:imagesdir: ./images
:stylesdir: ../css
:stylesheet: ind2uce.css
:linkcss:
:docinfo2:

[discrete]
== Short Links

++++
<script src="../js/jquery-1.11.3.min.js"> </script>
<script>$('#toc').prepend('<p><a href=".."><span class="fa fa-home"/> Home</a></p>')</script>
<p style="cursor:pointer;" onclick="$('#short-links').toggle()">Click here to show or hide short links that help you to directly jump to the element you are looking for.</p>
<div id='short-links' name='short-links'>
++++
* Policy: <<policy,<policy>>>, <<mechanism,<mechanism>>>, <<working-with-variables,<variableDeclaration>>>
* Conditions: <<if_elseif,<if>>>, <<if_elseif,<elseif>>>
* Decisions: <<then_else,<then>>>, <<then_else,<else>>>, <<allow,<allow>>>, <<inhibit,<allow>>>, <<modify,<modify>>>, <<execute,<execute>>>
* Variables: <<working-with-variables,<variable:boolean>>>, <<working-with-variables,<variable:number>>>, <<working-with-variables,<variable:string>>>, <<working-with-variables,<variable:object>>>, <<working-with-variables,<variable:list>>>    
* Parameters: <<parameter-group,<parameter:boolean>>>, <<parameter-group,<parameter:number>>>, <<parameter-group,<parameter:string>>>, <<parameter-group,<parameter:object>>>, <<parameter-group,<parameter:list>>>    
* Event References: <<event-group,<event:boolean>>>, <<event-group,<event:number>>>, <<event-group,<event:string>>>, <<event-group,<event:object>>>, <<pevent-group,<event:list>>>, <<eventHasParameter, <eventHasParameter>>>    
* Constants: <<constant-group,<constant:boolean>>>, <<constant-group,<constant:number>>>, <<constant-group,<constant:string>>>, <<constant-group,<constant:object>>>, <<constant-group,<constant:list>>>    
* PIPs: <<constant-group,<pip:boolean>>>, <<constant-group,<pip:number>>>, <<constant-group,<pip:string>>>, <<constant-group,<pip:object>>>, <<constant-group,<pip:list>>>    
* Arithmetic Functions: <<arithmetic-functions,<plus>>>, <<arithmetic-functions,<minus>>>, <<arithmetic-functions,<multiply>>>, <<arithmetic-functions,<divide>>>, <<arithmetic-functions,<size>>>
* Boolean Functions: <<basic-boolean-functions,<and>>>, <<basic-boolean-functions,<or>>>, <<basic-boolean-functions,<xor>>>, <<basic-boolean-functions,<not>>>, <<basic-boolean-functions,<implies>>>, <<contains, <contains>>>, <<regex,<regex>>>
* Comparison Functions: <<comparison-functions,<less>>>, <<comparison-functions,<lessEqual>>>, <<comparison-functions,<equals>>>, <<comparison-functions,<greaterEqual>>>, <<comparison-functions,<greater>>>
* Date Functions: <<date-functions,<date>>>, <<date-functions,<day>>>, <<date-functions,<time>>>
* Event History: <<count, <count>>>, <<valueChanged, <valueChanged>>>, <<continuousOccurrence, <continuousOccurrence>>>, <<eventOccurrence, <eventOccurrence>>>, <<when,<when>>>
* Concatenation: <<concat, <concat>>>
* Cron Jobs / Time Triggered Events: <<timer,<timer>>>, <<timer,<event>>>

++++
</div>
<script>
$('#short-links').toggle()
</script>
++++

[[introduction]]
== Introduction

This documentation is about the specification of *privacy policies with the IND^2^UCE policy language* regulating security-relevant system events.
The IND^2^UCE policy language is designed to express restrictions on data usage. 
It is an XML-based language, based on *boolean logic, arithmetics, temporal information based on an event history*.
Furthermore, it allows for evaluations based on push (event-triggered) or pull (timer-triggered).
Connection to external systems for information retrieval is fully supported.
Enforcement decisions can be specified by means of *event inhibition, data modification using JsonPath (via PEP modifier plugins) and via the execution of actions (via PXP plugins)*.

[[eca]]
== The Event-Condition-Action Schema

IND^2^UCE defines policies based security-relevant link:../api-core/de/fraunhofer/iese/ind2uce/api/policy/Event.html[*events*] that are occurring at a certain time in a system and are intercepted by *"Policy Enforcement Point" (PEP)*.
These events are sent to a *"Policy Decision Point" (PDP)*, which evaluates the policies and returns an link:../api-core/de/fraunhofer/iese/ind2uce/api/policy/AuthorizationDecision.html[*Authorization Decision*] based on the policies.
This decision is then enforced by the PEP.

.Basic IND^2^UCE flow
image::PEP.png[cwidth="75%"]

Depending on the system and PEP type, events can highly differ.
In general, events contain:

* An action name that defines the type of the event 
* The time the event occurred 
* A key-value list with event parameters 

The following example shows an event we will refer to multiple times in this documentation.
It shows an event that is executed when someone reads bank transactions in a web service.

.Example Event for Banking
----
Event ID: "urn:action:banking-demo:get-transactions"
Time: 1533545200 
Parameters:
 - "customerID" (Number)
 - "psd2Id" (Number)
 - "transactions" (List of Transaction Objects)
----



IND^2^UCE policies are based on the *Event-Condition-Action* (ECA).
If a system *event E* (see above) is fetched and a *condition C* is satisfied, then *action A* (authorization decision) is performed. 
IND^2^UCE follows a blacklisting approach.
Events that are not covered by policies are allowed by default.

The following policy shows a simple example. 
It translates to: "*Inhibit* the event *urn:action:banking-demo:get-transactions* if it is *after 3pm* (i.e., if the external information source getCurrentHour returns a value below or equal to 15)"

.Example IND^2^UCE security policy
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactions'>
  <mechanism event='urn:action:banking-demo:get-transactions'> <!-- Event -->
    <if> <!-- Condition -->
      <less>
        <pip:number method='urn:info:banking-demo:getCurrentHour' default='0'/>
        <constant:number value='15'/>
      </less>
      <then>
        <inhibit/> <!-- Action -->
      </then>
    </if>
  </mechanism>
</policy>
----

[[language-elements]]
== Policy Structure

A *<<policy,policy>>* consists of one or more *<<mechanism,mechanisms>>* that are based on the *Event-Condition-Action* (ECA) schema.
IND^2^UCE follows a blacklisting approach.
Events that are not covered by policies are allowed by default.

[[policy]]
=== Policy

The *<policy>* tag is the root element of an IND^2^UCE security policy. It has the following attributes:

.Policy attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required 		|Meaning
|id           |URN       |required      |The unique id of the policy. The id syntax is urn:policy:<solution>:<identifier>. The <solution> matches your solution ID and defines the range of effect. The <identifier> uniquely identifies the policy within the scope.
|name         |String    |optional      |A human readable (short) name for the policy.
|description  |String    |optional      |A more detailed natural language description of the security policy.
|======================================================================================================================================

The *<policy>* tag must have at least one *<<mechanism,mechanism>>* child and can optionally contain *<<variableDeclaration-group,variableDeclarations>>*.

[[mechanism]]
=== Mechanisms

A *<mechanism>* tag describes a rule of a policy based on a monitored, intercepted or <<timer,time triggered>> event.
An intercepted event can be allowed (event is released and executed normally),
modified (the event is changed according to the modifiers specified in the mechanism before it is executed) or
inhibited (the event is dropped and will not be executed). A time triggered or monitored event cannot be inhibited or modified - thus, are always allowed.
You can use <<execute, PXPs>> to react on these events.

The *<mechanism>* element has the following attributes:

.Mechanism attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required 		|Meaning
|event        |URN  	 |required      |Specifies the event by which the mechanism is triggered and for which the mechanism provides a security rule. The event follows this pattern: urn:action:<solution>:<identifier>
|id           |String    |optional      |A unique id for the mechanism, used for logging purposes only.
|description  |String    |optional      |A natural language description of the mechanism.
|======================================================================================================================================


Mechanisms follow the *if-then-else* schema. 
Thus, a *<mechanism>* tag can have the following children:

* <<if_elseif,<if>>>: A condition that leads to an authorization decision if it matches
* <<if_elseif,<elseif>>>: A condition that leads to an authorization decision if it matches and the previous if or else-ifs did not match
* <<then_else,<else>>>: A condition that leads to an authorization decision if none of the previous if or else-ifs matched
* <<execute,<execute>>>: The unconditional execution of an action

.Policy Specification Rules
[CAUTION]
===============================
* The child element <<if,<if>>> is mandatory and only allowed to be used once within a <<mechanism,<mechanism>>>.
* The child element <<else,<else>>> is optional but only allowed to be used once within a <<mechanism,<mechanism>>> also.
* The child elements <<elseif,<elseif>>> and <<execute,<execute>>> may be used multiple times.
===============================

.Policy Evaluation Rules
[IMPORTANT]
===============================
* A specified <<execute,<execute>>> as a direct child of <mechanism> the <<mechanism,<mechanism>>> tag is triggered independently of the satisfaction of a condition.
===============================

.Example: Inhibits access to transactions after 3pm and log the event
[source,xml]
----
<policy id='urn:policy:banking-demo:getAccounts' description='Inhibits access to transactions and loggs the action'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <less>
        <pip:number method='urn:info:banking-demo:getCurrentHour' default='0'/>
        <constant:number value='15'/>
      </less>
      <then>
        <inhibit/>
        <execute action='urn:action:banking-demo:logNotification'>
       		<parameter:string name='message' value='Access attempt to account data after 3pm'/>
    	</execute>
      </then>
    </if>
    <else>
    	<allow/>
    	<execute action='urn:action:banking-demo:logNotification'>
       		<parameter:string name='message' value='Access attempt to account data before 3pm'/>
    	</execute>
    </else>
    <execute action='urn:action:banking-demo:logNotification'>
       <parameter:string name='message' value='Access attempt to account data'/>
    </execute>
  </mechanism>
</policy>
----

[[if_elseif]]
=== Conditions

The *<if>* and the *<elseif>* elements declare the condition that is evaluated each time the mechanism fires.

A condition must have the following child elements:

* A <<boolean-functions,boolean-function>> that defines the condition
* A <<decision, <then>>> that defines an authorization decision that the mechanism enforces if the condition matches


.Policy Specification Rules
[CAUTION]
===============================
* The <if> and the <elseif> must have exactly one boolean function and one <then> child
===============================

.Policy Evaluation Rules
[IMPORTANT]
===============================
* If the <if> condition is not satisfied, then the first <elseif> condition will be evaluated.
* Only the <then> element of the *first* satisfied <if> or <elseif> condition will be enforced.
* If no condition is satisfied, then the action defined in the <else> element will be executed.
* If no condition is satisfied and no <else> element is specified, then the event will be allowed (blacklisting).
===============================

.Example: Prohibit access to transactions after 3pm and log access to transactions between 2pm and 3pm
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactions'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <lessEqual>
        <constant:number value='15'/>
        <pip:number method='urn:info:banking-demo:getCurrentHour' default='0'/>
      </lessEqual>
      <then>
        <inhibit/>
      </then>
    </if>
    <elseif>
      <and>
         <greaterEqual>
           <pip:number method='urn:banking-demo:getCurrentHour' default='0'/>
           <constant:number value='14'/>
         </greaterEqual>
         <less>
           <pip:number method='urn:info:banking-demo:getCurrentHour' default='0'/>
           <constant:number value='15'/>
         </less>
      </and>
      <then>
        <execute action='urn:action:banking-demo:logNotification'>
          <parameter:string name='message' value='Access attempt to account data between 2 and 3pm'/>
        </execute>
      </then>
    </elseif>
    <else>
      <constant:true/>
      <then>
        <allow/>
        <execute action='urn:action:banking-demo:logNotification'>
          <parameter:string name='message' value='Someone accessed the transactions'/>
        </execute>
      </then>
    </else>
  </mechanism>
</policy>
----

[[then_else]]
=== Decisions

Decisions are defined inside a *<then>* element inside a <<if_elseif,condition>> (<if> or <elseif>), or in an *<else>* element, which is used if no condition is fulfilled.
These two elements can have the following child elements:

* <<allow,<allow>>>: The event will be allowed
* <<modify,<modify>>>: The event is allowed, but modified before further execution
* <<inhibit,<inhibit>>>: The event will be inhibited
* <<execute,<execute>>>: Additional actions are executed, independent of the event allowance 

.Policy Specification Rules
[CAUTION]
===============================
* The <then> and the <else> elements must have at either
** exactly one <<allow,binary decision>> (<allow>, <inhibit>), or
** at least one <<modify, event modification>> (<modify>), and
** multiple <<execute,executes>>, which are executed in the specified order.
===============================

.Example: If the role of the user is "ROLE_USER" *then* modify task desricption, name and budget; *else* inhibit
[source,xml]
----
<policy id='urn:policy:cs4:anonymizeTasksOfOthers'>
  <mechanism event='urn:action:cs4:show-task'>
    <if>
      <equals>
        <pip:string method='urn:info:cs4:getRoleByUsername' default=''>
          <parameter:string name='userId'>
            <event:string eventParameter='user' default='' jsonPathQuery='$.userId'/>
          </parameter:string>
        </pip:string>
        <constant:string value='ROLE_USER'/>
      </equals>
      <then>
        <modify eventParameter='task' method='anagram' jsonPathQuery='$.description'>
          <parameter:number name='percentage' value='100'/>
        </modify>
        <modify eventParameter='task' method='replace' jsonPathQuery='$.name'>
          <parameter:string name='replaceWith' value='******'/>
        </modify>
        <modify eventParameter='task' method='delete' jsonPathQuery='$.budget'/>
      </then>
    </if>
    <else>
      <inhibit/>
    </else>
  </mechanism>
</policy>

----

[[allow_inhibit]]
==== Simple Decisions
Similar to basic access control mechanisms, an event can be allowed or inhibited.
The *<allow>* tag is part of a positive authorization decision. It informs the PEP that the intercepted event can be released for reaching its destination.
The *<inhibit>* tag is part of a negative authorization decision. It informs the PEP that the intercepted event must be dropped so that it never reaches its destination.
A reason can be added to both elements:

.Allow and inhibit attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required  	  |Meaning
|reason       |String    |optional        |The description or rationale for the decision.
|======================================================================================================================================

.Example: If role is banker then *allow* the event "get transactions" else *inhibit* the event
[source,xml]
----
<policy id='urn:policy:banking-demo:checkRoleExample'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <equals>
        <pip:string method='urn:info:banking-demo:checkRole' default=''/>
        <constant:string value='Banker'/>
      </equals>
      <then>
        <allow reason='Banker is authorized to get transactions'/>
      </then>
    </if>
    <else>
      <inhibit reason='User is not authorized'/>
    </else>
  </mechanism>
</policy>
----

[[modify]]
==== Complex Decisions with Event Modifications

In addition to basic access control mechanisms, IND^2^UCE allows the modification of the intercepted event.
The *<modify>* element is used to specify event modifications that the PEP must enforce before releasing the intercepted event.
It has the following attributes:

.Allow and inhibit attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute      |Type       |Required   |Meaning
|eventParameter |String     |required   |The name of the event parameter that should be modified.
|method         |String     |required   |The name of the modification that should be applied (e.g., delete, anonymize). This depends on the capabilities of the PEP. Available modifier methods can be checked in your component overview and are proposed by the policy editor.
|jsonPathQuery  |String     |optional   |If the parameter contains a complex object, modifications can be applied to specific parts of the data structure. For example a query "$.firstName" of a parameter "user" will result in the modification of the first name of the user object only. 
|reason         |String     |optional   |The description or rationale for the event modification.
|======================================================================================================================================

Some modification methods ("modifiers") require additional parameters. 
For example, the "replace" modifier gives you the option to replace a certain string (either the event parameter or part of a complex object) with another String.
This String has to be provided as a <<parameter, parameter>>, as the following example shows.
Our editor will automatically add stubs for all required parameters.

.Example: Replace the bank code number before showing it
[source,xml]
----
<policy id='urn:policy:banking-demo:getAccounts'>
  <mechanism event='urn:action:banking-demo:get-accounts'>
    <if>
      <constant:true/>
      <then>
        <modify eventParameter='accounts' method='replace' jsonPathQuery='$.accounts.bankCodeNumber'>
          <parameter:string name='replaceWith' value='XXXXX'/>
        </modify>
      </then>
    </if>
  </mechanism>
</policy>
----

.JSONPath
[TIP]
===============================
JSONPath is an instrument to query JSON structures, similar to XPath for XML. 
JSONPath uses special notation to represent nodes and their connections to adjacent nodes in a JsonPath path.
Plese refer to http://goessner.net/articles/JsonPath/ for a full documentation on JsonPath.
===============================

.Policy Evaluation Rules
[IMPORTANT]
===============================
When you are using JsonPath, *type safety cannot be guaranteed*.

This means for example that you can overwrite a Number with a String or List.
While this is not a problem for Json / JsonPath, it can (and most likely will) cause problems when deserializting the Json back to an Object at the PEP side. 
Thus, you need to take care that all modifications you are doing preserve the type. 
Our editor will show corresponding warnings.
===============================



=== Variable Declarations 
See chapter <<working-with-variables, Working with Variables>>.

[[data-types]]
== Data Types

IND^2^CE works with five data types: *String, Number, Boolean, Object and List*.
All operators can be combined only depending on their type.
For example, if you have a boolean <and> operator, any kind of boolean children can be attached (e.g., <variable:boolean>, <constant:boolean>, <event:boolen>, <not>, <execute>).


== Operators


=== Basic Operators
For each of the five <<data-types, data types>>, IND^2^UCE provides five basic operators: *Constants, Variables, Event References, Parameters, and PIPs*.
These operators will be explained in the following. 

[[constant-group]]
==== Constants
Constants are used to define a string, number, object, list or boolean that does not depend on any external factor, i.e. cannot change at runtime.
Thus, the value is set during policy specification and does never change.

The following constants are available:

* <constant:string>
* <constant:number>
* <constant:object>
* <constant:list>
* <constant:true>
* <constant:false>
 
Constants (except <constant:true> and <constant:false>) have the following attributes:

.Constant attributes 
[width="100%",cols="2,4,2,5",options="header"]
|======================================================================================================================================
|Attribute    |Type                                                 |Required |Meaning
|value        |depends on constant type (string, number, boolean)   |required      |The value of the constant.
|======================================================================================================================================

.Example: If Role is Banker, then inhibit action
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactions'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <equals>
        <pip:string method='urn:info:banking-demo:checkRole' default=''/>
        <constant:string value='Banker'/>
      </equals>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

.Policy Specification Rules
[CAUTION]
===============================
Boolean constants do not have a value. They are explicitly available as <constant:true> and <constant:false>.
===============================

==== Variables
See chapter <<working-with-variables, Working with Variables>>.

[[event-group]]
==== Event references 
The event elements are used to reference an event parameter.
The following event elements are available:

* <event:string>
* <event:number>
* <event:object>
* <event:list>
* <event:boolean>

The elements have the following attributes:

.Event attributes 
[width="100%",cols="2,4,2,5",options="header"]
|======================================================================================================================================
|Attribute              |Type                                           |Required |Meaning
|eventParameter         |String                                         |required      |The name of an event parameter.
|default                |same as the event parameter type (string, number, boolean)      |required      |The value that is returned if the parameter does not exist in the event.
|jsonPathQuery          |String                                         |optional       |The JSONPath expression to be executed on the parameter value, if the value is a complex object. Please refer to http://goessner.net/articles/JsonPath/ for more information about JsonPath.
|======================================================================================================================================

Remember our example event:

.Example Event
----
Event ID: "urn:action:banking-demo:get-transactions"
Time: 1533545200 
Parameters:
 - "customerID" (Number)
 - "psd2Id" (Number)
 - "transactions" (List of Transaction Objects)
----



.Example Policy
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactions'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <and>
        <equals>
          <event:number eventParameter='customerID' default='1'/>
          <constant:number value='1'/>
        </equals>
        <greater>
          <event:number eventParameter='transactions' default='1001' jsonPathQuery="$.transactions.amount.value"/>
      	  <constant:number value='1000'/>
        </greater>
      </and>
      <then>
        <modify eventParameter='transactions' method='replace'>
          <parameter:object name='replaceWith' value='XXX'/>
        </modify>
      </then>
    </if>
  </mechanism>
</policy>
----

[[parameter-group]]
==== Parameters
The parameter elements are used for parameterized methods. 
In our case this can be either

* a <<pip-group,PIP>> or
* an <<execute,execute>> or
* an event <<modify, modification>>

The following parameter-elements are available:

* <parameter:string>
* <parameter:number>
* <parameter:object>
* <parameter:list>
* <parameter:boolean>

Parameters always have a name and a value.

.Parameter attributes 
[width="100%",cols="2,4,2,5",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required |Meaning
|name         |String    |required      |The name of the parameter.
|value        |String    |optional      |The value of the parameter.
|======================================================================================================================================

Parameters can have one child of the same type (e.g., a string parameter can have a string child).

.Policy Specification Rules
[CAUTION]
===============================
A parameter must have either a value attribute or a child
===============================

.Policy Evaluation Rules
[IMPORTANT]
===============================
* If a parameter has child, the value of the child is used
* If a paramter has a value attribute, this attribute is used as a value
* If a parameter has both, value attribute and child, the child is ignored and the value attribute is used as a value.
===============================


.Example: String parameter inside a PIP
[source,xml]
----
<policy id='urn:policy:cs4:showUser'>
  <mechanism event='urn:action:cs4:show-project'>
    <if>
      <or>
        <equals>
          <pip:string method='urn:info:cs4:getRoleByUsername' default=''>
            <parameter:string name='userId' value='1'/> <!-- attribute value is used -->
          </pip:string>
          <constant:string value='Manager'/>
        </equals>
        <equals>
          <pip:string method='urn:info:cs4:getRoleByUsername' default=''>
            <parameter:string name='userId'> <!-- child value is used -->
              <event:string eventParameter='transactions' default='1001' jsonPathQuery="$.transactions.userId"/>
            </parameter:string>
          </pip:string>
          <constant:string value='Manager'/>
        </equals>
      </or>
      <then>
        <allow/>
      </then>
    </if>
  </mechanism>
</policy>
----

[[pip-group]]
==== External Information Sources (PIPs)
The pip elements are used to request information from a link:../sdk/howto_pip.html[PIP]. Each element returns their value (String, number, object, list, boolean). The parameters that can be added as child elements are sent to the PIP as request parameters.

The following pip elements are available:

* <pip:string>
* <pip:number>
* <pip:object>
* <pip:list>
* <pip:boolean>

The elements have the following attributes:

.PIP attributes 
[width="100%",cols="2,4,2,5",options="header"]
|======================================================================================================================================
|Attribute    |Type       |Required |Meaning
|method         |String    |required      |The name of a PIP method.
|default        |same as the pip type (string, number, boolean)   |required       |The value that is returned if the PIP is not reachable.
|ttl            |time interval notation    |optional         |The "time to live" value sets the time interval that the response value of the PIP is cached. The ttl follows the following syntax: (([0-9]+y)?([0-9]+w)?([0-9]+d)?([0-9]+h)?([0-9]+m)?([0-9]+s)?) (e.g., 1y), where y = years, w = weeks, d = days, h = hours, m = minutes, s = seconds. For example if the pip value should be cached for 1 week, 4 days and 2 hours it would look like "1w4d2h".
|======================================================================================================================================

.Policy Evaluation Rules
[CAUTION]
===============================
* During the "time to live" caching time interval, the pip-elements only returns the cached value. When the time to live has elapsed, the cache is refreshed by the current value retrieved by the PIP for the next interval.
* If the "time to live" caching time interval is not set, then the PIP is requested on each PIP element evaluation.
===============================

.Example: PIP to check a role of a user
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactions'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <and>
        <equals>
          <constant:string value='Banker'/>
          <pip:string method='urn:info:banking-demo:checkRole' default='' ttl="5m">
            <parameter:number name='loggedUser'/>
          </pip:string>
        </equals>
        <constant:true/>
      </and>
      <then>
        <modify eventParameter='transactions' method='replace'>
          <parameter:object name='replaceWith' value='XXX'/>
        </modify>
      </then>
    </if>
  </mechanism>
</policy>
----

[[number-functions]]
=== Number Operators

Number operators are all operators that have a number as return value.

[[basic-number-operators]]
==== Basic Number Operators

The basic number operators are <<variable-group,<variable:number>>>, <<constant-group,<constant:number>>>, <<parameter-group,<parameter:number>>>, <<event-group,<event:number>>>, and <<pip-group,<pip:number>>>.

[[arithmetic-functions]]
==== Arithmetic functions
The functions *<plus>, <minus>, <multiply>* and *<divide>* are available to perform the arithmetical operations (addition, subtraction, multiplication, division).
These four functions don't have any attributes and have at least two number children (e.g. <constant:number>, <pip:number>, <size>).

The *<size>* function is used to count the number of elements in a list or the number of characters in a string.
It doesn't have any attributes, either. You can assign all child elements with a list or string return value.

==== Counting of Events

<<count, Click here to learn how to work with the event history to count events>>.

==== Continuous Occurrence of Events

<<eventOccurrence, Click here to learn how to work with the event history to check if an event occurrs regularly>>.


[[boolean-functions]]
=== Boolean Operators
Number operators are all operators that have a boolean as return value.
////
The boolean functions are needed to process boolean values or compare and evaluate other values. The functions can be divided into three different groups:

The following elements are used to compare numbers, strings, objects, lists and boolean:

* <equals> (number, string, object, list, boolean)
* <less> (number)
* <lessEqual> (number)
* <greater> (number)
* <greaterEqual> (number)

These elements are used to aggregate boolean expressions:

* <and>
* <or>
* <xor>
* <not>
* <implies>

Further boolean functions are:

* <valueChanged>: evaluates if a value has changed
* <regex>: evaluates whether the value of the child elements match a given regular expression
* <contains>: evaluates whether the declared values are contained in a list
* <eventHasParameter>: evaluates whether the event contains a specified parameter
* <execute>: triggers an action in the system and returns a boolean value indicating the execution success
* <continuousOccurrence>: evaluates if an event occurs in a certain time frame (minimum and maximum occurrences optional)
////

[[basic-boolean-operators]]
==== Basic Boolean Operators
The basic number operators are <<variable-group,<variable:boolean>>>, <<constant-group,<constant:boolean>>>, <<parameter-group,<parameter:boolean>>>, <<event-group,<event:boolean>>>, and <<pip-group,<pip:boolean>>>.

[[basic-boolean-functions]]
==== Basic Boolean Functions
The elements *<and>, <or>, <not>, <xor>* and *<implies>* aggregate the values of the child elements according to Boolean logic.

The following attribute can be added:

.Basic Boolean Attributes 
[width="100%",cols="2,4,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Default Value |Required |Meaning
|mode        |String (LAZY, EAGER)     |LAZY          |optional     | It is possible to choose the mode lazy or eager. Lazy evaluation means that an evaluation is stopped as soon as the result cannot change anymore. For example, as soon as the first child of an <and> is false, the other children are not evaluated. If you want to force the evalution of all children (e.g., to eventually execute a PXP), you have to set the mode to EAGER. 
|======================================================================================================================================

Child elements can be all elements with boolean return value (e.g. <constant:true>, <event:boolean>) or elements of the <<boolean-functions,boolean-functions>>.

.Policy Specification Rules
[CAUTION]
===============================
* The *<and>, <or>, <xor>* and *<implies>* elements must have at least one child element.
* The *<not>* element must have exactly one child element.
===============================

.Example: Inhibit transaction if user has id 1 *and* the transaction amount value is greater than 10.000
[source,xml]
----
<policy id='urn:policy:banking-demo:InhibitUser1Transaction'>
  <mechanism event='urn:action:banking-demo:get-money'>
    <if>
      <and>
        <equals>
          <event:string eventParameter='sts' default='' jsonPathQuery='$.simpleusers.id'/>
          <constant:string value='1'/>
        </equals>
        <greater>
          <event:number eventParameter='sts' default='0' jsonPathQuery='$.transactions.amount.value'/>
          <constant:number value='10000'/>
        </greater>
      </and>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

[[comparison-functions]]
==== Comparison Functions 
The functions *<less>, <lessEqual>, <greater>* and *<greaterEqual>* are used to compare different numbers. 
For instance if you want to compare the number of usages with a constant number ("must not be used more than 3 times").
These functions don't have attributes and child elements can be elements with a number return value (like <<pip:number, <pip:number>>>)
 and the <<number-functions, number-functions>>.

The function *<equals>* is different, because besides numbers other values can be compared with each other. For example, it is possible to compare strings like a constant string and a user name.
 *<equals>* doesn't have an attribute, either.
 And besides child elements with a number return value, also elements with string, object, list and boolean return value as well as boolean-functions can be added to <equals>.

.Policy Specification Rules
[CAUTION]
===============================
* The elements <less>, <lessEqual>, <greater>, <greaterEqual> and <equals> must have at least two child elements.
* The order is essential, e.g. for the child elements values A, B and C specified in this order, the *<less>* element evaluates A < B < C.
//* In <equals> all child values have to be the same. (It is only possible to compare number with number, string with string,...).
===============================
////
.Example: Inhibit the event "get transactions" if the event already occurred more than 10 times today
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactionsGreaterEqualExample' xmlns='http://www.iese.fraunhofer.de/ind2uce/3.2.46/ind2uceLanguage' xmlns:tns='http://www.iese.fraunhofer.de/ind2uce/3.2.46/ind2uceLanguage' xmlns:parameter='http://www.iese.fraunhofer.de/ind2uce/3.2.46/parameter' xmlns:pip='http://www.iese.fraunhofer.de/ind2uce/3.2.46/pip' xmlns:function='http://www.iese.fraunhofer.de/ind2uce/3.2.46/function' xmlns:event='http://www.iese.fraunhofer.de/ind2uce/3.2.46/event' xmlns:constant='http://www.iese.fraunhofer.de/ind2uce/3.2.46/constant' xmlns:variable='http://www.iese.fraunhofer.de/ind2uce/3.2.46/variable' xmlns:variableDeclaration='http://www.iese.fraunhofer.de/ind2uce/3.2.46/variableDeclaration' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <greaterEqual>
        <count>
          <eventOccurrence event='urn:action:banking-demo:get-transactions'/>
          <when fixedTime='today'/>
        </count>
        <constant:number value='10'/>
      </greaterEqual>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----
////


[[regex]]
==== Regular Expressions
A regular expression (regex) is a formal language to describe a certain pattern or a sequence of characters.
If the regex pattern matches to the child-elements value the <regex> element returns true otherwise it would return false.
The *<regex>* element has the following attributes:

.Regex Attributes 
[width="100%",cols="2,4,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Default Value |Required |Meaning
|regex        |String    |-             |required      |The String defines the regex that is applied for matching the values of child elements. link:https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html[Java notation is used].
|mode         |enum (ALL, EXACTLY_ONE, AT_LEAST_ONE, NONE)      |ALL           |optional       |The mode declares which or how many child element must match.
|======================================================================================================================================

A *<regex>* can have the child elements with string return value and elements from the <<string-functions, string-functions>>.

.Policy Specification Rules
[CAUTION]
===============================
* The <regex> element must have at least one child.
===============================

.Policy Evaluation Rules
[IMPORTANT]
===============================
* The *<regex>* element has four evaluation modes:
** ALL: Regex must match all of the child element values.
** EXACTLY_ONE: Regex must match exactly one of the child element values, but not more than one.
** AT_LEAST_ONE: Regex must at least match one of the child element values.
** NONE: Regex must not match any of the child element values.
===============================

.Example: If the transaction purpose matches to "Gehalt", "Lohn", "salary" or "pay" than replace the amount
[source,xml]
----
<policy id='urn:policy:banking-demo:regexExample'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <regex regex='(Lohn)|(Gehalt)|(salary)|(pay)'>
        <event:string eventParameter='transactions' default='' jsonPathQuery='$.transactions.purpose'/>
      </regex>
      <then>
        <modify eventParameter='transactions' method='replace' jsonPathQuery='$.transactions.amount.value'>
          <parameter:object name='replaceWith' value='XXXX'/>
        </modify>
      </then>
    </if>
  </mechanism>
</policy>
----

[[date-functions]]
==== Date functions 

The date functions *<date>, <time>, and <day>* return true if the specified date / time / date matches the current date. 
Thus, you can check if the event occurred before, after or at a specific date.

.Example: Allow if event occurs in a specific time range
[source,xml]
----
<policy id='urn:policy:cs4:Datetime1' description='test date and time functions'>
  <mechanism event='urn:action:cs4:example'>
    <if>
      <and>
        <date is='before' value='15.05.2040'/>
        <date is='after' value='15.05.2001'/>
        <time is='before' value='20:30'/>
        <time is='after' value='08:30'/>
        <day value='Tuesday'/>
      </and>
      <then>
        <allow/>
      </then>
    </if>
  </mechanism>
</policy>
----

The *<date>* compares a specified date with the current date (of the event evaluation). 

.Date Attributes 
[width="100%",cols="2,2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Default Value |Required |Meaning
|is           |String    |-             |required      |The String defines the time expressions: Before, After, Exactly
|value        |date      |-             |required      |The date in the format of dd.mm.yyyy
|======================================================================================================================================

The *<time>* compares a specified time with the current time (of the event evaluation). 

.Time Attributes 
[width="100%",cols="2,2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Default Value |Required |Meaning
|is           |String    |-             |required      |The String defines the time expressions: Before, After, Exactly
|value        |time      |-             |required      |The time in the format of hh:mm
|======================================================================================================================================

The *<day>* compares a specified day with the current day (of the event evaluation). 

.Day Attributes 
[width="100%",cols="2,2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Default Value |Required |Meaning
|value        |String    |-             |required      |Or-linked, comma separated list of days that match: Tuesday,Friday,Sunday
|======================================================================================================================================


.Example: This policy allows the event when it is Tuesday and it is between 08:30 and 17:00 until the date 15.05.2020
[source,xml]
----
<policy id='urn:policy:cs4:Datetime'>
   <mechanism event='urn:action:cs4:show-tasks'>
      <if>
         <and>
            <date is='before' value='15.05.2020'/>
            <time is='before' value='17:00'/>
            <time is='after' value='08:30'/>
            <day value='Tuesday'/>
         </and>
         <then>
            <allow/>
         </then>
      </if>
   </mechanism>
</policy>
----
[[execute]]
==== Execution of external events

Execute Actions are actions that security policies can trigger in the system.
They are specified using the *<execute>* tag. Execute actions return a Boolean value indicating the execution success.

.Execute Attributes 
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required |Meaning
|action        |String   |required    | The name of action to be executed in an PXP.
|======================================================================================================================================

Child elements can be all elements of the <<parameter-group,parameter-group>>. They serve as input parameters for the corresponding execution function. The mandatory and optional parameters depend on the selected execute action.

.Example: PXP at different places
[source,xml]
----
<policy id='urn:policy:banking-demo:log_Message'>
  <mechanism event='urn:action:banking-demo:get-accounts'>
    <if>
      <execute action='urn:action:banking-demo:notifyAdmin'>
          <parameter:string name='message' value='Dear admin, user trying to access accounts.'/>
      </execute>
      <then>
        <allow/>
        <execute action='urn:action:banking-demo:logNotification'>
          <parameter:string name='message' value='Granted access to accounts.'/>
        </execute>
      </then>
    </if>
    <else>
      <inhibit/>
    </else>
    <execute action='urn:action:banking-demo:logNotification'>
          <parameter:string name='message' value='Access to account'/>
    </execute>
  </mechanism>
</policy>
----

==== Checking if something has changed

<<valueChanged, Click here to learn how to to check if something has changed since the last evaluation>>.

==== Checking if an event occurred periodically

<<valueChanged, Click here to learn how to to check if an event occurred regularly>>.



[[contains]]
==== Checking if a list contains certain elements

*<contains>* can be used to verify whether a list contains a certain value.
For instance, to verify whether the list "[Policy Language, Policy]" contains the string "Policy".

A <contains> element has a "mode" attribute that declares how many child element must match.

It must contain a list as first child and any number of arbitrary elements as further children.

.Contains Attributes 
[width="100%",cols="2,3,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      										|Default Value |Required 	|Meaning
|mode         |enum (ALL, EXACTLY_ONE, AT_LEAST_ONE, NONE)      |ALL           |optional       |The mode declares how many child element must match.
|======================================================================================================================================


.Example: If the accounts contains either IBAN "DE00999940000000000000" or "DE00999940000400000000" (xor) than inhibit.
[source,xml]
----
<policy id='urn:policy:banking-demo:containsExample'>
  <mechanism event='urn:action:banking-demo:get-accounts'>
    <if>
      <contains mode='EXACTLY_ONE'>
        <event:list eventParameter='accounts' default='0' jsonPathQuery='$..iban'/>
        <constant:string value='DE00999940000000000000'/>
        <constant:string value='DE00999940000400000000'/>
      </contains>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

.Policy Specification Rules
[CAUTION]
===============================
* The *<contains>* element must have at least two child elements
* The order is essential, e.g. for the child elements values A, B and C specified in this order, the *<contains>* element evaluates whether A contains B and C.
* In <contains> all child values have to be the same. (It is only possible to compare list of numbers with number, list of string with string,...).
===============================

.Policy Evaluation Rules
[IMPORTANT]
===============================
* The *<contains>* element has four evaluation modes:
** ALL: The first value must match all of the following element values.
** EXACTLY_ONE: The first value must match exactly one of the following element values, but not more than one.
** AT_LEAST_ONE: The first value must at least match one of the following element values.
** NONE: The first value must not match any of the following element values.
===============================


[[eventHasParameter]]
==== Checking whether an event has a specific parameter

To check whether an event has certain parameters the *<eventHasParameter>* element can be used.
Like the <regex> and the <contains> element the <eventHasParameter> has the following mode attribute:

.eventHasParameter Attributes 
[width="100%",cols="2,3,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Default Value |Required |Meaning
|mode         |enum (ALL, EXACTLY_ONE, AT_LEAST_ONE, NONE)     |ALL           |no       |The mode declares how many child element must match.
|======================================================================================================================================

Because <eventHasParameter> refers to a name of an event parameter, it can only have child elements which return string values.

.Example: Log event if user id is empty (event has no parameter $.id)
[source,xml]
----
<policy id='urn:policy:banking-demo:eventHasParameterExample'>
  <mechanism event='urn:action:banking-demo:get-userForStats'>
    <if>
      <eventHasParameter mode='NONE'>
        <event:string eventParameter='user' default='' jsonPathQuery='$.id'/>
      </eventHasParameter>
      <then>
        <execute action='urn:action:banking-demo:logNotification'>
          <parameter:string name='message' value='user id is empty'/>
        </execute>
      </then>
    </if>
  </mechanism>
</policy>
----


.Policy Specification Rules
[CAUTION]
===============================
* The *<eventHasParameter>* element must have at least one child element
===============================

.Policy Evaluation Rules
[IMPORTANT]
===============================
* The *<eventHasParameter>* element has four evaluation modes:
** ALL: All child element values have to be an event parameter.
** EXACTLY_ONE: Exactly one child element value has to be an event parameter, but not more than one.
** AT_LEAST_ONE: At least one child element value has to be an event parameter.
** NONE: No child element value has to be a event parameter.
===============================



[[string-functions]]
=== String Operators

[[basic-string-operators]]
==== Basic String Operators
The basic string operators are <<variable-group,<variable:string>>>, <<constant-group,<constant:string>>>, <<parameter-group,<parameter:string>>>, <<event-group,<event:string>>>, and <<pip-group,<pip:string>>>.


[[concat]]
==== String Concatenation (concat)
*<concat>* concatenates all values of child elements to a string.
It is possible to add any number and kinds of children. 

.Example: This policy prevents showing data if complete employee name is not on the list of priviledged employees
[source,xml]
----
<policy id='urn:policy:cs4:showEmployees'>
  <mechanism event='urn:action:cs4:showEmployees'>
    <if>
      <not>
        <contains>
          <pip:list method='urn:info:cs4:getAllPriviledgedEmployees' default='[]'/>
          <concat>
            <event:string eventParameter='user' default='' jsonPathQuery='$.firstName'/>
            <constant:string value=''/>
            <event:string eventParameter='user' default='' jsonPathQuery='$.lastName'/>
          </concat>
        </contains>
      </not>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

.Policy Specification Rules
[CAUTION]
===============================
* The *<concat>* element must have at least one child element
===============================


.Policy Evaluation Rules
[IMPORTANT]
===============================
For lists and objects, the toString() method is called, which might result in unwanted effects.
===============================


[[object-functions]]
=== Object Operators

Object operators return a complex object that can be application specific (e.g. "com.example.Transaction" or "de.beispiel.User"). 
All operations on these objects are done on their Json representation, as we do not know the class declaration.
To work with complex objects, you can use link:http://goessner.net/articles/JsonPath[JsonPath].  
To simplify the handling of these objects, our policy editor supports auto completion to navigate the Json structure. 
Direct comparison can be done using <equals>.

The object operators are <<variable-group,<variable:object>>>, <<constant-group,<constant:object>>>, <<parameter-group,<parameter:object>>>, <<event-group,<event:object>>>, and <<pip-group,<pip:object>>>.

[[list-functions]]
=== List Operators

List operators return a list of values or objects, which can be of any <<data-types, type>>.
Again, you can use link:http://goessner.net/articles/JsonPath[JsonPath] to work with lists. 
Furthermore, you can use the <<contains, <contains>>> function to check if a list contains a certain value or object.


 
The list operators are <<variable-group,<variable:list>>>, <<constant-group,<constant:list>>>, <<parameter-group,<parameter:list>>>, <<event-group,<event:list>>>, and <<pip-group,<pip:list>>>.


[[working-with-variables]]
== Working with Variables 
If you are reusing the same value at different positions in the policy, you can use variables.
Variables are declared using *variableDeclarations* and referenced by *variables* inside <<if_elseif, conditions>> or <<parameter-group, parameters>>.
At runtime, a variable value is set once per policy and request. The value is then shared by all mechanisms of the policy.

*VariableDeclarations* are defined  directly below the root element <policy>.

[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactions'>
  <variableDeclaration:string name='RoleLoggedUser'>
    <pip:string method='urn:info:banking-demo:checkRole' default=''>
      <parameter:number name='loggedUser' value='1'/>
    </pip:string>
  </variableDeclaration:string>
  <mechanism event='urn:action:banking-demo:get-bankusers'>
    ...
  </mechanism>
</policy>
----


Based on the <<data-types, data types>>, the following variableDeclarations are available:

* <variableDeclaration:string>
* <variableDeclaration:number>
* <variableDeclaration:object>
* <variableDeclaration:list>
* <variableDeclaration:boolean>

VariableDeclarations are basically key-value pairs. The name is used to refer to the variable and is defined as an attribute:

.variableDeclaration attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required 		|Meaning
|name         |String  	 |required      |The name of the variableDeclaration used to refer to it. Must be unique per policy.
|======================================================================================================================================


The variableDeclaration's value stems from the child element of the variableDeclaration.
The following example shows a declaration of a boolean variable *RoleLoggedUser*, that takes a PIP value.
The benefit of this construct is, that the PIP is only called once per policy and not everytime it is referenced.

.Policy Evaluation Rules
[IMPORTANT]
===============================
A variable declaration is only evaluated *once* per policy and event.
This is especially important if you are using <<pip-group,PIPs>> or <<execute,PXPs>> that can change their value on repeated evaluation. 
This means for example: If a policy contains 5 mechanisms referencing a variable based on a PIP, this PIP is only queried once and the result is used by all mechanisms.
===============================

To reference a variable value inside a <<if_else, condition>> or <<parameter-group, parameter>>, the following variables can be used:

* <variable:string>
* <variable:number>
* <variable:object>
* <variable:list>
* <variable:boolean>

Every element has the reference attribute:

.Variable attributes 
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required |Meaning
|reference    |String    |required |The reference name to a <<variableDeclaration-group,variableDeclaration>>.
|======================================================================================================================================

.Example: Variable used to reference a PIP value
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactions'>
  <variableDeclaration:string name='RoleLoggedUser'>
    <pip:string method='urn:info:banking-demo:checkRole' default=''>
      <parameter:number name='loggedUser' value='1'/>
    </pip:string>
  </variableDeclaration:string>
  <mechanism event='urn:action:banking-demo:get-bankusers'>
    <if>
      <or>
        <equals>
          <variable:string reference='RoleLoggedUser'/>
          <constant:string value='Customer'/>
        </equals>
        <equals>
          <variable:string reference='RoleLoggedUser'/>
          <constant:string value='External'/>
        </equals>
      </or>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----


== Working with the Event History

In order to express certain usage control obligations, it is necessary to refer to events that occurred in the past.
For example, if you want to prevent a file to be printed more than 3 times, this information must be stored.
For this purpose, we offer an event history with three basic query types:

* counting specific events in a certain time range
* checking if a value changed since the last evaluation
* checking if an event occurs periodically 

.Policy Evaluation Rules
[IMPORTANT]
===============================
* Events are only stored in the event history, if there is a policy that requires it by the usage of a event history operator
* All event information (ID and parameters) are hashed in the event history to guarantee privacy
* Events are deleted if they are needed anymore by any policy
===============================

[[count]]
=== Counting of Events
With *<count>*, it is possible to express the cardinal and temporal aspects.
It is used to count the number of event occurrences in a certain time interval. 
Thus, it has exactly two children: 

* <<eventOccurrence,<eventOccurrence>>> is used refer to certain event(s) you want to count
* <<time-elements,<when>>> is used to define a time range for the counting

In the following, you see an example counting the number of times the event "get-money" occurred this year for the customer with ID "5".

.Example: Counting get-money events
[source,xml]
----
<policy id='urn:policy:banking-demo:getTransactionsExample'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <greaterEqual>
        <count>
          <eventOccurrence event='urn:action:banking-demo:get-money'>
            <parameter:number name="customerID" value="5"/>
          <eventOccurrence>
          <when>
            <start time='1.1.* 00:00'/>
          </when>
        </count>
        <constant:number value='15'/>
      </greaterEqual>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

[[continuousOccurrence]]
=== Evaluation of Periodic Events

With the *<continuousOccurrence>* element it is possible to evaluate, if an event occurred periodically during a certain time interval.
Just like <count>, it has exactly two children: 

* <<eventOccurrence,<eventOccurrence>>> is used refer to certain event(s) you want to count
* <<time-elements,<when>>> is used to define a time range for the counting

However, there are the following additional attributes:

.continuousOccurrence attributes 
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute      |Type         |Required |Meaning
|interval       |String       |required      |The interval declares the time interval in which an event has to occur to fulfill the condition, for example "1 year". It has follow the following syntax: (([0-9]+y)?([0-9]+w)?([0-9]+d)?([0-9]+h)?([0-9]+m)?([0-9]+s)?) (e.g., 1y), where y = years, w = weeks, d = days, h = hours, m = minutes, s = seconds. 
|minOccurrences |number       |optional       |Declares the minimum number of occurrences until the condition is fulfilled.
|maxOccurrences |number       |optional       |Declares the maximum number of Occurrences that the condition is fulfilled.
|======================================================================================================================================


The following example shows a policy that inhibits an event if the user was not notified at least once a day since the beginning of the year. 

.Example: continuousOccurrence
[source,xml]
----
<policy id='urn:policy:banking-demo:continuousOccurrenceExample'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
    	<not>
	      <continuousOccurrence interval='1d' minOccurrences='1'>
	        <eventOccurrence event='urn:action:banking-demo:notify-user'/>
	        <when>
	        	<start time='1.1.* 00:00"/>
	        </when>
	      </continuousOccurrence>
        </not>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

[[valueChanged]]
=== Evaluation of Changes over Time
When using the *<valueChanged>* function, the system checks if the return value of the child has changed between the last evaluation cycle and this evaluation cycle to the value defined in the attribute.
The *<valueChanged>* element only returns true if the value is changed.
The following valueChanged elements are available:

* <valueChanged:string>
* <valueChanged:number>
* <valueChanged:object>
* <valueChanged:list>
* <valueChanged:boolean>

*<valueChanged>* has the following optional attribute:

.valueChanged attributes 
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required |Meaning
|to        |Boolean      |optional    | Defines if the value of the child element changed to true or false.
|id        |String (UUID)      |required    | Defines a UUID for the valueChanged block within the current policy. Automatically generated by our editor.
|======================================================================================================================================

Child elements can be all elements with matching return value (e.g. <constant:true>, <event:boolean> for valueChanged:boolean, <constant:string>, <event:string> for valueChanged:string) or elements of the <<boolean-functions>,boolean-functions>>, <<string-functions>,string-functions>>, <<number-functions,number-functions>>.

.Policy Specification Rules
[CAUTION]
===============================
* The <valueChanged> element must have exactly one child.
===============================

.Example: As soon as the number of show-user events is greater than 5, a notification is sent to the logs.
[source,xml]
----
<policy id='urn:policy:cs4:valueChanged'>
   <variableDeclaration:number name='numberAccess'>
      <count>
         <eventOccurrence event='urn:action:cs4:show-user'/>
         <when fixedTime='today'/>
      </count>
   </variableDeclaration:number>
   <mechanism event='urn:action:cs4:show-project'>
      <if>
         <valueChanged to='true' id='vc1'>
            <greater>
               <variable:number reference='numberAccess'/>
               <constant:number value='5'/>
            </greater>
         </valueChanged>
         <then>
            <execute action='urn:action:cs4:logNotification'>
               <parameter:string name='message' value='Users where display more than 5 times today'/>
            </execute>
         </then>
      </if>
   </mechanism>
</policy>
----

<<time-elements, Click here to learn how to define a time span>>.

[[eventOccurrence]]
=== Referring to an Event in the Event History
To refer to an event, the <eventOccurrence> element is used.
It can be used inside a <count> or <continuousOccurrence> tag.
The element has the following attributes:

.eventOccurrence attributes 
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute      |Type      |Required  		  |Meaning
|event          |String    |required          |Declares the event. The event id follows this pattern: urn:action:<scope>:<identifier>
|mode           |String    |optional          |Possible values are "FIRST" or "LAST", to get the first or last eventOccurrence.
|======================================================================================================================================

.Example: Event Occurrence of urn:action:banking-demo:get-transactions
[source,xml]
----
<policy id='urn:policy:banking-demo:containsExample'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <greater>
        <count>
          <eventOccurrence event='urn:action:banking-demo:get-transactions'/>
          <thisMonth/>
        </count>
        <constant:number value='100'/>
      </greater>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

[[time-elements]]
=== Working with Time Spans

To work with the event history, it is essential to define a time span in which you are looking for an event.
For example, you might look for certain events yesterday, or in the last 45 minutes.
*<when>* is used to express these time specifications inside *<continuousOccurrence>* and *<count>*.

Time spans can be defined using fixed (predefined) time spans as an attribute, or using custom time spans as children. 

.Policy Evaluation Rules
[IMPORTANT]
===============================
Fixed time spans override custom time spans.
===============================

[[fixedTime]]
==== Fixed Time Spans
The easiest way to work with time spans is to use predefined or fixed time spans as an attribute of *<when>*.
The following table shows you all available time spans, including an example for each.

.fixedTimes with examples 
[width="100%",cols="2,8,2,2,2",options="header"]
|======================================================================================================================================
|Expression   		|Description									|Event occurrance date (now)|Resulting start 		|Resulting end
|*thisMinute*		|event occurred within this minute				|06.02.2018 10:35:30		|06.02.2018 10:35:00	|06.02.2018 10:35:30
|*lastMinute*		|event occurred within the last 60 seconds		|06.02.2018 10:35:30		|06.02.2018 10:34:00	|06.02.2018 10:34:59
|*thisHour*		|event occurred within this hour				|06.02.2018 10:35:30		|06.02.2018 10:00:00	|06.02.2018 10:35:30
|*lastHour*		|event occurred within the last 60 minutes		|06.02.2018 10:35:30		|06.02.2018 09:00:00	|06.02.2018 09:59:59
|*today*			|event occurred today							|06.02.2018 10:35:30		|06.02.2018 00:00:00	|06.02.2018 10:35:30
|*yesterday*		|event occurred yesterday						|06.02.2018 10:35:30		|05.02.2018 00:00:00	|05.02.2018 23:59:59
|*thisMonth*		|event occurred this month						|06.02.2018 10:35:30		|01.02.2018 00:00:00	|06.02.2018 10:35:30
|*lastMonth*		|event occurred last month						|06.02.2018 10:35:30		|01.01.2018 00:00:00	|31.01.2018 23:59:59
|*thisYear*		|event occurred this year						|06.02.2018 10:35:30		|01.01.2018 00:00:00	|06.02.2018 10:35:30
|*lastYear*		|event occurred last year						|06.02.2018 10:35:30		|01.01.2017 00:00:00	|31.12.2017 23:59:59
|*thisWeek*		|event occurred this week (Monday to Sunday)	|06.02.2018 10:35:30		|05.02.2018 00:00:00	|06.02.2018 10:35:30
|*lastWeek*		|event occurred last week (Monday to Sunday)	|06.02.2018 10:35:30		|29.01.2018 00:00:00	|04.02.2018 23:59:59
|*thisSunWeek*	|event occurred this week (Sunday to Saturday)	|06.02.2018 10:35:30		|04.02.2018 00:00:00	|06.02.2018 10:35:30
|*lastSunWeek*	|event occurred last week (Sunday to Saturday)	|06.02.2018 10:35:30		|28.01.2018 00:00:00	|03.02.2018 23:59:59
|*always*			|event occurred, no matter when					|06.02.2018 10:35:30		|01.01.1970 00:00:00	|06.02.2018 10:35:30
|======================================================================================================================================

.Example: Inhibit action if event get-money was already fired more than 15 times in this minute
[source,xml]
----
<policy id='urn:policy:banking-demo:ExamplePolicy'>
  <mechanism event='urn:action:banking-demo:get-transactions'>
    <if>
      <greaterEqual>
        <count>
          <eventOccurrence event='urn:action:banking-demo:get-money'/>
          <when fixedTime="thisMinute"/>
        </count>
        <constant:number value='15'/>
      </greaterEqual>
      <then>
        <inhibit/>
      </then>
    </if>
  </mechanism>
</policy>
----

[[when]]
==== Dynamic Time Spans 
To define a custom time span, *<start>* and *<end>* can be used as children of *<when>*.
With these to elements, you can define a time span based on absolute dates (e.g., 04.02.2018), or on dates that are relative to now or to a certain event (e.g., in the last 5 minutes).
 
The <start> and <end> elements have the following attribute:

.start and end attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type       			|Required 		|Meaning
|time         |time reference       |required     	|Declares the absolute or relative point in time for start or end.
|======================================================================================================================================

===== Absolute Dates
Time references follows the following syntax: *DD.MM.YYYY hh:mm* in 24 hour format. So, if you want to define a time span with absolute dates, you can just do like in the following example:

.Example: Dynamic time spans with absolute dates
[source,xml]
----
<when>
	<start time="01.01.2018 00:00" /> 
	<end time="31.06.2018 00:00" />
</when>
----

However, this is not sufficient to express flexible time spans like "in the last 5 minutes". 
To do this, we use a special notation to override the individual date parts (day, month, year, hour, minute):

===== Wildcard
Relacing a part with an *asterisk ** overwrites the value of the absolute date with the value of the *current* date. 

.Example: Wildcard
[source,xml]
----
<when>
	<start time="*01.*.* 00:00" /> <!-- The first of the current month in the current year at 00:00 -->
</when>
----
  
===== Substraction  
Replacing a part of the date with a *-<n>* subtracts n to the value of the *current* date*.

.Example: Substraction
[source,xml]
----
<when>
	<start time="*.*.* *:-5" /> <!-- Five minutes ago -->
</when>
----

===== Addition  
Replacing a part with a *+<n>* adds n to the value of the *current* date*.

.Example: Addition
[source,xml]
----
<when>
	<start time="*.*.* -1:+5" /> <!-- started 55 minutes ago -->
</when>
----

===== Event References

Both <start> and <end> can have a <<eventOccurrence,<eventOccurrence>>> child.
In this case, the declared event is used as a reference for the time calculation, not the current one. 

.Example: Event References in when
[source,xml]
----
<when>
	<start time="*.-1.* *:*:*"> <!-- started one month before the first occurrence of the event B -->
    	 <eventOccurrence event="B" mode="FIRST"/>
	</start> 
	<end time="*.*.* *:-5" /> <!-- ends five minutes ago from NOW --> 
</when>
----

.Policy Evaluation Rules
[IMPORTANT]
===============================
* As we (unfortunately) cannot look into the future, the end is always the current date of evaluation (e.g., "1.1.+1000 00:00" will not make much sense and is equivalent to "\*.*.* \*:*" (now)). 
* fixedTimes overwrite dynamic time stamps
===============================




[[timer]]
== Working with Cron Jobs
Sometimes it is necessary to evaluate a policy independently of a system event, or in a regular manner.
To stick with the Event-Condition-Action paradigm, timers can be set that regularly trigger events. 
These events are handled exactly like "normal" events.

These *<timer>* are NOT part of an actual policy, but are configured separately via our UI or API (as they might trigger several policies at the same time).

A <timer> can have the following attributes:

.Timer attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute      |Type       |Required |Meaning
|id				|URN		|required |The unique ID of the timer. Syntax: urn:timer:<solution>:<id>
|cron           |cron       |required |Describes the time interval to fire an event in link:https://www.manpagez.com/man/5/crontab/[cron syntax].
|description    |String     |optional |Description of the timer.
|======================================================================================================================================


A <timer> must contain at least one <event> child, declaring the event to be triggered.
Like normal events, it has an event ID (as parameter) and a list of <<parameter-group,parameters>> as children.
The <timeEvent> has the following attribute:

.Event attributes
[width="100%",cols="2,2,2,10",options="header"]
|======================================================================================================================================
|Attribute    |Type      |Required |Meaning
|action       |String    |required |Defines the event id.
|======================================================================================================================================

The following example shows a timer that triggers the event "regular-check" every day at 1:01.

.Example timer
[source,xml]
----
<timer cron='0 1 1 * * ?' id='urn:timer:banking-demo:check'>
  <event action='urn:action:banking-demo:regular-check'>
    <parameter:number name='processID' value='12'/>
  </event>
</timer>
----