@@ -54,11 +54,11 @@ public async Task<bool> CreateRecordAsync(string recordName, string txtValue)
5454 {
5555 var cleanName = recordName . TrimEnd ( '.' ) ;
5656
57- // Extract the zone from the record name
58- var zoneName = ExtractZoneFromRecord ( cleanName ) ;
59- _logger ? . LogDebug ( "[Infoblox] Extracted zone: {ZoneName} from record: {RecordName}" , zoneName , cleanName ) ;
57+ // Find the authoritative zone for this record
58+ var zoneName = await FindAuthoritativeZoneAsync ( cleanName ) ;
59+ _logger ? . LogDebug ( "[Infoblox] Found authoritative zone: {ZoneName} for record: {RecordName}" , zoneName , cleanName ) ;
6060
61- // Verify zone exists first
61+ // Verify zone exists (already checked in FindAuthoritativeZoneAsync, but verify one more time for safety)
6262 var zoneExists = await VerifyZoneExistsAsync ( zoneName ) ;
6363 if ( ! zoneExists )
6464 {
@@ -228,7 +228,7 @@ public async Task<bool> DeleteRecordAsync(string recordName)
228228 }
229229 }
230230
231- private string ExtractZoneFromRecord ( string recordName )
231+ private async Task < string > FindAuthoritativeZoneAsync ( string recordName )
232232 {
233233 if ( string . IsNullOrWhiteSpace ( recordName ) )
234234 return string . Empty ;
@@ -237,8 +237,27 @@ private string ExtractZoneFromRecord(string recordName)
237237 if ( parts . Length < 2 )
238238 return recordName ;
239239
240- // Use last two labels as default zone: e.g., "keyfactortestb.com"
241- return string . Join ( "." , parts . Skip ( parts . Length - 2 ) ) ;
240+ // Try to find the zone by checking from most specific to least specific
241+ // For "_acme-challenge.hello.keyfactortestb.com", try:
242+ // 1. hello.keyfactortestb.com
243+ // 2. keyfactortestb.com
244+ // Skip the first part (_acme-challenge) as it's the record itself
245+ for ( int i = 1 ; i < parts . Length - 1 ; i ++ )
246+ {
247+ var candidateZone = string . Join ( "." , parts . Skip ( i ) ) ;
248+ _logger ? . LogDebug ( "[Infoblox] Checking if zone exists: {ZoneName}" , candidateZone ) ;
249+
250+ if ( await VerifyZoneExistsAsync ( candidateZone ) )
251+ {
252+ _logger ? . LogDebug ( "[Infoblox] Found authoritative zone: {ZoneName}" , candidateZone ) ;
253+ return candidateZone ;
254+ }
255+ }
256+
257+ // Fallback: use last two labels as default zone
258+ var fallbackZone = string . Join ( "." , parts . Skip ( parts . Length - 2 ) ) ;
259+ _logger ? . LogDebug ( "[Infoblox] No specific zone found, using fallback: {ZoneName}" , fallbackZone ) ;
260+ return fallbackZone ;
242261 }
243262
244263 private async Task < bool > VerifyZoneExistsAsync ( string zoneName )
0 commit comments