Skip to content

Commit

Permalink
Add config setting and hash check
Browse files Browse the repository at this point in the history
  • Loading branch information
bsmartradio committed Jan 31, 2025
1 parent 8d0ee72 commit 8127698
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion alertingest/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

import abc
import gzip
import hashlib
import logging
from typing import Any, Set


def calculate_sha256(content):
sha256_hash = hashlib.sha256()
sha256_hash.update(content)
return sha256_hash.hexdigest()


import boto3
from botocore.config import Config


class AlertDatabaseBackend(abc.ABC):
Expand Down Expand Up @@ -92,7 +101,14 @@ def __init__(
self.known_schemas: Set[int] = set()

def create_client(self, endpoint_url: str | None = None):
s3_client = boto3.client("s3", endpoint_url=endpoint_url)
s3_client = boto3.client(
"s3",
endpoint_url=endpoint_url,
config=Config(
request_checksum_calculation="when_required",
response_checksum_validation="when_required",
),
)
return s3_client

def store_alert(
Expand Down Expand Up @@ -120,6 +136,11 @@ def store_alert(

def store_schema(self, schema_id: int, encoded_schema: bytes):
schema_key = f"/alert_archive/v1/schemas/{schema_id}.json"

data = encoded_schema # this is the data you're trying to upload
calculated_hash = calculate_sha256(data)
print(f"Calculated SHA-256 hash: {calculated_hash}")

try:
self.object_store_client.put_object(
Bucket=self.schema_bucket, Key=schema_key, Body=encoded_schema
Expand Down

0 comments on commit 8127698

Please sign in to comment.