-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
1371 lines (1136 loc) · 56.5 KB
/
models.py
File metadata and controls
1371 lines (1136 loc) · 56.5 KB
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
models.py — Pydantic data models for PeerGlass.
Phase 1: IP, ASN, Abuse Contact queries across all 5 RIRs
Phase 2: RPKI/ROA validation, BGP routing status, Org resource auditing
Phase 3: Historical allocation tracking, transfer detection, IPv4 exhaustion,
prefix hierarchy (parent/child/sibling relationships)
"""
from __future__ import annotations
from pydantic import BaseModel, Field, field_validator, ConfigDict
from typing import Optional, List, Any
from enum import Enum
# ──────────────────────────────────────────────────────────────
# Enums
# ──────────────────────────────────────────────────────────────
class RIRName(str, Enum):
AFRINIC = "AFRINIC"
APNIC = "APNIC"
ARIN = "ARIN"
LACNIC = "LACNIC"
RIPE = "RIPE"
class ResponseFormat(str, Enum):
MARKDOWN = "markdown"
JSON = "json"
class RPKIValidity(str, Enum):
VALID = "valid"
INVALID = "invalid"
NOT_FOUND = "not-found"
UNKNOWN = "unknown"
# ──────────────────────────────────────────────────────────────
# Input Models — Phase 1
# ──────────────────────────────────────────────────────────────
class IPQueryInput(BaseModel):
"""Input for querying an IP address across all 5 RIRs."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
ip_address: str = Field(
...,
description="IPv4 or IPv6 address (e.g. '1.1.1.1' or '2001:4860:4860::8888')",
min_length=3, max_length=45,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class ASNQueryInput(BaseModel):
"""Input for querying an ASN across all 5 RIRs."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
asn: str = Field(
...,
description="Autonomous System Number. Accepts 'AS15169', '15169', or 'AS-GOOGLE'",
min_length=1, max_length=20,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class AbuseContactInput(BaseModel):
"""Input for abuse contact lookup by IP address."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
ip_address: str = Field(
...,
description="IPv4 or IPv6 address to find abuse contact for (e.g. '185.220.101.1')",
min_length=3, max_length=45,
)
# ──────────────────────────────────────────────────────────────
# Input Models — Phase 2
# ──────────────────────────────────────────────────────────────
class RPKICheckInput(BaseModel):
"""Input for RPKI/ROA validity check."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
prefix: str = Field(
...,
description="IP prefix in CIDR notation (e.g. '1.1.1.0/24' or '2400:cb00::/32')",
min_length=7, max_length=50,
)
asn: str = Field(
...,
description="ASN claiming to originate this prefix (e.g. 'AS13335' or '13335')",
min_length=1, max_length=20,
)
@field_validator("asn")
@classmethod
def normalize_asn(cls, v: str) -> str:
stripped = v.upper().lstrip("AS")
return stripped if stripped.isdigit() else v
class BGPStatusInput(BaseModel):
"""Input for BGP routing table status check."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
resource: str = Field(
...,
description=(
"IP prefix (e.g. '1.1.1.0/24') or ASN (e.g. 'AS15169') "
"to check in the global BGP routing table"
),
min_length=2, max_length=50,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class OrgAuditInput(BaseModel):
"""Input for organization-wide resource audit across all RIRs."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
org_name: str = Field(
...,
description=(
"Organization name or handle to audit (e.g. 'Cloudflare', 'GOOGL-ARIN'). "
"Partial matches are supported."
),
min_length=2, max_length=100,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class AnnouncedPrefixesInput(BaseModel):
"""Input for fetching all BGP-announced prefixes by an ASN."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
asn: str = Field(
...,
description="ASN whose announced prefixes to fetch (e.g. 'AS13335' or '15169')",
min_length=1, max_length=20,
)
min_peers_seeing: int = Field(
default=5,
description="Minimum BGP peer count seeing the prefix (filters out noise)",
ge=1, le=500,
)
# ──────────────────────────────────────────────────────────────
# Output Models — Phase 1
# ──────────────────────────────────────────────────────────────
class RIRQueryResult(BaseModel):
"""Raw result from a single RIR RDAP query."""
rir: RIRName
status: str # ok | not_found | error | rate_limited
queried_at: Optional[str] = None
data: Optional[dict[str, Any]] = None
error: Optional[str] = None
class NetworkResource(BaseModel):
"""Normalized IP network registration — unified schema across all 5 RIRs."""
rir: str
prefix: Optional[str] = None
handle: Optional[str] = None
name: Optional[str] = None
org_name: Optional[str] = None
country: Optional[str] = None
allocation_date: Optional[str] = None
last_changed: Optional[str] = None
abuse_email: Optional[str] = None
status: Optional[str] = None
ip_version: Optional[int] = None
raw: Optional[dict[str, Any]] = None
class ASNResource(BaseModel):
"""Normalized ASN registration — unified schema across all 5 RIRs."""
rir: str
asn: Optional[str] = None
name: Optional[str] = None
org_name: Optional[str] = None
country: Optional[str] = None
allocation_date: Optional[str] = None
last_changed: Optional[str] = None
abuse_email: Optional[str] = None
status: Optional[str] = None
raw: Optional[dict[str, Any]] = None
class AbuseContact(BaseModel):
"""Extracted abuse contact for a given IP address."""
ip_address: str
authoritative_rir: Optional[str] = None
abuse_email: List[str] = Field(default_factory=list)
abuse_phone: List[str] = Field(default_factory=list)
network_name: Optional[str] = None
network_handle: Optional[str] = None
org_name: Optional[str] = None
country: Optional[str] = None
raw: Optional[dict[str, Any]] = None
# ──────────────────────────────────────────────────────────────
# Output Models — Phase 2
# ──────────────────────────────────────────────────────────────
class RPKIResult(BaseModel):
"""RPKI/ROA validity result for a prefix + ASN pair."""
prefix: str
asn: str
validity: RPKIValidity
covering_roas: List[dict[str, Any]] = Field(default_factory=list)
source: str = "Cloudflare RPKI Validator"
description: Optional[str] = None
class BGPPrefix(BaseModel):
"""A single BGP-announced prefix entry from the routing table."""
prefix: str
origin_asn: Optional[str] = None
peers_seeing: Optional[int] = None
first_seen: Optional[str] = None
last_seen: Optional[str] = None
is_more_specific: Optional[bool] = None
class BGPCommunity(BaseModel):
"""A single BGP community value attached to a route announcement."""
asn: int
value: int
description: Optional[str] = None
# Well-known BGP communities (RFC 1997, RFC 7999) decoded to human-readable descriptions.
# Keyed by (asn, value) tuples.
BGP_WELL_KNOWN_COMMUNITIES: dict[tuple[int, int], str] = {
(65535, 65281): "NO_EXPORT — do not advertise beyond this AS boundary",
(65535, 65282): "NO_ADVERTISE — do not advertise to any BGP peer",
(65535, 65283): "NO_EXPORT_SUBCONFED — do not advertise outside local confederation",
(65535, 666): "BLACKHOLE (RFC 7999) — discard traffic, do not forward",
(65535, 65284): "NOPEER — do not export to peer AS (RFC 3765)",
}
class BGPStatusResult(BaseModel):
"""BGP routing table status for a prefix or ASN resource."""
resource: str
resource_type: str # prefix | asn
is_announced: bool
announcing_asns: List[str] = Field(default_factory=list)
announced_prefixes: List[BGPPrefix] = Field(default_factory=list)
visibility_percent: Optional[float] = None
communities: List[BGPCommunity] = Field(default_factory=list)
source: str = "RIPE Stat"
queried_at: Optional[str] = None
class OrgResource(BaseModel):
"""A single IP block or ASN resource belonging to an organization."""
rir: str
resource_type: str # ip | asn
handle: Optional[str] = None
prefix_or_asn: Optional[str] = None
name: Optional[str] = None
country: Optional[str] = None
status: Optional[str] = None
allocation_date: Optional[str] = None
class OrgAuditResult(BaseModel):
"""Aggregated view of all resources registered to an organization across all RIRs."""
org_query: str
total_resources: int
ip_blocks: List[OrgResource] = Field(default_factory=list)
asns: List[OrgResource] = Field(default_factory=list)
rirs_found_in: List[str] = Field(default_factory=list)
errors: List[str] = Field(default_factory=list)
# ──────────────────────────────────────────────────────────────
# Input Models — Phase 3
# ──────────────────────────────────────────────────────────────
class PrefixHistoryInput(BaseModel):
"""Input for historical ownership query on a prefix or ASN."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
resource: str = Field(
...,
description=(
"IP prefix in CIDR notation (e.g. '1.1.1.0/24') or ASN (e.g. 'AS15169'). "
"Returns full ownership timeline and registration change events."
),
min_length=2, max_length=50,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class TransferDetectInput(BaseModel):
"""Input for cross-org / cross-RIR transfer detection."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
resource: str = Field(
...,
description=(
"IP prefix (e.g. '8.8.8.0/24') or ASN (e.g. 'AS15169') "
"to scan for past ownership transfers between organizations or RIRs."
),
min_length=2, max_length=50,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class IPv4StatsInput(BaseModel):
"""Input for the global IPv4 exhaustion / allocation statistics dashboard."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
rir_filter: Optional[str] = Field(
default=None,
description=(
"Optional: filter to a single RIR. "
"Accepts 'AFRINIC', 'APNIC', 'ARIN', 'LACNIC', or 'RIPE'. "
"Leave empty to get all 5 RIRs."
),
pattern="^(AFRINIC|APNIC|ARIN|LACNIC|RIPE)?$",
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
include_blocks: bool = Field(
default=False,
description=(
"If true, include raw delegated IPv4 block rows from the selected RIR. "
"Requires rir_filter to be set."
),
)
status_filter: Optional[str] = Field(
default=None,
description=(
"Optional IPv4 status filter for block rows: allocated, assigned, available. "
"'free' is normalized to 'available'."
),
)
country_filter: Optional[str] = Field(
default=None,
description="Optional 2-letter ISO country code filter for IPv4 block rows (e.g. 'GH', 'ZA').",
)
limit: int = Field(
default=100,
description="Maximum number of IPv4 block rows to return when include_blocks=true.",
ge=1,
le=5000,
)
offset: int = Field(
default=0,
description="Pagination offset for IPv4 block rows when include_blocks=true.",
ge=0,
le=1_000_000,
)
@field_validator("status_filter")
@classmethod
def normalize_status_filter(cls, v: Optional[str]) -> Optional[str]:
if v is None or not v:
return None
status = v.strip().lower()
if status == "free":
status = "available"
allowed = {"allocated", "assigned", "available"}
if status not in allowed:
raise ValueError("status_filter must be one of: allocated, assigned, available, free")
return status
@field_validator("country_filter")
@classmethod
def normalize_country_filter(cls, v: Optional[str]) -> Optional[str]:
if v is None or not v:
return None
country = v.strip().upper()
if len(country) != 2 or not country.isalpha():
raise ValueError("country_filter must be a 2-letter ISO country code")
return country
class PrefixOverviewInput(BaseModel):
"""Input for prefix hierarchy and rich overview query."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
prefix: str = Field(
...,
description=(
"IP prefix in CIDR notation (e.g. '1.1.1.0/24'). "
"Returns the parent allocation, sibling blocks, and child assignments."
),
min_length=7, max_length=50,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
# ──────────────────────────────────────────────────────────────
# Output Models — Phase 3
# ──────────────────────────────────────────────────────────────
class HistoricalEvent(BaseModel):
"""A single dated event in a resource's registration history."""
event_date: Optional[str] = None # ISO date string
event_type: str = "" # created | updated | transferred | status_change
attribute: Optional[str] = None # which RDAP field changed (legacy sources may say WHOIS)
old_value: Optional[str] = None
new_value: Optional[str] = None
source: Optional[str] = None # which API provided this
class PrefixHistoryResult(BaseModel):
"""Full historical record for a prefix or ASN."""
resource: str
resource_type: str # prefix | asn
current_holder: Optional[str] = None
current_rir: Optional[str] = None
registration_date: Optional[str] = None
total_events: int = 0
events: List[HistoricalEvent] = Field(default_factory=list)
sources: List[str] = Field(default_factory=list)
errors: List[str] = Field(default_factory=list)
class TransferEvent(BaseModel):
"""A detected transfer of a resource between organizations or RIRs."""
transfer_date: Optional[str] = None
transfer_type: str = "" # inter-rir | intra-rir | org-change
from_org: Optional[str] = None
to_org: Optional[str] = None
from_rir: Optional[str] = None
to_rir: Optional[str] = None
evidence: Optional[str] = None # which field change triggered detection
class TransferDetectResult(BaseModel):
"""Transfer history for a prefix or ASN."""
resource: str
resource_type: str
transfers_detected: int
transfers: List[TransferEvent] = Field(default_factory=list)
current_holder: Optional[str] = None
current_rir: Optional[str] = None
first_registered: Optional[str] = None
sources: List[str] = Field(default_factory=list)
notes: List[str] = Field(default_factory=list)
class IPv4DelegatedBlock(BaseModel):
"""A single IPv4 delegated row from an RIR extended stats file."""
rir: str
country: Optional[str] = None
start_ip: str
end_ip: str
address_count: int
date: Optional[str] = None
status: str
class RIRDelegationStats(BaseModel):
"""IPv4, IPv6, and ASN delegation statistics for one RIR."""
rir: str
region: str
ipv4_allocated: int = 0 # /32 equivalents allocated to LIRs
ipv4_assigned: int = 0 # /32 equivalents assigned to end-users
ipv4_available: int = 0 # remaining free pool (where published)
ipv4_total_prefixes: int = 0 # count of distinct IPv4 records
ipv6_allocated: int = 0 # /48 equivalents
ipv6_total_prefixes: int = 0
asn_allocated: int = 0
asn_total: int = 0
stats_date: Optional[str] = None
source: str = "NRO Delegation Stats"
errors: List[str] = Field(default_factory=list)
class GlobalIPv4Stats(BaseModel):
"""Aggregated IPv4/IPv6/ASN stats across all 5 RIRs."""
queried_at: str
rirs: List[RIRDelegationStats] = Field(default_factory=list)
global_ipv4_prefixes: int = 0
global_ipv6_prefixes: int = 0
global_asns: int = 0
ipv4_blocks: List[IPv4DelegatedBlock] = Field(default_factory=list)
blocks_total: int = 0
blocks_returned: int = 0
blocks_limit: Optional[int] = None
blocks_offset: Optional[int] = None
blocks_filters: dict[str, Any] = Field(default_factory=dict)
errors: List[str] = Field(default_factory=list)
class RelatedPrefix(BaseModel):
"""A prefix related to the queried one (parent, sibling, or child)."""
prefix: str
relationship: str # parent | more-specific | less-specific | sibling
announced: Optional[bool] = None
holder: Optional[str] = None
origin_asn: Optional[str] = None
class PrefixOverviewResult(BaseModel):
"""Rich overview of a prefix: holder, hierarchy, BGP status, related blocks."""
prefix: str
holder: Optional[str] = None
holder_handle: Optional[str] = None
rir: Optional[str] = None
country: Optional[str] = None
announced: Optional[bool] = None
announcing_asns: List[str] = Field(default_factory=list)
block_size_ips: Optional[int] = None
related_prefixes: List[RelatedPrefix] = Field(default_factory=list)
allocation_status: Optional[str] = None
source: str = "RIPE Stat"
errors: List[str] = Field(default_factory=list)
# ──────────────────────────────────────────────────────────────
# Input Models — Phase 4
# ──────────────────────────────────────────────────────────────
class PeeringInfoInput(BaseModel):
"""Input for PeeringDB lookup of an ASN."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
asn: str = Field(
...,
description=(
"Autonomous System Number to look up in PeeringDB "
"(e.g. 'AS13335', '13335', 'AS-CLOUDFLARE'). "
"Returns peering policy, IXP presence, and NOC contact."
),
min_length=1, max_length=20,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class IXPLookupInput(BaseModel):
"""Input for IXP lookup by country or name."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
query: str = Field(
...,
description=(
"Country code (e.g. 'MU', 'US', 'DE') or partial IXP name "
"(e.g. 'LINX', 'AMS-IX', 'Nairobi'). "
"Returns matching Internet Exchange Points with member counts."
),
min_length=1, max_length=60,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class NetworkHealthInput(BaseModel):
"""Input for the combined network health report."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
resource: str = Field(
...,
description=(
"IP address, prefix in CIDR notation, or ASN "
"(e.g. '1.1.1.1', '1.1.1.0/24', 'AS13335'). "
"Runs RDAP + BGP + RPKI + PeeringDB checks in parallel."
),
min_length=2, max_length=50,
)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="'markdown' for human-readable, 'json' for machine-readable",
)
class ChangeMonitorInput(BaseModel):
"""Input for session-scoped change monitoring."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
resource: str = Field(
...,
description=(
"IP prefix (e.g. '8.8.8.0/24') or ASN (e.g. 'AS15169') to monitor. "
"On first call, captures a baseline snapshot. "
"On subsequent calls, reports what changed since the baseline."
),
min_length=2, max_length=50,
)
reset_baseline: bool = Field(
default=False,
description=(
"If True, discard the existing baseline and capture a fresh snapshot. "
"Use this to reset monitoring after reviewing detected changes."
),
)
# ──────────────────────────────────────────────────────────────
# Output Models — Phase 4
# ──────────────────────────────────────────────────────────────
class IXPRecord(BaseModel):
"""A single Internet Exchange Point from PeeringDB."""
ix_id: Optional[int] = None
name: str = ""
name_long: Optional[str] = None
city: Optional[str] = None
country: Optional[str] = None
region: Optional[str] = None
website: Optional[str] = None
tech_email: Optional[str] = None
member_count: Optional[int] = None
speed_avg_mbps: Optional[int] = None
traffic_stats_url: Optional[str] = None
# For peering_info: the AS's local peering IP at this IX
ipaddr4: Optional[str] = None
ipaddr6: Optional[str] = None
speed: Optional[int] = None
class PeeringInfoResult(BaseModel):
"""PeeringDB record for an ASN including peering policy and IXP presence."""
asn: str
network_name: Optional[str] = None
aka: Optional[str] = None
website: Optional[str] = None
info_type: Optional[str] = None # NSP | Cable | Educational | ...
policy_general: Optional[str] = None # Open | Selective | Restrictive | No Peering
policy_locations: Optional[str] = None
policy_ratio: Optional[bool] = None
policy_contracts: Optional[str] = None
noc_email: Optional[str] = None
noc_phone: Optional[str] = None
abuse_email: Optional[str] = None
peering_email: Optional[str] = None
irr_as_set: Optional[str] = None # e.g. AS-CLOUDFLARE
info_prefixes4: Optional[int] = None # IPv4 prefixes announced
info_prefixes6: Optional[int] = None
ixp_presence: List[IXPRecord] = Field(default_factory=list)
neighbour_asns: List[str] = Field(default_factory=list)
source: str = "PeeringDB + RIPE Stat"
errors: List[str] = Field(default_factory=list)
class IXPLookupResult(BaseModel):
"""Results of an IXP search by country or name."""
query: str
total_found: int
ixps: List[IXPRecord] = Field(default_factory=list)
errors: List[str] = Field(default_factory=list)
class NetworkHealthResult(BaseModel):
"""Combined health report: RDAP + BGP + RPKI + PeeringDB."""
resource: str
resource_type: str # ip | prefix | asn
queried_at: str
# RDAP
rdap_holder: Optional[str] = None
rdap_rir: Optional[str] = None
rdap_country: Optional[str] = None
rdap_abuse_email: Optional[str] = None
rdap_status: Optional[str] = None
# BGP
bgp_announced: Optional[bool] = None
bgp_announcing_asns: List[str] = Field(default_factory=list)
bgp_visibility_pct: Optional[float]= None
# RPKI (only for prefix queries)
rpki_validity: Optional[str] = None # valid | invalid | not-found | unknown | N/A
# PeeringDB (only when an ASN is known)
peering_policy: Optional[str] = None
peering_ixp_count: Optional[int] = None
peering_noc_email: Optional[str] = None
# Overall health signal
health_signals: List[str] = Field(default_factory=list)
errors: List[str] = Field(default_factory=list)
class FieldDelta(BaseModel):
"""A single changed field in a change monitoring diff."""
field: str
old_value: Optional[str] = None
new_value: Optional[str] = None
changed_at: str = ""
class ChangeMonitorResult(BaseModel):
"""Result of comparing current state against a stored baseline."""
resource: str
resource_type: str
status: str # "baseline_created" | "changes_detected" | "no_changes"
baseline_captured_at: Optional[str]= None
checked_at: str = ""
changes: List[FieldDelta] = Field(default_factory=list)
current_holder: Optional[str] = None
current_rir: Optional[str] = None
message: str = ""
# ──────────────────────────────────────────────────────────────
# Input Models — Sprint 2 DNS
# ──────────────────────────────────────────────────────────────
class DNSResolveInput(BaseModel):
"""Input for forward/reverse DNS resolution."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
target: str = Field(
...,
description="IPv4/IPv6 address (reverse PTR lookup) or domain name (forward A/AAAA lookup)",
min_length=2, max_length=253,
)
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN)
class DNSEnumerateInput(BaseModel):
"""Input for full DNS record enumeration."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
domain: str = Field(
...,
description="Domain name to enumerate all DNS records for (e.g. 'cloudflare.com')",
min_length=3, max_length=253,
)
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN)
class DNSSECInput(BaseModel):
"""Input for DNSSEC chain validation."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
domain: str = Field(
...,
description="Domain name to validate DNSSEC chain for (e.g. 'cloudflare.com')",
min_length=3, max_length=253,
)
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN)
class DNSBLInput(BaseModel):
"""Input for DNS blocklist (DNSBL) check."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
ip: str = Field(
...,
description="IPv4 address to check against DNS blocklists (e.g. '1.2.3.4')",
min_length=7, max_length=15,
)
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN)
class EmailSecurityInput(BaseModel):
"""Input for email security record analysis."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
domain: str = Field(
...,
description="Domain name to audit for SPF, DMARC, DKIM, MX records (e.g. 'gmail.com')",
min_length=3, max_length=253,
)
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN)
class DNSPropagationInput(BaseModel):
"""Input for DNS propagation check across global resolvers."""
model_config = ConfigDict(str_strip_whitespace=True, validate_assignment=True, extra="forbid")
domain: str = Field(
...,
description="Domain name to check propagation for (e.g. 'example.com')",
min_length=3, max_length=253,
)
record_type: str = Field(
default="A",
description="DNS record type to check: A, AAAA, MX, NS, TXT, CNAME",
pattern="^(A|AAAA|MX|NS|TXT|CNAME|SOA)$",
)
response_format: ResponseFormat = Field(default=ResponseFormat.MARKDOWN)
# ──────────────────────────────────────────────────────────────
# Output Models — Sprint 2 DNS
# ──────────────────────────────────────────────────────────────
class DNSResolveResult(BaseModel):
"""Result of a forward or reverse DNS resolution."""
target: str
is_ip: bool = False # True = reverse lookup, False = forward
ptr_records: List[str] = Field(default_factory=list)
a_records: List[str] = Field(default_factory=list)
aaaa_records: List[str] = Field(default_factory=list)
rdap_org: Optional[str] = None # RDAP owner of the IP (correlation)
rdap_mismatch: bool = False # PTR hostname vs RDAP owner mismatch flag
errors: List[str] = Field(default_factory=list)
class DNSRecord(BaseModel):
"""A single DNS resource record."""
rtype: str
value: str
ttl: Optional[int] = None
class DNSEnumerateResult(BaseModel):
"""Full DNS record set for a domain."""
domain: str
records: dict[str, List[DNSRecord]] = Field(default_factory=dict)
spf_value: Optional[str] = None
dmarc_value: Optional[str] = None
errors: List[str] = Field(default_factory=list)
class DNSSECResult(BaseModel):
"""DNSSEC chain-of-trust validation result."""
domain: str
status: str = "INDETERMINATE" # SECURE|INSECURE|BOGUS|INDETERMINATE
has_dnskey: bool = False
has_rrsig: bool = False
has_ds: bool = False
signature_expiry: Optional[str] = None
errors: List[str] = Field(default_factory=list)
class DNSBLEntry(BaseModel):
"""Result of checking an IP against a single DNS blocklist."""
list_name: str
list_description: str = ""
listed: bool = False
return_code: Optional[str] = None
description: Optional[str] = None
class DNSBLResult(BaseModel):
"""Aggregated result of checking an IP against all DNS blocklists."""
ip: str
listed_count: int = 0
checked_count: int = 0
entries: List[DNSBLEntry] = Field(default_factory=list)
errors: List[str] = Field(default_factory=list)
class EmailSecurityResult(BaseModel):
"""Email security posture for a domain: SPF, DMARC, DKIM, MX."""
domain: str
spf_valid: bool = False
spf_record: Optional[str] = None
spf_all_mechanism: Optional[str] = None # +all | -all | ~all | ?all
dmarc_present: bool = False
dmarc_policy: Optional[str] = None # none | quarantine | reject
dmarc_rua: Optional[str] = None # aggregate report URI
dmarc_ruf: Optional[str] = None # forensic report URI
dkim_selectors_found: List[str] = Field(default_factory=list)
mx_records: List[str] = Field(default_factory=list)
bimi_present: bool = False
risk_level: str = "UNKNOWN" # LOW|MEDIUM|HIGH|CRITICAL
recommendations: List[str] = Field(default_factory=list)
errors: List[str] = Field(default_factory=list)
class PropagationEntry(BaseModel):
"""DNS resolution result from a single resolver."""
resolver_ip: str
resolver_name: str
region: str
response: List[str] = Field(default_factory=list)
matches_majority: bool = False
error: Optional[str] = None
class DNSPropagationResult(BaseModel):
"""DNS propagation check across 10 global resolvers."""
domain: str
record_type: str
majority_answer: List[str] = Field(default_factory=list)
consistent: bool = False
propagated_count: int = 0
total_resolvers: int = 0
entries: List[PropagationEntry] = Field(default_factory=list)
errors: List[str] = Field(default_factory=list)
# ============================================================
# Sprint 3 — TLS, CT Logs, Threat Intel, Passive DNS
# ============================================================
# ── F1: TLS Inspection ──────────────────────────────────────
class TLSInspectInput(BaseModel):
hostname: str
port: int = Field(default=443, ge=1, le=65535)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="Output format: 'markdown' (default) or 'json'",
)
class TLSCertResult(BaseModel):
hostname: str
port: int
subject: dict = Field(default_factory=dict)
issuer: dict = Field(default_factory=dict)
san: List[str] = Field(default_factory=list)
not_before: str = ""
not_after: str = ""
days_until_expiry: int = -1
expired: bool = True
self_signed: bool = False
cipher_suite: Optional[str] = None
protocol_version: Optional[str] = None
chain_length: int = 0
hsts: bool = False
hsts_max_age: Optional[int] = None
error: Optional[str] = None
# ── F2: Certificate Transparency Logs ───────────────────────
class CTLogInput(BaseModel):
domain: str
limit: int = Field(default=50, ge=1, le=500)
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="Output format: 'markdown' (default) or 'json'",
)
class CTLogEntry(BaseModel):
id: int = 0
issuer_cn: str = ""
common_name: str = ""
name_value: str = ""
not_before: str = ""
not_after: str = ""
entry_timestamp: Optional[str] = None
class CTLogResult(BaseModel):
domain: str
total_found: int = 0
returned: int = 0
entries: List[CTLogEntry] = Field(default_factory=list)
unique_issuers: List[str] = Field(default_factory=list)
error: Optional[str] = None
warning: Optional[str] = None
# ── G1: Threat Intelligence ──────────────────────────────────
class ThreatIntelInput(BaseModel):
ip: str
response_format: ResponseFormat = Field(
default=ResponseFormat.MARKDOWN,
description="Output format: 'markdown' (default) or 'json'",
)
class ThreatIntelResult(BaseModel):
ip: str
# Shodan InternetDB (free, no key required)
open_ports: List[int] = Field(default_factory=list)
vulnerabilities: List[str] = Field(default_factory=list)
hostnames: List[str] = Field(default_factory=list)
tags: List[str] = Field(default_factory=list)
# GreyNoise Community (optional — requires GREYNOISE_API_KEY env var)
noise: Optional[bool] = None # True = benign internet scanner
riot: Optional[bool] = None # True = known trusted service
classification: Optional[str] = None # malicious / benign / unknown
greynoise_name: Optional[str] = None
greynoise_link: Optional[str] = None
greynoise_error: Optional[str] = None
# Aggregated risk
risk_score: int = 0 # 0–100
risk_level: str = "LOW" # LOW / MEDIUM / HIGH / CRITICAL
shodan_error: Optional[str] = None