cfg: add read/write opts

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-05-28 12:26:40 +02:00
parent 104de8e87e
commit bab21a9bae
2 changed files with 58 additions and 6 deletions

View file

@ -1,11 +1,14 @@
clients:
# Exclusive writers
- name: pyranometer
metrics:
write_metrics:
- irradiance
- temperature
auth:
type: sha256
hash: ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb # a
# Reader and writer on the same metric
- name: tgbt
metrics:
- testproxy.*
@ -13,6 +16,29 @@ clients:
type: sha256
hash: ac790471b321143716e7773d589af923236ebdd435ba17c671df3558becc5154 # 7a5becc5b5bb581522fd0bb8891bb99a70275620
# Reader and writer on different metrics
- name: tgbt
write_metrics:
- barfoo
read_metrics:
- foobar.*
auth:
# ...
# Reader only
- name: consumer
read_metrics:
- irradiance
auth:
# ...
# Anonymous login
- name: public_consumer
read_metrics:
- weather.*
auth:
type: anonymous
config:
opentsdb:

View file

@ -15,8 +15,13 @@ pub struct Config {
#[derive(Debug, Deserialize, Clone)]
pub struct Client {
pub name: String,
#[serde(default)]
pub metrics: Vec<String>,
pub auth: Auth,
#[serde(default)]
pub read_metrics: Vec<String>,
#[serde(default)]
pub write_metrics: Vec<String>,
pub auth: Option<Auth>,
}
impl Client {
@ -26,6 +31,24 @@ impl Client {
return true;
}
}
for m in &self.write_metrics {
if glob_match(m, metric) {
return true;
}
}
false
}
pub fn can_read(&self, metric: &str) -> bool {
for m in &self.metrics {
if glob_match(m, metric) {
return true;
}
}
for m in &self.read_metrics {
if glob_match(m, metric) {
return true;
}
}
false
}
}
@ -34,7 +57,7 @@ impl Client {
pub struct Auth {
#[serde(rename = "type")]
pub auth_type: String,
pub hash: String,
pub hash: Option<String>,
}
impl Auth {
@ -44,7 +67,10 @@ impl Auth {
let mut hasher = Sha256::new();
hasher.update(token);
let result = hasher.finalize();
format!("{:x}", result) == self.hash
if let Some(hash) = &self.hash {
return format!("{:x}", result) == *hash;
}
false
}
_ => false,
}
@ -81,7 +107,7 @@ fn default_opentsdb_url() -> String {
pub fn load_config_file(filename: &str) -> Config {
let yaml_content = fs::read_to_string(filename)
.unwrap_or_else(|_| panic!("Unable to read config file {}", filename));
.unwrap_or_else(|_| panic!("Unable to read config file `{}`", filename));
let config: Config = serde_yaml::from_str(&yaml_content).expect("Unable to parse YAML");
config
}
@ -89,5 +115,5 @@ pub fn load_config_file(filename: &str) -> Config {
pub fn try_authenticate_client<'a>(clients: &'a [Client], token: &str) -> Option<&'a Client> {
clients
.iter()
.find(|client| client.auth.is_valid_token(token))
.find(|client| client.auth.is_some() && client.auth.as_ref().unwrap().is_valid_token(token))
}