fix(settings): get message data hmac key setting to work with env variables (#110) r=@philbooth,@vladikoff

Fixes #109
This commit is contained in:
Beatriz Rizental 2018-06-28 11:56:13 -07:00 коммит произвёл Vlad Filippov
Родитель 05f93b9da5
Коммит 90bec93fc6
5 изменённых файлов: 15 добавлений и 8 удалений

Просмотреть файл

@ -19,7 +19,7 @@
{ "period": "5 minutes", "limit": 0 }
]
},
"message_id_hmac_key": "YOU MUST CHANGE ME",
"hmackey": "YOU MUST CHANGE ME",
"logging": "mozlog",
"provider": "ses",
"redis": {

Просмотреть файл

@ -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.hmackey.clone(),
}
}

Просмотреть файл

@ -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.hmackey.as_bytes()).unwrap();
hmac.input(unhashed_key.as_bytes());
let internal_key = format!("msg:{:x}", hmac.result().code());
TestFixture {

Просмотреть файл

@ -93,7 +93,7 @@ pub struct Settings {
pub authdb: AuthDb,
pub aws: Aws,
pub bouncelimits: BounceLimits,
pub message_id_hmac_key: String,
pub hmackey: String,
pub logging: String,
#[serde(deserialize_with = "deserialize::provider")]
pub provider: String,
@ -130,14 +130,17 @@ impl Settings {
}
config.merge(File::with_name("config/local").required(false))?;
config.merge(Environment::with_prefix("fxa_email"))?;
let mut env = Environment::with_prefix("fxa_email");
// Event though "_" is the default separator for config-rs right now,
// that is going to change for the next versions.
// https://github.com/mehcode/config-rs/commit/536f52fed4a22ed158681edce08211845abff985
env.separator("_".to_string());
config.merge(env)?;
match config.try_into::<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"
{
if rocket_env == "production" && &settings.hmackey == "YOU MUST CHANGE ME" {
panic!("Please set a valid HMAC key.")
}
}

Просмотреть файл

@ -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",
@ -114,6 +115,7 @@ fn env_vars_take_precedence() {
}
};
let bounce_limits_enabled = !settings.bouncelimits.enabled;
let hmac_key = String::from("something else");
let provider = if settings.provider == "ses" {
"sendgrid"
} else {
@ -142,6 +144,7 @@ fn env_vars_take_precedence() {
"FXA_EMAIL_BOUNCELIMITS_ENABLED",
&bounce_limits_enabled.to_string(),
);
env::set_var("FXA_EMAIL_HMACKEY", &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());
@ -154,6 +157,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.hmackey, hmac_key);
assert_eq!(env_settings.provider, provider);
assert_eq!(env_settings.redis.host, redis_host);
assert_eq!(env_settings.redis.port, redis_port);