-
Notifications
You must be signed in to change notification settings - Fork 879
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
Add basic authentication #967
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,6 +393,239 @@ func TestReloadHandlers(t *testing.T) { | |
} | ||
} | ||
|
||
func TestIsBasicAuthConfigured(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
tests := []struct { | ||
name string | ||
username string | ||
password string | ||
want bool | ||
}{ | ||
{ | ||
name: "no credentials configured", | ||
username: "", | ||
password: "", | ||
want: false, | ||
}, | ||
{ | ||
name: "only username configured", | ||
username: "user", | ||
password: "", | ||
want: false, | ||
}, | ||
{ | ||
name: "only password configured", | ||
username: "", | ||
password: "pass", | ||
want: false, | ||
}, | ||
{ | ||
name: "both credentials configured", | ||
username: "user", | ||
password: "pass", | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e, _ := NewRedisExporter("", Options{ | ||
BasicAuthUsername: tt.username, | ||
BasicAuthPassword: tt.password, | ||
}) | ||
|
||
if got := e.isBasicAuthConfigured(); got != tt.want { | ||
t.Errorf("isBasicAuthConfigured() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestVerifyBasicAuth(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
configUser string | ||
configPass string | ||
providedUser string | ||
providedPass string | ||
authHeaderSet bool | ||
wantErr bool | ||
wantErrString string | ||
}{ | ||
{ | ||
name: "no auth configured - no credentials provided", | ||
configUser: "", | ||
configPass: "", | ||
providedUser: "", | ||
providedPass: "", | ||
authHeaderSet: false, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "auth configured - no auth header", | ||
configUser: "user", | ||
configPass: "pass", | ||
providedUser: "", | ||
providedPass: "", | ||
authHeaderSet: false, | ||
wantErr: true, | ||
wantErrString: "Unauthorized", | ||
}, | ||
{ | ||
name: "auth configured - correct credentials", | ||
configUser: "user", | ||
configPass: "pass", | ||
providedUser: "user", | ||
providedPass: "pass", | ||
authHeaderSet: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "auth configured - wrong username", | ||
configUser: "user", | ||
configPass: "pass", | ||
providedUser: "wronguser", | ||
providedPass: "pass", | ||
authHeaderSet: true, | ||
wantErr: true, | ||
wantErrString: "Unauthorized", | ||
}, | ||
{ | ||
name: "auth configured - wrong password", | ||
configUser: "user", | ||
configPass: "pass", | ||
providedUser: "user", | ||
providedPass: "wrongpass", | ||
authHeaderSet: true, | ||
wantErr: true, | ||
wantErrString: "Unauthorized", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e, _ := NewRedisExporter("", Options{ | ||
BasicAuthUsername: tt.configUser, | ||
BasicAuthPassword: tt.configPass, | ||
}) | ||
|
||
err := e.verifyBasicAuth(tt.providedUser, tt.providedPass, tt.authHeaderSet) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with having a test that calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review! I've addressed the missing tests and added a proper status code for failed authentication in b0aabba |
||
|
||
if (err != nil) != tt.wantErr { | ||
t.Errorf("verifyBasicAuth() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
|
||
if err != nil && err.Error() != tt.wantErrString { | ||
t.Errorf("verifyBasicAuth() error = %v, wantErrString %v", err, tt.wantErrString) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestBasicAuth(t *testing.T) { | ||
if os.Getenv("TEST_REDIS_URI") == "" { | ||
t.Skipf("TEST_REDIS_URI not set - skipping") | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
username string | ||
password string | ||
configUsername string | ||
configPassword string | ||
wantStatusCode int | ||
}{ | ||
{ | ||
name: "No auth configured - no credentials provided", | ||
username: "", | ||
password: "", | ||
configUsername: "", | ||
configPassword: "", | ||
wantStatusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "Auth configured - correct credentials", | ||
username: "testuser", | ||
password: "testpass", | ||
configUsername: "testuser", | ||
configPassword: "testpass", | ||
wantStatusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "Auth configured - wrong username", | ||
username: "wronguser", | ||
password: "testpass", | ||
configUsername: "testuser", | ||
configPassword: "testpass", | ||
wantStatusCode: http.StatusUnauthorized, | ||
}, | ||
{ | ||
name: "Auth configured - wrong password", | ||
username: "testuser", | ||
password: "wrongpass", | ||
configUsername: "testuser", | ||
configPassword: "testpass", | ||
wantStatusCode: http.StatusUnauthorized, | ||
}, | ||
{ | ||
name: "Auth configured - no credentials provided", | ||
username: "", | ||
password: "", | ||
configUsername: "testuser", | ||
configPassword: "testpass", | ||
wantStatusCode: http.StatusUnauthorized, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e, _ := NewRedisExporter(os.Getenv("TEST_REDIS_URI"), Options{ | ||
Namespace: "test", | ||
Registry: prometheus.NewRegistry(), | ||
BasicAuthUsername: tt.configUsername, | ||
BasicAuthPassword: tt.configPassword, | ||
}) | ||
ts := httptest.NewServer(e) | ||
defer ts.Close() | ||
|
||
client := &http.Client{} | ||
req, err := http.NewRequest("GET", ts.URL+"/metrics", nil) | ||
if err != nil { | ||
t.Fatalf("Failed to create request: %v", err) | ||
} | ||
|
||
if tt.username != "" || tt.password != "" { | ||
req.SetBasicAuth(tt.username, tt.password) | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
t.Fatalf("Failed to send request: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != tt.wantStatusCode { | ||
t.Errorf("Expected status code %d, got %d", tt.wantStatusCode, resp.StatusCode) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatalf("Failed to read response body: %v", err) | ||
} | ||
|
||
if tt.wantStatusCode == http.StatusOK { | ||
if !strings.Contains(string(body), "test_up") { | ||
t.Errorf("Expected body to contain 'test_up', got: %s", string(body)) | ||
} | ||
} else { | ||
if !strings.Contains(resp.Header.Get("WWW-Authenticate"), "Basic realm=\"redis-exporter") { | ||
t.Errorf("Expected WWW-Authenticate header, got: %s", resp.Header.Get("WWW-Authenticate")) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func downloadURL(t *testing.T, u string) string { | ||
_, res := downloadURLWithStatusCode(t, u) | ||
return res | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: looks like you're always using these as
byte[]
instead of string so might as well just move the conversion into the "setter" in main.go:206/207There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've decided to leave this as it is for now, I hope that's fine.