Can specify environment variables in client configuration
This commit is contained in:
		
							
								
								
									
										24
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										24
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							@@ -421,6 +421,7 @@ dependencies = [
 | 
				
			|||||||
 "futures-util",
 | 
					 "futures-util",
 | 
				
			||||||
 "include_dir",
 | 
					 "include_dir",
 | 
				
			||||||
 "jwt-simple",
 | 
					 "jwt-simple",
 | 
				
			||||||
 | 
					 "lazy-regex",
 | 
				
			||||||
 "log",
 | 
					 "log",
 | 
				
			||||||
 "mime_guess",
 | 
					 "mime_guess",
 | 
				
			||||||
 "rand",
 | 
					 "rand",
 | 
				
			||||||
@@ -1211,6 +1212,29 @@ version = "0.3.2"
 | 
				
			|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
 | 
					checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[package]]
 | 
				
			||||||
 | 
					name = "lazy-regex"
 | 
				
			||||||
 | 
					version = "2.3.0"
 | 
				
			||||||
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
 | 
					checksum = "b6b12f2eb6ed7d39405c5eb25a034b4c106a9ad87a6d9be3298de6c5f32fd57d"
 | 
				
			||||||
 | 
					dependencies = [
 | 
				
			||||||
 | 
					 "lazy-regex-proc_macros",
 | 
				
			||||||
 | 
					 "once_cell",
 | 
				
			||||||
 | 
					 "regex",
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[package]]
 | 
				
			||||||
 | 
					name = "lazy-regex-proc_macros"
 | 
				
			||||||
 | 
					version = "2.3.0"
 | 
				
			||||||
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
 | 
					checksum = "f2496e5264069bc726ccf37eb76b9cd89406ae110d836c3f76729f99c8a23293"
 | 
				
			||||||
 | 
					dependencies = [
 | 
				
			||||||
 | 
					 "proc-macro2",
 | 
				
			||||||
 | 
					 "quote",
 | 
				
			||||||
 | 
					 "regex",
 | 
				
			||||||
 | 
					 "syn",
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "lazy_static"
 | 
					name = "lazy_static"
 | 
				
			||||||
version = "1.4.0"
 | 
					version = "1.4.0"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,4 +26,5 @@ rand = "0.8.5"
 | 
				
			|||||||
base64 = "0.13.0"
 | 
					base64 = "0.13.0"
 | 
				
			||||||
jwt-simple = "0.10.9"
 | 
					jwt-simple = "0.10.9"
 | 
				
			||||||
digest = "0.10.3"
 | 
					digest = "0.10.3"
 | 
				
			||||||
sha2 = "0.10.2"
 | 
					sha2 = "0.10.2"
 | 
				
			||||||
 | 
					lazy-regex = "2.3.0"
 | 
				
			||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
use crate::data::entity_manager::EntityManager;
 | 
					use crate::data::entity_manager::EntityManager;
 | 
				
			||||||
 | 
					use crate::utils::string_utils::apply_env_vars;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
 | 
					#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
 | 
				
			||||||
pub struct ClientID(pub String);
 | 
					pub struct ClientID(pub String);
 | 
				
			||||||
@@ -32,4 +33,14 @@ impl EntityManager<Client> {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        None
 | 
					        None
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn apply_environment_variables(&mut self) {
 | 
				
			||||||
 | 
					        for c in self.iter_mut() {
 | 
				
			||||||
 | 
					            c.id = ClientID(apply_env_vars(&c.id.0));
 | 
				
			||||||
 | 
					            c.name = apply_env_vars(&c.name);
 | 
				
			||||||
 | 
					            c.description = apply_env_vars(&c.description);
 | 
				
			||||||
 | 
					            c.secret = apply_env_vars(&c.secret);
 | 
				
			||||||
 | 
					            c.redirect_uri = apply_env_vars(&c.redirect_uri);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,5 +1,5 @@
 | 
				
			|||||||
use std::path::{Path, PathBuf};
 | 
					use std::path::{Path, PathBuf};
 | 
				
			||||||
use std::slice::Iter;
 | 
					use std::slice::{Iter, IterMut};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::utils::err::Res;
 | 
					use crate::utils::err::Res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -87,6 +87,11 @@ impl<E> EntityManager<E>
 | 
				
			|||||||
        self.list.iter()
 | 
					        self.list.iter()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Iterate over the entries of this entity manager
 | 
				
			||||||
 | 
					    pub fn iter_mut(&mut self) -> IterMut<'_, E> {
 | 
				
			||||||
 | 
					        self.list.iter_mut()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Get a cloned list of entries
 | 
					    /// Get a cloned list of entries
 | 
				
			||||||
    pub fn cloned(&self) -> Vec<E> {
 | 
					    pub fn cloned(&self) -> Vec<E> {
 | 
				
			||||||
        self.list.clone()
 | 
					        self.list.clone()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -74,8 +74,9 @@ async fn main() -> std::io::Result<()> {
 | 
				
			|||||||
    let listen_address = config.listen_address.to_string();
 | 
					    let listen_address = config.listen_address.to_string();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    HttpServer::new(move || {
 | 
					    HttpServer::new(move || {
 | 
				
			||||||
        let clients = ClientManager::open_or_create(config.clients_file())
 | 
					        let mut clients = ClientManager::open_or_create(config.clients_file())
 | 
				
			||||||
            .expect("Failed to load clients list!");
 | 
					            .expect("Failed to load clients list!");
 | 
				
			||||||
 | 
					        clients.apply_environment_variables();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let policy = CookieIdentityPolicy::new(config.token_key.as_bytes())
 | 
					        let policy = CookieIdentityPolicy::new(config.token_key.as_bytes())
 | 
				
			||||||
            .name(SESSION_COOKIE_NAME)
 | 
					            .name(SESSION_COOKIE_NAME)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					use lazy_regex::regex_find;
 | 
				
			||||||
use rand::distributions::Alphanumeric;
 | 
					use rand::distributions::Alphanumeric;
 | 
				
			||||||
use rand::Rng;
 | 
					use rand::Rng;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -9,3 +10,24 @@ pub fn rand_str(len: usize) -> String {
 | 
				
			|||||||
        .take(len)
 | 
					        .take(len)
 | 
				
			||||||
        .collect()
 | 
					        .collect()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Parse environment variables
 | 
				
			||||||
 | 
					pub fn apply_env_vars(val: &str) -> String {
 | 
				
			||||||
 | 
					    let mut val = val.to_string();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if let Some(varname_with_wrapper) = regex_find!(r#"\$\{[a-zA-Z0-9_-]+\}"#, &val) {
 | 
				
			||||||
 | 
					        let varname = varname_with_wrapper.strip_prefix("${").unwrap().strip_suffix('}').unwrap();
 | 
				
			||||||
 | 
					        let value = match std::env::var(varname) {
 | 
				
			||||||
 | 
					            Ok(v) => v,
 | 
				
			||||||
 | 
					            Err(e) => {
 | 
				
			||||||
 | 
					                log::error!("Failed to find environment variable {}!", varname);
 | 
				
			||||||
 | 
					                eprintln!("Failed to find environment variable! {:?}", e);
 | 
				
			||||||
 | 
					                std::process::exit(2);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        val = val.replace(varname_with_wrapper, &value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    val
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user