garrison 0.7.3

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Copyright (c) 2026 Kirky.X. All rights reserved.
//! See LICENSE for full license text.

//! MaxMindDb 生产后端实现。
//!
//! 提供 [`MaxMindDbGeoLookup`](crate::strategy::firewall::geo::maxminddb::MaxMindDbGeoLookup) 和
//! [`MaxMindDbCountryLookup`](crate::strategy::firewall::geo::maxminddb::MaxMindDbCountryLookup) 两个生产实现,
//! 分别读取 GeoIP2-City / GeoIP2-Country mmdb 数据库文件。
//!
//! # 依赖
//!
//! - `maxminddb` 0.29 crate(由 `firewall-maxminddb` feature 启用)
//! - 测试数据:`tests/data/GeoLite2-City-Test.mmdb` / `GeoLite2-Country-Test.mmdb`
//!
//! # API 说明
//!
//! maxminddb 0.28+ API:`reader.lookup(ip)?` 返回 `LookupResult`,
//! 再 `result.decode::<City>()?` 返回 `Option<City>`。
//!
//! # 线程安全
//!
//! `maxminddb::Reader<Vec<u8>>` 实现 `Send + Sync`,可安全跨线程共享。

use super::{CountryLookup, GeoCoord, GeoLookup};
use crate::error::{GarrisonError, GarrisonResult};
use async_trait::async_trait;
use maxminddb::geoip2;
use std::net::IpAddr;

// ============================================================================
// MaxMindDbGeoLookup:IP → 坐标(GeoIP2-City 数据库)
// ============================================================================

/// MaxMindDb GeoIP2-City 查询后端(生产实现)。
///
/// 读取 GeoIP2-City / GeoLite2-City mmdb 文件,提供 IP → 坐标查询。
/// 实现 [`GeoLookup`] trait。
///
/// # 构造
///
/// ```ignore
/// use garrison::strategy::firewall::geo::maxminddb::MaxMindDbGeoLookup;
///
/// // 从文件打开
/// let lookup = MaxMindDbGeoLookup::open("tests/data/GeoLite2-City-Test.mmdb")?;
///
/// // 从内存字节打开
/// let bytes = std::fs::read("tests/data/GeoLite2-City-Test.mmdb")?;
/// let lookup = MaxMindDbGeoLookup::from_bytes(bytes)?;
/// ```
pub struct MaxMindDbGeoLookup {
    /// maxminddb Reader(持有 mmdb 文件数据,`Reader<Vec<u8>>` 是 Send + Sync)。
    reader: maxminddb::Reader<Vec<u8>>,
}

impl std::fmt::Debug for MaxMindDbGeoLookup {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MaxMindDbGeoLookup").finish_non_exhaustive()
    }
}

impl MaxMindDbGeoLookup {
    /// 从文件路径打开 mmdb 数据库。
    ///
    /// # 参数
    /// - `path`: mmdb 文件路径(如 `GeoLite2-City.mmdb`)。
    ///
    /// # 错误
    /// - `GarrisonError::Internal`: 文件不存在、格式错误或 IO 错误。
    pub fn open(path: &str) -> GarrisonResult<Self> {
        let reader = maxminddb::Reader::open_readfile(path).map_err(|e| {
            GarrisonError::Internal(format!("strategy-maxmind-open::{}::{}", path, e))
        })?;
        Ok(Self { reader })
    }

    /// 从内存字节构造 mmdb Reader。
    ///
    /// # 参数
    /// - `data`: mmdb 文件完整字节内容。
    ///
    /// # 错误
    /// - `GarrisonError::Internal`: 数据格式错误。
    pub fn from_bytes(data: Vec<u8>) -> GarrisonResult<Self> {
        let reader = maxminddb::Reader::from_source(data)
            .map_err(|e| GarrisonError::Internal(format!("strategy-maxmind-from-bytes::{}", e)))?;
        Ok(Self { reader })
    }
}

#[async_trait]
impl GeoLookup for MaxMindDbGeoLookup {
    async fn lookup(&self, ip: &str) -> GarrisonResult<Option<GeoCoord>> {
        let ip_addr: IpAddr = ip
            .parse()
            .map_err(|_| GarrisonError::InvalidParam(format!("strategy-invalid-ip::{}", ip)))?;

        let result = self.reader.lookup(ip_addr).map_err(|e| {
            GarrisonError::Internal(format!("strategy-maxmind-query::{}::{}", ip, e))
        })?;

        match result.decode::<geoip2::City>() {
            Ok(Some(city)) => {
                let lat = city.location.latitude;
                let lon = city.location.longitude;
                match (lat, lon) {
                    (Some(lat), Some(lon)) => Ok(Some(GeoCoord::new(lat, lon))),
                    _ => Ok(None),
                }
            },
            Ok(None) => Ok(None),
            Err(e) => Err(GarrisonError::Internal(format!(
                "MaxMindDb 解码 City 记录失败 (IP={}): {}",
                ip, e
            ))),
        }
    }
}

