Skip to content

Commit 39da711

Browse files
authored
Merge pull request #45 from jacobyoby/fix/issue-17-snmp-credential-precedence
Use longest-prefix matching for SNMP credentials
2 parents 58aa686 + dd61ec9 commit 39da711

3 files changed

Lines changed: 127 additions & 11 deletions

File tree

‎NetDiagram-PS/NetDiagram-PS.psm1‎

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ function Get-SnmpNeighbors {
337337
Uses credential map to resolve community strings from SecretManagement.
338338
Adds discovered edges to the topology with L2-SNMP confidence.
339339
340+
CREDENTIAL MATCHING: The most specific matching IPv4 CIDR wins regardless of
341+
JSON property order. Default is used only when no CIDR matches. If the selected
342+
entry has no available community secret, the node is skipped unless -TryPublic
343+
was explicitly supplied.
344+
340345
NODE ELIGIBILITY: By default every node is queried EXCEPT nodes that have been
341346
explicitly marked unreachable (Reachable -eq $false). Nodes with unknown
342347
reachability (Reachable -eq $null, e.g. straight after Import-Inventory) ARE
@@ -453,16 +458,14 @@ All SNMP attempts will be logged to verbose output.
453458
$source = 'CredentialMap'
454459

455460
if ($credMap.PSObject.Properties['snmp'] -and $credMap.snmp) {
456-
foreach ($cidrEntry in $credMap.snmp.PSObject.Properties) {
457-
$cidr = $cidrEntry.Name
458-
$config = $cidrEntry.Value
459-
460-
# Simple CIDR matching (for MVP, just match Default or exact match)
461-
if ($cidr -eq 'Default' -or (Test-IPInSubnet -IP $node.IP -CIDR $cidr)) {
462-
if ($config.PSObject.Properties['communitySecret']) {
463-
$communitySecret = $config.communitySecret
464-
break
465-
}
461+
$credentialMatch = Resolve-SnmpCredentialConfig -SnmpMap $credMap.snmp -IPAddress $node.IP
462+
if ($credentialMatch) {
463+
$source = "CredentialMap:$($credentialMatch.CIDR)"
464+
if ($credentialMatch.Config.PSObject.Properties['communitySecret']) {
465+
$communitySecret = $credentialMatch.Config.communitySecret
466+
}
467+
else {
468+
Write-Warning "SNMP credential entry '$($credentialMatch.CIDR)' has no communitySecret; skipping configured credential for $($node.IP)"
466469
}
467470
}
468471
}
@@ -573,6 +576,57 @@ All SNMP attempts will be logged to verbose output.
573576
}
574577
}
575578

579+
function Resolve-SnmpCredentialConfig {
580+
param(
581+
[Parameter(Mandatory)][psobject]$SnmpMap,
582+
[Parameter(Mandatory)][string]$IPAddress
583+
)
584+
585+
$defaultConfig = $null
586+
$cidrMatches = @()
587+
588+
foreach ($entry in $SnmpMap.PSObject.Properties) {
589+
if ($entry.Name -eq 'Default') {
590+
$defaultConfig = $entry.Value
591+
continue
592+
}
593+
594+
if ($entry.Name -notmatch '^(\d{1,3}(?:\.\d{1,3}){3})\/(\d{1,2})$') {
595+
throw "Invalid SNMP credential-map CIDR '$($entry.Name)'. Expected an IPv4 CIDR or Default."
596+
}
597+
598+
$prefixLength = [int]$matches[2]
599+
$networkAddress = $null
600+
if ($prefixLength -gt 32 -or
601+
-not [System.Net.IPAddress]::TryParse($matches[1], [ref]$networkAddress) -or
602+
$networkAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork) {
603+
throw "Invalid SNMP credential-map CIDR '$($entry.Name)'. Expected an IPv4 CIDR or Default."
604+
}
605+
606+
if (Test-IPInSubnet -IP $IPAddress -CIDR $entry.Name) {
607+
$cidrMatches += [pscustomobject]@{
608+
CIDR = $entry.Name
609+
PrefixLength = $prefixLength
610+
Config = $entry.Value
611+
}
612+
}
613+
}
614+
615+
$bestMatch = $cidrMatches | Sort-Object PrefixLength -Descending | Select-Object -First 1
616+
if ($bestMatch) {
617+
return $bestMatch
618+
}
619+
if ($null -ne $defaultConfig) {
620+
return [pscustomobject]@{
621+
CIDR = 'Default'
622+
PrefixLength = -1
623+
Config = $defaultConfig
624+
}
625+
}
626+
627+
return $null
628+
}
629+
576630
function Test-IPInSubnet {
577631
<#
578632
.SYNOPSIS

‎examples/credmap.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"_instructions": {
1818
"setup": "1. Install: Install-Module Microsoft.PowerShell.SecretManagement",
1919
"store_secret": "2. Store secrets: Set-Secret -Name 'YourSNMPSecretName' -Secret 'your-community-string'",
20-
"usage": "3. Use: Get-SnmpNeighbors -CredentialMapPath .\\examples\\credmap.json"
20+
"usage": "3. Use: Get-SnmpNeighbors -CredentialMapPath .\\examples\\credmap.json",
21+
"matching": "The most specific matching IPv4 CIDR wins. Default is used only when no CIDR matches.",
22+
"missing_secret": "A missing or unavailable selected secret skips that node unless -TryPublic is explicitly supplied."
2123
}
2224
}

‎tests/NetDiagram.Tests.ps1‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,66 @@ Describe 'Test-IPInSubnet (private helper)' {
494494
}
495495
}
496496

