Skip to content

Commit ccf159c

Browse files
authored
Merge pull request #32 from VincentTam/docker-secrets
Use docker secrets to store Discord token
2 parents 4925943 + 6a9a0ac commit ccf159c

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/cache
44
# Used for docker containers and might contain secrets.
55
/.env
6+
/discord_token.txt
67
*.sqlite

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@ To run, CD into this directory, set `DISCORD_TOKEN` to your bot token, set `CACH
2626

2727
There is a `Dockerfile` and `docker-compose.yml` for running the bot inside a Docker container.
2828

29-
To set up the bot with Docker, create a `.env` file like the following:
30-
31-
```
32-
DISCORD_TOKEN=YourBotTokenHere
33-
```
34-
35-
and start the container with `docker compose up -d`.
29+
To set up the bot with Docker, create a `discord_token.txt` file containing your bot token and start the container with `docker compose up -d`.
3630

3731
### Public Instance
3832

crates/bot/src/bot.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,14 @@ async fn handle_error(
754754
}
755755

756756
pub async fn run() {
757+
let token = match (std::env::var_os("DISCORD_TOKEN"), std::env::var_os("DISCORD_TOKEN_FILE")) {
758+
(Some(token), None) => token.into_string().expect("`DISCORD_TOKEN` not UTF-8"),
759+
(None, Some(path)) => std::fs::read_to_string(path).expect("reading from `DISCORD_TOKEN_FILE`"),
760+
(Some(_token), Some(_path)) => panic!("both `DISCORD_TOKEN` and `DISCORD_TOKEN_FILE` provided.\nThis is ambiguous and insecure. Please only use one or the other."),
761+
(None, None) => panic!("need `DISCORD_TOKEN` or `DISCORD_TOKEN_FILE` env var"),
762+
};
763+
let token = token.trim();
764+
757765
let database = Connection::open_with_flags(
758766
std::env::var_os("DB_PATH").expect("need `DB_PATH` env var"),
759767
OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE,
@@ -766,7 +774,6 @@ pub async fn run() {
766774

767775
let edit_tracker_time = std::time::Duration::from_secs(3600);
768776

769-
let token = std::env::var("DISCORD_TOKEN").expect("need `DISCORD_TOKEN` env var");
770777
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
771778
let framework = poise::Framework::builder()
772779
.options(poise::FrameworkOptions {

docker-compose.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
---
2-
31
name: typst-bot
4-
version: "3.8"
52
services:
63
bot:
74
# This service is built using the Dockerfile in the current directory.
@@ -12,7 +9,9 @@ services:
129
# has when Compose is run. It is not saved in the image. Compose will automatically grab its
1310
# value from `.env` or the host OS. `?:error` makes it mandatory.
1411
environment:
15-
- DISCORD_TOKEN=${DISCORD_TOKEN?:error}
12+
DISCORD_TOKEN_FILE: /run/secrets/discord_token
13+
secrets:
14+
- discord_token
1615
# The `/bot/sqlite` and `/bot/cache` directories are mapped to volumes.
1716
volumes:
1817
- sqlite:/bot/sqlite
@@ -23,3 +22,7 @@ services:
2322
volumes:
2423
sqlite:
2524
cache:
25+
26+
secrets:
27+
discord_token:
28+
file: discord_token.txt

0 commit comments

Comments
 (0)