#!/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'
{member}
' f'

{description}

Type: String

Required: Yes

') def trust(service: str) -> str: return ('
{ "Version": "2012-10-17", "Statement": '
            '[ { "Effect": "Allow", "Principal": { "Service": "' + service + '" }, '
            '"Action": "sts:AssumeRole" } ] }
') def parameters(example: str, *terms: tuple[str, str]) -> str: listed = "".join(f'
{name} (required)
' f'

{description}

' for name, description in terms) return f'
{example}
{listed}
' CONFUSED_DEPUTY = ( '

Setup and data-access roles

' + trust("omni.amazonaws.com") + '

Dataset integration ' 'execution role

Dataset integration reads your CloudWatch Logs data through a role that CloudWatch Logs ' 'assumes on your behalf.

' + trust("logs.amazonaws.com") + '

Experiment execution role

') USE_CASES = ( '' '' '' '' '' '' '
Use caseIntegration methodSource type in pipeline ' 'configurationIAM sections you need
Third-party integrations (API Pull)Pullsokta_sso, and so ' 'on.API caller permissions + ' 'Third-party sources (API Pull)
Custom data from S3Readss3' 'Custom data from S3 + Resource policies
Custom data from CloudWatch LogsInterceptscloudwatch_logsCustom data from CloudWatch Logs
CloudWatch Metrics (OTel)OTLPcloudwatch_metricsPipeline rule permissions
') ALSO = ('

For third-party pipelines that use a lookup processor or conditional routing, also allow ' 'logs.amazonaws.com in iam:PassedToService.

') def pipeline_iam(use_cases: str = USE_CASES, also: str = ALSO, s3: str = "") -> str: return (f'

Pipelines IAM

{use_cases}' f'

API caller permissions

{also}' '

Pipeline rule permissions

' '

Source role policies

' '

Third-party sources (API Pull)

' + trust("telemetry-pipelines.observabilityadmin.amazonaws.com") + '

Complete IAM setup

' + trust("telemetry-pipelines.observabilityadmin.amazonaws.com") + '

Custom data from S3

' + (s3 or '

The IAM setup is identical to Third-party sources (API Pull)' '.

') + '

Custom data from CloudWatch Logs

' + trust("logs.amazonaws.com") + '

Resource policies

' + 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: ('

The configuration is specified in YAML format and must include a valid pipeline definition.' '

' + 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: ('

grok processor

processor:\n  - grok:
' '

lookup processor

Enriches log events.

' '
processor:\n  - lookup:\n      lookup_table: "arn"
'), f.SINKS: ('

Route logs to multiple log groups

Define a ' 'when expression on each conditional route and set is_default.

'), } 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..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, '

Other

', "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], "
", "has no example"), (f.PIPELINE_IAM, pipeline_iam(use_cases=""), "has no table of source types"), (f.PIPELINE_IAM, pipeline_iam(s3="

Use the S3 policies.

"), "no section its setup is identical to"), (f.PIPELINE_IAM, pipeline_iam(also="

Allow the service.

"), "what else a third-party pipeline"), (f.SINKS, '

Route

Use routes.

', "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 when", "a if")})) 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"}