Global credentials for HTTP basic auth, can be overrided by per-service creds
This commit is contained in:
parent
bc72f09198
commit
5db043bd26
5 changed files with 17 additions and 11 deletions
|
@ -14,6 +14,8 @@ type Config struct {
|
|||
IconSrc string
|
||||
Database string
|
||||
UnixSocket string
|
||||
BasicAuthUsername string
|
||||
BasicAuthPassword string
|
||||
Service []scanner.ServiceConfig
|
||||
Matrix MatrixConfig
|
||||
}
|
||||
|
|
4
main.go
4
main.go
|
@ -62,7 +62,7 @@ func index(w http.ResponseWriter, r *http.Request) {
|
|||
timeout := 3 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
data := scanner.CheckServiceBatch(ctx, config.Service)
|
||||
data := scanner.CheckServiceBatch(ctx, config.Service, config.BasicAuthUsername, config.BasicAuthPassword)
|
||||
non_duplicates, err := dbm.StoreServiceStatusBatch(data, time.Now())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
|
@ -232,7 +232,7 @@ func routineCheck(timeout time.Duration) error {
|
|||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
data := scanner.CheckServiceBatch(ctx, config.Service)
|
||||
data := scanner.CheckServiceBatch(ctx, config.Service, config.BasicAuthUsername, config.BasicAuthPassword)
|
||||
non_duplicates, err := dbm.StoreServiceStatusBatch(data, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
siteName = "Status Page"
|
||||
iconSrc = "https://exozy.me/img/logo.svg"
|
||||
# unixSocket = "/srv/http/status"
|
||||
# basicAuthUsername = "user"
|
||||
# basicAuthPassword = "pass"
|
||||
# database = "/opt/status-config/db"
|
||||
database = "/tmp/exozyme-status-test-db"
|
||||
|
||||
|
@ -9,7 +11,7 @@ service = [
|
|||
{ description = "drgns.space", url = "https://drgns.space" },
|
||||
{ description = "invalid.example.com", url = "https://invalid.example.com" },
|
||||
{ description = "port 1234", url = "tcp://localhost:1234" },
|
||||
{ description = "basic auth", url = "https://some-service.exozy.me/", user = "user", pass = "pass" },
|
||||
{ description = "basic auth", url = "https://some-service.exozy.me/", user = "overrideUser", pass = "overridePass" },
|
||||
]
|
||||
|
||||
[matrix]
|
||||
|
|
|
@ -6,14 +6,12 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func CheckHTTP200(ctx Context, url string, basicAuthUsername string, basicAuthPassword string) (bool, string) {
|
||||
func CheckHTTP200(ctx Context, url, basicAuthUsername string, basicAuthPassword string) (bool, string) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
if basicAuthUsername != "" || basicAuthPassword != "" {
|
||||
req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
|
||||
}
|
||||
req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
var tlsCertErr x509.CertificateInvalidError
|
||||
|
|
|
@ -32,14 +32,18 @@ type ServiceStatus struct {
|
|||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func CheckService(service ServiceConfig, ctx context.Context) ServiceStatus {
|
||||
func CheckService(service ServiceConfig, ctx context.Context, basicAuthUsername, basicAuthPassword string) ServiceStatus {
|
||||
var serviceResult bool
|
||||
var status string
|
||||
|
||||
url := service.URL()
|
||||
switch url.Scheme {
|
||||
case "http", "https":
|
||||
serviceResult, status = CheckHTTP200(ctx, url.String(), service.User, service.Pass)
|
||||
if service.User != "" && service.Pass != "" {
|
||||
serviceResult, status = CheckHTTP200(ctx, url.String(), service.User, service.Pass)
|
||||
} else {
|
||||
serviceResult, status = CheckHTTP200(ctx, url.String(), basicAuthUsername, basicAuthPassword)
|
||||
}
|
||||
case "tcp":
|
||||
// go includes port in host (url.Host is actually "authority")
|
||||
serviceResult, status = CheckTCPOpen(ctx, url.Host)
|
||||
|
@ -61,7 +65,7 @@ func CheckService(service ServiceConfig, ctx context.Context) ServiceStatus {
|
|||
|
||||
type ServiceStatusBatch []ServiceStatus
|
||||
|
||||
func CheckServiceBatch(ctx Context, services []ServiceConfig) ServiceStatusBatch {
|
||||
func CheckServiceBatch(ctx Context, services []ServiceConfig, basicAuthUsername, basicAuthPassword string) ServiceStatusBatch {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
var data []ServiceStatus = make([]ServiceStatus, len(services))
|
||||
|
@ -70,7 +74,7 @@ func CheckServiceBatch(ctx Context, services []ServiceConfig) ServiceStatusBatch
|
|||
wg.Add(1)
|
||||
go func(i int, config ServiceConfig) {
|
||||
defer wg.Done()
|
||||
data[i] = CheckService(config, ctx)
|
||||
data[i] = CheckService(config, ctx, basicAuthUsername, basicAuthPassword)
|
||||
}(i, v)
|
||||
}
|
||||
wg.Wait()
|
||||
|
|
Loading…
Reference in a new issue