Skip to content
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

Use keys in headers for endpoints that allow this #284

Merged
merged 3 commits into from
Nov 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
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func (client *Client) Validate() (bool, error) {
if err != nil {
return false, err
}
req.Header.Set("DD-API-KEY", client.apiKey)
req.Header.Set("DD-APPLICATION-KEY", client.appKey)

resp, err = client.doRequestWithRetries(req, client.RetryTimeout)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions integration/series_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package integration

import (
"testing"

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

func TestSeriesSubmit(t *testing.T) {
metrics := []dd.Metric{{
Metric: dd.String("test.metric"),
Points: []dd.DataPoint{{dd.Float64(1.0), dd.Float64(2.0)}},
Type: dd.String("gauge"),
Host: dd.String("myhost"),
Tags: []string{"some:tag"},
Unit: dd.String("unit"),
}}

err := client.PostMetrics(metrics)
if err != nil {
t.Fatalf("Posting metrics failed when it shouldn't. (%s)", err)
}
}
19 changes: 17 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ type Response struct {
Error string `json:"error"`
}

func (client *Client) apiAcceptsKeysInHeaders(api string) bool {
for _, prefix := range []string{"/v1/series", "/v1/check_run", "/v1/events", "/v1/screen"} {
if strings.HasPrefix(api, prefix) {
return false
}
}
return true
}

// 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.
Expand All @@ -40,8 +49,10 @@ func (client *Client) uriForAPI(api string) (string, error) {
return "", err
}
q := apiBase.Query()
q.Add("api_key", client.apiKey)
q.Add("application_key", client.appKey)
if !client.apiAcceptsKeysInHeaders(api) {
q.Add("api_key", client.apiKey)
q.Add("application_key", client.appKey)
}
apiBase.RawQuery = q.Encode()
return apiBase.String(), nil
}
Expand Down Expand Up @@ -233,6 +244,10 @@ func (client *Client) createRequest(method, api string, reqbody interface{}) (*h
if err != nil {
return nil, err
}
if client.apiAcceptsKeysInHeaders(api) {
req.Header.Set("DD-API-KEY", client.apiKey)
req.Header.Set("DD-APPLICATION-KEY", client.appKey)
}
if bodyReader != nil {
req.Header.Add("Content-Type", "application/json")
}
Expand Down
44 changes: 44 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

var needKeysInQueryParams = []string{"/v1/series", "/v1/check_run", "/v1/events", "/v1/screen"}

func TestUriForApi(t *testing.T) {
c := Client{
apiKey: "sample_api_key",
Expand All @@ -28,6 +31,47 @@ func TestUriForApi(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "https://base.datadoghq.com/api/v1/events?api_key=sample_api_key&application_key=sample_app_key", uri)
})
t.Run("Test all endpoints that need keys in query params", func(t *testing.T) {
for _, api := range needKeysInQueryParams {
uri, err := c.uriForAPI(api)
assert.Nil(t, err)
parsed, err := url.Parse(uri)
assert.Nil(t, err)
assert.Equal(t, parsed.Query().Get("api_key"), "sample_api_key")
assert.Equal(t, parsed.Query().Get("application_key"), "sample_app_key")
}
})
t.Run("Test an endpoint that doesn't need keys in query params", func(t *testing.T) {
uri, err := c.uriForAPI("/v1/dashboard")
assert.Nil(t, err)
assert.Equal(t, "https://base.datadoghq.com/api/v1/dashboard", uri)
})
}

func TestCreateRequest(t *testing.T) {
c := Client{
apiKey: "sample_api_key",
appKey: "sample_app_key",
baseUrl: "https://base.datadoghq.com",
HttpClient: &http.Client{},
RetryTimeout: 1000,
}
t.Run("Test an endpoint that doesn't need keys in query params", func(t *testing.T) {
req, err := c.createRequest("GET", "/v1/dashboard", nil)
assert.Nil(t, err)
assert.Equal(t, "sample_api_key", req.Header.Get("DD-API-KEY"))
assert.Equal(t, "sample_app_key", req.Header.Get("DD-APPLICATION-KEY"))
})
t.Run("Test endpoints that need keys in query params", func(t *testing.T) {
for _, api := range needKeysInQueryParams {
req, err := c.createRequest("GET", api, nil)
assert.Nil(t, err)
// we make sure that we *don't* have keys in query params, because some endpoints
// fail if we send keys both in headers and query params
assert.Equal(t, "", req.Header.Get("DD-API-KEY"))
assert.Equal(t, "", req.Header.Get("DD-APPLICATION-KEY"))
}
})
}

func TestRedactError(t *testing.T) {
Expand Down