Skip to content

Commit 759bc4d

Browse files
dmkepatryk
authored andcommitted
Add support for IDN zone names in Punycode form (cloudflare#348)
* Add support for IDN zone names in Punycode form fixes cloudflare#347 * vendor x/net/idna package
1 parent 6c86c20 commit 759bc4d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+39845
-8
lines changed

cloudflare.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func (api *API) SetAuthType(authType int) {
137137

138138
// ZoneIDByName retrieves a zone's ID from the name.
139139
func (api *API) ZoneIDByName(zoneName string) (string, error) {
140+
zoneName = normalizeZoneName(zoneName)
140141
res, err := api.ListZonesContext(context.TODO(), WithZoneFilter(zoneName))
141142
if err != nil {
142143
return "", errors.Wrap(err, "ListZonesContext command failed")
@@ -422,7 +423,7 @@ type reqOption struct {
422423
// WithZoneFilter applies a filter based on zone name.
423424
func WithZoneFilter(zone string) ReqOption {
424425
return func(opt *reqOption) {
425-
opt.params.Set("name", zone)
426+
opt.params.Set("name", normalizeZoneName(zone))
426427
}
427428
}
428429

cloudflare_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,150 @@ func TestZoneIDByNameWithNonUniqueZonesWithOrgId(t *testing.T) {
485485
assert.Equal(t, actual, "7c5dae5552338874e5053f2534d2767a")
486486
}
487487
}
488+
489+
func TestZoneIDByNameWithIDN(t *testing.T) {
490+
setup(UsingAccount("01a7362d577a6c3019a474fd6f485823"))
491+
defer teardown()
492+
493+
handler := func(w http.ResponseWriter, r *http.Request) {
494+
assert.Equal(t, r.Method, "GET", "Expected method 'GET', got %s", r.Method)
495+
w.Header().Set("content-type", "application/json")
496+
fmt.Fprintf(w, `{
497+
"success": true,
498+
"errors": [],
499+
"messages": [],
500+
"result": [
501+
{
502+
"id": "023e105f4ecef8ad9ca31a8372d0c353",
503+
"name": "example.com",
504+
"development_mode": 7200,
505+
"original_name_servers": [
506+
"ns1.originaldnshost.com",
507+
"ns2.originaldnshost.com"
508+
],
509+
"original_registrar": "GoDaddy",
510+
"original_dnshost": "NameCheap",
511+
"created_on": "2014-01-01T05:20:00.12345Z",
512+
"modified_on": "2014-01-01T05:20:00.12345Z",
513+
"owner": {
514+
"id": "7c5dae5552338874e5053f2534d2767a",
515+
"email": "[email protected]",
516+
"owner_type": "user"
517+
},
518+
"account": {
519+
"id": "e592fd9519420ba7405e1307bff33214",
520+
"name": "Another Demo Account"
521+
},
522+
"permissions": [
523+
"#zone:read",
524+
"#zone:edit"
525+
],
526+
"plan": {
527+
"id": "e592fd9519420ba7405e1307bff33214",
528+
"name": "Pro Plan",
529+
"price": 20,
530+
"currency": "USD",
531+
"frequency": "monthly",
532+
"legacy_id": "pro",
533+
"is_subscribed": true,
534+
"can_subscribe": true
535+
},
536+
"plan_pending": {
537+
"id": "e592fd9519420ba7405e1307bff33214",
538+
"name": "Pro Plan",
539+
"price": 20,
540+
"currency": "USD",
541+
"frequency": "monthly",
542+
"legacy_id": "pro",
543+
"is_subscribed": true,
544+
"can_subscribe": true
545+
},
546+
"status": "active",
547+
"paused": false,
548+
"type": "full",
549+
"name_servers": [
550+
"tony.ns.cloudflare.com",
551+
"woz.ns.cloudflare.com"
552+
]
553+
},
554+
{
555+
"id": "7c5dae5552338874e5053f2534d2767a",
556+
"name": "exämple.com",
557+
"development_mode": 7200,
558+
"original_name_servers": [
559+
"ns1.originaldnshost.com",
560+
"ns2.originaldnshost.com"
561+
],
562+
"original_registrar": "GoDaddy",
563+
"original_dnshost": "NameCheap",
564+
"created_on": "2014-01-01T05:20:00.12345Z",
565+
"modified_on": "2014-01-01T05:20:00.12345Z",
566+
"owner": {
567+
"id": "7c5dae5552338874e5053f2534d2767a",
568+
"email": "user@exämple.com",
569+
"owner_type": "user"
570+
},
571+
"account": {
572+
"id": "01a7362d577a6c3019a474fd6f485823",
573+
"name": "Demo Account"
574+
},
575+
"permissions": [
576+
"#zone:read",
577+
"#zone:edit"
578+
],
579+
"plan": {
580+
"id": "e592fd9519420ba7405e1307bff33214",
581+
"name": "Pro Plan",
582+
"price": 20,
583+
"currency": "USD",
584+
"frequency": "monthly",
585+
"legacy_id": "pro",
586+
"is_subscribed": true,
587+
"can_subscribe": true
588+
},
589+
"plan_pending": {
590+
"id": "e592fd9519420ba7405e1307bff33214",
591+
"name": "Pro Plan",
592+
"price": 20,
593+
"currency": "USD",
594+
"frequency": "monthly",
595+
"legacy_id": "pro",
596+
"is_subscribed": true,
597+
"can_subscribe": true
598+
},
599+
"status": "active",
600+
"paused": false,
601+
"type": "full",
602+
"name_servers": [
603+
"tony.ns.cloudflare.com",
604+
"woz.ns.cloudflare.com"
605+
]
606+
}
607+
],
608+
"result_info": {
609+
"page": 1,
610+
"per_page": 20,
611+
"count": 1,
612+
"total_count": 2000
613+
}
614+
}
615+
`)
616+
}
617+
618+
// `HandleFunc` doesn't handle query parameters so we just need to
619+
// handle the `/zones` endpoint instead.
620+
mux.HandleFunc("/zones", handler)
621+
622+
actual, err := client.ZoneIDByName("exämple.com")
623+
if assert.NoError(t, err) {
624+
assert.Equal(t, actual, "7c5dae5552338874e5053f2534d2767a")
625+
}
626+
627+
actual, err = client.ZoneIDByName("xn--exmple-cua.com")
628+
if assert.NoError(t, err) {
629+
assert.Equal(t, actual, "7c5dae5552338874e5053f2534d2767a")
630+
}
631+
632+
_, err = client.ZoneIDByName("xn--exmple-corruted.com")
633+
assert.Error(t, err)
634+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ require (
99
github.com/pkg/errors v0.8.1
1010
github.com/stretchr/testify v1.4.0
1111
github.com/urfave/cli v1.22.1
12+
golang.org/x/net v0.0.0-20190926025831-c00fd9afed17
1213
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0
1314
)

go.sum

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
1717
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
1818
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
1919
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20-
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
21-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
20+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2221
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
23-
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
24-
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
2522
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
2623
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
27-
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
28-
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
24+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
25+
golang.org/x/net v0.0.0-20190926025831-c00fd9afed17 h1:qPnAdmjNA41t3QBTx2mFGf/SD1IoslhYu7AmdsVzCcs=
26+
golang.org/x/net v0.0.0-20190926025831-c00fd9afed17/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
27+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
28+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
29+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2930
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 h1:xQwXv67TxFo9nC1GJFyab5eq/5B590r6RlnL/G8Sz7w=
3031
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
32+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3133
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
3235
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

vendor/golang.org/x/net/AUTHORS

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/CONTRIBUTORS

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/PATENTS

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)