Skip to content

Commit

Permalink
Merge pull request #43 from FireTail-io/fix/logging-pii
Browse files Browse the repository at this point in the history
fixed key for request + response
  • Loading branch information
rileyfiretail authored Aug 2, 2024
2 parents ff7cbd5 + 752b703 commit 7724698
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions firetail/auditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import logging.config
import time
from functools import lru_cache

import jwt
import requests
Expand Down Expand Up @@ -91,40 +90,39 @@ def set_token(self, token_secret):
self.token = token_secret

@staticmethod
def sha1_hash(value):
hash_object = hashlib.sha1(value.encode("utf-8"))
return "sha1:" + hash_object.hexdigest()
def sha_hash(value):
hash_object = hashlib.sha256(value.encode("utf-8"))
return "sha256:" + hash_object.hexdigest()

@staticmethod
def get_ttl_hash(seconds=600):
return round(time.time() / seconds)

@lru_cache(maxsize=128)
def decode_token(token, ttl_hash=None):
def decode_token(auth_token):
return jwt.decode(
token,
auth_token.encode(),
options={"verify_signature": False, "verify_exp": False},
)

def clean_pii(self, payload):
oauth = False
auth_token = None

for k, v in payload["req"].get("headers", {}).items():
if k.lower() == "authorization" and "bearer " in v.lower():
if auth_header := request.headers.get("Authorization", request.headers.get("authorization")):

if "bearer " in auth_header.lower():
oauth = True
auth_token = v.split(" ")[1] if " " in v else None
auth_token = auth_header.split(" ")[1] if " " in auth_header else None

for k, v in payload["request"].get("headers", {}).items():
if k.lower() in self.scrub_headers:
payload["req"]["headers"][k] = "{SANITIZED_HEADER:" + self.sha1_hash(v) + "}"
payload["request"]["headers"][k] = ["{SANITIZED_HEADER:" + self.sha_hash(item) + "}" for item in v]

for k, v in payload["res"].get("headers", {}).items():
for k, v in payload["response"].get("headers", {}).items():
if k.lower() in self.scrub_headers:
payload["res"]["headers"][k] = "{SANITIZED_HEADER:" + self.sha1_hash(v) + "}"

payload["response"]["headers"][k] = ["{SANITIZED_HEADER:" + self.sha_hash(item) + "}" for item in v]
if auth_token not in [None, ""] and oauth and self.enrich_oauth:
try:
jwt_decoded = self.decode_token(auth_token, ttl_hash=self.get_ttl_hash())
payload["oauth"] = {"sub": jwt_decoded["sub"]}
jwt_decoded = self.decode_token(auth_token)
payload["oauth"] = {"subject": jwt_decoded["sub"]}
except jwt.exceptions.DecodeError:
pass
return payload
Expand Down

0 comments on commit 7724698

Please sign in to comment.