Skip to content

Commit b757a71

Browse files
authored
Merge pull request #49 from Team-MostWanted/feature/INFRA-333-add-basic-auth
INFRA-333 Add basic auth
2 parents 3b28efd + 5c64e23 commit b757a71

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) with the minor change that we use a prefix instead of grouping.
55
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.21.0] - 2025-04-17
8+
- Added: support added for basic auth
9+
710
## [1.20.0] - 2025-04-14
811
- Security: dependency and security updates
912
- Added: UPX compression

auth.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
func withBasicAuth(next interface{}) http.Handler {
8+
switch h := next.(type) {
9+
case http.Handler:
10+
return basicAuthHandler(h)
11+
case func(http.ResponseWriter, *http.Request):
12+
return basicAuthHandler(http.HandlerFunc(h))
13+
default:
14+
panic("[Auth] unsupported handler type")
15+
}
16+
}
17+
18+
func basicAuthHandler(next http.Handler) http.Handler {
19+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20+
username, password, ok := r.BasicAuth()
21+
22+
requiredUsername := config.server.authUser
23+
requiredPassword := config.server.authPW
24+
25+
if !ok || username != requiredUsername || password != requiredPassword {
26+
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
27+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
28+
return
29+
}
30+
31+
next.ServeHTTP(w, r)
32+
})
33+
}

config.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ type YamlConfig struct {
3333

3434
// YamlServerConfig mapping to Server part
3535
type YamlServerConfig struct {
36-
Host *string `yaml:"host"`
37-
Port *int `yaml:"port"`
36+
Host *string `yaml:"host"`
37+
Port *int `yaml:"port"`
38+
AuthUser *string `yaml:"auth_user"`
39+
AuthPW *string `yaml:"auth_pw"`
3840
}
3941

4042
// YamlProbeConfig mapping to Probes part
@@ -55,8 +57,10 @@ type YamlProbeConfig []struct {
5557

5658
type internalConfig struct {
5759
server struct {
58-
host string
59-
port int
60+
host string
61+
port int
62+
authUser string
63+
authPW string
6064
}
6165

6266
probes map[string]probeType
@@ -160,6 +164,22 @@ func configServer(serverConfig YamlServerConfig, fileName string) {
160164

161165
config.server.port = *serverConfig.Port
162166
}
167+
168+
if serverConfig.AuthUser != nil {
169+
if config.server.authUser != "" {
170+
log.Fatalf("Config failure 'authUser' is already set (%s)", fileName)
171+
}
172+
173+
config.server.authUser = *serverConfig.AuthUser
174+
}
175+
176+
if serverConfig.AuthPW != nil {
177+
if config.server.authPW != "" {
178+
log.Fatalf("Config failure 'authPW' is already set (%s)", fileName)
179+
}
180+
181+
config.server.authPW = *serverConfig.AuthPW
182+
}
163183
}
164184

165185
func configProbes(probesConfig YamlProbeConfig, fileName string) {

config/server.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
server:
44
host: 127.0.0.1 # ip used for listening, remove or leave empty for all available IP addresses
55
port: 8501 # port used for listening
6+
auth_user: authuser
7+
auth_pw: authpw

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ func main() {
2929
log.Info("Started on ", addr)
3030

3131
r := http.NewServeMux()
32-
33-
r.HandleFunc("/", landingPage)
34-
r.Handle("/metrics", promhttp.Handler())
32+
if config.server.authUser != "" && config.server.authPW != "" {
33+
r.Handle("/", withBasicAuth(landingPage))
34+
r.Handle("/metrics", withBasicAuth(promhttp.Handler()))
35+
} else {
36+
r.HandleFunc("/", landingPage)
37+
r.Handle("/metrics", promhttp.Handler())
38+
}
3539
r.HandleFunc("/probe", probe)
3640

3741
err := http.ListenAndServe(

0 commit comments

Comments
 (0)