From e4df6416f5c468b2eb483dde0920e54dc4259114 Mon Sep 17 00:00:00 2001
From: dynco-nym <173912580+dynco-nym@users.noreply.github.com>
Date: Tue, 19 Nov 2024 23:41:13 +0100
Subject: [PATCH] /request better authentication - moved agent API calls to
Client struct
---
Cargo.lock | 4 +
common/models/src/ns_api.rs | 17 +-
nym-node-status-agent/Cargo.toml | 3 +
nym-node-status-agent/run.sh | 6 +-
nym-node-status-agent/src/cli/run_probe.rs | 171 ++++++++++--------
nym-node-status-api/Cargo.toml | 1 +
nym-node-status-api/launch_node_status_api.sh | 4 +-
.../src/db/queries/testruns.rs | 6 +-
nym-node-status-api/src/http/api/testruns.rs | 60 +++---
9 files changed, 165 insertions(+), 107 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index bbd6905ab5..c9c60e4298 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6053,12 +6053,15 @@ name = "nym-node-status-agent"
version = "0.1.6"
dependencies = [
"anyhow",
+ "bincode",
+ "chrono",
"clap 4.5.20",
"nym-bin-common",
"nym-common-models",
"nym-crypto",
"rand",
"reqwest 0.12.4",
+ "serde",
"serde_json",
"tempfile",
"tokio",
@@ -6073,6 +6076,7 @@ version = "0.1.6"
dependencies = [
"anyhow",
"axum 0.7.7",
+ "bincode",
"chrono",
"clap 4.5.20",
"cosmwasm-std",
diff --git a/common/models/src/ns_api.rs b/common/models/src/ns_api.rs
index 306fd2ab93..f0c37c91af 100644
--- a/common/models/src/ns_api.rs
+++ b/common/models/src/ns_api.rs
@@ -1,6 +1,21 @@
-use nym_crypto::asymmetric::ed25519::Signature;
+use nym_crypto::asymmetric::ed25519::{PublicKey, Signature};
use serde::{Deserialize, Serialize};
+pub mod get_testrun {
+ use super::*;
+ #[derive(Debug, Clone, Deserialize, Serialize)]
+ pub struct Payload {
+ pub agent_public_key: PublicKey,
+ pub timestamp: i64,
+ }
+
+ #[derive(Debug, Clone, Deserialize, Serialize)]
+ pub struct GetTestrunRequest {
+ pub payload: Payload,
+ pub signature: Signature,
+ }
+}
+
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TestrunAssignment {
pub testrun_id: i64,
diff --git a/nym-node-status-agent/Cargo.toml b/nym-node-status-agent/Cargo.toml
index 9a61cd5084..8bf6148cd2 100644
--- a/nym-node-status-agent/Cargo.toml
+++ b/nym-node-status-agent/Cargo.toml
@@ -16,6 +16,8 @@ readme.workspace = true
[dependencies]
anyhow = { workspace = true}
+bincode = { workspace = true }
+chrono = { workspace = true }
clap = { workspace = true, features = ["derive", "env"] }
nym-bin-common = { path = "../common/bin-common", features = ["models"]}
nym-common-models = { path = "../common/models" }
@@ -26,6 +28,7 @@ tokio-util = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
reqwest = { workspace = true, features = ["json"] }
+serde = { workspace = true }
serde_json = { workspace = true }
[dev-dependencies]
diff --git a/nym-node-status-agent/run.sh b/nym-node-status-agent/run.sh
index af1fa2353e..a0cf362e16 100755
--- a/nym-node-status-agent/run.sh
+++ b/nym-node-status-agent/run.sh
@@ -1,9 +1,9 @@
#!/bin/bash
set -eu
-environment="qa"
+export ENVIRONMENT=${ENVIRONMENT:-"sandbox"}
-probe_git_ref="0dd5dacdda92b1ddd51cd30a3399515e45613371"
+probe_git_ref="nym-vpn-core-v1.0.0-rc.6"
crate_root=$(dirname $(realpath "$0"))
monorepo_root=$(dirname "${crate_root}")
@@ -13,7 +13,7 @@ echo "gateway_probe_src=$gateway_probe_src"
echo "crate_root=$crate_root"
set -a
-source "${monorepo_root}/envs/${environment}.env"
+source "${monorepo_root}/envs/${ENVIRONMENT}.env"
set +a
export RUST_LOG="debug"
diff --git a/nym-node-status-agent/src/cli/run_probe.rs b/nym-node-status-agent/src/cli/run_probe.rs
index aee1116f4a..17c7d2a36a 100644
--- a/nym-node-status-agent/src/cli/run_probe.rs
+++ b/nym-node-status-agent/src/cli/run_probe.rs
@@ -1,33 +1,34 @@
use anyhow::{bail, Context};
-use nym_common_models::ns_api::{SubmitResults, TestrunAssignment};
-use nym_crypto::asymmetric::ed25519::{PrivateKey, PublicKey};
+use nym_common_models::ns_api::{get_testrun, SubmitResults, TestrunAssignment};
+use nym_crypto::asymmetric::ed25519::{PrivateKey, Signature};
+use std::fmt::Display;
use tracing::instrument;
use crate::cli::GwProbe;
-const URL_BASE: &str = "internal/testruns";
+const INTERNAL_TESTRUNS: &str = "internal/testruns";
pub(crate) async fn run_probe(
- server_address: &str,
+ server_ip: &str,
server_port: u16,
ns_api_auth_key: &str,
probe_path: &str,
) -> anyhow::Result<()> {
- let server_address = format!("{}:{}", server_address, server_port);
- test_ns_api_conn(&server_address).await?;
-
let auth_key = PrivateKey::from_base58_string(ns_api_auth_key)
.context("Couldn't parse auth key, exiting")?;
+ let ns_api_client = Client::new(server_ip, server_port, auth_key);
let probe = GwProbe::new(probe_path.to_string());
let version = probe.version().await;
tracing::info!("Probe version:\n{}", version);
- if let Some(testrun) = request_testrun(auth_key.public_key(), &server_address).await? {
+ if let Some(testrun) = ns_api_client.request_testrun().await? {
let log = probe.run_and_get_log(&Some(testrun.gateway_identity_key));
- submit_results(auth_key, &server_address, testrun.testrun_id, log).await?;
+ ns_api_client
+ .submit_results(testrun.testrun_id, log)
+ .await?;
} else {
tracing::info!("No testruns available, exiting")
}
@@ -35,84 +36,102 @@ pub(crate) async fn run_probe(
Ok(())
}
-async fn test_ns_api_conn(server_addr: &str) -> anyhow::Result<()> {
- reqwest::get(server_addr)
- .await
- .map(|res| {
- tracing::info!(
- "Testing connection to NS API at {server_addr}: {}",
- res.status()
- );
- })
- .map_err(|err| anyhow::anyhow!("Couldn't connect to server on {}: {}", server_addr, err))
+struct Client {
+ server_address: String,
+ client: reqwest::Client,
+ auth_key: PrivateKey,
}
-#[instrument(level = "debug", skip_all)]
-pub(crate) async fn request_testrun(
- auth_key: PublicKey,
- server_addr: &str,
-) -> anyhow::Result