-
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 4 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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package exporter | ||
|
||
import ( | ||
"crypto/subtle" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
@@ -12,6 +14,12 @@ | |
) | ||
|
||
func (e *Exporter) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
err := e.verifyBasicAuth(r.BasicAuth()) | ||
if err != nil { | ||
w.Header().Set("WWW-Authenticate", `Basic realm="redis-exporter, charset=UTF-8"`) | ||
return | ||
} | ||
|
||
e.mux.ServeHTTP(w, r) | ||
} | ||
|
||
|
@@ -107,3 +115,27 @@ | |
e.Unlock() | ||
_, _ = w.Write([]byte(`ok`)) | ||
} | ||
|
||
func (e *Exporter) isBasicAuthConfigured() bool { | ||
return e.options.BasicAuthUsername != "" && e.options.BasicAuthPassword != "" | ||
} | ||
|
||
func (e *Exporter) verifyBasicAuth(user, password string, authHeaderSet bool) error { | ||
|
||
if !e.isBasicAuthConfigured() { | ||
return nil | ||
} | ||
|
||
if !authHeaderSet { | ||
return errors.New("Unauthorized") | ||
} | ||
|
||
userCorrect := subtle.ConstantTimeCompare([]byte(user), []byte(e.options.BasicAuthUsername)) | ||
passCorrect := subtle.ConstantTimeCompare([]byte(password), []byte(e.options.BasicAuthPassword)) | ||
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. nit: looks like you're always using these as 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've decided to leave this as it is for now, I hope that's fine. |
||
|
||
if userCorrect == 0 || passCorrect == 0 { | ||
return errors.New("Unauthorized") | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,6 +393,135 @@ 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 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: you can combine this with line 18 for tighter scoping
if err := verifyBasicAuth(...); err != nil {
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.
Did so in b0aabba