Repository: rucio/rucio

Issue:
NoneType errors during trace validation
Motivation
----------
serverprodwriter-rucio-server-trace-* pods are generating a lot of errors that look like:
```
ERROR:root:Internal Error
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/rucio/web/rest/flaskapi/v1/common.py", line 66, in dispatch_request
     return super(ErrorHandlingMethodView, self).dispatch_request(*args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/flask/views.py", line 163, in dispatch_request
     return meth(*args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/rucio/web/rest/flaskapi/v1/traces.py", line 74, in post
     trace(payload=payload)
   File "/usr/local/lib/python3.6/site-packages/rucio/core/trace.py", line 242, in trace
     validate_schema(report)
   File "/usr/local/lib/python3.6/site-packages/rucio/core/trace.py", line 285, in validate_schema
     validate(obj, SCHEMAS.get(obj['eventType'].lower()))
  File "/usr/local/lib/python3.6/site-packages/jsonschema/validators.py", line 926, in validate
     cls = validator_for(schema)
   File "/usr/local/lib/python3.6/site-packages/jsonschema/validators.py", line 956, in validator_for
     if schema is True or schema is False or "$schema" not in schema:
 TypeError: argument of type 'NoneType' is not iterable

```


Modification
------------
Investigate the trace schema violation and modify the schemas accordingly




Discussion:
The error is caused by the `eventType` of incoming traces to be `put_sm`, 
```
{
  "eventType": "put_sm", 
  "eventVersion": "2.12.8 (1)+", 
  "protocol": "davs", 
  "clientState": "DONE",
...
```

which is not mapped to any of the schemas:
https://github.com/rucio/rucio/blob/d19db9eeb09440ae97477840fff57112f9b25620/lib/rucio/core/trace.py#L199-L208 

The lookup for a schema for `put_sm`eventType returns None. This causes jsonschema to raise the NoneType exception.

https://github.com/Julian/jsonschema/blob/6787b213d8b5bdad5af29b7c96aeef7ec715aa02/jsonschema/validators.py#L1028-L1029


 The following fields must be addressed when working on #5105 and defining more concretely the `put_schema`:
```
a_lot_of_data = [
    #     '{"eventType": "put_sm_a", "eventVersion": "2.12.8 (1)+1.25.5.post1", "protocol": "davs", "clientState": "DONE", "localSite": "LOCALSITE", "remoteSite": "REMOTE_SCRATCHDISK", "timeStart": 1642584168.9667413, "catStart": 1642584169.4159014, "relativeStart": 1642584168.9671576, "transferStart": 1642584168.967158, "validateStart": null, "timeEnd": 1642584173.6030173, "dataset": "datasetdataset", "version": null, "duid": null, "filename": "someFile", "guid": "F534F392B10924478AD8C15D13406B0E", "filesize": 140061588, "usr": "asd323esadasd", "appid": "54353454", "hostname": "abcd.fr", "ip": "11.1.22.22", "suspicious": "0", "usrdn": "ddmadmin", "url": "/tmp/pilot/user.asds", "stateReason": "OK", "uuid": "uuiduuiduuid", "taskid": "27855201", "pq": "SITENAME", "scope": "test", "traceTimeentry": "2022-01-19T09:22:54.235706", "traceTimeentryUnix": 1642584174.235706, "traceIp": "::ffff:134.158.50.52", "traceId": "asdasd12312312312"}',
    #     '{"eventType": "put_sm", "eventVersion": "3.1.0 (63)+1.25.5.post1", "protocol": "root", "clientState": "DONE", "localSite": "LOCALSITE", "remoteSite": "REMOTE-PROD_DATADISK", "timeStart": 1642584196.04648, "catStart": 1642584198.319246, "relativeStart": 1642584196.0473182, "transferStart": 1642584196.0473185, "validateStart": null, "timeEnd": 1642584232.5642772, "dataset": "mc16dataset", "version": null, "duid": null, "filename": "someFile", "guid": "F7FFE76349B66645B766294D7148704E", "filesize": 2399320416, "usr": "asda312423sad", "appid": "345245345", "hostname": "asdwe.cern.ch", "ip": "128.111.33.222", "suspicious": "0", "usrdn": "ddmadmin", "url": "/pool/condor/dir", "stateReason": "OK", "uuid": "uiduuuiduiuidu", "taskid": "27766391", "pq": "SITENAME", "scope": "test", "traceTimeentry": "2022-01-19T09:23:52.734546", "traceTimeentryUnix": 1642584232.734546, "traceIp": "2001:1458:301:39::100:18", "traceId": "23423dfsdfsd"}',
 ]
```

Resolving patch:
diff --git a/lib/rucio/core/trace.py b/lib/rucio/core/trace.py
--- a/lib/rucio/core/trace.py
+++ b/lib/rucio/core/trace.py
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright 2013-2021 CERN
+# Copyright 2013-2022 CERN
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
 # - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2020-2021
 # - Martin Barisits <martin.barisits@cern.ch>, 2021
 # - David Población Criado <david.poblacion.criado@cern.ch>, 2021
-# - Mayank Sharma <mayank.sharma@cern.ch>, 2021
+# - Mayank Sharma <mayank.sharma@cern.ch>, 2021-2022
 
 """
 Core tracer module
@@ -196,6 +196,34 @@
                  'filename', 'dataset']
 }
 
+PUT_SCHEMA = {
+    "description": "get method, mainly sent by pilots",
+    "type": "object",
+    "properties": {
+        "eventType": {"enum": ["put_sm", "put_sm_a"]},
+        "clientState": CLIENT_STATE,
+        "stateReason": {"type": "string"},
+        "url": {"type": "string"},
+        "vo": {"type": "string"},
+        "scope": SCOPE,
+        "eventVersion": {"type": "string"},
+        "remoteSite": RSE,
+        "datasetScope": {"type": "string"},
+        "dataset": {"type": "string"},
+        "filename": {"type": "string"},
+        "name": {"type": "string"},
+        "traceTimeentry": TIME_ENTRY,
+        "traceTimeentryUnix": {"type": "number"},
+        "traceIp": IPv4orIPv6,
+        "traceId": UUID,
+        "usrdn": {"type": "string"},
+        "pq": {"type": "string"},
+        "localSite": RSE
+    },
+    "required": ['eventType', 'localSite', 'eventVersion', 'uuid', 'scope',
+                 'filename', 'dataset']
+}
+
 SCHEMAS = {
     'touch': TOUCH_SCHEMA,
     'upload': UPLOAD_SCHEMA,
@@ -204,7 +232,12 @@
     'get_sm': GET_SCHEMA,
     'sm_get': GET_SCHEMA,
     'get_sm_a': GET_SCHEMA,
-    'sm_get_a': GET_SCHEMA
+    'sm_get_a': GET_SCHEMA,
+    'put': PUT_SCHEMA,
+    'put_sm': PUT_SCHEMA,
+    'put_sm_a': PUT_SCHEMA,
+    'sm_put': PUT_SCHEMA,
+    'sm_put_a': PUT_SCHEMA
 }
 
 FORMAT_CHECKER = draft7_format_checker


Test patch: