Skip to content

Commit

Permalink
Use cloud.json to find out cloud-netblocks Fixes #6996 (#3946) (#2543)
Browse files Browse the repository at this point in the history
Co-authored-by: Çağatay Gürtürk <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Çağatay Gürtürk <[email protected]>
  • Loading branch information
modular-magician and cagataygurturk authored Oct 2, 2020
1 parent b4b8f86 commit 26738ae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .changelog/3946.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
netblock: changed `google_netblock_ip_ranges` to read from cloud.json file rather than DNS record
```
79 changes: 3 additions & 76 deletions google-beta/data_source_google_netblock_ip_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -58,9 +57,9 @@ func dataSourceGoogleNetblockIpRangesRead(d *schema.ResourceData, meta interface
switch rt {
// Dynamic ranges
case "cloud-netblocks":
// https://cloud.google.com/compute/docs/faq#where_can_i_find_product_name_short_ip_ranges
const CLOUD_NETBLOCK_DNS = "_cloud-netblocks.googleusercontent.com"
CidrBlocks, err := getCidrBlocksFromDns(CLOUD_NETBLOCK_DNS)
// https://cloud.google.com/compute/docs/faq#find_ip_range
const CLOUD_NETBLOCK_URL = "https://www.gstatic.com/ipranges/cloud.json"
CidrBlocks, err := getCidrBlocksFromUrl(CLOUD_NETBLOCK_URL)

if err != nil {
return err
Expand Down Expand Up @@ -164,78 +163,6 @@ func dataSourceGoogleNetblockIpRangesRead(d *schema.ResourceData, meta interface
return nil
}

func netblock_request(name string) (string, error) {
response, err := http.Get(fmt.Sprintf("https://dns.google.com/resolve?name=%s&type=TXT", name))

if err != nil {
return "", fmt.Errorf("Error from _cloud-netblocks: %s", err)
}

defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)

if err != nil {
return "", fmt.Errorf("Error to retrieve the domains list: %s", err)
}

return string(body), nil
}

func getCidrBlocksFromDns(netblock string) (map[string][]string, error) {
var dnsNetblockList []string
cidrBlocks := make(map[string][]string)

response, err := netblock_request(netblock)

if err != nil {
return nil, err
}

splitedResponse := strings.Split(response, " ")

for _, sp := range splitedResponse {
if strings.HasPrefix(sp, "include:") {
dnsNetblock := strings.Replace(sp, "include:", "", 1)
dnsNetblockList = append(dnsNetblockList, dnsNetblock)
}
}

for len(dnsNetblockList) > 0 {

dnsNetblock := dnsNetblockList[0]

dnsNetblockList[0] = ""
dnsNetblockList = dnsNetblockList[1:]

response, err = netblock_request(dnsNetblock)

if err != nil {
return nil, err
}

splitedResponse = strings.Split(response, " ")

for _, sp := range splitedResponse {
if strings.HasPrefix(sp, "ip4") {
cdrBlock := strings.Replace(sp, "ip4:", "", 1)
cidrBlocks["cidr_blocks_ipv4"] = append(cidrBlocks["cidr_blocks_ipv4"], cdrBlock)
cidrBlocks["cidr_blocks"] = append(cidrBlocks["cidr_blocks"], cdrBlock)

} else if strings.HasPrefix(sp, "ip6") {
cdrBlock := strings.Replace(sp, "ip6:", "", 1)
cidrBlocks["cidr_blocks_ipv6"] = append(cidrBlocks["cidr_blocks_ipv6"], cdrBlock)
cidrBlocks["cidr_blocks"] = append(cidrBlocks["cidr_blocks"], cdrBlock)

} else if strings.HasPrefix(sp, "include:") {
cidr_block := strings.Replace(sp, "include:", "", 1)
dnsNetblockList = append(dnsNetblockList, cidr_block)
}
}
}

return cidrBlocks, nil
}

func getCidrBlocksFromUrl(url string) (map[string][]string, error) {
cidrBlocks := make(map[string][]string)

Expand Down

0 comments on commit 26738ae

Please sign in to comment.