Skip to content

Commit 5ca86fc

Browse files
committed
refactor: update encryption key handling and configuration in backend
1 parent f562045 commit 5ca86fc

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

apps/backend/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
DATABASE_URL=postgres://user:password@localhost:5432/sourcemaps
2-
ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
2+
FILE_ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
3+
APIKEY_ENCRYPTION_KEY=1111111111111111111111111111111111111111111111111111111111111111
34
S3_BUCKET=
45
S3_REGION=
56
S3_ENDPOINT=

apps/backend/src/auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl FromRequestParts<SharedState> for AuthenticatedProject {
4747
let project_id: Uuid = row.get("project_id");
4848
let encrypted_key: String = row.get("encrypted_key");
4949

50-
if !verify_api_key(&state.crypto, &encrypted_key, token)? {
50+
if !verify_api_key(&state.apikey_crypto, &encrypted_key, token)? {
5151
return Err(AppError::Unauthorized);
5252
}
5353

apps/backend/src/config.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::net::SocketAddr;
33
#[derive(Clone)]
44
pub struct Config {
55
pub database_url: String,
6-
pub encryption_key: String,
6+
pub file_encryption_key: String,
7+
pub apikey_encryption_key: String,
78
pub s3_bucket: String,
89
pub s3_region: String,
910
pub s3_endpoint: String,
@@ -29,7 +30,9 @@ impl Config {
2930

3031
Ok(Self {
3132
database_url: std::env::var("DATABASE_URL")?,
32-
encryption_key: std::env::var("ENCRYPTION_KEY")
33+
file_encryption_key: std::env::var("FILE_ENCRYPTION_KEY")
34+
.or_else(|_| std::env::var("ENCRYPTION_KEY"))?,
35+
apikey_encryption_key: std::env::var("APIKEY_ENCRYPTION_KEY")
3336
.or_else(|_| std::env::var("SOURCEMAP_API_KEY_SECRET"))?,
3437
s3_bucket: std::env::var("S3_BUCKET")?,
3538
s3_region: std::env::var("S3_REGION").unwrap_or_else(|_| "us-east-1".into()),

apps/backend/src/main.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use storage::Storage;
1717
pub struct AppState {
1818
pub db: sqlx::PgPool,
1919
pub storage: Storage,
20-
pub crypto: Arc<Crypto>,
20+
pub apikey_crypto: Arc<Crypto>,
2121
pub admin_token: Arc<str>,
2222
}
2323

@@ -61,19 +61,27 @@ async fn main() {
6161
}
6262

6363
async fn build_state(config: &Config) -> AppState {
64-
let crypto = Arc::new(Crypto::new(&config.encryption_key).expect("invalid ENCRYPTION_KEY"));
64+
if config.file_encryption_key == config.apikey_encryption_key {
65+
panic!("FILE_ENCRYPTION_KEY and APIKEY_ENCRYPTION_KEY must be different");
66+
}
67+
68+
let file_crypto =
69+
Arc::new(Crypto::new(&config.file_encryption_key).expect("invalid FILE_ENCRYPTION_KEY"));
70+
let apikey_crypto = Arc::new(
71+
Crypto::new(&config.apikey_encryption_key).expect("invalid APIKEY_ENCRYPTION_KEY"),
72+
);
6573
let db = PgPoolOptions::new()
6674
.max_connections(10)
6775
.connect(&config.database_url)
6876
.await
6977
.expect("failed to connect to database");
7078
let s3_client = s3_client(config);
71-
let storage = Storage::new(s3_client, config.s3_bucket.clone(), crypto.clone());
79+
let storage = Storage::new(s3_client, config.s3_bucket.clone(), file_crypto.clone());
7280

7381
AppState {
7482
db,
7583
storage,
76-
crypto,
84+
apikey_crypto,
7785
admin_token: Arc::<str>::from(config.admin_token.clone()),
7886
}
7987
}

0 commit comments

Comments
 (0)