From 0a367bfbdafffde06743f7f649e71b841cbcd1c8 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Mon, 17 Jul 2023 00:50:46 -0400 Subject: [PATCH] Add common crate to access env variables In the future, we should use a proper secret store (not just env variables). This lets us update one block of code and not n in the future. --- .github/workflows/tests.yml | 3 ++- Cargo.lock | 6 ++++++ Cargo.toml | 1 + common/env/Cargo.toml | 13 +++++++++++++ common/env/LICENSE | 15 +++++++++++++++ common/env/src/lib.rs | 8 ++++++++ deny.toml | 2 ++ message-queue/Cargo.toml | 2 ++ message-queue/src/main.rs | 4 ++-- processor/Cargo.toml | 1 + processor/src/main.rs | 3 ++- 11 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 common/env/Cargo.toml create mode 100644 common/env/LICENSE create mode 100644 common/env/src/lib.rs diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 49e74194..c052bcaf 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -23,7 +23,8 @@ jobs: GITHUB_CI=true cargo test --all-features \ -p std-shims \ -p zalloc \ - -p serai-db + -p serai-db \ + -p serai-env test-crypto: runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index 3447c8ce..db5b15ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8653,6 +8653,10 @@ dependencies = [ "rocksdb", ] +[[package]] +name = "serai-env" +version = "0.1.0" + [[package]] name = "serai-in-instructions-pallet" version = "0.1.0" @@ -8699,6 +8703,7 @@ dependencies = [ "rocksdb", "schnorr-signatures", "serai-db", + "serai-env", "serai-primitives", "serde", "serde_json", @@ -8800,6 +8805,7 @@ dependencies = [ "secp256k1", "serai-client", "serai-db", + "serai-env", "serai-message-queue", "serai-processor-messages", "serde", diff --git a/Cargo.toml b/Cargo.toml index c2b1f884..1f7e1d5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "common/std-shims", "common/zalloc", "common/db", + "common/env", "crypto/transcript", diff --git a/common/env/Cargo.toml b/common/env/Cargo.toml new file mode 100644 index 00000000..7e40073b --- /dev/null +++ b/common/env/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "serai-env" +version = "0.1.0" +description = "A common library for Serai apps to access environment variables" +license = "AGPL-3.0-only" +repository = "https://github.com/serai-dex/serai/tree/develop/common/env" +authors = ["Luke Parker "] +keywords = [] +edition = "2021" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] diff --git a/common/env/LICENSE b/common/env/LICENSE new file mode 100644 index 00000000..f684d027 --- /dev/null +++ b/common/env/LICENSE @@ -0,0 +1,15 @@ +AGPL-3.0-only license + +Copyright (c) 2023 Luke Parker + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License Version 3 as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . diff --git a/common/env/src/lib.rs b/common/env/src/lib.rs new file mode 100644 index 00000000..7b5cd049 --- /dev/null +++ b/common/env/src/lib.rs @@ -0,0 +1,8 @@ +#![cfg_attr(docsrs, feature(doc_cfg))] +#![cfg_attr(docsrs, feature(doc_auto_cfg))] + +// Obtain a variable from the Serai environment/secret store. +pub fn var(variable: &str) -> Option { + // TODO: Move this to Kubernetes + std::env::var(variable).ok() +} diff --git a/deny.toml b/deny.toml index 015970b3..0361de9f 100644 --- a/deny.toml +++ b/deny.toml @@ -43,6 +43,8 @@ allow-osi-fsf-free = "neither" default = "deny" exceptions = [ + { allow = ["AGPL-3.0"], name = "serai-env" }, + { allow = ["AGPL-3.0"], name = "ethereum-serai" }, { allow = ["AGPL-3.0"], name = "serai-message-queue" }, diff --git a/message-queue/Cargo.toml b/message-queue/Cargo.toml index 9167b71c..3d6d36be 100644 --- a/message-queue/Cargo.toml +++ b/message-queue/Cargo.toml @@ -35,6 +35,8 @@ tokio = { version = "1", features = ["full"] } serai-db = { path = "../common/db", features = ["rocksdb"] } rocksdb = "0.21" +serai-env = { path = "../common/env" } + serai-primitives = { path = "../substrate/primitives" } jsonrpsee = { version = "0.16", features = ["server"] } diff --git a/message-queue/src/main.rs b/message-queue/src/main.rs index c2cff525..611f3c1b 100644 --- a/message-queue/src/main.rs +++ b/message-queue/src/main.rs @@ -100,10 +100,10 @@ fn ack_message(service: Service, id: u64, _signature: SchnorrSignature::G as GroupEncoding>::Repr::default(); repr.as_mut().copy_from_slice(&hex::decode(key).unwrap()); diff --git a/processor/Cargo.toml b/processor/Cargo.toml index 19c879f5..f1c26afc 100644 --- a/processor/Cargo.toml +++ b/processor/Cargo.toml @@ -55,6 +55,7 @@ log = "0.4" tokio = { version = "1", features = ["full"] } serai-db = { path = "../common/db", default-features = false } +serai-env = { path = "../common/env" } serai-client = { path = "../substrate/client", default-features = false } messages = { package = "serai-processor-messages", path = "./messages" } diff --git a/processor/src/main.rs b/processor/src/main.rs index f1412ce0..ead6c52b 100644 --- a/processor/src/main.rs +++ b/processor/src/main.rs @@ -1,5 +1,4 @@ use std::{ - env, time::Duration, collections::{VecDeque, HashMap}, }; @@ -25,6 +24,8 @@ use serai_client::{ use messages::{SubstrateContext, CoordinatorMessage, ProcessorMessage}; +use serai_env as env; + mod plan; pub use plan::*;