497+
Describe 'SNMP credential-map precedence (#17 regression)' {
498+
It 'Uses a matching subnet even when Default appears first' {
499+
InModuleScope 'NetDiagram-PS' {
500+
$map = '{"Default":{"communitySecret":"fallback"},"192.168.1.0/24":{"communitySecret":"office"}}' | ConvertFrom-Json
501+
$match = Resolve-SnmpCredentialConfig -SnmpMap $map -IPAddress '192.168.1.40'
502+
503+
$match.CIDR | Should -Be '192.168.1.0/24'
504+
$match.Config.communitySecret | Should -Be 'office'
505+
}
506+
}
507+
508+
It 'Uses a matching subnet when Default appears last' {
509+
InModuleScope 'NetDiagram-PS' {
510+
$map = '{"192.168.1.0/24":{"communitySecret":"office"},"Default":{"communitySecret":"fallback"}}' | ConvertFrom-Json
511+
$match = Resolve-SnmpCredentialConfig -SnmpMap $map -IPAddress '192.168.1.40'
512+
513+
$match.CIDR | Should -Be '192.168.1.0/24'
514+
}
515+
}
516+
517+
It 'Chooses the longest matching prefix' {
518+
InModuleScope 'NetDiagram-PS' {
519+
$map = '{"10.0.0.0/16":{"communitySecret":"site"},"10.0.4.0/24":{"communitySecret":"floor"}}' | ConvertFrom-Json
520+
$match = Resolve-SnmpCredentialConfig -SnmpMap $map -IPAddress '10.0.4.25'
521+
522+
$match.CIDR | Should -Be '10.0.4.0/24'
523+
$match.Config.communitySecret | Should -Be 'floor'
524+
}
525+
}
526+
527+
It 'Falls back to Default when no subnet matches' {
528+
InModuleScope 'NetDiagram-PS' {
529+
$map = '{"10.0.0.0/8":{"communitySecret":"internal"},"Default":{"communitySecret":"fallback"}}' | ConvertFrom-Json
530+
$match = Resolve-SnmpCredentialConfig -SnmpMap $map -IPAddress '192.168.1.40'
531+
532+
$match.CIDR | Should -Be 'Default'
533+
$match.Config.communitySecret | Should -Be 'fallback'
534+
}
535+
}
536+
537+
It 'Rejects an invalid CIDR entry' {
538+
InModuleScope 'NetDiagram-PS' {
539+
$map = '{"192.168.999.0/24":{"communitySecret":"bad"}}' | ConvertFrom-Json
540+
541+
{ Resolve-SnmpCredentialConfig -SnmpMap $map -IPAddress '192.168.1.40' } |
542+
Should -Throw '*Invalid SNMP credential-map CIDR*'
543+
}
544+
}
545+
546+
It 'Returns the selected entry when its secret name is missing' {
547+
InModuleScope 'NetDiagram-PS' {
548+
$map = '{"192.168.1.0/24":{"version":"v2c"},"Default":{"communitySecret":"fallback"}}' | ConvertFrom-Json
549+
$match = Resolve-SnmpCredentialConfig -SnmpMap $map -IPAddress '192.168.1.40'
550+
551+
$match.CIDR | Should -Be '192.168.1.0/24'
552+
$match.Config.PSObject.Properties['communitySecret'] | Should -BeNullOrEmpty
553+
}
554+
}
555+
}
556+
497557
Describe 'Get-SnmpNeighbors node eligibility (#1 regression)' {
498558
BeforeEach {
499559
# Empty credential map is valid; -TryPublic supplies the community so no

0 commit comments

Comments
 (0)