mirror of
https://github.com/cert-manager/webhook-example.git
synced 2025-07-03 07:05:50 +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
28
main.go
28
main.go
|
@ -64,7 +64,10 @@ func (c *ibmCloudCisProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
longestMatchZone := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
longestMatchZone, err := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if longestMatchZone != nil {
|
if longestMatchZone != nil {
|
||||||
if err := c.createDNSChallengeRecord(crn, longestMatchZone.Id, ch); err != nil {
|
if err := c.createDNSChallengeRecord(crn, longestMatchZone.Id, ch); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -75,19 +78,27 @@ func (c *ibmCloudCisProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findLongestMatchingZone(zones []cis.Zone, fqdn string) *cis.Zone {
|
func findLongestMatchingZone(zones []cis.Zone, fqdn string) (*cis.Zone, error) {
|
||||||
var longestMatchZone *cis.Zone
|
var longestMatchZone *cis.Zone
|
||||||
var longestMatchLength int
|
var longestMatchLength int
|
||||||
|
var longestMatchIndex = -1
|
||||||
|
|
||||||
for _, zone := range zones {
|
for i, zone := range zones {
|
||||||
zoneNameWithDot := zone.Name + "."
|
zoneNameWithDot := zone.Name + "."
|
||||||
if strings.HasSuffix(fqdn, zoneNameWithDot) && len(zoneNameWithDot) > longestMatchLength {
|
if strings.HasSuffix(fqdn, zoneNameWithDot) && len(zoneNameWithDot) > longestMatchLength {
|
||||||
longestMatchLength = len(zoneNameWithDot)
|
longestMatchLength = len(zoneNameWithDot)
|
||||||
longestMatchZone = &zone
|
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 {
|
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,
|
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 {
|
if err != nil {
|
||||||
log.WithError(err).WithFields(log.Fields{"crn": crn, "zoneID": zoneID}).Error("Error creating DNS01 challenge")
|
log.WithError(err).WithFields(log.Fields{"crn": crn, "zoneID": zoneID}).Error("Error creating DNS01 challenge")
|
||||||
return err
|
return err
|
||||||
|
@ -123,7 +136,10 @@ func (c *ibmCloudCisProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
longestMatchZone := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
longestMatchZone, err := findLongestMatchingZone(myZones, ch.ResolvedFQDN)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if longestMatchZone != nil {
|
if longestMatchZone != nil {
|
||||||
if err := c.deleteMatchingTXTRecords(crn, longestMatchZone.Id, ch); err != nil {
|
if err := c.deleteMatchingTXTRecords(crn, longestMatchZone.Id, ch); err != nil {
|
||||||
log.WithError(err).Error("Error deleting TXT record")
|
log.WithError(err).Error("Error deleting TXT record")
|
||||||
|
|
Loading…
Reference in a new issue