Skip to content

Commit

Permalink
Use keys in headers for endpoints that allow this (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slavek Kabrda authored Nov 13, 2019
1 parent 43491ff commit 557151e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
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

0 comments on commit 557151e

Please sign in to comment.