This repository was archived by the owner on May 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +48
-11
lines changed Expand file tree Collapse file tree 7 files changed +48
-11
lines changed Original file line number Diff line number Diff line change 1414 Github
1515 Slack
1616 Webhook
17+ Prometheus
1718 }
1819
1920 Server struct {
@@ -57,6 +58,10 @@ type (
5758 Webhook struct {
5859 WebhookSecret string `split_words:"true"`
5960 }
61+
62+ Prometheus struct {
63+ PrometheusAuthSecret string `split_words:"true"`
64+ }
6065)
6166
6267func NewConfigFromEnv () (* Config , error ) {
Original file line number Diff line number Diff line change @@ -91,11 +91,12 @@ func newServerConfig(c *Config) *server.ServerConfig {
9191 }
9292
9393 return & server.ServerConfig {
94- Host : c .ServerHost ,
95- Proto : c .ServerProto ,
96- ProxyHost : proxyHost ,
97- ProxyProto : proxyProto ,
98- WebhookSecret : c .WebhookSecret ,
94+ Host : c .ServerHost ,
95+ Proto : c .ServerProto ,
96+ ProxyHost : proxyHost ,
97+ ProxyProto : proxyProto ,
98+ WebhookSecret : c .WebhookSecret ,
99+ PrometheusAuthSecret : c .PrometheusAuthSecret ,
99100 }
100101}
101102
Original file line number Diff line number Diff line change 1+ # GITPLOY_PROMETHEUS_AUTH_SECRET
2+
3+ Optional string value to authorize the scrape request from the Prometheus. * It authorizes with the ` Authorization ` header on request.*
4+
5+ ```
6+ GITPLOY_PROMETHEUS_AUTH_SECRET=92e6c41f002e71bf84e6c6b02e4c1e1b
7+ ```
Original file line number Diff line number Diff line change 33Optional string value to create an http-signature for the webhook. The webhook recipient use this secret to verify request authenticity.
44
55```
6- GITPLOY_WEBHOOK_SECRET=asd212fuas2lfjxye
6+ GITPLOY_WEBHOOK_SECRET=ae354839ad94078b9ea125eec4874370
77```
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ Index of server configuration settings:
99* [ GITPLOY_GITHUB_SCOPES] ( ./GITPLOY_GITHUB_SCOPES.md )
1010* [ GITPLOY_LICENSE] ( ./GITPLOY_LICENSE.md )
1111* [ GITPLOY_MEMBER_ENTRIES] ( ./GITPLOY_MEMBER_ENTRIES.md )
12+ * [ GITPLOY_PROMETHEUS_AUTH_SECRET] ( ./GITPLOY_PROMETHEUS_AUTH_SECRET.md )
1213* [ GITPLOY_ORGANIZATION_ENTRIES] ( ./GITPLOY_ORGANIZATION_ENTRIES.md )
1314* [ GITPLOY_PROXY_SERVER_HOST] ( ./GITPLOY_PROXY_SERVER_HOST.md )
1415* [ GITPLOY_PROXY_SERVER_PROTO] ( ./GITPLOY_PROXY_SERVER_PROTO.md )
Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ package metrics
22
33import (
44 "context"
5+ "net/http"
6+ "strings"
57 "time"
68
79 "github.com/gin-gonic/gin"
@@ -12,11 +14,18 @@ import (
1214 "github.com/gitploy-io/gitploy/ent"
1315)
1416
17+ const (
18+ headerAuth = "Authorization"
19+ )
20+
1521type (
16- Metric struct {}
22+ Metric struct {
23+ prometheusAuthSecret string
24+ }
1725
1826 MetricConfig struct {
1927 Interactor
28+ PrometheusAuthSecret string
2029 }
2130
2231 collector struct {
@@ -34,10 +43,22 @@ func NewMetric(c *MetricConfig) *Metric {
3443 newCollector (c .Interactor ),
3544 )
3645
37- return & Metric {}
46+ return & Metric {
47+ prometheusAuthSecret : c .PrometheusAuthSecret ,
48+ }
3849}
3950
4051func (m * Metric ) CollectMetrics (c * gin.Context ) {
52+ if m .prometheusAuthSecret != "" {
53+ if value := strings .TrimPrefix (
54+ c .GetHeader (headerAuth ),
55+ "Bearer " ,
56+ ); m .prometheusAuthSecret != value {
57+ c .Status (http .StatusUnauthorized )
58+ return
59+ }
60+ }
61+
4162 h := promhttp .Handler ()
4263 h .ServeHTTP (c .Writer , c .Request )
4364}
Original file line number Diff line number Diff line change 4141 ProxyHost string
4242 ProxyProto string
4343
44- WebhookSecret string
44+ WebhookSecret string
45+ PrometheusAuthSecret string
4546 }
4647
4748 SCMType string
@@ -196,9 +197,10 @@ func NewRouter(c *RouterConfig) *gin.Engine {
196197 metricsapi := r .Group ("/metrics" )
197198 {
198199 m := metrics .NewMetric (& metrics.MetricConfig {
199- Interactor : c .Interactor ,
200+ Interactor : c .Interactor ,
201+ PrometheusAuthSecret : c .PrometheusAuthSecret ,
200202 })
201- metricsapi .GET ("" , mw . OnlyAuthorized (), m .CollectMetrics )
203+ metricsapi .GET ("" , m .CollectMetrics )
202204 }
203205
204206 r .HEAD ("/slack" , func (gc * gin.Context ) {
You can’t perform that action at this time.
0 commit comments