Skip to content

Commit b1c7b93

Browse files
committed
fix: findLongestMatchingZone now works as intended
previously it would always return the last zone
1 parent d498adc commit b1c7b93

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

main.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func (c *ibmCloudCisProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error
6464
continue
6565
}
6666

67-
longestMatchZone := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
67+
longestMatchZone, err := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
68+
if err != nil {
69+
return err
70+
}
6871
if longestMatchZone != nil {
6972
if err := c.createDNSChallengeRecord(crn, longestMatchZone.Id, ch); err != nil {
7073
return err
@@ -75,19 +78,27 @@ func (c *ibmCloudCisProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error
7578
return nil
7679
}
7780

78-
func findLongestMatchingZone(zones []cis.Zone, fqdn string) *cis.Zone {
79-
var longestMatchZone *cis.Zone
80-
var longestMatchLength int
81-
82-
for _, zone := range zones {
83-
zoneNameWithDot := zone.Name + "."
84-
if strings.HasSuffix(fqdn, zoneNameWithDot) && len(zoneNameWithDot) > longestMatchLength {
85-
longestMatchLength = len(zoneNameWithDot)
86-
longestMatchZone = &zone
87-
}
88-
}
89-
90-
return longestMatchZone
81+
func findLongestMatchingZone(zones []cis.Zone, fqdn string) (*cis.Zone, error) {
82+
var longestMatchZone *cis.Zone
83+
var longestMatchLength int
84+
var longestMatchIndex = -1
85+
86+
for i, zone := range zones {
87+
zoneNameWithDot := zone.Name + "."
88+
if strings.HasSuffix(fqdn, zoneNameWithDot) && len(zoneNameWithDot) > longestMatchLength {
89+
longestMatchLength = len(zoneNameWithDot)
90+
longestMatchIndex = i
91+
}
92+
}
93+
94+
if longestMatchIndex != -1 {
95+
longestMatchZone = &zones[longestMatchIndex]
96+
} else {
97+
log.Printf("No matching zone found")
98+
return nil, fmt.Errorf("No matching zone found for fqdn: %s", fqdn)
99+
}
100+
101+
return longestMatchZone, nil
91102
}
92103

93104
func (c *ibmCloudCisProviderSolver) createDNSChallengeRecord(crn, zoneID string, ch *v1alpha1.ChallengeRequest) error {
@@ -99,6 +110,8 @@ func (c *ibmCloudCisProviderSolver) createDNSChallengeRecord(crn, zoneID string,
99110
Content: ch.Key,
100111
})
101112

113+
log.Printf("Creating challenge TXT record %s (content: %s), crn: %s, zoneId: %s", ch.ResolvedFQDN, ch.Key, crn, zoneID)
114+
102115
if err != nil {
103116
log.WithError(err).WithFields(log.Fields{"crn": crn, "zoneID": zoneID}).Error("Error creating DNS01 challenge")
104117
return err
@@ -123,7 +136,10 @@ func (c *ibmCloudCisProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error
123136
continue
124137
}
125138

126-
longestMatchZone := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
139+
longestMatchZone, err := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
140+
if err != nil {
141+
return err
142+
}
127143
if longestMatchZone != nil {
128144
if err := c.deleteMatchingTXTRecords(crn, longestMatchZone.Id, ch); err != nil {
129145
log.WithError(err).Error("Error deleting TXT record")

0 commit comments

Comments
 (0)