Skip to content

Commit 2f9f52d

Browse files
authored
Merge pull request #1991 from apiraino/rename-zulip-env-var
Rename ZULIP_TOKEN env var to ZULIP_WEBHOOK_SECRET
2 parents 34e9b54 + d74cb20 commit 2f9f52d

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ GITHUB_WEBHOOK_SECRET=MUST_BE_CONFIGURED
2020
# ZULIP_API_TOKEN=yyy
2121

2222
# Authenticates inbound webhooks from Github
23-
# ZULIP_TOKEN=xxx
23+
# ZULIP_WEBHOOK_SECRET=xxx
2424

2525
# Use another endpoint to retrieve teams of the Rust project (useful for local testing)
2626
# default: https://team-api.infra.rust-lang.org/v1

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ curl http://localhost:8000/zulip-hook \
107107
-H "Content-Type: application/json" \
108108
-d '{
109109
"data": "<CMD>",
110-
"token": "<ZULIP_TOKEN>",
110+
"token": "<ZULIP_WEBHOOK_SECRET>",
111111
"message": {
112112
"sender_id": <YOUR_ID>,
113113
"recipient_id": <YOUR_ID>,
@@ -121,7 +121,7 @@ curl http://localhost:8000/zulip-hook \
121121
Where:
122122
- `CMD` is the exact command you would issue @triagebot on Zulip (ex. open a direct chat with the
123123
bot and send "work show")
124-
- `ZULIP_TOKEN`: can be anything. Must correspond to the env var `$ZULIP_TOKEN` on your workstation
124+
- `ZULIP_WEBHOOK_SECRET`: can be anything. Must correspond to the env var `$ZULIP_WEBHOOK_SECRET` on your workstation
125125
- `YOUR_ID`: your GitHub user ID. Must be existing in your local triagebot database (table `users` and as
126126
foreign key also in `review_prefs`)
127127

src/zulip.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,29 @@ pub async fn respond(ctx: &Context, req: Request) -> String {
127127
serde_json::to_string(&Response { content }).unwrap()
128128
}
129129

130+
pub fn get_token_from_env() -> Result<String, anyhow::Error> {
131+
// ZULIP_WEBHOOK_SECRET is preferred, ZULIP_TOKEN is kept for retrocompatibility but will be deprecated
132+
match std::env::var("ZULIP_WEBHOOK_SECRET") {
133+
Ok(v) => return Ok(v),
134+
Err(_) => (),
135+
}
136+
137+
match std::env::var("ZULIP_TOKEN") {
138+
Ok(v) => return Ok(v),
139+
Err(_) => (),
140+
}
141+
142+
log::error!(
143+
"Cannot communicate with Zulip: neither ZULIP_WEBHOOK_SECRET or ZULIP_TOKEN are set."
144+
);
145+
anyhow::bail!("Cannot communicate with Zulip.");
146+
}
147+
130148
/// Processes a Zulip webhook.
131149
///
132150
/// Returns a string of the response, or None if no response is needed.
133151
async fn process_zulip_request(ctx: &Context, req: Request) -> anyhow::Result<Option<String>> {
134-
let expected_token = std::env::var("ZULIP_TOKEN").expect("`ZULIP_TOKEN` set for authorization");
135-
152+
let expected_token = get_token_from_env()?;
136153
if !bool::from(req.token.as_bytes().ct_eq(expected_token.as_bytes())) {
137154
anyhow::bail!("Invalid authorization.");
138155
}

0 commit comments

Comments
 (0)