8000 Add IP ranges by dabcoder · Pull Request #259 · zorkian/go-datadog-api · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add IP ranges #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions ip_ranges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2019 by authors and contributors.
*/

package datadog

import (
"fmt"
)

// IP ranges US: https://ip-ranges.datadoghq.com
// EU: https://ip-ranges.datadoghq.eu
// Same structure
type IPRangesResp struct {
Agents map[string][]string `json:"agents"`
API map[string][]string `json:"api"`
Apm map[string][]string `json:"apm"`
Logs map[string][]string `json:"logs"`
Process map[string][]string `json:"process"`
Synthetics map[string][]string `json:"synthetics"`
Webhooks map[string][]string `json:"webhooks"`
}

// GetIPRanges returns all IP addresses by section: agents, api, apm, logs, process, synthetics, webhooks
func (client *Client) GetIPRanges() (*IPRangesResp, error) {
var out IPRangesResp
urlIPRanges, err := client.URLIPRanges()
if err != nil {
return nil, fmt.Errorf("Error getting IP Ranges URL: %s", err)
}
if err := client.doJsonRequest("GET", urlIPRanges, nil, &out); err != nil {
return nil, err
}
return &out, nil
}
46 changes: 46 additions & 0 deletions ip_ranges_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2019 by authors and contributors.
*/

package datadog_test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
dd "github.com/zorkian/go-datadog-api"
)

func TestIPRangesSerialization(t *testing.T) {

var mapResponse map[string][]string
mapResponse = make(map[string][]string)

mapResponse["prefixes_ipv4"] = []string{"10.10.10.10/32", "10.10.10.10/32"}
mapResponse["prefixes_ipv6"] = []string{"2000:1900:0:100c::/128", "2000:1900:0:c100::/128"}

raw := `
{
"agents": {
"prefixes_ipv4": [
"10.10.10.10/32",
"10.10.10.10/32"
],
"prefixes_ipv6": [
"2000:1900:0:100c::/128",
"2000:1900:0:c100::/128"
]
}
}`

var ipranges dd.IPRangesResp
err := json.Unmarshal([]byte(raw), &ipranges)
assert.Equal(t, err, nil)
assert.Equal(t, ipranges.Agents["prefixes_ipv4"], mapResponse["prefixes_ipv4"])
assert.Equal(t, ipranges.Agents["prefixes_ipv6"], mapResponse["prefixes_ipv6"])
}
41 changes: 34 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,47 @@ type Response struct {
Error string `json:"error"`
}

// uriForAPI is to be called with something like "/v1/events" and it will give
// the proper request URI to be posted to.
// uriForAPI is to be called with either an API resource like "/v1/events"
// or a full URL like the IP Ranges one
// and it will give the proper request URI to be posted to.
func (client *Client) uriForAPI(api string) (string, error) {
apiBase, err := url.Parse(client.baseUrl + "/api" + api)
var err error
// If api is a URI such as /v1/hosts/, /v2/dashboards... add credentials and return a properly formatted URL
if !(strings.HasPrefix(api, "https://") || strings.HasPrefix(api, "http://")) {
apiBase, err := url.Parse(client.baseUrl + "/api" + api)
if err != nil {
return "", err
}
q := apiBase.Query()
q.Add("api_key", client.apiKey)
q.Add("application_key", client.appKey)
apiBase.RawQuery = q.Encode()
return apiBase.String(), nil
}
// if api is a generic URL we simply return it
apiBase, err := url.Parse(api)
if err != nil {
return "", err
}
q := apiBase.Query()
q.Add("api_key", client.apiKey)
q.Add(" 75FF application_key", client.appKey)
apiBase.RawQuery = q.Encode()
return apiBase.String(), nil
}

// URLIPRanges returns the IP Ranges URL used to whitelist IP addresses in use to send data to Datadog
// agents, api, apm, logs, process, synthetics, webhooks
func (client *Client) URLIPRanges() (string, error) {
baseURL := client.GetBaseUrl()
// Get the domain from the URL: eu, com...
domain := strings.Split(baseURL, ".")[2]
var urlIPRanges string
switch domain {
case "eu":
urlIPRanges = "https://ip-ranges.datadoghq.eu"
case "com":
urlIPRanges = "https://ip-ranges.datadoghq.com"
}
return urlIPRanges, nil
}

// redactError removes api and application keys from error strings
func (client *Client) redactError(err error) error {
if err == nil {
Expand Down
0