// ============================================================================
// MaxMindDbCountryLookup:IP → 国家码(GeoIP2-Country 数据库)
// ============================================================================

/// MaxMindDb GeoIP2-Country 查询后端(生产实现)。
///
/// 读取 GeoIP2-Country / GeoLite2-Country mmdb 文件,提供 IP → 国家码查询。
/// 实现 [`CountryLookup`] trait。
///
/// # 构造
///
/// ```ignore
/// use garrison::strategy::firewall::geo::maxminddb::MaxMindDbCountryLookup;
///
/// let lookup = MaxMindDbCountryLookup::open("tests/data/GeoLite2-Country-Test.mmdb")?;
/// ```
pub struct MaxMindDbCountryLookup {
    /// maxminddb Reader。
    reader: maxminddb::Reader<Vec<u8>>,
}

impl std::fmt::Debug for MaxMindDbCountryLookup {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MaxMindDbCountryLookup")
            .finish_non_exhaustive()
    }
}

impl MaxMindDbCountryLookup {
    /// 从文件路径打开 mmdb 数据库。
    ///
    /// # 参数
    /// - `path`: mmdb 文件路径(如 `GeoLite2-Country.mmdb`)。
    ///
    /// # 错误
    /// - `GarrisonError::Internal`: 文件不存在、格式错误或 IO 错误。
    pub fn open(path: &str) -> GarrisonResult<Self> {
        let reader = maxminddb::Reader::open_readfile(path).map_err(|e| {
            GarrisonError::Internal(format!("strategy-maxmind-open::{}::{}", path, e))
        })?;
        Ok(Self { reader })
    }

    /// 从内存字节构造 mmdb Reader。
    ///
    /// # 参数
    /// - `data`: mmdb 文件完整字节内容。
    ///
    /// # 错误
    /// - `GarrisonError::Internal`: 数据格式错误。
    pub fn from_bytes(data: Vec<u8>) -> GarrisonResult<Self> {
        let reader = maxminddb::Reader::from_source(data)
            .map_err(|e| GarrisonError::Internal(format!("strategy-maxmind-from-bytes::{}", e)))?;
        Ok(Self { reader })
    }
}

