|
3 | 3 | import logging |
4 | 4 | import logging.config |
5 | 5 | import time |
| 6 | +from functools import lru_cache |
6 | 7 |
|
7 | 8 | import jwt |
8 | 9 | import requests |
@@ -42,7 +43,6 @@ def __init__( |
42 | 43 | self.requests_session = requests.Session() |
43 | 44 | self.url = url |
44 | 45 | self.token = token |
45 | | - self.auth_token = None |
46 | 46 | self.logs_drain_timeout = logs_drain_timeout |
47 | 47 | self.stdout_logger = get_stdout_logger(debug) |
48 | 48 | self.backup_logs = backup_logs |
@@ -90,37 +90,43 @@ def init_app(self, app, token): |
90 | 90 | def set_token(self, token_secret): |
91 | 91 | self.token = token_secret |
92 | 92 |
|
93 | | - def sha1_hash(self, value): |
| 93 | + @staticmethod |
| 94 | + def sha1_hash(value): |
94 | 95 | hash_object = hashlib.sha1(value.encode("utf-8")) |
95 | 96 | return "sha1:" + hash_object.hexdigest() |
96 | 97 |
|
| 98 | + @staticmethod |
| 99 | + def get_ttl_hash(seconds=600): |
| 100 | + return round(time.time() / seconds) |
| 101 | + |
| 102 | + @lru_cache(maxsize=128) |
| 103 | + def decode_token(token, ttl_hash=None): |
| 104 | + return jwt.decode( |
| 105 | + token, |
| 106 | + options={"verify_signature": False, "verify_exp": False}, |
| 107 | + ) |
| 108 | + |
97 | 109 | def clean_pii(self, payload): |
98 | | - clean_headers = self.scrub_headers |
99 | | - if "req" in payload and "headers" in payload["req"]: |
100 | | - for k, v in payload["req"]["headers"].items(): |
101 | | - if k.lower() in clean_headers: |
102 | | - if k.lower() == "authorization" and "bearer " in v.lower(): |
103 | | - self.oauth = True |
104 | | - v = v.split(" ")[1] |
105 | | - self.auth_token = v |
106 | | - payload["req"]["headers"][k] = self.sha1_hash(v) |
107 | | - if "res" in payload and "headers" in payload["res"]: |
108 | | - for k, v in payload["res"]["headers"].items(): |
109 | | - if k.lower() in clean_headers: |
110 | | - payload["req"]["headers"][k] = self.sha1_hash(v) |
111 | | - |
112 | | - if self.oauth and self.enrich_oauth: |
| 110 | + oauth = False |
| 111 | + auth_token = None |
| 112 | + |
| 113 | + for k, v in payload["req"].get("headers", {}).items(): |
| 114 | + if k.lower() == "authorization" and "bearer " in v.lower(): |
| 115 | + oauth = True |
| 116 | + auth_token = v.split(" ")[1] if " " in v else None |
| 117 | + if k.lower() in self.scrub_headers: |
| 118 | + payload["req"]["headers"][k] = "{SANITIZED_HEADER:" + self.sha1_hash(v) + "}" |
| 119 | + |
| 120 | + for k, v in payload["res"].get("headers", {}).items(): |
| 121 | + if k.lower() in self.scrub_headers: |
| 122 | + payload["res"]["headers"][k] = "{SANITIZED_HEADER:" + self.sha1_hash(v) + "}" |
| 123 | + |
| 124 | + if auth_token not in [None, ""] and oauth and self.enrich_oauth: |
113 | 125 | try: |
114 | | - jwt_decoded = jwt.decode( |
115 | | - self.auth_token, |
116 | | - options={"verify_signature": False, "verify_exp": False}, |
117 | | - ) |
118 | | - except jwt.exceptions.DecodeError: |
119 | | - self.oauth = False |
120 | | - if self.oauth: |
| 126 | + jwt_decoded = self.decode_token(auth_token, ttl_hash=self.get_ttl_hash()) |
121 | 127 | payload["oauth"] = {"sub": jwt_decoded["sub"]} |
122 | | - if "email" in jwt_decoded: |
123 | | - payload["oauth"]["email"] = jwt_decoded["email"] |
| 128 | + except jwt.exceptions.DecodeError: |
| 129 | + pass |
124 | 130 | return payload |
125 | 131 |
|
126 | 132 | def format_headers(self, req_headers): |
@@ -154,7 +160,6 @@ def create(self, response, token, diff=-1, scrub_headers=None, debug=False): |
154 | 160 | "resource": request.url_rule.rule if request.url_rule is not None else request.path, |
155 | 161 | "method": request.method, |
156 | 162 | "body": request.get_data(as_text=True), |
157 | | - |
158 | 163 | "ip": request.remote_addr, |
159 | 164 | }, |
160 | 165 | "response": { |
|
0 commit comments