Files
floci/tools/aws/test_fetch_codeartifact_permissions.py
T
Priamo Germosen e6a9092be8 feat(iam): check AppConfig, CodeArtifact and CodeGuru Reviewer by their REST routes (#9530)
Under enforcement a REST route no rule maps and no vendored model names
runs unchecked. AWS AppConfig with its data plane, AWS CodeArtifact and
Amazon CodeGuru Reviewer were among those: Floci serves each and no rule
mapped them.

Vendors the appconfig, appconfigdata, codeartifact and codeguru-reviewer
models' routes, 120 operations, so each request is checked as its
operation's action: a caller holding no grant for one is now refused
where the request used to pass. A request signed with a vendored model's
own name, which Floci also serves (appconfigdata), is checked under the
signing name the routes file records for that model (appconfig).

The three namespaces join the table of request resources. CodeArtifact's
48 operations and CodeGuru Reviewer's 14 read every type the Service
Authorization Reference marks required. Nine of AppConfig's 58 act on a
required type the table reads no member for (a deployment, a hosted
configuration version, an experiment run, a configuration profile named
as Configuration), so those are checked on the types it does read.
AppConfig Data's StartConfigurationSession is checked on the application
its ApplicationIdentifier names by its id or its name, from a measured
entry quoting AWS's own refusal of it on application/<app_id>, and
GetLatestConfiguration on the configuration of the session its
ConfigurationToken names: a reading gives a request's token that a
create of its service answers as naming what that create's session is
on, which the service finds.

A request member whose botocore shape is an account id now gives the
account of what the request names wherever a REST request carries it,
its path and query too, as the generator's own rule says, not only in
its body: CodeArtifact's domain-owner, QuickSight's AwsAccountId path
label on 196 operations, and Signer's GetSigningProfile profileOwner.
Such a resource in another account than the caller's is named as an ARN
of another account already is, in that account and as the caller's of
that name too, unless its service acts on it in that account. Signer,
ECR, CodeArtifact and Redshift do and say so, so each is checked on that
account's resource alone, as AWS checks it: Signer now reads the profile
or job its profileOwner or jobOwner names in that account, as AWS does,
where it read the caller's own, and saves a job it revokes there. Such a
request needs the owner's grant beside the caller's own policy, as AWS
requires of a cross-account request: CodeArtifact gives IAM a domain's
and a repository's policy, and Signer the profile permissions
AddProfilePermission adds, as the resource's policy (ResourcePolicyProvider),
a permission of one profile version holding for that version alone: a
job's, the version that signed it.
A resource a request names by another spelling than its service stores
it under is checked as the service reads it (resolveStoredArn, now also
for an ARN the reader builds from members): an AppConfig environment or
configuration profile by its id when named by its name, a CodeArtifact
PyPI or NuGet package by its normalized name, as AWS writes it in ARNs.

AppConfig, CodeArtifact and CodeGuru Reviewer give IAM the tags they hold
on the resource a request names (an application, a domain, repository or
package group, an association), so a grant conditioned on aws:ResourceTag
reaches a tagged resource; Signer reads a profile's tags in the profile's
own account, another account's included. A tagged create is checked for tagging on what
it makes where botocore's response gives it: CreateEnvironment answers
an Environment whose Id is the new environment's, AssociateRepository
the RepositoryAssociation whose AssociationArn is the association it
makes. A tagged StartDeployment's tags go on the deployment it makes,
its DeploymentNumber AppConfig's to mint, open. AppConfig stays out of
the passed-roles table: CreateExtension and UpdateExtension pass a role
in Actions{}[].RoleArn, a map of lists that file does not read.

A new vendored source, tools/aws/sources/codeartifact-permissions.json,
written by its own script from the CodeArtifact user guide, gives what
neither botocore nor the Service Reference says: CopyPackageVersions is
checked as ReadFromRepository on its source repository and as
CopyPackageVersions on its destination repository, and a package with
no namespace has an empty one in its ARN (package/my_domain/my_repo/
npm//react), so a request leaving namespace out is checked on that ARN.
A variable the reference names Encoded and a botocore shape's name is
filled from the member of that shape and encoded as its service encodes
it, through StoredResourceArns: CodeArtifact's package group pattern,
percent-encoded in lower case, which CodeArtifact now writes into the
group's own ARN as AWS does (/npm/team/* is /npm/team/%2a). A tag call
spelling a group's pattern otherwise (%2A), which CodeArtifact reads as
the same group, is checked on the group's own ARN, which CodeArtifact
resolves it to, so a deny on that ARN holds.

With AppConfig in the table, a tag call naming one of its ARNs with the
slash unencoded is refused as a path its model does not give, as for
every namespace the table reads; AppConfig's tests now send the ARN
encoded, as an SDK does. AppConfig Data's GetLatestConfiguration, which
a route filter moves off GET /configuration so S3's bucket of that name
keeps its route, is moved under an internal prefix the request records,
as every such route is, so IAM matches it on its modeled path. The table
tests place a create's members where the table locates them, expect a
member holding the type's own ARN to name it as it is, and expect each
resource in the account its own account member gives.
2026-11-26 18:45:59 -04:00

58 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Tests for fetch_codeartifact_permissions.py, on pages in the AWS CodeArtifact user guide's markup."""
from __future__ import annotations
import pytest
import fetch_codeartifact_permissions as f
COPY = ('<html><body><h2 id="copypackageversions-iam-actions">Required IAM permissions to copy packages</h2>'
'<p>To copy package versions in CodeArtifact, the calling user must have the required IAM permissions.</p>'
'<p>The user calling <code class="code">copy-package-versions</code> must have the '
'<code class="code">ReadFromRepository</code> permission on the source repository and the '
'<code class="code">CopyPackageVersions</code> permission on the destination repository.</p>'
'<p>The source repository must have the <code class="code">ReadFromRepository</code> permission.</p>'
'</body></html>')
POLICIES = ('<html><body><p>The ARN for an npm package without a scope has the empty string for the namespace field. '
'For example, the following is the ARN for a package without a scope and with name '
'<code class="code">react</code>.</p>'
'<pre class="programlisting"><div class="code-btn-container"><div class="btn-copy-code" title="Copy">'
'<awsui-icon name="copy"></awsui-icon></div></div><!--DEBUG: cli ()--><code class="nohighlight">'
'arn:aws:codeartifact:<code class="replaceable">region-id</code>:<code class="replaceable">111122223333'
'</code>:package/<code class="replaceable">my_domain</code>/<code class="replaceable">my_repo</code>/'
'<code class="replaceable">npm</code>//<code class="replaceable">react</code></code></pre>'
'<p>The following policy grants account 123456789012 permission.</p><pre>{"Version":"2012-10-17"}</pre>'
'</body></html>')
def test_the_copy_page_gives_each_permission_and_the_resource_it_is_on():
assert f.read_caller_permissions(COPY) == [{
"operation": "CopyPackageVersions",
"sentence": "The user calling copy-package-versions must have the ReadFromRepository permission on the source "
"repository and the CopyPackageVersions permission on the destination repository.",
"permissions": [{"action": "codeartifact:ReadFromRepository", "resource": "source repository"},
{"action": "codeartifact:CopyPackageVersions", "resource": "destination repository"}]}]
def test_the_policies_page_gives_each_example_arn_with_its_paragraph():
assert f.read_arn_examples(POLICIES) == [{
"paragraph": "The ARN for an npm package without a scope has the empty string for the namespace field. For "
"example, the following is the ARN for a package without a scope and with name react .",
"arn": "arn:aws:codeartifact:region-id:111122223333:package/my_domain/my_repo/npm//react"}]
def test_a_page_whose_layout_changed_is_refused():
with pytest.raises(ValueError, match="no permissions"):
f.read_caller_permissions("<html><body><p>Nothing here.</p></body></html>")
with pytest.raises(ValueError, match="does not name each permission"):
f.read_caller_permissions(COPY.replace("permission on the source repository", "rights on the source"))
with pytest.raises(ValueError, match="no example ARN"):
f.read_arn_examples("<html><body><p>Nothing here.</p></body></html>")
def test_the_file_is_stale_when_a_page_says_something_else():
vendored = f.build(COPY, POLICIES)
assert not f.stale(vendored, COPY, POLICIES)
assert f.stale(vendored, COPY.replace("destination repository", "target repository"), POLICIES)
assert f.stale(vendored, COPY, POLICIES.replace("npm</code>//", "npm</code>/none/"))