Moved auth secret from env to config file

This commit is contained in:
Antoine Gersant 2016-09-23 00:52:17 -07:00
parent 3923229834
commit 0f2327ca4e
3 changed files with 15 additions and 2 deletions

View file

@ -1,3 +1,5 @@
auth_secret = 'Something very secret'
album_art_pattern = '^Folder\.(png|jpg|jpeg)$' album_art_pattern = '^Folder\.(png|jpg|jpeg)$'
[[mount_dirs]] [[mount_dirs]]

View file

@ -9,6 +9,7 @@ use collection::User;
use ddns::DDNSConfig; use ddns::DDNSConfig;
use vfs::MountDir; use vfs::MountDir;
const CONFIG_SECRET: &'static str = "auth_secret";
const CONFIG_MOUNT_DIRS: &'static str = "mount_dirs"; const CONFIG_MOUNT_DIRS: &'static str = "mount_dirs";
const CONFIG_MOUNT_DIR_NAME: &'static str = "name"; const CONFIG_MOUNT_DIR_NAME: &'static str = "name";
const CONFIG_MOUNT_DIR_SOURCE: &'static str = "source"; const CONFIG_MOUNT_DIR_SOURCE: &'static str = "source";
@ -26,6 +27,7 @@ pub enum ConfigError {
IoError(io::Error), IoError(io::Error),
TOMLParseError, TOMLParseError,
RegexError(regex::Error), RegexError(regex::Error),
SecretParseError,
AlbumArtPatternParseError, AlbumArtPatternParseError,
UsersParseError, UsersParseError,
MountDirsParseError, MountDirsParseError,
@ -45,6 +47,7 @@ impl From<regex::Error> for ConfigError {
} }
pub struct Config { pub struct Config {
pub secret: String,
pub mount_dirs: Vec<MountDir>, pub mount_dirs: Vec<MountDir>,
pub users: Vec<User>, pub users: Vec<User>,
pub album_art_pattern: Option<regex::Regex>, pub album_art_pattern: Option<regex::Regex>,
@ -60,12 +63,14 @@ impl Config {
let parsed_config = try!(parsed_config.ok_or(ConfigError::TOMLParseError)); let parsed_config = try!(parsed_config.ok_or(ConfigError::TOMLParseError));
let mut config = Config { let mut config = Config {
secret: String::new(),
mount_dirs: Vec::new(), mount_dirs: Vec::new(),
users: Vec::new(), users: Vec::new(),
album_art_pattern: None, album_art_pattern: None,
ddns: None, ddns: None,
}; };
try!(config.parse_secret(&parsed_config));
try!(config.parse_mount_points(&parsed_config)); try!(config.parse_mount_points(&parsed_config));
try!(config.parse_users(&parsed_config)); try!(config.parse_users(&parsed_config));
try!(config.parse_album_art_pattern(&parsed_config)); try!(config.parse_album_art_pattern(&parsed_config));
@ -74,6 +79,13 @@ impl Config {
Ok(config) Ok(config)
} }
fn parse_secret(&mut self, source: &toml::Table) -> Result<(), ConfigError> {
let secret = try!(source.get(CONFIG_SECRET).ok_or(ConfigError::SecretParseError));
let secret = try!(secret.as_str().ok_or(ConfigError::SecretParseError));
self.secret = secret.to_owned();
Ok(())
}
fn parse_album_art_pattern(&mut self, source: &toml::Table) -> Result<(), ConfigError> { fn parse_album_art_pattern(&mut self, source: &toml::Table) -> Result<(), ConfigError> {
let pattern = match source.get(CONFIG_ALBUM_ART_PATTERN) { let pattern = match source.get(CONFIG_ALBUM_ART_PATTERN) {
Some(s) => s, Some(s) => s,

View file

@ -57,8 +57,7 @@ fn main() {
} }
api_chain = Chain::new(api_handler); api_chain = Chain::new(api_handler);
let auth_secret = std::env::var("POLARIS_SECRET") let auth_secret = config.secret.to_owned();
.expect("Environment variable POLARIS_SECRET must be set");
let cookie_middleware = oven::new(auth_secret.into_bytes()); let cookie_middleware = oven::new(auth_secret.into_bytes());
api_chain.link(cookie_middleware); api_chain.link(cookie_middleware);
} }