ansrivas / fiberprometheus

prometheus middleware for Fiber
MIT License
172 stars 37 forks source link

un auth metrics should return HTTP.STATUS=401 and not 2000 #171

Closed sysmat closed 1 year ago

sysmat commented 1 year ago
prometheus := fiberprometheus.New("my_server")
prometheus.RegisterAt(app, "/metrics", middleware.PrometheusAuthMiddleware(conf.Metric.User, conf.Metric.Pass))
app.Use(prometheus.Middleware)
return func(c *fiber.Ctx) error {
        authHeader := c.Get(fiber.HeaderAuthorization)

        // Parse the credentials from the Authorization header
        username, password, err := parseBasicAuth(authHeader)
        if err != nil {
            return fiber.ErrUnauthorized
        }

        // Check the username and password
        if username != user || password != pass {
            return fiber.ErrUnauthorized
        }

        return c.Next()
    }
sysmat commented 1 year ago
ansrivas commented 1 year ago

Hey @sysmat I tried to reproduce this on my end, but it seems to be working just fine:

package main

import (
    "github.com/ansrivas/fiberprometheus/v2"
    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    prometheus := fiberprometheus.New("my_server")
    prometheus.RegisterAt(app, "/metrics", func(c *fiber.Ctx) error {
        return fiber.ErrUnauthorized
    })
    app.Use(prometheus.Middleware)

    app.Listen(":3000")
}

And then I invoked it like this:

✦ ❯ curl -v http://localhost:3000/metrics

*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /metrics HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.0.1
> Accept: */*
> 
< HTTP/1.1 401 Unauthorized    <--------------------------------------------
< Date: Fri, 16 Jun 2023 14:35:20 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 12
< 
* Connection #0 to host localhost left intact
Unauthorized
sysmat commented 1 year ago
prometheus.RegisterAt(app, "/metrics", func(c *fiber.Ctx) error {
        return fiber.ErrUnauthorized
    })
sysmat commented 1 year ago