Skip to content

Commit 691a40c

Browse files
Support path exclusion from basic authentication
Signed-off-by: heylongdacoder <[email protected]>
1 parent 0633342 commit 691a40c

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

docs/web-config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ tls_server_config:
1010
basic_auth_users:
1111
alice: $2y$10$mDwo.lAisC94iLAyP81MCesa29IzH37oigHC/42V2pdJlUprsJPze
1212
bob: $2y$10$hLqFl9jSjoAAy95Z/zw8Ye8wkdMBM8c5Bn1ptYqP/AXyV0.oy0S8m
13+
14+
# Exclude /-/healthy and /-/ready from basic authentication
15+
basic_auth_excluded_paths:
16+
- /-/healthy
17+
- /-/ready

docs/web-configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ http_server_config:
9797
# required. Passwords are hashed with bcrypt.
9898
basic_auth_users:
9999
[ <string>: <secret> ... ]
100+
101+
# Exclude URL path from basic authentication. For example, health check or
102+
# metrics endpoints.
103+
basic_auth_excluded_paths:
104+
[ - <string> ]
100105
```
101106

102107
[A sample configuration file](web-config.yml) is provided.

web/handler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ func (u *webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
102102
return
103103
}
104104

105+
for _, path := range c.AuthExcludedPaths {
106+
if path == r.URL.Path {
107+
u.handler.ServeHTTP(w, r)
108+
return
109+
}
110+
}
111+
105112
user, pass, auth := r.BasicAuth()
106113
if auth {
107114
hashedPassword, validUser := c.Users[user]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
basic_auth_excluded_paths:
2+
- /
3+
4+
basic_auth_users:
5+
alice: $2y$12$1DpfPeqF9HzHJt.EWswy1exHluGfbhnn3yXhR7Xes6m3WJqFg0Wby
6+
bob: $2y$18$4VeFDzXIoPHKnKTU3O3GH.N.vZu06CVqczYZ8WvfzrddFU6tGqjR.
7+
carol: $2y$10$qRTBuFoULoYNA7AQ/F3ck.trZBPyjV64.oA4ZsSBCIWvXuvQlQTuu
8+
dave: $2y$10$2UXri9cIDdgeKjBo4Rlpx.U3ZLDV8X1IxKmsfOvhcM5oXQt/mLmXq

web/tls_config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ var (
3434
)
3535

3636
type Config struct {
37-
TLSConfig TLSStruct `yaml:"tls_server_config"`
38-
HTTPConfig HTTPStruct `yaml:"http_server_config"`
39-
Users map[string]config_util.Secret `yaml:"basic_auth_users"`
37+
TLSConfig TLSStruct `yaml:"tls_server_config"`
38+
HTTPConfig HTTPStruct `yaml:"http_server_config"`
39+
Users map[string]config_util.Secret `yaml:"basic_auth_users"`
40+
AuthExcludedPaths []string `yaml:"basic_auth_excluded_paths"`
4041
}
4142

4243
type TLSStruct struct {

web/tls_config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ func TestUsers(t *testing.T) {
615615
Password: "dave123",
616616
ExpectedError: nil,
617617
},
618+
{
619+
Name: `with correct basic auth and exclude path`,
620+
YAMLConfigPath: "testdata/web_config_users_noTLS_excludePath.good.yml",
621+
ExpectedError: nil,
622+
},
618623
{
619624
Name: `without basic auth and TLS`,
620625
YAMLConfigPath: "testdata/web_config_users.good.yml",

0 commit comments

Comments
 (0)