Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 9b758dd

Browse files
author
Noah Hanjun Lee
authored
Add a new configuration for the authorization of Prometheus (#159)
* Add a new configuration for the Prometheus auth secret * Add the doc
1 parent 49aad6f commit 9b758dd

File tree

7 files changed

+48
-11
lines changed

7 files changed

+48
-11
lines changed

cmd/server/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type (
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

6267
func NewConfigFromEnv() (*Config, error) {

cmd/server/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff 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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
```

docs/references/GITPLOY_WEBHOOK_SECRET.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
Optional 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
```

docs/references/configurations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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)

internal/server/metrics/metrics.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package metrics
22

33
import (
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+
1521
type (
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

4051
func (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
}

internal/server/router.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type (
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) {

0 commit comments

Comments
 (0)