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 Prometheus bearer token only when provided #39

Merged
merged 2 commits into from
Nov 30, 2023
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
6 changes: 5 additions & 1 deletion prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func (bat authTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if bat.username != "" {
req.SetBasicAuth(bat.username, bat.password)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bat.token))

if bat.token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bat.token))
}

return bat.Transport.RoundTrip(req)
}

Expand Down
51 changes: 51 additions & 0 deletions prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prometheus

import (
"encoding/base64"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -33,6 +34,56 @@ var _ = Describe("Tests for Prometheus", func() {
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})

It("Test2 bearer header is used when token is provided", func() {
url := "https://example.com/api"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
_, err = bat.RoundTrip(req)
Expect(req.Header.Get("Authorization")).To(Equal("Bearer someRandomToken"))
//Asserting no of times mocks are called
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})

It("Test3 basic auth header is used when no token is provided", func() {
bat.token = ""
url := "https://example.com/api"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
_, err = bat.RoundTrip(req)

encodedAuthHeader := base64.StdEncoding.EncodeToString([]byte("someRandomUsername:someRandomPassword"))

Expect(req.Header.Get("Authorization")).To(Equal("Basic " + encodedAuthHeader))
//Asserting no of times mocks are called
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})

It("Test4 no auth header set when auth details are omitted", func() {
bat.token = ""
bat.username = ""
bat.password = ""
url := "https://example.com/api"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
_, err = bat.RoundTrip(req)

Expect(req.Header.Get("Authorization")).To(Equal(""))
//Asserting no of times mocks are called
Expect(count).To(BeEquivalentTo(0))
Expect(err).To(BeNil())
})
})

Context("Tests for NewClient()", func() {
Expand Down
Loading