Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
fix(settings): get message data hmac key setting to work with env var…
Browse files Browse the repository at this point in the history
…iables
  • Loading branch information
brizental committed Jun 28, 2018
1 parent 05f93b9 commit 576cb6e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
{ "period": "5 minutes", "limit": 0 }
]
},
"message_id_hmac_key": "YOU MUST CHANGE ME",
"messagedata": {
"hmackey": "YOU MUST CHANGE ME"
},
"logging": "mozlog",
"provider": "ses",
"redis": {
Expand Down
2 changes: 1 addition & 1 deletion src/message_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl MessageData {
client: RedisClient::open(
format!("redis://{}:{}/", settings.redis.host, settings.redis.port).as_str(),
).expect("redis connection error"),
hmac_key: settings.message_id_hmac_key.clone(),
hmac_key: settings.messagedata.hmackey.clone(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/message_data/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl TestFixture {
pub fn setup(test: &str) -> TestFixture {
let settings = Settings::new().expect("config error");
let unhashed_key = format!("fxa-email-service.test.message-data.{}.{}", test, now());
let mut hmac = Hmac::<Sha256>::new_varkey(settings.message_id_hmac_key.as_bytes()).unwrap();
let mut hmac = Hmac::<Sha256>::new_varkey(settings.messagedata.hmackey.as_bytes()).unwrap();
hmac.input(unhashed_key.as_bytes());
let internal_key = format!("msg:{:x}", hmac.result().code());
TestFixture {
Expand Down
9 changes: 7 additions & 2 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub struct BounceLimits {
pub soft: Vec<BounceLimit>,
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct MessageData {
pub hmackey: String,
}

#[derive(Debug, Default, Deserialize, Serialize)]
pub struct Redis {
#[serde(deserialize_with = "deserialize::host")]
Expand Down Expand Up @@ -93,7 +98,7 @@ pub struct Settings {
pub authdb: AuthDb,
pub aws: Aws,
pub bouncelimits: BounceLimits,
pub message_id_hmac_key: String,
pub messagedata: MessageData,
pub logging: String,
#[serde(deserialize_with = "deserialize::provider")]
pub provider: String,
Expand Down Expand Up @@ -136,7 +141,7 @@ impl Settings {
Ok(settings) => {
if let Ok(rocket_env) = env::var("ROCKET_ENV") {
if rocket_env == "production"
&& &settings.message_id_hmac_key == "YOU MUST CHANGE ME"
&& &settings.messagedata.hmackey == "YOU MUST CHANGE ME"
{
panic!("Please set a valid HMAC key.")
}
Expand Down
7 changes: 7 additions & 0 deletions src/settings/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn env_vars_take_precedence() {
"FXA_EMAIL_AWS_SQSURLS_DELIVERY",
"FXA_EMAIL_AWS_SQSURLS_NOTIFICATION",
"FXA_EMAIL_BOUNCELIMITS_ENABLED",
"FXA_EMAIL_MESSAGEDATA_HMACKEY",
"FXA_EMAIL_PROVIDER",
"FXA_EMAIL_REDIS_HOST",
"FXA_EMAIL_REDIS_PORT",
Expand Down Expand Up @@ -114,6 +115,7 @@ fn env_vars_take_precedence() {
}
};
let bounce_limits_enabled = !settings.bouncelimits.enabled;
let message_data_hmac_key = String::from("something else");
let provider = if settings.provider == "ses" {
"sendgrid"
} else {
Expand Down Expand Up @@ -142,6 +144,10 @@ fn env_vars_take_precedence() {
"FXA_EMAIL_BOUNCELIMITS_ENABLED",
&bounce_limits_enabled.to_string(),
);
env::set_var(
"FXA_EMAIL_MESSAGEDATA_HMACKEY",
&message_data_hmac_key.to_string(),
);
env::set_var("FXA_EMAIL_PROVIDER", &provider);
env::set_var("FXA_EMAIL_REDIS_HOST", &redis_host);
env::set_var("FXA_EMAIL_REDIS_PORT", &redis_port.to_string());
Expand All @@ -154,6 +160,7 @@ fn env_vars_take_precedence() {
assert_eq!(env_settings.authdb.baseuri, auth_db_base_uri);
assert_eq!(env_settings.aws.region, aws_region);
assert_eq!(env_settings.bouncelimits.enabled, bounce_limits_enabled);
assert_eq!(env_settings.messagedata.hmackey, message_data_hmac_key);
assert_eq!(env_settings.provider, provider);
assert_eq!(env_settings.redis.host, redis_host);
assert_eq!(env_settings.redis.port, redis_port);
Expand Down

0 comments on commit 576cb6e

Please sign in to comment.