mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
Part 1 of #7030. GetSeries (/api/v1/series) and GetLabels (/api/v1/labels, /api/v1/label/<name>/values) answer over the metrics the OTLP endpoint ingests, with the labels, selector syntax, limits and Prometheus envelope the CloudWatch user guide's PromQL pages give. Regular expressions are RE2's, as Prometheus's are. The guide's "IAM permissions for PromQL" table is vendored by a new fetcher and read by the authorized-actions generator, so the operations are checked as cloudwatch:ListMetrics, and QueryMetrics carries cloudwatch:ListMetrics as a companion every request needs, which the enforcement filter checks. @IamOperation can name a method's operation. The OTLP 40 KB metadata limit counts the resource and scope schema URLs, which PromQL reads as labels of the series.
121 lines
6.8 KiB
Python
121 lines
6.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for fetch_cloudwatch_promql_permissions.py, on pages in the Amazon CloudWatch user guide's markup."""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
import fetch_cloudwatch_promql_permissions as f
|
|
|
|
|
|
def row(path: str, methods: str, *actions: str) -> str:
|
|
return (f'<tr><td tabindex="-1"><p><code class="code">{path}</code></p></td>'
|
|
f'<td tabindex="-1"><p>{methods}</p></td>'
|
|
f'<td tabindex="-1"><p>{", ".join(f"<code class=\"code\">{action}</code>" for action in actions)}</p></td>'
|
|
f'</tr>')
|
|
|
|
|
|
VALUES = '/api/v1/label/<code class="replaceable">label_name</code>/values'
|
|
ROWS = [row("/api/v1/query", "GET, POST", "cloudwatch:GetMetricData", "cloudwatch:ListMetrics"),
|
|
row("/api/v1/series", "GET, POST", "cloudwatch:ListMetrics"),
|
|
row("/api/v1/labels", "GET, POST", "cloudwatch:ListMetrics"),
|
|
row(VALUES, "GET", "cloudwatch:ListMetrics")]
|
|
|
|
|
|
def permissions_page(rows: list[str] = ROWS, heading: str = "IAM permissions for PromQL",
|
|
headers: tuple[str, ...] = ("API endpoint path", "HTTP methods", "Required actions")) -> str:
|
|
return (f'<h2 id="CloudWatch-PromQL-IAM">{heading}</h2><p>To execute PromQL queries, you need both.</p>'
|
|
f'<table id="d0e4"><thead><tr>{"".join(f"<th>{header}</th>" for header in headers)}</tr></thead>'
|
|
f'{"".join(rows)}</table>')
|
|
|
|
|
|
INDEX = ('<p><a href="./CloudWatch-PromQL-API-QueryMetrics.html">QueryMetrics</a></p>'
|
|
'<p><a href="./CloudWatch-PromQL-API-GetSeries.html">GetSeries</a></p>'
|
|
'<p><a href="./CloudWatch-PromQL-API-QueryMetrics.html">QueryMetrics again</a></p>')
|
|
|
|
|
|
def operation_page(title: str, term: str, *uris: str) -> str:
|
|
paragraphs = "".join(f'<p><code class="code">{uri}</code>, what it does.</p>' for uri in uris)
|
|
return (f'<h1 class="topictitle" id="x">{title}</h1><dl><dt><span class="term">Valid HTTP verbs</span></dt>'
|
|
f'<dd><p><code class="code">GET</code></p></dd><dt><span class="term">{term}</span></dt>'
|
|
f'<dd>{paragraphs}</dd></dl>')
|
|
|
|
|
|
def pages(query: str | None = None) -> dict[str, str]:
|
|
return {"CloudWatch-PromQL-API-QueryMetrics.html": query or operation_page("QueryMetrics", "Valid URIs",
|
|
"/api/v1/query"),
|
|
"CloudWatch-PromQL-API-GetSeries.html": operation_page("GetSeries", "Valid URI", "/api/v1/series")}
|
|
|
|
|
|
def test_the_table_is_copied_row_by_row_with_a_placeholder_kept_in_braces():
|
|
built = f.build(permissions_page(), INDEX, pages())
|
|
assert built["permissions"] == [
|
|
{"path": "/api/v1/query", "methods": ["GET", "POST"],
|
|
"actions": ["cloudwatch:GetMetricData", "cloudwatch:ListMetrics"]},
|
|
{"path": "/api/v1/series", "methods": ["GET", "POST"], "actions": ["cloudwatch:ListMetrics"]},
|
|
{"path": "/api/v1/labels", "methods": ["GET", "POST"], "actions": ["cloudwatch:ListMetrics"]},
|
|
{"path": "/api/v1/label/{label_name}/values", "methods": ["GET"], "actions": ["cloudwatch:ListMetrics"]}]
|
|
|
|
|
|
def test_each_linked_operation_is_named_by_its_title_with_the_paths_of_its_valid_uris():
|
|
built = f.build(permissions_page(), INDEX, pages(operation_page(
|
|
"QueryMetrics", "Valid URIs", "/api/v1/query", "/api/v1/query_range")))
|
|
# The index links QueryMetrics twice, and the page is read once; GetSeries heads its one path "Valid URI".
|
|
assert built["operations"] == [
|
|
{"operation": "QueryMetrics", "page": f.GUIDE + "CloudWatch-PromQL-API-QueryMetrics.html",
|
|
"uris": ["/api/v1/query", "/api/v1/query_range"]},
|
|
{"operation": "GetSeries", "page": f.GUIDE + "CloudWatch-PromQL-API-GetSeries.html",
|
|
"uris": ["/api/v1/series"]}]
|
|
assert built["_source"]["pages"][2:] == [f.GUIDE + "CloudWatch-PromQL-API-QueryMetrics.html",
|
|
f.GUIDE + "CloudWatch-PromQL-API-GetSeries.html"]
|
|
|
|
|
|
def test_a_table_whose_layout_or_wording_changed_is_refused():
|
|
with pytest.raises(ValueError, match="no 'IAM permissions for PromQL' section"):
|
|
f.build(permissions_page(heading="Permissions"), INDEX, pages())
|
|
with pytest.raises(ValueError, match="columns are"):
|
|
f.build(permissions_page(headers=("Path", "Methods", "Actions")), INDEX, pages())
|
|
with pytest.raises(ValueError, match="which is not a path, its methods and cloudwatch: actions"):
|
|
f.build(permissions_page([row("/api/v1/series", "GET", "cloudwatch:ListMetrics or logs:Query")]), INDEX,
|
|
pages())
|
|
with pytest.raises(ValueError, match="which is not a path, its methods and cloudwatch: actions"):
|
|
f.build(permissions_page([row("/api/v1/series", "GET or POST", "cloudwatch:ListMetrics")]), INDEX, pages())
|
|
with pytest.raises(ValueError, match="gives no rows"):
|
|
f.build(permissions_page([]), INDEX, pages())
|
|
|
|
|
|
def test_an_operation_page_whose_layout_or_wording_changed_is_refused():
|
|
with pytest.raises(ValueError, match="gives no operation title or no valid URIs"):
|
|
f.build(permissions_page(), INDEX, pages(operation_page("QueryMetrics", "Endpoints", "/api/v1/query")))
|
|
with pytest.raises(ValueError, match="which names no path"):
|
|
f.build(permissions_page(), INDEX, pages(operation_page("QueryMetrics", "Valid URIs", "query")))
|
|
with pytest.raises(ValueError, match="links no operation page"):
|
|
f.build(permissions_page(), "<p>No links.</p>", pages())
|
|
|
|
|
|
def test_the_file_is_stale_when_a_page_says_something_else():
|
|
vendored = f.build(permissions_page(), INDEX, pages())
|
|
assert not f.stale(vendored, permissions_page(), INDEX, pages())
|
|
assert f.stale(vendored, permissions_page(ROWS[1:]), INDEX, pages())
|
|
assert f.stale(vendored, permissions_page(), INDEX, pages(operation_page(
|
|
"QueryMetrics", "Valid URIs", "/api/v1/query", "/api/v1/query_range")))
|
|
# The date it was read on alone is no change.
|
|
vendored["_source"]["retrieved"] = "2000-01-01"
|
|
assert not f.stale(vendored, permissions_page(), INDEX, pages())
|
|
|
|
|
|
def test_verify_fails_on_a_stale_file_and_cannot_run_without_the_guide(tmp_path, monkeypatch, capsys):
|
|
served = {f.PERMISSIONS_PAGE: permissions_page(), f.OPERATIONS_PAGE: INDEX,
|
|
**{f.GUIDE + link: page for link, page in pages().items()}}
|
|
output = tmp_path / "cloudwatch-promql-permissions.json"
|
|
monkeypatch.setattr(f, "OUTPUT", output)
|
|
monkeypatch.setattr(f, "REPO_ROOT", tmp_path)
|
|
assert f.main([], fetch=served.__getitem__) == 0
|
|
assert f.main(["--verify"], fetch=served.__getitem__) == 0
|
|
served[f.PERMISSIONS_PAGE] = permissions_page(ROWS[1:])
|
|
assert f.main(["--verify"], fetch=served.__getitem__) == 1
|
|
|
|
def unreachable(url: str) -> str:
|
|
raise OSError("unreachable")
|
|
assert f.main(["--verify"], fetch=unreachable) == 2
|
|
assert "could not read the Amazon CloudWatch user guide" in capsys.readouterr().err
|