Emit warning when no 'secret_key' is set in prod.

This commit is contained in:
Sergio Benitez 2017-06-08 23:33:16 -07:00
parent b8ba7b855f
commit d09920c021
3 changed files with 79 additions and 12 deletions

View File

@ -14,17 +14,26 @@ pub enum SecretKey {
impl SecretKey {
#[inline]
pub fn kind(&self) -> &'static str {
pub(crate) fn inner(&self) -> &Key {
match *self {
SecretKey::Generated(_) => "generated",
SecretKey::Provided(_) => "provided",
SecretKey::Generated(ref key) | SecretKey::Provided(ref key) => key
}
}
#[inline]
pub(crate) fn inner(&self) -> &Key {
pub(crate) fn is_generated(&self) -> bool {
match *self {
SecretKey::Generated(ref key) | SecretKey::Provided(ref key) => key
SecretKey::Generated(_) => true,
_ => false
}
}
}
impl fmt::Display for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SecretKey::Generated(_) => write!(f, "generated"),
SecretKey::Provided(_) => write!(f, "provided"),
}
}
}

View File

@ -19,6 +19,62 @@ pub enum Environment {
Production,
}
impl Environment {
/// Returns `true` if `self` is `Environment::Development`.
///
/// # Example
///
/// ```rust
/// use rocket::config::Environment;
///
/// assert!(Environment::Development.is_dev());
/// assert!(!Environment::Production.is_dev());
/// ```
#[inline]
pub fn is_dev(self) -> bool {
match self {
Development => true,
_ => false
}
}
/// Returns `true` if `self` is `Environment::Staging`.
///
/// # Example
///
/// ```rust
/// use rocket::config::Environment;
///
/// assert!(Environment::Staging.is_stage());
/// assert!(!Environment::Production.is_stage());
/// ```
#[inline]
pub fn is_stage(self) -> bool {
match self {
Staging => true,
_ => false
}
}
/// Returns `true` if `self` is `Environment::Production`.
///
/// # Example
///
/// ```rust
/// use rocket::config::Environment;
///
/// assert!(Environment::Production.is_prod());
/// assert!(!Environment::Staging.is_prod());
/// ```
#[inline]
pub fn is_prod(self) -> bool {
match self {
Production => true,
_ => false
}
}
}
impl Environment {
/// Retrieves the "active" environment as determined by the `ROCKET_ENV`
/// environment variable. If `ROCKET_ENV` is not set, returns `Development`.

View File

@ -391,19 +391,21 @@ impl Rocket {
info_!("port: {}", Paint::white(&config.port));
info_!("log: {}", Paint::white(config.log_level));
info_!("workers: {}", Paint::white(config.workers));
info_!("secret key: {}", Paint::white(config.secret_key.kind()));
info_!("secret key: {}", Paint::white(&config.secret_key));
info_!("limits: {}", Paint::white(&config.limits));
let tls_configured = config.tls.is_some();
if tls_configured && cfg!(feature = "tls") {
info_!("tls: {}", Paint::white("enabled"));
} else {
if tls_configured {
} else if tls_configured {
error_!("tls: {}", Paint::white("disabled"));
error_!("tls is configured, but the tls feature is disabled");
} else {
info_!("tls: {}", Paint::white("disabled"));
}
if config.secret_key.is_generated() && config.environment.is_prod() {
warn!("environment is 'production', but no `secret_key` is configured");
}
for (name, value) in config.extras() {