Skip to content

Commit 36fb0a3

Browse files
authored
fix(ci): use option_env for HMAC secret and add it to benchmark jobs (#182)
1 parent ad6a9bf commit 36fb0a3

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ jobs:
6565
with:
6666
type: micro
6767
ferrflow-token: ${{ secrets.GITHUB_TOKEN }}
68+
env:
69+
FERRFLOW_HMAC_SECRET: ${{ secrets.FERRFLOW_HMAC_SECRET }}
6870

6971
benchmark:
7072
name: Benchmark
@@ -82,6 +84,8 @@ jobs:
8284
with:
8385
type: full
8486
ferrflow-token: ${{ secrets.GITHUB_TOKEN }}
87+
env:
88+
FERRFLOW_HMAC_SECRET: ${{ secrets.FERRFLOW_HMAC_SECRET }}
8589

8690
release:
8791
name: Release

‎src/telemetry.rs‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ type HmacSha256 = Hmac<sha2::Sha256>;
77

88
const DEFAULT_API_URL: &str = "https://api.ferrflow.com";
99

10-
fn hmac_secret() -> &'static str {
11-
option_env!("FERRFLOW_HMAC_SECRET").unwrap_or(env!("FERRFLOW_HMAC_SECRET"))
10+
fn hmac_secret() -> Option<&'static str> {
11+
option_env!("FERRFLOW_HMAC_SECRET")
1212
}
1313

1414
#[derive(Debug, Clone, Copy, Serialize)]
@@ -107,19 +107,21 @@ pub fn send_event(
107107
.as_secs()
108108
.to_string();
109109

110-
let message = format!("{timestamp}.{body}");
111-
let mut mac = HmacSha256::new_from_slice(hmac_secret().as_bytes())
112-
.expect("HMAC accepts any key length");
113-
mac.update(message.as_bytes());
114-
let signature = hex::encode(mac.finalize().into_bytes());
115-
116110
let agent = ureq::Agent::new_with_defaults();
117-
let _ = agent
118-
.post(&url)
119-
.header("Content-Type", "application/json")
120-
.header("X-Timestamp", &timestamp)
121-
.header("X-Signature", &signature)
122-
.send(body.as_bytes());
111+
let mut req = agent.post(&url).header("Content-Type", "application/json");
112+
113+
if let Some(secret) = hmac_secret() {
114+
let message = format!("{timestamp}.{body}");
115+
let mut mac =
116+
HmacSha256::new_from_slice(secret.as_bytes()).expect("HMAC accepts any key length");
117+
mac.update(message.as_bytes());
118+
let signature = hex::encode(mac.finalize().into_bytes());
119+
req = req
120+
.header("X-Timestamp", &timestamp)
121+
.header("X-Signature", &signature);
122+
}
123+
124+
let _ = req.send(body.as_bytes());
123125
});
124126
}
125127

0 commit comments

Comments
 (0)