mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 10:38:00 -05:00
CloudWatch's OTel enrichment reads resource tags through ObservabilityAdmin's resource tags for telemetry feature, which has to be running before StartOTelEnrichment enables tag enrichment. Add the ObservabilityAdmin service with StartTelemetryEnrichment, StopTelemetryEnrichment and GetTelemetryEnrichmentStatus, one feature per account and Region. Starting the feature has Resource Explorer create a LOCAL index when the Region has none and a managed view that includes tags, as the CloudWatch user guide describes. GetManagedView and ListManagedViews now answer the views Floci's services create. A status read before the first start is the modeled ResourceNotFoundException, and a stop of a stopped or never-started feature changes nothing. The passed-roles table now reads ObservabilityAdmin. A vendored source, written from the CloudWatch user guide and the Observability Admin API reference, gives a dataset integration's role to CloudWatch Logs, whose trust policy it names, and gives where a telemetry pipeline's YAML configuration names its roles: its source's and its Secrets Manager extension's, each going to the service its source type's role trusts, and to CloudWatch Logs as well with a lookup processor or a conditional route. Part of #7030.
171 lines
12 KiB
Python
171 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for fetch_observabilityadmin_roles.py, on pages in the Observability Admin API reference's and the CloudWatch
|
|
user guide's markup."""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
import fetch_observabilityadmin_roles as f
|
|
|
|
|
|
def api_entry(prefix: str, member: str, description: str) -> str:
|
|
return (f'<dl><dt id="cwoa-{prefix}-{member}"><b><code class="code">{member}</code></b></dt>'
|
|
f'<dd><p>{description}</p><p>Type: String</p><p>Required: Yes</p></dd></dl>')
|
|
|
|
|
|
def trust(service: str) -> str:
|
|
return ('<pre class="programlisting"><code class="nohighlight"><span>{</span> "Version": "2012-10-17", "Statement": '
|
|
'[ <span>{</span> "Effect": "Allow", "Principal": <span>{</span> "Service": "' + service + '" }, '
|
|
'"Action": "sts:AssumeRole" } ] }</code></pre>')
|
|
|
|
|
|
def parameters(example: str, *terms: tuple[str, str]) -> str:
|
|
listed = "".join(f'<dt><span class="term"><code class="code">{name}</code> (required)</span></dt> '
|
|
f'<dd> <p>{description}</p> </dd>' for name, description in terms)
|
|
return f'<pre class="programlisting"><code>{example}</code></pre><div class="variablelist"><dl>{listed}</dl></div>'
|
|
|
|
|
|
CONFUSED_DEPUTY = (
|
|
'<h2 id="omni-cross-service-confused-deputy-prevention-setup-and-data-access-roles">Setup and data-access roles</h2>'
|
|
+ trust("omni.amazonaws.com")
|
|
+ '<h2 id="omni-cross-service-confused-deputy-prevention-dataset-integration-execution-role">Dataset integration '
|
|
'execution role</h2><p>Dataset integration reads your CloudWatch Logs data through a role that CloudWatch Logs '
|
|
'assumes on your behalf.</p>' + trust("logs.amazonaws.com")
|
|
+ '<h2 id="omni-cross-service-confused-deputy-prevention-experiment-execution-role">Experiment execution role</h2>')
|
|
|
|
USE_CASES = (
|
|
'<table id="d0e1"><thead><tr><th>Use case</th><th>Integration method</th><th>Source type in pipeline '
|
|
'configuration</th><th>IAM sections you need</th></tr></thead>'
|
|
'<tr><td>Third-party integrations (API Pull)</td><td>Pulls</td><td><code class="code">okta_sso</code>, and so '
|
|
'on.</td><td><a href="#api-caller-permissions">API caller permissions</a> + <a href="#third-party-api-pull">'
|
|
'Third-party sources (API Pull)</a></td></tr>'
|
|
'<tr><td>Custom data from S3</td><td>Reads</td><td><code class="code">s3</code></td><td><a href="#custom-data-s3">'
|
|
'Custom data from S3</a> + <a href="#resource-policies">Resource policies</a></td></tr>'
|
|
'<tr><td>Custom data from CloudWatch Logs</td><td>Intercepts</td><td><code class="code">cloudwatch_logs</code></td>'
|
|
'<td><a href="#custom-data-cloudwatch-logs">Custom data from CloudWatch Logs</a></td></tr>'
|
|
'<tr><td>CloudWatch Metrics (OTel)</td><td>OTLP</td><td><code class="code">cloudwatch_metrics</code></td>'
|
|
'<td><a href="#pipeline-rule-permissions-metrics">Pipeline rule permissions</a></td></tr></table>')
|
|
ALSO = ('<p>For third-party pipelines that use a lookup processor or conditional routing, also allow '
|
|
'<code class="code">logs.amazonaws.com</code> in <code class="code">iam:PassedToService</code>.</p>')
|
|
|
|
|
|
def pipeline_iam(use_cases: str = USE_CASES, also: str = ALSO, s3: str = "") -> str:
|
|
return (f'<h1 id="pipeline-iam-reference">Pipelines IAM</h1>{use_cases}'
|
|
f'<h2 id="api-caller-permissions">API caller permissions</h2>{also}'
|
|
'<h3 id="pipeline-rule-permissions-metrics">Pipeline rule permissions</h3>'
|
|
'<h2 id="source-role-policies">Source role policies</h2>'
|
|
'<h3 id="third-party-api-pull">Third-party sources (API Pull)</h3>'
|
|
+ trust("telemetry-pipelines.observabilityadmin.amazonaws.com")
|
|
+ '<h4 id="api-pull-complete-setup">Complete IAM setup</h4>'
|
|
+ trust("telemetry-pipelines.observabilityadmin.amazonaws.com")
|
|
+ '<h3 id="custom-data-s3">Custom data from S3</h3>'
|
|
+ (s3 or '<p>The IAM setup is identical to <a href="#third-party-api-pull">Third-party sources (API Pull)'
|
|
'</a>.</p>')
|
|
+ '<h3 id="custom-data-cloudwatch-logs">Custom data from CloudWatch Logs</h3>' + trust("logs.amazonaws.com")
|
|
+ '<h2 id="resource-policies">Resource policies</h2>' + trust("ignored.amazonaws.com"))
|
|
|
|
|
|
API = f.API_REFERENCE
|
|
PAGES = {
|
|
API + "API_CreateDatasetIntegration.html": api_entry("CreateDatasetIntegration-request", "RoleArn",
|
|
"The ARN of the role for the dataset integration."),
|
|
API + "API_UpdateDatasetIntegration.html": api_entry("UpdateDatasetIntegration-request", "RoleArn",
|
|
"The ARN of the role to associate."),
|
|
f.CONFUSED_DEPUTY: CONFUSED_DEPUTY,
|
|
API + "API_CreateTelemetryPipeline.html": api_entry("CreateTelemetryPipeline-request", "Configuration",
|
|
"The configuration of the pipeline."),
|
|
API + "API_UpdateTelemetryPipeline.html": api_entry("UpdateTelemetryPipeline-request", "Configuration",
|
|
"The new configuration."),
|
|
f.CONFIGURATION: ('<p>The configuration is specified in YAML format and must include a valid pipeline definition.'
|
|
'</p>' + api_entry("Type-TelemetryPipelineConfiguration", "Body", "The pipeline body.")),
|
|
f.PARAMETER_PAGES[0]: parameters("source:\n cloudwatch_logs:\n aws:\n sts_role_arn: x",
|
|
("aws.sts_role_arn", "The ARN of the IAM role to assume for interception."),
|
|
("log_event_metadata.data_source_name", "Identifies the source.")),
|
|
f.PARAMETER_PAGES[1]: parameters("source:\n s3:\n aws:", ("aws.region", "The AWS region."),
|
|
("aws.sts_role_arn", "The ARN of the IAM role to assume for accessing S3.")),
|
|
f.PARAMETER_PAGES[2]: parameters("extension:\n aws:\n secrets:",
|
|
("aws.secrets.<secret-name>.sts_role_arn",
|
|
"The ARN of the IAM role to assume for accessing the secret.")),
|
|
f.PIPELINE_IAM: pipeline_iam(),
|
|
f.PROCESSORS: ('<h2 id="grok">grok processor</h2><pre><code>processor:\n - grok:</code></pre>'
|
|
'<h2 id="lookup-processor">lookup processor</h2><p>Enriches log events.</p>'
|
|
'<pre class="programlisting"><code>processor:\n - lookup:\n lookup_table: "arn"</code></pre>'),
|
|
f.SINKS: ('<h3 id="cloudwatch-logs-sink-routing">Route logs to multiple log groups</h3><p>Define a '
|
|
'<code class="code">when</code> expression on each conditional route and set is_default.</p>'),
|
|
}
|
|
|
|
|
|
def test_each_role_and_where_a_pipeline_configuration_names_one_is_read():
|
|
built = f.build(lambda url: PAGES[url])
|
|
assert [(m["operation"], m["trustedBy"], m["section"]) for m in built["members"]] == [
|
|
("CreateDatasetIntegration", ["logs.amazonaws.com"], "Dataset integration execution role"),
|
|
("UpdateDatasetIntegration", ["logs.amazonaws.com"], "Dataset integration execution role")]
|
|
assert built["members"][0]["roleText"].startswith("Dataset integration reads your CloudWatch Logs data")
|
|
assert [(p["operation"], p["member"], p["description"]) for p in built["pipelines"]] == [
|
|
("CreateTelemetryPipeline", "Configuration", "The configuration of the pipeline."),
|
|
("UpdateTelemetryPipeline", "Configuration", "The new configuration.")]
|
|
assert built["configuration"]["member"] == "Body"
|
|
assert built["configuration"]["format"].startswith("The configuration is specified in YAML format")
|
|
# Only the parameters naming a role, each under the section its page's example opens.
|
|
assert [(p["under"], p["parameter"]) for p in built["roleParameters"]] == [
|
|
("source", "aws.sts_role_arn"), ("source", "aws.sts_role_arn"),
|
|
("extension", "aws.secrets.<secret-name>.sts_role_arn")]
|
|
|
|
|
|
def test_each_use_case_gives_its_source_types_and_its_source_roles_services():
|
|
sources = f.build(lambda url: PAGES[url])["sources"]
|
|
# A section of the policies of a source role names its trust policy's service, or the section it is identical to;
|
|
# a use case with no such section has no source role.
|
|
assert sources == [
|
|
{"useCase": "Third-party integrations (API Pull)", "sourceTypes": ["okta_sso"], "andSoOn": True,
|
|
"section": "Third-party sources (API Pull)",
|
|
"trustedBy": ["telemetry-pipelines.observabilityadmin.amazonaws.com"]},
|
|
{"useCase": "Custom data from S3", "sourceTypes": ["s3"], "section": "Custom data from S3",
|
|
"identicalTo": "Third-party sources (API Pull)",
|
|
"trustedBy": ["telemetry-pipelines.observabilityadmin.amazonaws.com"]},
|
|
{"useCase": "Custom data from CloudWatch Logs", "sourceTypes": ["cloudwatch_logs"],
|
|
"section": "Custom data from CloudWatch Logs", "trustedBy": ["logs.amazonaws.com"]},
|
|
{"useCase": "CloudWatch Metrics (OTel)", "sourceTypes": ["cloudwatch_metrics"]}]
|
|
|
|
|
|
def test_what_a_lookup_processor_or_conditional_routing_also_passes_the_roles_to_is_read():
|
|
also = f.build(lambda url: PAGES[url])["also"]
|
|
assert (also["passedToService"], also["processor"], also["routeKey"]) == (["logs.amazonaws.com"], "lookup", "when")
|
|
assert also["text"].startswith("For third-party pipelines that use a lookup processor or conditional routing")
|
|
|
|
|
|
@pytest.mark.parametrize("url, page, message", [
|
|
(API + "API_CreateDatasetIntegration.html", api_entry("CreateDatasetIntegration-request", "Tags", "Tags."),
|
|
"has no entry"),
|
|
(f.CONFUSED_DEPUTY, '<h2 id="other">Other</h2>', "has no section"),
|
|
(f.CONFUSED_DEPUTY, CONFUSED_DEPUTY.replace("logs.amazonaws.com", "logs"), "names no trust policy's service"),
|
|
(f.CONFIGURATION, api_entry("Type-TelemetryPipelineConfiguration", "Body", "The body."), "format"),
|
|
(f.PARAMETER_PAGES[0], parameters("source:", ("aws.region", "The AWS region.")), "lists no parameter naming a role"),
|
|
(f.PARAMETER_PAGES[2], "<dl></dl>", "has no example"),
|
|
(f.PIPELINE_IAM, pipeline_iam(use_cases=""), "has no table of source types"),
|
|
(f.PIPELINE_IAM, pipeline_iam(s3="<p>Use the S3 policies.</p>"), "no section its setup is identical to"),
|
|
(f.PIPELINE_IAM, pipeline_iam(also="<p>Allow the service.</p>"), "what else a third-party pipeline"),
|
|
(f.SINKS, '<h3 id="cloudwatch-logs-sink-routing">Route</h3><p>Use routes.</p>', "conditional route"),
|
|
])
|
|
def test_a_page_whose_layout_changed_is_refused(url, page, message):
|
|
changed = dict(PAGES, **{url: page})
|
|
with pytest.raises(ValueError, match=message):
|
|
f.build(lambda u: changed[u])
|
|
|
|
|
|
def test_the_file_is_stale_when_a_page_says_something_else():
|
|
vendored = f.build(lambda url: PAGES[url])
|
|
assert not f.stale(vendored, PAGES)
|
|
assert f.stale(vendored, dict(PAGES, **{f.CONFUSED_DEPUTY: CONFUSED_DEPUTY.replace(
|
|
'"logs.amazonaws.com"', '"test.logs.amazonaws.com"')}))
|
|
assert f.stale(vendored, dict(PAGES, **{f.SINKS: PAGES[f.SINKS].replace("a <code class=\"code\">when</code>",
|
|
"a <code class=\"code\">if</code>")}))
|
|
|
|
|
|
def test_the_vendored_file_is_what_its_fetcher_writes():
|
|
vendored = __import__("json").loads(f.OUTPUT.read_text(encoding="utf-8"))
|
|
assert vendored["_source"]["fetcher"] == "tools/aws/fetch_observabilityadmin_roles.py"
|
|
assert [member["trustedBy"] for member in vendored["members"]] == [["logs.amazonaws.com"]] * 2
|
|
assert {source_type for source in vendored["sources"] if source.get("trustedBy") == ["logs.amazonaws.com"]
|
|
for source_type in source["sourceTypes"]} == {"cloudwatch_logs"}
|