From d09920c0217ad2701ddd756a19dbbf4718d88dd1 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 8 Jun 2017 23:33:16 -0700 Subject: [PATCH] Emit warning when no 'secret_key' is set in prod. --- lib/src/config/custom_values.rs | 19 ++++++++--- lib/src/config/environment.rs | 56 +++++++++++++++++++++++++++++++++ lib/src/rocket.rs | 16 +++++----- 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/lib/src/config/custom_values.rs b/lib/src/config/custom_values.rs index cfe3f3a1..672ce351 100644 --- a/lib/src/config/custom_values.rs +++ b/lib/src/config/custom_values.rs @@ -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"), } } } diff --git a/lib/src/config/environment.rs b/lib/src/config/environment.rs index 895506f3..3a154921 100644 --- a/lib/src/config/environment.rs +++ b/lib/src/config/environment.rs @@ -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`. diff --git a/lib/src/rocket.rs b/lib/src/rocket.rs index d662f207..f2e823c0 100644 --- a/lib/src/rocket.rs +++ b/lib/src/rocket.rs @@ -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 { + error_!("tls: {}", Paint::white("disabled")); + error_!("tls is configured, but the tls feature is disabled"); } 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")); - } + 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() {