Skip to content

Commit

Permalink
feat: generate random JWT_SECRET when not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Alw3ys committed Apr 10, 2024
1 parent 4611218 commit 4a725c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 17 additions & 1 deletion doseid/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use clap::Parser;
use dosei_proto::ping::NodeType;
use dotenv::dotenv;
use home::home_dir;
use rand::distributions::Alphanumeric;
use rand::Rng;
use reqwest::header::CONTENT_TYPE;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand All @@ -14,6 +16,7 @@ use std::io::Read;
use std::io::Write;
use std::path::PathBuf;
use std::{env, fmt, fs, write};
use tracing::{info, warn};
use uuid::Uuid;

pub const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -116,7 +119,20 @@ impl Config {
},
primary_address: args.connect,
database_url: env::var("DATABASE_URL").context("DATABASE_URL is required.")?,
jwt_secret: env::var("JWT_SECRET").context("JWT_SECRET is required.")?,
jwt_secret: env::var("JWT_SECRET")
.unwrap_or_else(|_| {
let random_id: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(24)
.map(char::from)
.collect();
warn!(
"No JWT_SECRET provided - generated random secret. \
This may cause problems when Dosei operates on cluster mode. \
To provide a shared secret set the JWT_SECRET environment variable."
);
random_id
}),
container_registry_url: env::var("CONTAINER_REGISTRY_URL")
.context("CONTAINER_REGISTRY_URL is required.")?,
telemetry: Telemetry::new()
Expand Down
4 changes: 0 additions & 4 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ if [ -z "$CONTAINER_REGISTRY_URL" ]; then
export CONTAINER_REGISTRY_URL=ghcr.io
fi

if [ -z "$JWT_SECRET" ]; then
export JWT_SECRET=$(openssl rand -hex 8)
fi

/usr/local/bin/doseid --host 0.0.0.0

0 comments on commit 4a725c5

Please sign in to comment.