|
| 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