Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Prometheus metrics #1299

Merged
merged 25 commits into from
Aug 23, 2024
Merged

[Feat] Prometheus metrics #1299

merged 25 commits into from
Aug 23, 2024

Conversation

AKEB
Copy link
Contributor

@AKEB AKEB commented Aug 19, 2024

Description

This update adds metrics for prometheus.
Dashboard for grafana can be imported by id 21733 or with JSON
WireGuard-1724075031304.json

Motivation and Context

#1285

How has this been tested?

On my server

Screenshots (if appropriate):

Screenshot at Aug 19 16-41-57
Screenshot at Aug 19 16-55-04

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@AKEB AKEB requested a review from pheiduck as a code owner August 19, 2024 18:07
@pheiduck
Copy link
Contributor

Dashboard for grafana can be imported by id 21733 or with JSON

Very nice!

@pheiduck pheiduck changed the title Prometheus metrics [Feat] Prometheus metrics Aug 19, 2024
@offizium-berndstorath
Copy link
Contributor

lgtm. but please stop fixing other stuff (like formating) completely unrelated to your feature

@AKEB
Copy link
Contributor Author

AKEB commented Aug 20, 2024

@AKEB any reason why closed and resubmitted?

I made a mistake in the commit commentary there. So I rearranged

@pheiduck pheiduck added the type: feature request New feature or request label Aug 20, 2024
@pheiduck pheiduck added this to the v15 milestone Aug 20, 2024
@pheiduck
Copy link
Contributor

lgtm. but please stop fixing other stuff (like formating) completely unrelated to your feature

Or even better split this into 2 PRs please! @AKEB

Vadim Babadzhanyan added 2 commits August 20, 2024 23:30
[Feat]: Simple Stats API wg-easy#1285
@offizium-berndstorath
Copy link
Contributor

Could also please post your prometheus config for other users to reference?

@AKEB
Copy link
Contributor Author

AKEB commented Aug 21, 2024

 - job_name: wireguard
    scrape_interval: 30s
    scrape_timeout: 20s
    static_configs:
    - targets:
      - 'localhost:51281'

https://github.com/AKEB/docker-composers/tree/main/grafana

@offizium-berndstorath
Copy link
Contributor

What if the user has authentication setup?

@pheiduck
Copy link
Contributor

pheiduck commented Aug 21, 2024

What if the user has authentication setup?

@offizium-berndstorath What kind of authentication setup do you mean? Authelia?

@offizium-berndstorath
Copy link
Contributor

offizium-berndstorath commented Aug 21, 2024

@offizium-berndstorath What kind of authentication setup do you mean? Authelia?

No I mean if Password in wg-easy is set. Right now Prometheus can't scrape the endpoint as it would require a valid session or the correct headers
@AKEB uses authentik according to his linked repo therefore he probably didn't test how it works if PASSWORD_HASH is set. (I could be completely wrong tho)

@AKEB
Copy link
Contributor Author

AKEB commented Aug 21, 2024

No I mean if Password in wg-easy is set. Right now Prometheus can't scrape the endpoint as it would require a valid session or the correct headers
@AKEB uses authentik according to his linked repo therefore he probably didn't test how it works if PASSWORD_HASH is set. (I could be completely wrong tho)

No. You're completely wrong. The password is only needed if the path contains /api/
And metrics for Prometheus are located in the path /metrics

@pheiduck
Copy link
Contributor

If the feature should be accepted a Rebase is needed, as I sayed before due, all new PRs should be at the top of older commits.

@offizium-berndstorath
Copy link
Contributor

No. You're completely wrong. The password is only needed if the path contains /api/ And metrics for Prometheus are located in the path /metrics

Yes ok, I think that that every route underneath the middleware is grouped logically to be handled with that middleware. But shouldn't the route be secured? Anybody just knowing your throughput, which clients are connected etc seems like a bad idea

@AKEB
Copy link
Contributor Author

AKEB commented Aug 22, 2024

Yes ok, I think that that every route underneath the middleware is grouped logically to be handled with that middleware. But shouldn't the route be secured? Anybody just knowing your throughput, which clients are connected etc seems like a bad idea

If you are afraid, you don't need to use it. Or you can do separately as suggested by other users on the net. There are many solutions on github. For example https://github.com/tolkonepiu/wg-easy-extended

@AKEB AKEB marked this pull request as draft August 22, 2024 08:37
@AKEB

This comment was marked as outdated.

Vadim Babadzhanyan added 2 commits August 22, 2024 11:53
Separate port for prometheus metrics
Add Prometheus metrics [Feat]: Simple Stats API wg-easy#1285
Separate port for prometheus metrics
Add Prometheus metrics [Feat]: Simple Stats API wg-easy#1285
@offizium-berndstorath
Copy link
Contributor

offizium-berndstorath commented Aug 22, 2024

I added a separate port for prometheus metrics. It's safer that way. You can keep it open, but give access only to prometheus itself.

Makes sense. This does increase security.

On a different note this feature will not work anymore with #1244 as this spins up a whole server just for prometheus which i think doesn't make much sense anyway.

Prometheus supports passing headers to the route, so I don't see a reason against hiding it behind auth and just extending the prometheus config a bit

@AKEB AKEB marked this pull request as draft August 22, 2024 09:30
@AKEB AKEB marked this pull request as ready for review August 22, 2024 11:22
@AKEB
Copy link
Contributor Author

AKEB commented Aug 22, 2024

@offizium-berndstorath are you happy now? )

@offizium-berndstorath
Copy link
Contributor

offizium-berndstorath commented Aug 22, 2024

@offizium-berndstorath are you happy now? )

Somewhat. i made some reviews. Also I dont really think theres a reason to add basic-auth package if prometheus supports e.g. bearer auth1 but im fine with the current implementation. but a extra password for prometheus seems like a good solution.
Im just most focused on easy of use and security

Footnotes

  1. https://prometheus.io/docs/prometheus/latest/configuration/configuration/#:~:text=authorization%3A%0A%20%20%23%20Sets%20the%20authentication%20type%20of%20the%20request.

README.md Outdated Show resolved Hide resolved
src/config.js Outdated Show resolved Hide resolved
Comment on lines +448 to +450
if (client.endpoint !== null) {
wireguardConnectedPeersCount++;
}
Copy link
Contributor

@offizium-berndstorath offizium-berndstorath Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to this pr but could this be used to check if client is online in frontend instead of latest handshake? #1305 (comment)

[Feat]: Simple Stats API wg-easy#1285
return { error: 'Not Logged In' };
}

if (req.url.startsWith('/metrics') && user.pass) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

req.url.startsWith('/metrics') is redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// WireGuard
    app.use(
      fromNodeMiddleware((req, res, next) => {
        if (!requiresPassword || !req.url.startsWith('/api/')) {
          return next();
        }

        if (req.session && req.session.authenticated) {
          return next();
        }

        if (req.url.startsWith('/api/') && req.headers['authorization']) {    # The Same
          if (isPasswordValid(req.headers['authorization'], PASSWORD_HASH)) {
            return next();
          }
          return res.status(401).json({           # This ERROR!!! 500
            error: 'Incorrect Password',
          });
        }

        return res.status(401).json({           # This ERROR!!! 500
          error: 'Not Logged In',
        });
      }),
    );

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could be right. I'll think about it later

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AKEB what did you mean with # This ERROR!!! 500?

Copy link
Contributor Author

@AKEB AKEB Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no res.status() function and at this point the code returns a status of 500 instead of 401
You need to do it like this:

res.statusCode = 401;
return { error: ‘Not Logged In’ };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood.
we are currently doing a rewrite using nuxt and typescript #1244 where this isn't the case. so i think its fine by now

@AKEB
Copy link
Contributor Author

AKEB commented Aug 22, 2024

@pheiduck Is there anything else you need from me?

@offizium-berndstorath
Copy link
Contributor

@AKEB i dont think. but i'll wait for his review

@AKEB AKEB requested a review from kaaax0815 as a code owner August 23, 2024 07:51
README.md Outdated Show resolved Hide resolved
@AKEB AKEB requested a review from kaaax0815 August 23, 2024 08:26
@kaaax0815 kaaax0815 mentioned this pull request Aug 23, 2024
14 tasks
README.md Outdated Show resolved Hide resolved
@kaaax0815 kaaax0815 merged commit 7be9884 into wg-easy:master Aug 23, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants