From b1b41151c676483a5dd6f2c898333085d9ba0140 Mon Sep 17 00:00:00 2001
From: Dan Notestein <dan@syncad.com>
Date: Fri, 12 Apr 2024 20:58:07 +0000
Subject: [PATCH] Add a X-Jussi-Param-Hash header for analysis tools to use

---
 Cargo.lock  | 20 ++++++++++++++++++++
 Cargo.toml  |  1 +
 Dockerfile  | 21 +++++++++++++++++++++
 src/main.rs |  4 ++++
 4 files changed, 46 insertions(+)

diff --git a/Cargo.lock b/Cargo.lock
index aa53c8d..f7509ed 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -694,6 +694,7 @@ dependencies = [
  "serde",
  "serde_json",
  "serde_yaml",
+ "sha256",
  "tokio",
 ]
 
@@ -968,6 +969,12 @@ version = "0.3.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd"
 
+[[package]]
+name = "hex"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
+
 [[package]]
 name = "http"
 version = "0.2.11"
@@ -1867,6 +1874,19 @@ dependencies = [
  "digest",
 ]
 
+[[package]]
+name = "sha256"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0"
+dependencies = [
+ "async-trait",
+ "bytes",
+ "hex",
+ "sha2",
+ "tokio",
+]
+
 [[package]]
 name = "signal-hook-registry"
 version = "1.4.1"
diff --git a/Cargo.toml b/Cargo.toml
index 2531ffb..bfd614f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,3 +24,4 @@ tokio = { version = "1.36.0", features = ["sync"] }
 actix-request-identifier = "4.1.0"
 log = { version = "0.4.21", features = ["kv"] }
 env_logger = { version = "0.11.3", features = ["unstable-kv"] }
+sha256 = "1.5.0"
diff --git a/Dockerfile b/Dockerfile
index 61f7792..42c9f7f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,3 +13,24 @@ COPY --from=builder /drone/target/release/drone /drone
 EXPOSE 3000
 
 CMD ["./drone"]
+
+ARG BUILD_TIME
+ARG GIT_COMMIT_SHA
+ARG GIT_CURRENT_BRANCH
+ARG GIT_LAST_LOG_MESSAGE
+ARG GIT_LAST_COMMITTER
+ARG GIT_LAST_COMMIT_DATE
+LABEL org.opencontainers.image.created="$BUILD_TIME"
+LABEL org.opencontainers.image.url="https://hive.io/"
+LABEL org.opencontainers.image.documentation="https://gitlab.syncad.com/hive/drone"
+LABEL org.opencontainers.image.source="https://gitlab.syncad.com/hive/drone"
+#LABEL org.opencontainers.image.version="${VERSION}"
+LABEL org.opencontainers.image.revision="$GIT_COMMIT_SHA"
+LABEL org.opencontainers.image.licenses="MIT"
+LABEL org.opencontainers.image.ref.name="Drone API cache for Hive"
+LABEL org.opencontainers.image.title="Drone JSON-RPC API caching reverse-proxy for Hive and HAF"
+LABEL org.opencontainers.image.description="Blockchain-aware caching reverse proxy for the HIVE/HAF JSON-RPC calls, fills a similar role to jussi"
+LABEL io.hive.image.branch="$GIT_CURRENT_BRANCH"
+LABEL io.hive.image.commit.log_message="$GIT_LAST_LOG_MESSAGE"
+LABEL io.hive.image.commit.author="$GIT_LAST_COMMITTER"
+LABEL io.hive.image.commit.date="$GIT_LAST_COMMIT_DATE"
diff --git a/src/main.rs b/src/main.rs
index 6d1b74b..c63aa3a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,6 +10,7 @@ use std::time::{Duration, Instant, SystemTime};
 use tokio::sync::RwLock;
 use chrono::DateTime;
 use actix_request_identifier::{RequestId, RequestIdentifier, IdReuse};
+use sha256::digest;
 
 use log::{error, warn, info, debug, trace, log_enabled, Level::Info};
 
@@ -136,6 +137,9 @@ impl ResponseTrackingInfo {
         // removed params because it can be huge for posts, and can easily overflow nginx 
         // proxy buffer
         // reply_builder.insert_header(("X-Jussi-Params", self.mapped_method.params.map_or("[]".to_string(), |v| v.to_string())));
+        // instead print the hash of the parameters, we can use that to tell which calls are
+        // identical even if we don't know exactly what the parameters were
+        reply_builder.insert_header(("X-Jussi-Param-Hash", digest(self.mapped_method.params.map_or("[]".to_string(), |v| v.to_string()))));
         if self.backend_url.is_some() {
             reply_builder.insert_header(("X-Jussi-Backend-Url", self.backend_url.unwrap()));
         }
-- 
GitLab