From c00d937f4c3e69245c988ccc5cbe4293c441ad32 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Wed, 19 Feb 2025 17:28:13 +0800 Subject: [PATCH 1/3] feat(monitor): add basic-auth feature for protected sites this closes [issue #316](https://github.com/glanceapp/glance/issues/316) Furthermore, this could be expanded to also pass the configured basic auth credentials to the request when the user clicks on the specific monitor widget --- internal/glance/widget-monitor.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/glance/widget-monitor.go b/internal/glance/widget-monitor.go index 76f0d45..ee4cb85 100644 --- a/internal/glance/widget-monitor.go +++ b/internal/glance/widget-monitor.go @@ -28,6 +28,10 @@ type monitorWidget struct { StatusText string `yaml:"-"` StatusStyle string `yaml:"-"` AltStatusCodes []int `yaml:"alt-status-codes"` + BasicAuth struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + } `yaml:"basic-auth"` } `yaml:"sites"` Style string `yaml:"style"` ShowFailingOnly bool `yaml:"show-failing-only"` @@ -45,6 +49,10 @@ func (widget *monitorWidget) update(ctx context.Context) { for i := range widget.Sites { requests[i] = widget.Sites[i].SiteStatusRequest + if widget.Sites[i].BasicAuth.Username != "" || widget.Sites[i].BasicAuth.Password != "" { + requests[i].Username = widget.Sites[i].BasicAuth.Username + requests[i].Password = widget.Sites[i].BasicAuth.Password + } } statuses, err := fetchStatusForSites(requests) @@ -118,6 +126,8 @@ type SiteStatusRequest struct { DefaultURL string `yaml:"url"` CheckURL string `yaml:"check-url"` AllowInsecure bool `yaml:"allow-insecure"` + Username string `yaml:"-"` + Password string `yaml:"-"` } type siteStatus struct { @@ -141,6 +151,10 @@ func fetchSiteStatusTask(statusRequest *SiteStatusRequest) (siteStatus, error) { }, nil } + if statusRequest.Username != "" || statusRequest.Password != "" { + request.SetBasicAuth(statusRequest.Username, statusRequest.Password) + } + ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) defer cancel() request = request.WithContext(ctx) From 5b45751c67497ae00bd96cff1cd2538165764e77 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Wed, 19 Feb 2025 17:40:56 +0800 Subject: [PATCH 2/3] docs(monitor): add documentation for basic-auth feature --- docs/configuration.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 844d34d..b14c568 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1476,6 +1476,7 @@ Properties for each site: | allow-insecure | boolean | no | false | | same-tab | boolean | no | false | | alt-status-codes | array | no | | +| basic-auth | object | no | | `title` @@ -1524,6 +1525,16 @@ alt-status-codes: - 403 ``` +`basic-auth` + +HTTP Basic Authentication credentials for protected sites. + +```yaml +basic-auth: + usename: your-username + password: your-password +``` + ### Releases Display a list of latest releases for specific repositories on Github, GitLab, Codeberg or Docker Hub. From dac0d15e785fea5843fc5a0aee8e2e6d880bf13c Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Sat, 22 Feb 2025 13:29:00 +0000 Subject: [PATCH 3/3] Update implementation --- internal/glance/widget-monitor.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/internal/glance/widget-monitor.go b/internal/glance/widget-monitor.go index ee4cb85..e42a710 100644 --- a/internal/glance/widget-monitor.go +++ b/internal/glance/widget-monitor.go @@ -28,10 +28,6 @@ type monitorWidget struct { StatusText string `yaml:"-"` StatusStyle string `yaml:"-"` AltStatusCodes []int `yaml:"alt-status-codes"` - BasicAuth struct { - Username string `yaml:"username"` - Password string `yaml:"password"` - } `yaml:"basic-auth"` } `yaml:"sites"` Style string `yaml:"style"` ShowFailingOnly bool `yaml:"show-failing-only"` @@ -49,10 +45,6 @@ func (widget *monitorWidget) update(ctx context.Context) { for i := range widget.Sites { requests[i] = widget.Sites[i].SiteStatusRequest - if widget.Sites[i].BasicAuth.Username != "" || widget.Sites[i].BasicAuth.Password != "" { - requests[i].Username = widget.Sites[i].BasicAuth.Username - requests[i].Password = widget.Sites[i].BasicAuth.Password - } } statuses, err := fetchStatusForSites(requests) @@ -126,8 +118,10 @@ type SiteStatusRequest struct { DefaultURL string `yaml:"url"` CheckURL string `yaml:"check-url"` AllowInsecure bool `yaml:"allow-insecure"` - Username string `yaml:"-"` - Password string `yaml:"-"` + BasicAuth struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + } `yaml:"basic-auth"` } type siteStatus struct { @@ -151,8 +145,8 @@ func fetchSiteStatusTask(statusRequest *SiteStatusRequest) (siteStatus, error) { }, nil } - if statusRequest.Username != "" || statusRequest.Password != "" { - request.SetBasicAuth(statusRequest.Username, statusRequest.Password) + if statusRequest.BasicAuth.Username != "" || statusRequest.BasicAuth.Password != "" { + request.SetBasicAuth(statusRequest.BasicAuth.Username, statusRequest.BasicAuth.Password) } ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)