fix(security): protect gateway pairing codes (#11427)

Signed-off-by: Jasper Hugo <jasper@spiral.xyz>
This commit is contained in:
Jasper
2026-09-09 19:19:10 +00:00
committed by GitHub
parent a8a4dbce82
commit ab6e6c025e
2 changed files with 816 additions and 35 deletions
+82 -5
View File
@@ -136,6 +136,16 @@ enum SecretStorage {
},
}
enum SecretMutation<T> {
Write(T),
Unchanged(T),
}
pub(crate) enum SecretUpdate<V, R> {
Write(V, R),
Unchanged(R),
}
// Global instance
static GLOBAL_CONFIG: OnceCell<Config> = OnceCell::new();
@@ -776,6 +786,38 @@ impl Config {
}
}
pub(crate) fn get_param_source_values<T: for<'de> Deserialize<'de>>(
&self,
key: &str,
) -> Result<Vec<T>, ConfigError> {
let mut source_values = Vec::new();
let env_key = key.to_uppercase();
if let Some(value) = env::var_os(&env_key) {
let value = value.into_string().map_err(|_| {
ConfigError::DeserializeError(format!(
"environment variable {env_key} is not valid UTF-8"
))
})?;
source_values.push(serde_json::from_value(Self::parse_env_value(&value)?)?);
}
for path in &self.config_paths {
let content = match std::fs::read_to_string(path) {
Ok(content) => content,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
Err(error) => return Err(error.into()),
};
let mut values = parse_yaml_content(&content)?;
crate::config::migrations::run_read_migrations(&mut values);
let Some(value) = values.get(key) else {
continue;
};
source_values.push(serde_yaml::from_value(value.clone())?);
}
Ok(source_values)
}
/// Read-modify-write a configuration value atomically through the write path.
pub fn update_param<T, V, F>(&self, key: &str, f: F) -> Result<(), ConfigError>
where
@@ -987,15 +1029,20 @@ impl Config {
Ok(())
}
fn mutate_secrets(
fn mutate_secrets<T>(
&self,
mutate: impl FnOnce(&mut HashMap<String, Value>),
) -> Result<(), ConfigError> {
mutate: impl FnOnce(&mut HashMap<String, Value>) -> Result<SecretMutation<T>, ConfigError>,
) -> Result<T, ConfigError> {
let _guard = self.guard.lock().unwrap();
let _storage_lock = self.lock_secrets_for_mutation()?;
let mut values = self.load_secrets_from_storage()?;
mutate(&mut values);
self.write_all_secrets(&values)
match mutate(&mut values)? {
SecretMutation::Write(result) => {
self.write_all_secrets(&values)?;
Ok(result)
}
SecretMutation::Unchanged(result) => Ok(result),
}
}
/// Set a secret value in the system keyring.
@@ -1019,6 +1066,33 @@ impl Config {
let value = serde_json::to_value(value)?;
self.mutate_secrets(|values| {
values.insert(key.to_string(), value);
Ok(SecretMutation::Write(()))
})
}
pub(crate) fn update_secret<T, V, R>(
&self,
key: &str,
update: impl FnOnce(T) -> SecretUpdate<V, R>,
) -> Result<R, ConfigError>
where
T: for<'de> Deserialize<'de> + Default,
V: Serialize,
{
self.mutate_secrets(|values| {
let current = values
.get(key)
.cloned()
.map(serde_json::from_value)
.transpose()?
.unwrap_or_default();
match update(current) {
SecretUpdate::Write(updated, result) => {
values.insert(key.to_string(), serde_json::to_value(updated)?);
Ok(SecretMutation::Write(result))
}
SecretUpdate::Unchanged(result) => Ok(SecretMutation::Unchanged(result)),
}
})
}
@@ -1036,6 +1110,7 @@ impl Config {
for (key, value) in updates {
values.insert(key.clone(), value.clone());
}
Ok(SecretMutation::Write(()))
})
}
@@ -1052,6 +1127,7 @@ impl Config {
pub fn delete_secret(&self, key: &str) -> Result<(), ConfigError> {
self.mutate_secrets(|values| {
values.remove(key);
Ok(SecretMutation::Write(()))
})
}
@@ -1065,6 +1141,7 @@ impl Config {
for key in keys {
values.remove(key);
}
Ok(SecretMutation::Write(()))
})
}
+734 -30
View File
@@ -3,12 +3,14 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use crate::config::base::SecretUpdate;
use crate::config::Config;
use super::{PairingState, PlatformUser};
const PAIRINGS_CONFIG_KEY: &str = "gateway_pairings";
const PENDING_CODES_CONFIG_KEY: &str = "gateway_pending_codes";
const PENDING_CODES_SECRET_KEY: &str = "gateway_pending_codes";
#[derive(Debug, Clone, Serialize, Deserialize)]
struct StoredPairing {
@@ -25,6 +27,54 @@ struct StoredPendingCode {
expires_at: i64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct StoredPendingCodes {
codes: Vec<StoredPendingCode>,
#[serde(default)]
legacy_import_complete: bool,
#[serde(default)]
imported_legacy_codes: Vec<String>,
}
impl StoredPendingCodes {
fn revoke_imported_codes(&mut self) -> bool {
let previous_len = self.codes.len();
self.codes.retain(|pending| {
!self
.imported_legacy_codes
.iter()
.any(|code| code == &pending.code)
});
self.codes.len() != previous_len
}
}
#[derive(Deserialize)]
#[serde(untagged)]
enum StoredPendingCodesValue {
State(StoredPendingCodes),
Codes(Vec<StoredPendingCode>),
}
impl Default for StoredPendingCodesValue {
fn default() -> Self {
Self::State(StoredPendingCodes::default())
}
}
impl StoredPendingCodesValue {
fn into_state(self) -> StoredPendingCodes {
match self {
Self::State(state) => state,
Self::Codes(codes) => StoredPendingCodes {
codes,
legacy_import_complete: false,
imported_legacy_codes: Vec::new(),
},
}
}
}
pub struct PairingStore {
pairings: RwLock<HashMap<PlatformUser, PairingState>>,
}
@@ -69,16 +119,115 @@ impl PairingStore {
.map_err(|e| anyhow::anyhow!("failed to save gateway pairings: {}", e))
}
fn load_pending_codes() -> Vec<StoredPendingCode> {
Config::global()
.get_param(PENDING_CODES_CONFIG_KEY)
.unwrap_or_default()
fn migrate_pending_codes(config: &Config) -> anyhow::Result<()> {
let legacy_codes: Vec<_> = config
.get_param_source_values::<Vec<StoredPendingCode>>(PENDING_CODES_CONFIG_KEY)
.map_err(|error| anyhow::anyhow!("failed to verify legacy pending codes: {error}"))?
.into_iter()
.flatten()
.collect();
if legacy_codes.is_empty() {
return Ok(());
}
Self::revoke_legacy_pending_codes(config, legacy_codes)?;
Err(anyhow::anyhow!(
"legacy pending codes remain in ordinary configuration and have been revoked; stop older Goose processes, remove GATEWAY_PENDING_CODES and gateway_pending_codes from configured files, then retry and generate a new pairing code"
))
}
fn save_pending_codes(codes: &[StoredPendingCode]) -> anyhow::Result<()> {
Config::global()
.set_param(PENDING_CODES_CONFIG_KEY, codes)
.map_err(|e| anyhow::anyhow!("failed to save pending codes: {}", e))
fn revoke_legacy_pending_codes(
config: &Config,
legacy_codes: Vec<StoredPendingCode>,
) -> anyhow::Result<()> {
config
.update_secret(
PENDING_CODES_SECRET_KEY,
|stored: StoredPendingCodesValue| {
let mut state = stored.into_state();
let mut changed = !state.legacy_import_complete;
for legacy_code in legacy_codes {
if state
.imported_legacy_codes
.iter()
.any(|code| code == &legacy_code.code)
{
continue;
}
state.imported_legacy_codes.push(legacy_code.code);
changed = true;
}
changed |= state.revoke_imported_codes();
state.legacy_import_complete = true;
if changed {
SecretUpdate::Write(state, ())
} else {
SecretUpdate::Unchanged(())
}
},
)
.map_err(|error| anyhow::anyhow!("failed to migrate pending codes: {}", error))
}
fn store_pending_code_in(
config: &Config,
code: &str,
gateway_type: &str,
expires_at: i64,
) -> anyhow::Result<()> {
Self::migrate_pending_codes(config)?;
config
.update_secret(
PENDING_CODES_SECRET_KEY,
|stored: StoredPendingCodesValue| {
let mut state = stored.into_state();
state.revoke_imported_codes();
state
.imported_legacy_codes
.retain(|legacy_code| legacy_code != code);
state.legacy_import_complete = true;
state.codes.retain(|pending| pending.code != code);
state.codes.push(StoredPendingCode {
code: code.to_string(),
gateway_type: gateway_type.to_string(),
expires_at,
});
SecretUpdate::Write(state, ())
},
)
.map_err(|error| anyhow::anyhow!("failed to save pending codes: {}", error))
}
fn consume_pending_code_in(
config: &Config,
code: &str,
now: i64,
) -> anyhow::Result<Option<String>> {
Self::migrate_pending_codes(config)?;
config
.update_secret(
PENDING_CODES_SECRET_KEY,
|stored: StoredPendingCodesValue| {
let mut state = stored.into_state();
let mut needs_write = !state.legacy_import_complete;
state.legacy_import_complete = true;
needs_write |= state.revoke_imported_codes();
let Some(position) =
state.codes.iter().position(|pending| pending.code == code)
else {
return if needs_write {
SecretUpdate::Write(state, None)
} else {
SecretUpdate::Unchanged(None)
};
};
let consumed = Some(state.codes.remove(position))
.filter(|pending| now <= pending.expires_at)
.map(|pending| pending.gateway_type);
SecretUpdate::Write(state, consumed)
},
)
.map_err(|error| anyhow::anyhow!("failed to consume pending code: {}", error))
}
pub async fn get(&self, user: &PlatformUser) -> anyhow::Result<PairingState> {
@@ -107,32 +256,12 @@ impl PairingStore {
gateway_type: &str,
expires_at: i64,
) -> anyhow::Result<()> {
let mut codes = Self::load_pending_codes();
codes.retain(|c| c.code != code);
codes.push(StoredPendingCode {
code: code.to_string(),
gateway_type: gateway_type.to_string(),
expires_at,
});
Self::save_pending_codes(&codes)
Self::store_pending_code_in(Config::global(), code, gateway_type, expires_at)
}
pub async fn consume_pending_code(&self, code: &str) -> anyhow::Result<Option<String>> {
let mut codes = Self::load_pending_codes();
let pos = codes.iter().position(|c| c.code == code);
let Some(pos) = pos else {
return Ok(None);
};
let entry = codes.remove(pos);
Self::save_pending_codes(&codes)?;
let now = chrono::Utc::now().timestamp();
if now > entry.expires_at {
return Ok(None);
}
Ok(Some(entry.gateway_type))
Self::consume_pending_code_in(Config::global(), code, now)
}
pub fn generate_code() -> String {
@@ -177,6 +306,16 @@ impl PairingStore {
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Arc, Barrier};
use tempfile::TempDir;
fn test_config(directory: &TempDir) -> Config {
Config::new_with_file_secrets(
directory.path().join("config.yaml"),
directory.path().join("secrets.yaml"),
)
.unwrap()
}
#[test]
fn test_code_generation() {
@@ -186,4 +325,569 @@ mod tests {
.chars()
.all(|c| "ABCDEFGHJKLMNPQRSTUVWXYZ23456789".contains(c)));
}
#[test]
fn pending_codes_are_secret_and_consumed_once() {
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
let code = "SECRET-CODE";
PairingStore::store_pending_code_in(&config, code, "telegram", 101).unwrap();
let ordinary_config =
std::fs::read_to_string(directory.path().join("config.yaml")).unwrap_or_default();
assert!(!ordinary_config.contains(code));
assert!(!config
.all_values()
.unwrap()
.contains_key(PENDING_CODES_CONFIG_KEY));
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
Some("telegram".to_string())
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
None
);
PairingStore::store_pending_code_in(&config, code, "telegram", 99).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
None
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 98).unwrap(),
None
);
}
#[test]
fn pending_code_store_revokes_legacy_config_and_requires_cleanup() {
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
let legacy_code = "LEGACY-CODE";
let new_code = "NEW-CODE";
config
.set_param(
PENDING_CODES_CONFIG_KEY,
&[StoredPendingCode {
code: legacy_code.to_string(),
gateway_type: "slack".to_string(),
expires_at: 101,
}],
)
.unwrap();
let error =
PairingStore::store_pending_code_in(&config, new_code, "telegram", 101).unwrap_err();
assert!(error.to_string().contains("stop older Goose processes"));
let ordinary_config =
std::fs::read_to_string(directory.path().join("config.yaml")).unwrap_or_default();
assert!(ordinary_config.contains(legacy_code));
assert!(!ordinary_config.contains(new_code));
config.delete(PENDING_CODES_CONFIG_KEY).unwrap();
PairingStore::store_pending_code_in(&config, new_code, "telegram", 101).unwrap();
let ordinary_config =
std::fs::read_to_string(directory.path().join("config.yaml")).unwrap_or_default();
assert!(!ordinary_config.contains(legacy_code));
assert!(!ordinary_config.contains(new_code));
assert!(!config
.all_values()
.unwrap()
.contains_key(PENDING_CODES_CONFIG_KEY));
assert_eq!(
PairingStore::consume_pending_code_in(&config, legacy_code, 100).unwrap(),
None
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, new_code, 100).unwrap(),
Some("telegram".to_string())
);
}
#[test]
fn pending_code_migration_rejects_non_writable_config_sources() {
let directory = TempDir::new().unwrap();
let system_config_path = directory.path().join("system.yaml");
let user_config_path = directory.path().join("user.yaml");
let secrets_path = directory.path().join("secrets.yaml");
let system_config =
Config::new_with_file_secrets(&system_config_path, &secrets_path).unwrap();
let legacy_code = StoredPendingCode {
code: "SYSTEM-CODE".to_string(),
gateway_type: "slack".to_string(),
expires_at: 101,
};
system_config
.set_param(PENDING_CODES_CONFIG_KEY, [&legacy_code])
.unwrap();
let config = Config::new_with_config_paths(
vec![system_config_path, user_config_path.clone()],
&secrets_path,
)
.unwrap();
let error =
PairingStore::store_pending_code_in(&config, "NEW-CODE", "telegram", 101).unwrap_err();
assert!(error
.to_string()
.contains("legacy pending codes remain in ordinary configuration"));
assert_eq!(
config
.get_param::<Vec<StoredPendingCode>>(PENDING_CODES_CONFIG_KEY)
.unwrap()[0]
.code,
legacy_code.code
);
let user_config = std::fs::read_to_string(user_config_path).unwrap_or_default();
assert!(!user_config.contains(&legacy_code.code));
std::fs::write(directory.path().join("system.yaml"), "").unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, &legacy_code.code, 100).unwrap(),
None
);
}
#[test]
fn empty_non_writable_legacy_source_does_not_disable_pairing() {
let directory = TempDir::new().unwrap();
let system_config_path = directory.path().join("system.yaml");
let user_config_path = directory.path().join("user.yaml");
let secrets_path = directory.path().join("secrets.yaml");
let system_config =
Config::new_with_file_secrets(&system_config_path, &secrets_path).unwrap();
system_config
.set_param(PENDING_CODES_CONFIG_KEY, Vec::<StoredPendingCode>::new())
.unwrap();
let config = Config::new_with_config_paths(
vec![system_config_path, user_config_path],
&secrets_path,
)
.unwrap();
PairingStore::store_pending_code_in(&config, "NEW-CODE", "telegram", 101).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, "NEW-CODE", 100).unwrap(),
Some("telegram".to_string())
);
}
#[test]
fn empty_overlay_cannot_hide_nonempty_legacy_source() {
let directory = TempDir::new().unwrap();
let system_config_path = directory.path().join("system.yaml");
let additional_config_path = directory.path().join("additional.yaml");
let user_config_path = directory.path().join("user.yaml");
let secrets_path = directory.path().join("secrets.yaml");
let system_config =
Config::new_with_file_secrets(&system_config_path, &secrets_path).unwrap();
system_config
.set_param(
PENDING_CODES_CONFIG_KEY,
[StoredPendingCode {
code: "SYSTEM-CODE".to_string(),
gateway_type: "slack".to_string(),
expires_at: 101,
}],
)
.unwrap();
let additional_config =
Config::new_with_file_secrets(&additional_config_path, &secrets_path).unwrap();
additional_config
.set_param(PENDING_CODES_CONFIG_KEY, Vec::<StoredPendingCode>::new())
.unwrap();
let config = Config::new_with_config_paths(
vec![system_config_path, additional_config_path, user_config_path],
&secrets_path,
)
.unwrap();
assert!(
PairingStore::store_pending_code_in(&config, "NEW-CODE", "telegram", 101)
.unwrap_err()
.to_string()
.contains("legacy pending codes remain")
);
}
#[test]
fn pending_code_migration_revokes_codes_from_every_source() {
let directory = TempDir::new().unwrap();
let system_config_path = directory.path().join("system.yaml");
let user_config_path = directory.path().join("user.yaml");
let secrets_path = directory.path().join("secrets.yaml");
let system_config =
Config::new_with_file_secrets(&system_config_path, &secrets_path).unwrap();
system_config
.set_param(
PENDING_CODES_CONFIG_KEY,
[StoredPendingCode {
code: "SYSTEM-CODE".to_string(),
gateway_type: "slack".to_string(),
expires_at: 101,
}],
)
.unwrap();
let user_config = Config::new_with_file_secrets(&user_config_path, &secrets_path).unwrap();
user_config
.set_param(
PENDING_CODES_CONFIG_KEY,
[StoredPendingCode {
code: "USER-CODE".to_string(),
gateway_type: "telegram".to_string(),
expires_at: 101,
}],
)
.unwrap();
let config = Config::new_with_config_paths(
vec![system_config_path.clone(), user_config_path],
&secrets_path,
)
.unwrap();
assert!(
PairingStore::store_pending_code_in(&config, "NEW-CODE", "telegram", 101)
.unwrap_err()
.to_string()
.contains("legacy pending codes remain")
);
std::fs::write(system_config_path, "").unwrap();
config.delete(PENDING_CODES_CONFIG_KEY).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, "USER-CODE", 100).unwrap(),
None
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, "SYSTEM-CODE", 100).unwrap(),
None
);
}
#[test]
fn pending_code_consumption_is_serialized_across_config_instances() {
let directory = TempDir::new().unwrap();
let config_path = directory.path().join("config.yaml");
let secrets_path = directory.path().join("secrets.yaml");
let config = Config::new_with_file_secrets(&config_path, &secrets_path).unwrap();
let code = "ONE-TIME-CODE";
PairingStore::store_pending_code_in(&config, code, "telegram", 101).unwrap();
let barrier = Arc::new(Barrier::new(2));
let handles: Vec<_> = (0..2)
.map(|_| {
let config_path = config_path.clone();
let secrets_path = secrets_path.clone();
let barrier = Arc::clone(&barrier);
std::thread::spawn(move || {
let config = Config::new_with_file_secrets(config_path, secrets_path).unwrap();
barrier.wait();
PairingStore::consume_pending_code_in(&config, code, 100).unwrap()
})
})
.collect();
let results: Vec<_> = handles
.into_iter()
.map(|handle| handle.join().unwrap())
.collect();
assert_eq!(results.iter().filter(|result| result.is_some()).count(), 1);
assert_eq!(results.iter().filter(|result| result.is_none()).count(), 1);
}
#[test]
fn delayed_legacy_migration_cannot_activate_revoked_code() {
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
let code = "LEGACY-ONE-TIME-CODE";
config
.set_param(
PENDING_CODES_CONFIG_KEY,
&[StoredPendingCode {
code: code.to_string(),
gateway_type: "telegram".to_string(),
expires_at: 101,
}],
)
.unwrap();
let stale_snapshot: Vec<StoredPendingCode> =
config.get_param(PENDING_CODES_CONFIG_KEY).unwrap();
PairingStore::revoke_legacy_pending_codes(&config, stale_snapshot.clone()).unwrap();
config.delete(PENDING_CODES_CONFIG_KEY).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
None
);
PairingStore::revoke_legacy_pending_codes(&config, stale_snapshot).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
None
);
}
#[test]
fn new_legacy_code_after_completed_migration_is_revoked() {
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
PairingStore::store_pending_code_in(&config, "CURRENT-CODE", "telegram", 101).unwrap();
assert!(
config
.get_secret::<StoredPendingCodes>(PENDING_CODES_SECRET_KEY)
.unwrap()
.legacy_import_complete
);
config
.set_param(
PENDING_CODES_CONFIG_KEY,
[StoredPendingCode {
code: "ROLLBACK-CODE".to_string(),
gateway_type: "slack".to_string(),
expires_at: 101,
}],
)
.unwrap();
assert!(
PairingStore::consume_pending_code_in(&config, "ROLLBACK-CODE", 100)
.unwrap_err()
.to_string()
.contains("stop older Goose processes")
);
config.delete(PENDING_CODES_CONFIG_KEY).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, "ROLLBACK-CODE", 100).unwrap(),
None
);
PairingStore::store_pending_code_in(&config, "FRESH-CODE", "slack", 101).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, "FRESH-CODE", 100).unwrap(),
Some("slack".to_string())
);
}
#[test]
fn legacy_code_written_during_migration_is_preserved_until_cleanup() {
let directory = TempDir::new().unwrap();
let config_path = directory.path().join("config.yaml");
let secrets_path = directory.path().join("secrets.yaml");
let config = Config::new_with_file_secrets(&config_path, &secrets_path).unwrap();
let old_process = Config::new_with_file_secrets(&config_path, &secrets_path).unwrap();
let first = StoredPendingCode {
code: "FIRST-CODE".to_string(),
gateway_type: "telegram".to_string(),
expires_at: 101,
};
let second = StoredPendingCode {
code: "SECOND-CODE".to_string(),
gateway_type: "slack".to_string(),
expires_at: 101,
};
config
.set_param(PENDING_CODES_CONFIG_KEY, [&first])
.unwrap();
let stale_snapshot = config.get_param(PENDING_CODES_CONFIG_KEY).unwrap();
PairingStore::revoke_legacy_pending_codes(&config, stale_snapshot).unwrap();
old_process
.set_param(PENDING_CODES_CONFIG_KEY, [&first, &second])
.unwrap();
assert!(PairingStore::migrate_pending_codes(&config)
.unwrap_err()
.to_string()
.contains("stop older Goose processes"));
let legacy_codes: Vec<StoredPendingCode> =
config.get_param(PENDING_CODES_CONFIG_KEY).unwrap();
assert_eq!(
legacy_codes
.iter()
.map(|pending| pending.code.as_str())
.collect::<Vec<_>>(),
["FIRST-CODE", "SECOND-CODE"]
);
config.delete(PENDING_CODES_CONFIG_KEY).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, "FIRST-CODE", 100).unwrap(),
None
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, "SECOND-CODE", 100).unwrap(),
None
);
}
#[test]
fn legacy_codes_consumed_by_old_writer_cannot_be_reused() {
let directory = TempDir::new().unwrap();
let config_path = directory.path().join("config.yaml");
let secrets_path = directory.path().join("secrets.yaml");
let config = Config::new_with_file_secrets(&config_path, &secrets_path).unwrap();
let old_process = Config::new_with_file_secrets(&config_path, &secrets_path).unwrap();
let first = StoredPendingCode {
code: "FIRST-CODE".to_string(),
gateway_type: "telegram".to_string(),
expires_at: 101,
};
let second = StoredPendingCode {
code: "SECOND-CODE".to_string(),
gateway_type: "slack".to_string(),
expires_at: 101,
};
old_process
.set_param(PENDING_CODES_CONFIG_KEY, [&first, &second])
.unwrap();
assert!(PairingStore::migrate_pending_codes(&config).is_err());
old_process
.set_param(PENDING_CODES_CONFIG_KEY, [&second])
.unwrap();
old_process.delete(PENDING_CODES_CONFIG_KEY).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, &first.code, 100).unwrap(),
None
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, &second.code, 100).unwrap(),
None
);
}
#[test]
fn imported_legacy_codes_already_in_secret_state_are_revoked() {
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
let code = "IMPORTED-CODE";
config
.set_secret(
PENDING_CODES_SECRET_KEY,
&StoredPendingCodes {
codes: vec![StoredPendingCode {
code: code.to_string(),
gateway_type: "telegram".to_string(),
expires_at: 101,
}],
legacy_import_complete: true,
imported_legacy_codes: vec![code.to_string()],
},
)
.unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
None
);
assert!(config
.get_secret::<StoredPendingCodes>(PENDING_CODES_SECRET_KEY)
.unwrap()
.codes
.is_empty());
}
#[test]
fn pending_code_migration_accepts_legacy_secret_vec() {
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
let code = "SECRET-VEC-CODE";
let legacy_codes = vec![StoredPendingCode {
code: code.to_string(),
gateway_type: "telegram".to_string(),
expires_at: 101,
}];
config
.set_secret(PENDING_CODES_SECRET_KEY, &legacy_codes)
.unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
Some("telegram".to_string())
);
assert!(
config
.get_secret::<StoredPendingCodes>(PENDING_CODES_SECRET_KEY)
.unwrap()
.legacy_import_complete
);
PairingStore::revoke_legacy_pending_codes(&config, legacy_codes).unwrap();
assert_eq!(
PairingStore::consume_pending_code_in(&config, code, 100).unwrap(),
None
);
}
#[test]
fn migration_without_legacy_config_does_not_touch_secret_storage() {
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
let secrets_path = directory.path().join("secrets.yaml");
assert!(!secrets_path.exists());
PairingStore::migrate_pending_codes(&config).unwrap();
assert!(!secrets_path.exists());
}
#[cfg(unix)]
#[test]
fn unmatched_pending_codes_do_not_rewrite_secret_storage() {
use std::os::unix::fs::MetadataExt;
let directory = TempDir::new().unwrap();
let config = test_config(&directory);
let secrets_path = directory.path().join("secrets.yaml");
PairingStore::store_pending_code_in(&config, "VALID-CODE", "telegram", 101).unwrap();
PairingStore::store_pending_code_in(&config, "EXPIRED-CODE", "slack", 99).unwrap();
let initial_inode = std::fs::metadata(&secrets_path).unwrap().ino();
assert_eq!(
PairingStore::consume_pending_code_in(&config, "ZZZZZZ", 100).unwrap(),
None
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, "ZZZZZZ", 100).unwrap(),
None
);
assert_eq!(
std::fs::metadata(&secrets_path).unwrap().ino(),
initial_inode
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, "VALID-CODE", 100).unwrap(),
Some("telegram".to_string())
);
let consumed_inode = std::fs::metadata(&secrets_path).unwrap().ino();
assert_ne!(consumed_inode, initial_inode);
assert_eq!(
PairingStore::consume_pending_code_in(&config, "VALID-CODE", 100).unwrap(),
None
);
assert_eq!(
std::fs::metadata(&secrets_path).unwrap().ino(),
consumed_inode
);
assert_eq!(
PairingStore::consume_pending_code_in(&config, "EXPIRED-CODE", 100).unwrap(),
None
);
let expired_inode = std::fs::metadata(&secrets_path).unwrap().ino();
assert_ne!(expired_inode, consumed_inode);
assert_eq!(
PairingStore::consume_pending_code_in(&config, "EXPIRED-CODE", 98).unwrap(),
None
);
assert_eq!(
std::fs::metadata(&secrets_path).unwrap().ino(),
expired_inode
);
}
}