Repository navigation
monitoring: harden Grafana and keep non-P2P ports local #507
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
445e0a4
monitoring: harden Grafana and keep non-P2P ports local
OisinKyne e02bb01
monitoring: let Alloy list networks through the socket proxy
OisinKyne ad83fb4
ci: address SonarCloud findings in the exposure check and security sc…
OisinKyne f8d49fc
ci: quote the pip command so the workflow parses
OisinKyne be95aa3
ci: limit the tests workflow token to read-only contents
OisinKyne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #!/usr/bin/env python3 | ||
| """Fails if the default configuration exposes more than the P2P ports to the internet. | ||
|
|
||
| - Every port published by a top-level compose file must, with its default values, either bind to | ||
| a loopback address or be a P2P port. | ||
| - Anonymous Grafana users must not get more than the Viewer role. | ||
| """ | ||
|
|
||
| import configparser | ||
| import glob | ||
| import re | ||
| import sys | ||
|
|
||
| import yaml | ||
|
|
||
| # Container ports that must be reachable by peers: EL, CL (incl. Prysm's 13000/12000) and charon. | ||
| P2P_PORTS = {"30303", "9000", "12000", "13000", "3610"} | ||
| LOOPBACK = {"127.0.0.1", "::1", "[::1]"} | ||
|
|
||
|
|
||
| def expand_defaults(value): | ||
| """Replaces ${VAR:-default} and ${VAR-default} with the default, and ${VAR} with an empty string.""" | ||
| return re.sub(r"\$\{\w+(?::?-([^}]*))?\}", lambda m: m.group(1) or "", value) | ||
|
|
||
|
|
||
| def check_port(where, port): | ||
| """Returns an error if the port is published on all interfaces by default and is not a P2P port.""" | ||
| if not isinstance(port, str): | ||
| return f"{where}: use the short port syntax so this check can read it: {port}" | ||
| parts = expand_defaults(port).split("/")[0].rsplit(":", 2) | ||
| host_ip = parts[0] if len(parts) == 3 else "" | ||
| if host_ip in LOOPBACK or parts[-1] in P2P_PORTS: | ||
| return None | ||
| return f"{where}: '{port}' is published on all interfaces by default; bind it to 127.0.0.1" | ||
|
|
||
|
|
||
| def check_ports(): | ||
| errors = [] | ||
| for path in sorted(glob.glob("*.yml")): | ||
| with open(path) as f: | ||
| compose = yaml.safe_load(f) or {} | ||
| for name, service in (compose.get("services") or {}).items(): | ||
| for port in (service or {}).get("ports") or []: | ||
| errors.append(check_port(f"{path}: {name}", port)) | ||
| return [error for error in errors if error] | ||
|
|
||
|
|
||
| def check_grafana(): | ||
| ini = configparser.ConfigParser(interpolation=None) | ||
| ini.read("grafana/grafana.ini") | ||
| anonymous = ini["auth.anonymous"] if ini.has_section("auth.anonymous") else {} | ||
| if anonymous.get("enabled", "false").lower() == "true" and anonymous.get("org_role", "Viewer") != "Viewer": | ||
| return [f"grafana/grafana.ini: anonymous users get the {anonymous['org_role']} role; use Viewer"] | ||
| return [] | ||
|
|
||
|
|
||
| def main(): | ||
| errors = check_ports() + check_grafana() | ||
| for error in errors: | ||
| print(f"::error::{error}") | ||
| if not errors: | ||
| print("Only P2P ports are exposed by default and anonymous Grafana access is read-only.") | ||
| return 1 if errors else 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,35 @@ | ||
| # Grafana is published on 127.0.0.1 by default (see MONITORING_IP_GRAFANA). To view it from | ||
| # another machine use an SSH tunnel: ssh -L 3000:127.0.0.1:3000 <user>@<node> | ||
|
|
||
| # Anonymous users can view the provisioned dashboards but cannot change anything. | ||
| [auth.anonymous] | ||
| enabled = true | ||
| org_role = Admin | ||
| org_role = Viewer | ||
|
|
||
| # No password logins: dashboards and datasources are provisioned from files in this repo. | ||
| # This also disables the factory admin:admin account, including on existing data/grafana databases. | ||
| [auth] | ||
| disable_login_form = true | ||
|
|
||
| [auth.basic] | ||
| enabled = false | ||
|
|
||
| [users] | ||
| allow_sign_up = false | ||
| allow_org_create = false | ||
|
|
||
| [security] | ||
| disable_gravatar = true | ||
| cookie_samesite = strict | ||
| # Only allow the datasource proxy to reach the provisioned monitoring backends. | ||
| data_source_proxy_whitelist = prometheus:9090 loki:3100 tempo:3200 | ||
|
|
||
| [plugins] | ||
| plugin_admin_enabled = false | ||
| preinstall_disabled = true | ||
|
|
||
| [public_dashboards] | ||
| enabled = false | ||
|
|
||
| [snapshots] | ||
| enabled = false |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.