Skip to content

Commit f637203

Browse files
committed
Complience policy incorrect
Problem: The Current Outstanding compliance policy is missing the closing_event_display_type field. Additionally there are no functional tests or example code, Solution: Added functional tests and corrected the current outstanding compliance policy. Added example code and notebook. Signed-off-by: Paul Hewlett <[email protected]>
1 parent 948ce1c commit f637203

File tree

8 files changed

+831
-3
lines changed

8 files changed

+831
-3
lines changed

archivist/compliance_policies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def count(self, *, props: Optional[dict[str, Any]] = None) -> int:
167167
Counts number of compliance policies that match criteria.
168168
169169
Args:
170-
props (dict): e.g. {"display_name": "foo" }
170+
props (dict): e.g. {"compliance_type": "COMPLIANCE_RICHNESS" }
171171
172172
Returns:
173173
integer count of compliance policies.

archivist/compliance_policy_requests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class CompliancePolicyCurrentOutstanding(CompliancePolicyBase):
5050
"""
5151

5252
event_display_type: str
53+
closing_event_display_type: str
5354
compliance_type: str = CompliancePolicyType.COMPLIANCE_CURRENT_OUTSTANDING.name
5455

5556

docs/compliance_policies_since.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. _compliance_policies_sinceref:
2+
3+
Since Compliance Policy
4+
........................
5+
6+
.. literalinclude:: ../examples/compliance_policies_since.py
7+
:language: python
8+
9+
10+

docs/getting_started.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ See the examples and functests directories.
2525
access_policy_create
2626
access_policies_filter
2727

28+
compliance_policies_since
29+
2830
runner/index
2931

3032
scan_test
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
"""Define a compliance policy that alerts when an asset has expired.
2+
3+
Main function parses in a url to the Archivist and client credentials , which is
4+
a user authorization. The main function would initialize an archivist connection
5+
using the url and the credentials, called "arch", then call arch.access_policies.list()
6+
with suitable properties and attributes.
7+
8+
"""
9+
from json import dumps as json_dumps
10+
from os import getenv
11+
from time import sleep
12+
from uuid import uuid4
13+
from warnings import filterwarnings
14+
15+
from archivist.archivist import Archivist
16+
from archivist.compliance_policy_requests import (
17+
CompliancePolicySince,
18+
)
19+
from archivist.utils import get_auth
20+
21+
filterwarnings("ignore", message="Unverified HTTPS request")
22+
23+
24+
def get_archivist():
25+
"""Create Archivist endpoint."""
26+
27+
# client id and client secret is obtained from the appidp endpoint - see the
28+
# application registrations example code in examples/applications_registration.py
29+
#
30+
# client id is an environment variable. client_secret is stored in a file in a
31+
# directory that has 0700 permissions. The location of this file is set in
32+
# the client_secret_file environment variable.
33+
#
34+
auth = get_auth(
35+
auth_token=getenv("ARCHIVIST_AUTHTOKEN"),
36+
auth_token_filename=getenv("ARCHIVIST_AUTHTOKEN_FILENAME"),
37+
client_id=getenv("ARCHIVIST_CLIENT_ID"),
38+
client_secret=getenv("ARCHIVIST_CLIENT_SECRET"),
39+
client_secret_filename=getenv("ARCHIVIST_CLIENT_SECRET_FILENAME"),
40+
)
41+
42+
# Initialize connection to Archivist
43+
arch = Archivist(
44+
"https://app.rkvst.io",
45+
auth,
46+
)
47+
return arch
48+
49+
50+
def create_compliance_policy(arch, tag):
51+
"""Compliance policy which expires 10 seconds after a
52+
Maintenance Performed event on a 'Traffic Light' has occurred.
53+
54+
Usually the expiry time is on the order of days or weeks..
55+
56+
Additionally the use of tag is simply to make this example
57+
repeatable.
58+
"""
59+
compliance_policy = arch.compliance_policies.create(
60+
CompliancePolicySince(
61+
description="Maintenance should be performed every 10 seconds",
62+
display_name="Regular Maintenance of Traffic light",
63+
asset_filter=[
64+
["attributes.arc_display_type=Traffic Light"],
65+
],
66+
event_display_type=f"Maintenance Performed {tag}",
67+
time_period_seconds=10, # very short so we can test
68+
)
69+
)
70+
print("SINCE_POLICY:", json_dumps(compliance_policy, indent=4))
71+
return compliance_policy
72+
73+
74+
def create_traffic_light(arch):
75+
"""
76+
Creates a traffic light.
77+
78+
Note that arc_display_type siginfies a Traffic Light
79+
"""
80+
81+
traffic_light = arch.assets.create(
82+
attrs={
83+
"arc_display_name": "Traffic light model 54",
84+
"arc_description": "Traffic flow control light at A603 North East",
85+
"arc_display_type": "Traffic Light",
86+
},
87+
confirm=True,
88+
)
89+
print("TRAFFIC_LIGHT:", json_dumps(traffic_light, indent=4))
90+
return traffic_light
91+
92+
93+
def perform_maintenance(arch, traffic_light, tag):
94+
"""
95+
Perform maintenance on traffic light
96+
"""
97+
maintenance_performed = arch.events.create(
98+
traffic_light["identity"],
99+
{
100+
"operation": "Record",
101+
"behaviour": "RecordEvidence",
102+
},
103+
{
104+
"arc_description": "Maintenance performed on traffic light",
105+
"arc_display_type": f"Maintenance Performed {tag}",
106+
},
107+
confirm=True,
108+
)
109+
print("MAINTENANCE_PERFORMED:", json_dumps(maintenance_performed, indent=4))
110+
111+
112+
def main():
113+
"""
114+
Connect to archivist, create an asset, create a compliance policy
115+
execute an event on the asset and check if the asset has expired
116+
"""
117+
# first get Archivist connection.
118+
arch = get_archivist()
119+
120+
tag = uuid4() # make this example repeatable
121+
122+
# make a SINCE compliance policy that alerts when the
123+
# maintenance performed event has expired.
124+
compliance_policy = create_compliance_policy(arch, tag)
125+
126+
# create an asset that matches the assets_filter field in the
127+
# compliance policy.
128+
traffic_light = create_traffic_light(arch)
129+
130+
# perform maintenance on the asset which is valid for 10 seconds.
131+
perform_maintenance(arch, traffic_light, tag)
132+
133+
# and check compliance - should be OK.
134+
print("Sleep 1 second...")
135+
sleep(1)
136+
compliance = arch.compliance.compliant_at(
137+
traffic_light["identity"],
138+
)
139+
print("COMPLIANCE (true):", json_dumps(compliance, indent=4))
140+
141+
# however waiting long enough (> 10s) will cause the asset to
142+
# become non-compliant...
143+
print("Sleep 15 seconds...")
144+
sleep(15)
145+
compliance = arch.compliance.compliant_at(
146+
traffic_light["identity"],
147+
)
148+
print("COMPLIANCE (false):", json_dumps(compliance, indent=4))
149+
150+
# finally delete the compliance_policy
151+
arch.compliance_policies.delete(
152+
compliance_policy["identity"],
153+
)
154+
155+
156+
if __name__ == "__main__":
157+
main()

0 commit comments

Comments
 (0)