+ api token + ref #43
11
.env.example
11
.env.example
|
|
@ -1,15 +1,18 @@
|
|||
# Template .env for API grabber
|
||||
|
||||
# PostgreSQL connection [DEPRECATED]
|
||||
# -------------------------------
|
||||
DB_HOST = "ip.addr.postgresql.server"
|
||||
DB_USER = "db_user"
|
||||
DB_PASSWORD = "db_user_password"
|
||||
DB_DBNAME = "db_name"1
|
||||
|
||||
# Prometheus-Exporter info
|
||||
# -------------------------------
|
||||
EXPORTER_URL = "http(s)://ip.ip.ip.ip:port"
|
||||
|
||||
# VINTEO Jitter puller (needed to init Jitter native grab)
|
||||
# -------------------------------
|
||||
VINTEO_URL_BASE = "http(s)://ip.ip.ip.ip:port"
|
||||
VINTEO_ENDPOINT_CONFERENCES = "/api/v1/to/something"
|
||||
VINTEO_ENDPOINT_PARTICIPANTS = "/api/v1/to/something"
|
||||
|
|
@ -20,11 +23,19 @@ VINTEO_API_KEY = "6fe8b0db-62b4-4065-9c1e-441ec4228341.9acec20bd17d7178f332896f8
|
|||
STATUS_SYSTEM_URL = "http://192.168.2.39:9999/api/input"
|
||||
|
||||
# eNODE.Monitoring configuration
|
||||
# -------------------------------
|
||||
# eNODE.Monitoring server IP
|
||||
ENODE_MONITORING_IP = "ip.ip.ip.ip"
|
||||
# eNODE.Monitoring credentials
|
||||
ENODE_MONITORING_LOGIN = "admin_user_enode_monitoring" # admin user is required
|
||||
ENODE_MONITORING_PASSWORD = "admin_password_enode_monitoring" # # admin password is required
|
||||
# List of target devices
|
||||
ENODE_TARGET_DEVICES = "18, 19"
|
||||
# to work with unlimit API-Token
|
||||
ENODE_API_TOKEN = "sssswwwwaaaaffff"
|
||||
|
||||
# OPTIONAL SETTINGS
|
||||
# -------------------------------
|
||||
# IM configuration for max level of logging info
|
||||
# for example DEBUG, INFO, WARN, ERROR, TRACE
|
||||
IM_LOG_INFO = "INFO"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::{collections::{HashMap, HashSet}, future::Future};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use integr_structs::api::v3::{PrometheusMetricsExtended, MetricOutputExtended};
|
||||
use std::sync::Arc;
|
||||
use reqwest::Client;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use chrono::Local;
|
||||
use anyhow::Result;
|
||||
use tracing::info;
|
||||
|
||||
|
|
|
|||
|
|
@ -183,6 +183,7 @@ pub struct MonitoringImporter {
|
|||
login : String,
|
||||
password : String,
|
||||
access_token : String,
|
||||
api_token : String,
|
||||
ts : String,
|
||||
timeout : usize,
|
||||
}
|
||||
|
|
@ -205,6 +206,7 @@ impl MonitoringImporter {
|
|||
login : env::var("ENODE_MONITORING_LOGIN").unwrap_or_else(|_| String::new()),
|
||||
password : env::var("ENODE_MONITORING_PASSWORD").unwrap_or_else(|_| String::new()),
|
||||
access_token : String::new(),
|
||||
api_token : env::var("ENODE_API_TOKEN").unwrap_or_else(|_| String::new()),
|
||||
ts : String::new(),
|
||||
timeout : std::env::var("IM_CONNECTION_TIMEOUT").unwrap_or_else(|_| "10".to_string()).parse().unwrap_or_else(|_| 10)
|
||||
}
|
||||
|
|
@ -213,7 +215,13 @@ impl MonitoringImporter {
|
|||
/// and can be used to pull and push info to and from CM
|
||||
///
|
||||
async fn is_valid(&self) -> bool {
|
||||
!self.ip.is_empty() && !self.login.is_empty() && !self.password.is_empty()
|
||||
!self.ip.is_empty() && ((!self.login.is_empty() && !self.password.is_empty() ) || !self.api_token.is_empty())
|
||||
}
|
||||
/// Function that checks is current `MonitoringImporter` valid
|
||||
/// and can be used to pull and push info to and from CM
|
||||
///
|
||||
async fn is_minimal(&self) -> bool {
|
||||
(self.login.is_empty() || self.password.is_empty()) && !self.api_token.is_empty()
|
||||
}
|
||||
/// A setter of `timestamp`
|
||||
///
|
||||
|
|
@ -235,38 +243,43 @@ impl MonitoringImporter {
|
|||
#[tracing::instrument(name = "cm_fn_session_start", skip_all)]
|
||||
pub async fn start_session(&mut self) -> anyhow::Result<()> {
|
||||
if !self.is_valid().await {
|
||||
if self.is_minimal().await {
|
||||
return Err(Error::msg(format!("Given API-Token is no more actual now ({})", &self.access_token)));
|
||||
}
|
||||
return Err(Error::msg("Invalid eNODE-Monitoring configuration"));
|
||||
}
|
||||
let client = Client::new();
|
||||
let url = format!("http://{}/e-data-front/auth/login", self.ip);
|
||||
let fortoken = ForTokenCredentials::new(&self.login, &self.password);
|
||||
let mut delay = 1;
|
||||
if !self.api_token.is_empty() {
|
||||
std::mem::swap(&mut self.access_token, &mut self.api_token);
|
||||
info!("API-Token that was in the ENODE configuration was set as access-token");
|
||||
} else {
|
||||
let client = Client::new();
|
||||
let url = format!("http://{}/e-data-front/auth/login", self.ip);
|
||||
let fortoken = ForTokenCredentials::new(&self.login, &self.password);
|
||||
let mut delay = 1;
|
||||
|
||||
loop {
|
||||
let client = client
|
||||
.post(&url)
|
||||
.timeout(tokio::time::Duration::from_secs(self.timeout as u64))
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&fortoken);
|
||||
// let resp = client.send().await?;
|
||||
if let Ok(resp) = client.send().await {
|
||||
// let auth = resp.json::<AuthResponse>().await?;
|
||||
|
||||
match resp.json::<AuthResponse>().await {
|
||||
Ok(auth) => {
|
||||
self.set_ts(&fortoken.ts).await;
|
||||
self.access_token = auth.access_token.to_owned();
|
||||
tracing::trace!("Access key was changed");
|
||||
break;
|
||||
},
|
||||
Err(er) => error!("Error with extracting access-key from CM response due to {}", er),
|
||||
loop {
|
||||
let client = client
|
||||
.post(&url)
|
||||
.timeout(tokio::time::Duration::from_secs(self.timeout as u64))
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&fortoken);
|
||||
if let Ok(resp) = client.send().await {
|
||||
match resp.json::<AuthResponse>().await {
|
||||
Ok(auth) => {
|
||||
self.set_ts(&fortoken.ts).await;
|
||||
self.access_token = auth.access_token.to_owned();
|
||||
tracing::trace!("Access key was changed");
|
||||
break;
|
||||
},
|
||||
Err(er) => error!("Error with extracting access-key from CM response due to {}", er),
|
||||
}
|
||||
}
|
||||
error!("Error while trying to create a new session, waiting {} secs and retrying ...", delay);
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
|
||||
delay = delay * 2;
|
||||
}
|
||||
error!("Error while trying to create a new session, waiting {} secs and retrying ...", delay);
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(delay)).await;
|
||||
delay = delay * 2;
|
||||
info!("Started a new CM session");
|
||||
}
|
||||
info!("Started a new CM session");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue