Skip to content

Commit 2951d6a

Browse files
committed
only public host zones are targeted #1
1 parent dd0eb0b commit 2951d6a

File tree

5 files changed

+202
-71
lines changed

5 files changed

+202
-71
lines changed

export_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package goacm
2+
3+
var ExportedGetPublicHostedZoneIDByDomainName = getPublicHostedZoneIDByDomainName

goacm.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ func IssueCertificate(ctx context.Context, aAPI ACMAPI, rAPI Route53API, method,
180180
result.ValidationRecordName = *vRecordName
181181
result.ValidationRecordValue = *vRecordValue
182182

183-
lhzIn := route53.ListHostedZonesInput{}
184-
h, err := rAPI.ListHostedZones(ctx, &lhzIn)
183+
// allowed only public hosted zones
184+
hzID, err := getPublicHostedZoneIDByDomainName(ctx, rAPI, hostedDomain)
185185
if err != nil {
186186
errMsg := err.Error()
187187
if err := RollbackIssueCertificate(ctx, aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
@@ -192,14 +192,8 @@ func IssueCertificate(ctx context.Context, aAPI ACMAPI, rAPI Route53API, method,
192192
return IssueCertificateResult{}, errors.New(errMsg)
193193
}
194194

195-
hzID := ""
196-
for _, hz := range h.HostedZones {
197-
if *hz.Name == hostedDomain+"." {
198-
hzID = *hz.Id
199-
}
200-
}
201195
if hzID == "" {
202-
errMsg := "Cannot get hosted zone ID"
196+
errMsg := "Cannot get public hosted zone ID"
203197
if err := RollbackIssueCertificate(ctx, aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
204198
errMsg += fmt.Sprintf("; Failed to rollback to issue certificate: %v", err)
205199
} else {
@@ -252,18 +246,10 @@ func RollbackIssueCertificate(ctx context.Context, aAPI ACMAPI, rAPI Route53API,
252246

253247
// DeleteRoute53RecordSet deletes a Route 53 record set.
254248
func DeleteRoute53RecordSet(ctx context.Context, aAPI ACMAPI, rAPI Route53API, rs RecordSet) error {
255-
lhzIn := route53.ListHostedZonesInput{}
256-
h, err := rAPI.ListHostedZones(ctx, &lhzIn)
249+
hzID, err := getPublicHostedZoneIDByDomainName(ctx, rAPI, rs.HostedDomainName)
257250
if err != nil {
258251
return err
259252
}
260-
261-
hzID := ""
262-
for _, hz := range h.HostedZones {
263-
if aws.ToString(hz.Name) == rs.HostedDomainName+"." {
264-
hzID = aws.ToString(hz.Id)
265-
}
266-
}
267253
if hzID == "" {
268254
return errors.New("Cannot get hosted zone ID")
269255
}
@@ -316,3 +302,23 @@ func DeleteRoute53RecordSet(ctx context.Context, aAPI ACMAPI, rAPI Route53API, r
316302

317303
return nil
318304
}
305+
306+
// Get public hosted zone ID by domain name.
307+
// domainName is a string without a "." at the end.
308+
func getPublicHostedZoneIDByDomainName(ctx context.Context, rAPI Route53API, domainName string) (string, error) {
309+
dn := domainName + "."
310+
311+
lhzIn := route53.ListHostedZonesInput{}
312+
out, err := rAPI.ListHostedZones(ctx, &lhzIn)
313+
if err != nil {
314+
return "", err
315+
}
316+
317+
for _, hz := range out.HostedZones {
318+
if hz.Config != nil && !hz.Config.PrivateZone && *hz.Name == dn {
319+
return aws.ToString(hz.Id), nil
320+
}
321+
}
322+
323+
return "", nil
324+
}

0 commit comments

Comments
 (0)