Skip to main content

gl_client/
utils.rs

1use crate::tls::TlsConfig;
2use anyhow::{anyhow, Result};
3
4pub fn scheduler_uri() -> String {
5    std::env::var("GL_SCHEDULER_GRPC_URI")
6        .unwrap_or_else(|_| "https://scheduler.gl.blckstrm.com".to_string())
7}
8
9pub fn get_node_id_from_tls_config(tls_config: &TlsConfig) -> Result<Vec<u8>> {
10    let subject_common_name = match &tls_config.x509_cert {
11        Some(x) => match x.subject_common_name() {
12            Some(cn) => cn,
13            None => {
14                return Err(anyhow!(
15                    "Failed to parse the subject common name in the provided x509 certificate"
16                ))
17            }
18        },
19        None => {
20            return Err(anyhow!(
21                "The certificate could not be parsed in the x509 format"
22            ))
23        }
24    };
25
26    let split_subject_common_name = subject_common_name.split("/").collect::<Vec<&str>>();
27
28    // Must have at least 3 components: "" / "users" / "node_id"
29    if split_subject_common_name.len() < 3 {
30        return Err(anyhow!(
31            "Could not parse subject common name: {}",
32            subject_common_name
33        ));
34    } else if split_subject_common_name[1] != "users" {
35        return Err(anyhow!("Not a users certificate: {}", subject_common_name));
36    }
37
38    hex::decode(split_subject_common_name[2]).map_err(|e| {
39        anyhow!(
40            "Unable to decode node_id ({}): {}",
41            split_subject_common_name[2],
42            e
43        )
44    })
45}