#[async_trait]
impl CountryLookup for MaxMindDbCountryLookup {
    async fn lookup_country(&self, ip: &str) -> GarrisonResult<Option<String>> {
        let ip_addr: IpAddr = ip
            .parse()
            .map_err(|_| GarrisonError::InvalidParam(format!("strategy-invalid-ip::{}", ip)))?;

        let result = self.reader.lookup(ip_addr).map_err(|e| {
            GarrisonError::Internal(format!("strategy-maxmind-query::{}::{}", ip, e))
        })?;

        match result.decode::<geoip2::Country>() {
            Ok(Some(country)) => {
                let iso_code = country.country.iso_code.map(|s| s.to_string());
                Ok(iso_code)
            },
            Ok(None) => Ok(None),
            Err(e) => Err(GarrisonError::Internal(format!(
                "MaxMindDb 解码 Country 记录失败 (IP={}): {}",
                ip, e
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    /// 测试数据文件路径常量。
    const CITY_TEST_DB: &str = "tests/data/GeoLite2-City-Test.mmdb";
    const COUNTRY_TEST_DB: &str = "tests/data/GeoLite2-Country-Test.mmdb";

    // =========================================================================
    // MaxMindDbGeoLookup 测试(T049-T057)
    // =========================================================================

    /// 验证从测试 mmdb 文件打开 MaxMindDbGeoLookup 成功(T051)。
    #[test]
    fn maxminddb_geo_open_success() {
        let result = MaxMindDbGeoLookup::open(CITY_TEST_DB);
        assert!(
            result.is_ok(),
            "打开 City 测试数据库应成功: {:?}",
            result.err()
        );
    }

    /// 验证打开不存在的文件返回 Err(T052)。
    #[test]
    fn maxminddb_geo_open_file_not_found() {
        let result = MaxMindDbGeoLookup::open("tests/data/nonexistent.mmdb");
        assert!(result.is_err(), "打开不存在的文件应返回 Err");
        let err = result.unwrap_err();
        assert!(
            matches!(err, GarrisonError::Internal(_)),
            "错误类型应为 Internal,实际: {:?}",
            err
        );
    }

    /// 验证从字节构造 MaxMindDbGeoLookup 成功。
    #[test]
    fn maxminddb_geo_from_bytes_success() {
        let bytes = std::fs::read(CITY_TEST_DB).expect("读取测试数据库文件失败");
        let result = MaxMindDbGeoLookup::from_bytes(bytes);
        assert!(result.is_ok(), "从字节构造应成功: {:?}", result.err());
    }

    /// 验证查询已知 IP 返回 Some(GeoCoord)(T053)。
    ///
    /// 81.2.69.142 是 MaxMind 测试数据中的已知 IP(英国伦敦)。
    #[tokio::test]
    async fn maxminddb_geo_lookup_known_ip() {
        let lookup = MaxMindDbGeoLookup::open(CITY_TEST_DB).expect("打开数据库失败");
        let result = lookup.lookup("81.2.69.142").await;
        assert!(result.is_ok(), "查询已知 IP 应成功: {:?}", result.err());
        let coord = result.unwrap();
        assert!(coord.is_some(), "已知 IP 应返回 Some(GeoCoord)");
        let coord = coord.unwrap();
        // 伦敦纬度约 51.x,经度约 -0.x
        assert!(
            coord.lat > 50.0 && coord.lat < 52.0,
            "伦敦纬度应在 50-52 之间,实际: {}",
            coord.lat
        );
        assert!(
            coord.lon > -1.0 && coord.lon < 1.0,
            "伦敦经度应在 -1~1 之间,实际: {}",
            coord.lon
        );
    }

    /// 验证查询私有 IP 返回 None(T054)。
    ///
    /// 192.168.1.1 是私有 IP,mmdb 数据库中无记录。
    #[tokio::test]
    async fn maxminddb_geo_lookup_private_ip() {
        let lookup = MaxMindDbGeoLookup::open(CITY_TEST_DB).expect("打开数据库失败");
        let result = lookup.lookup("192.168.1.1").await;
        assert!(result.is_ok(), "查询私有 IP 应成功: {:?}", result.err());
        assert!(
            result.unwrap().is_none(),
            "私有 IP 应返回 None(数据库无记录)"
        );
    }

    /// 验证查询无效 IP 返回 Err(T055)。
    #[tokio::test]
    async fn maxminddb_geo_lookup_invalid_ip() {
        let lookup = MaxMindDbGeoLookup::open(CITY_TEST_DB).expect("打开数据库失败");
        let result = lookup.lookup("invalid").await;
        assert!(result.is_err(), "无效 IP 应返回 Err");
        let err = result.unwrap_err();
        assert!(
            matches!(err, GarrisonError::InvalidParam(_)),
            "无效 IP 错误类型应为 InvalidParam,实际: {:?}",
            err
        );
    }

    // =========================================================================
    // MaxMindDbCountryLookup 测试(T058-T064)
    // =========================================================================

    /// 验证从测试 mmdb 文件打开 MaxMindDbCountryLookup 成功(T059)。
    #[test]
    fn maxminddb_country_open_success() {
        let result = MaxMindDbCountryLookup::open(COUNTRY_TEST_DB);
        assert!(
            result.is_ok(),
            "打开 Country 测试数据库应成功: {:?}",
            result.err()
        );
    }

    /// 验证打开不存在的文件返回 Err。
    #[test]
    fn maxminddb_country_open_file_not_found() {
        let result = MaxMindDbCountryLookup::open("tests/data/nonexistent.mmdb");
        assert!(result.is_err(), "打开不存在的文件应返回 Err");
    }

    /// 验证从字节构造 MaxMindDbCountryLookup 成功。
    #[test]
    fn maxminddb_country_from_bytes_success() {
        let bytes = std::fs::read(COUNTRY_TEST_DB).expect("读取测试数据库文件失败");
        let result = MaxMindDbCountryLookup::from_bytes(bytes);
        assert!(result.is_ok(), "从字节构造应成功: {:?}", result.err());
    }

    /// 验证查询已知 IP 返回 Some(国家码)(T060)。
    ///
    /// 81.2.69.142 是 MaxMind 测试数据中的已知 IP(英国,国家码 GB)。
    #[tokio::test]
    async fn maxminddb_country_lookup_known_ip() {
        let lookup = MaxMindDbCountryLookup::open(COUNTRY_TEST_DB).expect("打开数据库失败");
        let result = lookup.lookup_country("81.2.69.142").await;
        assert!(result.is_ok(), "查询已知 IP 应成功: {:?}", result.err());
        let country = result.unwrap();
        assert!(country.is_some(), "已知 IP 应返回 Some(国家码)");
        let country = country.unwrap();
        assert!(
            country == "GB" || country == "gb",
            "81.2.69.142 国家码应为 GB,实际: {}",
            country
        );
    }

    /// 验证查询私有 IP 返回 None(T061)。
    #[tokio::test]
    async fn maxminddb_country_lookup_private_ip() {
        let lookup = MaxMindDbCountryLookup::open(COUNTRY_TEST_DB).expect("打开数据库失败");
        let result = lookup.lookup_country("192.168.1.1").await;
        assert!(result.is_ok(), "查询私有 IP 应成功: {:?}", result.err());
        assert!(
            result.unwrap().is_none(),
            "私有 IP 应返回 None(数据库无记录)"
        );
    }

    /// 验证查询无效 IP 返回 Err(T062)。
    #[tokio::test]
    async fn maxminddb_country_lookup_invalid_ip() {
        let lookup = MaxMindDbCountryLookup::open(COUNTRY_TEST_DB).expect("打开数据库失败");
        let result = lookup.lookup_country("invalid").await;
        assert!(result.is_err(), "无效 IP 应返回 Err");
        let err = result.unwrap_err();
        assert!(
            matches!(err, GarrisonError::InvalidParam(_)),
            "无效 IP 错误类型应为 InvalidParam,实际: {:?}",
            err
        );
    }

    // =========================================================================
    // 集成测试(T065-T066)
    // =========================================================================

    /// 验证 GeoIPStrategy 注入 MaxMindDbCountryLookup 的白名单拦截行为(T065)。
    ///
    /// 81.2.69.142 → GB,白名单 ["CN"],应拦截。
    #[tokio::test]
    async fn geoip_strategy_with_maxminddb() {
        use crate::strategy::firewall::{FirewallContext, GarrisonFirewallStrategy};
        use crate::strategy::firewall::{GeoIPConfig, GeoIPStrategy};

        let country_lookup: Arc<dyn CountryLookup> =
            Arc::new(MaxMindDbCountryLookup::open(COUNTRY_TEST_DB).expect("打开数据库失败"));
        let config = GeoIPConfig {
            allowed_countries: vec!["CN".into()],
            blocked_countries: vec![],
        };
        let strategy = GeoIPStrategy::new(config, country_lookup);
        let ctx = FirewallContext::new("81.2.69.142");

        let result = strategy.check(&ctx).await;
        assert!(
            matches!(result, Err(GarrisonError::FirewallBlocked(_))),
            "GB 不在白名单 [CN] 应拦截,实际: {:?}",
            result
        );
    }

    /// 验证 AnomalousLoginStrategy 注入 MaxMindDbGeoLookup 的异地登录检测(T066)。
    ///
    /// 使用 City 测试数据库,81.2.69.142 → 伦敦坐标。
    /// MockDao 提供历史坐标(北京),haversine 距离 > 500km,应拦截。
    #[tokio::test]
    async fn anomalous_strategy_with_maxminddb() {
        use crate::dao::tests::MockDao;
        use crate::strategy::firewall::{AnomalousConfig, AnomalousLoginStrategy};
        use crate::strategy::firewall::{FirewallContext, GarrisonFirewallStrategy};

        let dao: Arc<dyn crate::dao::GarrisonDao> = Arc::new(MockDao::new());
        let geo_lookup: Arc<dyn GeoLookup> =
            Arc::new(MaxMindDbGeoLookup::open(CITY_TEST_DB).expect("打开数据库失败"));
        let config = AnomalousConfig {
            known_geo_threshold: 500,
        };
        let strategy = AnomalousLoginStrategy::new(config, dao, geo_lookup);

        // 首次登录:81.2.69.142(伦敦),无历史,应放行
        let ctx_first = FirewallContext::new("81.2.69.142").with_login_id("1001");
        let result_first = strategy.check(&ctx_first).await;
        assert!(
            result_first.is_ok(),
            "首次登录应放行(无历史记录),实际: {:?}",
            result_first.err()
        );

        // 第二次登录:用另一个已知 IP(如有),检查是否触发异地检测
        // 81.2.69.142 伦敦 → 历史记录为伦敦
        // 再次登录 81.2.69.142 → 同一位置,应放行
        let ctx_second = FirewallContext::new("81.2.69.142").with_login_id("1001");
        let result_second = strategy.check(&ctx_second).await;
        assert!(
            result_second.is_ok(),
            "同地登录(伦敦→伦敦)应放行,实际: {:?}",
            result_second.err()
        );
    }
}