mirror of
https://github.com/cert-manager/webhook-example.git
synced 2025-07-01 22:35:49 +02:00
fix: findLongestMatchingZone now works as intended
previously it would always return the last zone
This commit is contained in:
parent
d498adc4fc
commit
b1c7b931a9
1 changed files with 29 additions and 13 deletions
42
main.go
42
main.go
|
@ -64,7 +64,10 @@ func (c *ibmCloudCisProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error
|
|||
continue
|
||||
}
|
||||
|
||||
longestMatchZone := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
||||
longestMatchZone, err := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if longestMatchZone != nil {
|
||||
if err := c.createDNSChallengeRecord(crn, longestMatchZone.Id, ch); err != nil {
|
||||
return err
|
||||
|
@ -75,19 +78,27 @@ func (c *ibmCloudCisProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func findLongestMatchingZone(zones []cis.Zone, fqdn string) *cis.Zone {
|
||||
var longestMatchZone *cis.Zone
|
||||
var longestMatchLength int
|
||||
func findLongestMatchingZone(zones []cis.Zone, fqdn string) (*cis.Zone, error) {
|
||||
var longestMatchZone *cis.Zone
|
||||
var longestMatchLength int
|
||||
var longestMatchIndex = -1
|
||||
|
||||
for _, zone := range zones {
|
||||
zoneNameWithDot := zone.Name + "."
|
||||
if strings.HasSuffix(fqdn, zoneNameWithDot) && len(zoneNameWithDot) > longestMatchLength {
|
||||
longestMatchLength = len(zoneNameWithDot)
|
||||
longestMatchZone = &zone
|
||||
}
|
||||
}
|
||||
for i, zone := range zones {
|
||||
zoneNameWithDot := zone.Name + "."
|
||||
if strings.HasSuffix(fqdn, zoneNameWithDot) && len(zoneNameWithDot) > longestMatchLength {
|
||||
longestMatchLength = len(zoneNameWithDot)
|
||||
longestMatchIndex = i
|
||||
}
|
||||
}
|
||||
|
||||
return longestMatchZone
|
||||
if longestMatchIndex != -1 {
|
||||
longestMatchZone = &zones[longestMatchIndex]
|
||||
} else {
|
||||
log.Printf("No matching zone found")
|
||||
return nil, fmt.Errorf("No matching zone found for fqdn: %s", fqdn)
|
||||
}
|
||||
|
||||
return longestMatchZone, nil
|
||||
}
|
||||
|
||||
func (c *ibmCloudCisProviderSolver) createDNSChallengeRecord(crn, zoneID string, ch *v1alpha1.ChallengeRequest) error {
|
||||
|
@ -99,6 +110,8 @@ func (c *ibmCloudCisProviderSolver) createDNSChallengeRecord(crn, zoneID string,
|
|||
Content: ch.Key,
|
||||
})
|
||||
|
||||
log.Printf("Creating challenge TXT record %s (content: %s), crn: %s, zoneId: %s", ch.ResolvedFQDN, ch.Key, crn, zoneID)
|
||||
|
||||
if err != nil {
|
||||
log.WithError(err).WithFields(log.Fields{"crn": crn, "zoneID": zoneID}).Error("Error creating DNS01 challenge")
|
||||
return err
|
||||
|
@ -123,7 +136,10 @@ func (c *ibmCloudCisProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error
|
|||
continue
|
||||
}
|
||||
|
||||
longestMatchZone := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
||||
longestMatchZone, err := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if longestMatchZone != nil {
|
||||
if err := c.deleteMatchingTXTRecords(crn, longestMatchZone.Id, ch); err != nil {
|
||||
log.WithError(err).Error("Error deleting TXT record")
|
||||
|
|
Loading…
Reference in a new issue