From 17560e8940076a82ace702ef388e288436e95d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Tr=C4=85d?= Date: Wed, 27 Dec 2023 18:25:51 +0100 Subject: [PATCH] Further dockerization support (#12) * Predefined api key for dockerization * Fix Dockerfile --- Dockerfile | 3 --- config.toml | 1 + src/api_key.rs | 11 ++++++++++- src/config.rs | 3 +++ src/db.rs | 2 +- src/server/routes/relayer.rs | 4 ++-- src/service.rs | 9 +++++++++ tests/common/service_builder.rs | 3 +++ 8 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index ae9dd47..b1b104f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,17 +24,14 @@ RUN rustup component add cargo # TODO: Hacky but it works RUN mkdir -p ./src RUN mkdir -p ./crates/postgres-docker-utils/src -RUN mkdir -p ./crates/fake-rpc/src # Copy only Cargo.toml for better caching COPY ./Cargo.toml ./Cargo.toml COPY ./Cargo.lock ./Cargo.lock COPY ./crates/postgres-docker-utils/Cargo.toml ./crates/postgres-docker-utils/Cargo.toml -COPY ./crates/fake-rpc/Cargo.toml ./crates/fake-rpc/Cargo.toml RUN echo "fn main() {}" > ./src/main.rs RUN echo "fn main() {}" > ./crates/postgres-docker-utils/src/main.rs -RUN echo "fn main() {}" > ./crates/fake-rpc/src/main.rs # Prebuild dependencies RUN cargo fetch diff --git a/config.toml b/config.toml index cc2e14e..3977e73 100644 --- a/config.toml +++ b/config.toml @@ -12,6 +12,7 @@ ws_url = "ws://127.0.0.1:8545" id = "1b908a34-5dc1-4d2d-a146-5eb46e975830" chain_id = 31337 key_id = "d10607662a85424f02a33fb1e6d095bd0ac7154396ff09762e41f82ff2233aaa" +api_key = "G5CKNF3BTS2hRl60bpdYMNPqXvXsP-QZd2lrtmgctsnllwU9D3Z4D8gOt04M0QNH" [server] host = "127.0.0.1:3000" diff --git a/src/api_key.rs b/src/api_key.rs index f180a83..a1dca7a 100644 --- a/src/api_key.rs +++ b/src/api_key.rs @@ -13,7 +13,16 @@ pub struct ApiKey { } impl ApiKey { - pub fn new(relayer_id: impl ToString) -> Self { + pub fn new(relayer_id: impl ToString, key: [u8; 32]) -> Self { + let relayer_id = relayer_id.to_string(); + + Self { + relayer_id, + api_key: key, + } + } + + pub fn random(relayer_id: impl ToString) -> Self { let relayer_id = relayer_id.to_string(); let mut api_key = [0u8; 32]; diff --git a/src/config.rs b/src/config.rs index 2869c1e..6d2ed1c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,8 @@ use std::time::Duration; use serde::{Deserialize, Serialize}; +use crate::api_key::ApiKey; + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub struct Config { @@ -65,6 +67,7 @@ pub struct PredefinedRelayer { pub name: String, pub key_id: String, pub chain_id: u64, + pub api_key: ApiKey, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/db.rs b/src/db.rs index 40457f1..0504c67 100644 --- a/src/db.rs +++ b/src/db.rs @@ -935,7 +935,7 @@ impl Database { Ok(items.into_iter().map(|(x,)| x as u64).collect()) } - pub async fn save_api_key( + pub async fn create_api_key( &self, relayer_id: &str, api_key_hash: [u8; 32], diff --git a/src/server/routes/relayer.rs b/src/server/routes/relayer.rs index d262cdc..88d923e 100644 --- a/src/server/routes/relayer.rs +++ b/src/server/routes/relayer.rs @@ -158,10 +158,10 @@ pub async fn create_relayer_api_key( State(app): State>, Path(relayer_id): Path, ) -> Result, ApiError> { - let api_key = ApiKey::new(&relayer_id); + let api_key = ApiKey::random(&relayer_id); app.db - .save_api_key(&relayer_id, api_key.api_key_hash()) + .create_api_key(&relayer_id, api_key.api_key_hash()) .await?; Ok(Json(CreateApiKeyResponse { api_key })) diff --git a/src/service.rs b/src/service.rs index 37a71c1..f08b663 100644 --- a/src/service.rs +++ b/src/service.rs @@ -94,6 +94,8 @@ async fn initialize_predefined_values( return Ok(()); }; + tracing::warn!("Running with predefined values is not recommended in a production environment"); + app.db .create_network( predefined.network.chain_id, @@ -121,5 +123,12 @@ async fn initialize_predefined_values( ) .await?; + app.db + .create_api_key( + &predefined.relayer.api_key.relayer_id, + predefined.relayer.api_key.api_key_hash(), + ) + .await?; + Ok(()) } diff --git a/tests/common/service_builder.rs b/tests/common/service_builder.rs index 492e538..b76e92e 100644 --- a/tests/common/service_builder.rs +++ b/tests/common/service_builder.rs @@ -2,6 +2,7 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::time::Duration; use ethers::utils::AnvilInstance; +use tx_sitter::api_key::ApiKey; use tx_sitter::client::TxSitterClient; use tx_sitter::config::{ Config, DatabaseConfig, KeysConfig, LocalKeysConfig, Predefined, @@ -71,6 +72,8 @@ impl ServiceBuilder { id: DEFAULT_RELAYER_ID.to_string(), key_id: anvil_private_key, chain_id: DEFAULT_ANVIL_CHAIN_ID, + // TODO: Use this key in tests + api_key: ApiKey::random(DEFAULT_RELAYER_ID), }, }), },