mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
fix(codescan): break the runtime import cycles CodeQL reports and lint new ones
CodeQL py/cyclic-import alerts #1023-#1029 and #1112-#1113 map to three cycles: utils.inventory.tools <-> libretranslate, utils.env.builder <-> its handlers, and utils.roles.applications.variants <-> the inventory legacy resolver. Each cycle loaded only because one side deferred an import into a function body. The shared piece now lives in a leaf module both sides import: utils/inventory/tools_vars.py, utils/env/context.py (BuildContext, EnvBuilder), and _resolve_round_include inside variants.py. The stricter lint found four more cycles, each broken the same way: compose <-> down (cli/administration/deploy/development/docker_root.py), the utils.cache package web (utils/cache/reset.py), utils.cache.carrier <-> users (utils/cache/tokens.py replaces users/store.py) and credentials -> provision (cli/administration/inventory/ruamel_io.py).
tests/lint/repository/python/test_import_cycles.py fails on any runtime import cycle between the repository's own modules. It counts TYPE_CHECKING imports and implied parent-package edges, and skips only __main__ blocks and submodule-to-parent reads. Negative control: 3 of the 11 self-tests fail on the old logic. Whole repository: 12 passed. Targeted tests of every touched module: 4275 passed.
Accepted security findings: 78 medusa findings, all on lines that exist verbatim in base 28d05e0183. They come from code moved unchanged into docker_root.py, tools_vars.py and tokens.py, or from untouched lines of touched files (compose.py, credentials_generator.py, credentials/reset/cli.py, users/__init__.py, yaml.py). Reviewed as by design or false positive: argv-list subprocess calls, dev docker-root teardown, NotASecret test-lane keys, SafeLoader yaml.load, md5 memo key, Jinja expression strings.
🎯 Fix: 90 %
💥 Break: 5 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 86
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
28d05e0183
commit
357eb3bd32
158 files changed
+1065
-642
No files matched your search
+20
-24
@@ -2,8 +2,8 @@
|
||||
|
||||
Pins the public API (`get_user_defaults`, `get_merged_users`) plus the
|
||||
internal helpers that only this module owns: `_load_user_defs`,
|
||||
`_build_users`, `_validate_forwards`, `_compute_reserved_usernames`, `_load_store_users`,
|
||||
`_resolve_tokens_file`, `_hydrate_users_tokens`, `_merge_users`,
|
||||
`_build_users`, `_validate_forwards`, `_compute_reserved_usernames`,
|
||||
`_hydrate_users_tokens`, `_merge_users`,
|
||||
`_materialize_domain_label_reservations`. Module name is `test_users_module`
|
||||
to disambiguate from the existing `tests/unit/python/plugins/lookup/test_users.py`
|
||||
which targets the lookup plugin.
|
||||
@@ -18,11 +18,11 @@ import unittest.mock as mock
|
||||
from collections import OrderedDict
|
||||
from pathlib import Path
|
||||
|
||||
from utils.cache import _reset_cache_for_tests
|
||||
from utils.cache import base as cache_base
|
||||
from utils.cache import tokens as cache_tokens
|
||||
from utils.cache import users as cache_users
|
||||
from utils.cache.carrier import USERS_RENDERED_FACT, merged_users_cache_key
|
||||
from utils.cache.users import store as cache_users_store
|
||||
from utils.cache.reset import _reset_cache_for_tests
|
||||
from utils.cache.yaml import dump_yaml_str
|
||||
from utils.domains.default_primary import default_domain_primary
|
||||
from utils.roles.mapping import ROLE_FILE_META_SERVICES, ROLE_FILE_META_USERS
|
||||
@@ -345,7 +345,7 @@ class TestResolveTokensFile(unittest.TestCase):
|
||||
sentinel.write_text("users: {}\n", encoding="utf-8")
|
||||
cache_base.DEFAULT_TOKENS_FILE = sentinel
|
||||
try:
|
||||
result = cache_users._resolve_tokens_file(variables={})
|
||||
result = cache_tokens.resolve_tokens_file(variables={})
|
||||
self.assertEqual(result, sentinel)
|
||||
finally:
|
||||
cache_base.DEFAULT_TOKENS_FILE = previous
|
||||
@@ -354,7 +354,7 @@ class TestResolveTokensFile(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
explicit = Path(tmp) / "tokens.yml"
|
||||
explicit.write_text("users: {}\n", encoding="utf-8")
|
||||
result = cache_users._resolve_tokens_file(
|
||||
result = cache_tokens.resolve_tokens_file(
|
||||
variables={"FILE_TOKENS": str(explicit)}
|
||||
)
|
||||
self.assertEqual(result, explicit)
|
||||
@@ -362,8 +362,8 @@ class TestResolveTokensFile(unittest.TestCase):
|
||||
|
||||
class TestLoadStoreUsers(unittest.TestCase):
|
||||
def test_missing_path_returns_empty(self):
|
||||
self.assertEqual(cache_users._load_store_users(None), {})
|
||||
self.assertEqual(cache_users._load_store_users(""), {})
|
||||
self.assertEqual(cache_tokens.load_store_users(None), {})
|
||||
self.assertEqual(cache_tokens.load_store_users(""), {})
|
||||
|
||||
def test_yaml_users_block_passes_through(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
@@ -372,7 +372,7 @@ class TestLoadStoreUsers(unittest.TestCase):
|
||||
dump_yaml_str({"users": {"alice": {"tokens": {"x": "y"}}}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
result = cache_users._load_store_users(path)
|
||||
result = cache_tokens.load_store_users(path)
|
||||
self.assertEqual(result, {"alice": {"tokens": {"x": "y"}}})
|
||||
|
||||
|
||||
@@ -574,9 +574,7 @@ class TestGetMergedUsersCarrierFact(unittest.TestCase):
|
||||
return {
|
||||
"inventory_hostname": host,
|
||||
"users": {"foo": {"email": "foo@example.org"}},
|
||||
cache_users_store.STORE_FACT: {
|
||||
"foo": {"tokens": {"web-app-foo": "minted"}}
|
||||
},
|
||||
cache_tokens.STORE_FACT: {"foo": {"tokens": {"web-app-foo": "minted"}}},
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
@@ -650,7 +648,7 @@ class TestGetMergedUsersCarrierFact(unittest.TestCase):
|
||||
roles = _seed_minimal_user_role(Path(tmp))
|
||||
variables = self._variables()
|
||||
self._park(roles, variables, {"foo": {"carried": True}})
|
||||
variables[cache_users_store.STORE_FACT] = {
|
||||
variables[cache_tokens.STORE_FACT] = {
|
||||
"foo": {"tokens": {"web-app-foo": "rotated"}}
|
||||
}
|
||||
merged, renders = self._merged(roles, variables)
|
||||
@@ -667,7 +665,7 @@ class TestGetMergedUsersCarrierFact(unittest.TestCase):
|
||||
encoding="utf-8",
|
||||
)
|
||||
variables = self._variables()
|
||||
del variables[cache_users_store.STORE_FACT]
|
||||
del variables[cache_tokens.STORE_FACT]
|
||||
variables["FILE_TOKENS"] = str(tokens)
|
||||
carried = self._park(roles, variables, {"foo": {"carried": True}})
|
||||
return roles, variables, tokens, carried
|
||||
@@ -692,7 +690,7 @@ class TestGetMergedUsersCarrierFact(unittest.TestCase):
|
||||
roles, variables, _, carried = self._park_from_the_tokens_file(
|
||||
Path(tmp), "minted"
|
||||
)
|
||||
variables[cache_users_store.STORE_FACT] = {
|
||||
variables[cache_tokens.STORE_FACT] = {
|
||||
"foo": {"tokens": {"web-app-foo": "minted"}}
|
||||
}
|
||||
merged, renders = self._merged(roles, variables)
|
||||
@@ -749,8 +747,8 @@ class TestStoreSignature(unittest.TestCase):
|
||||
@staticmethod
|
||||
def _signature(store: dict) -> tuple:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
return cache_users._store_signature(
|
||||
{cache_users_store.STORE_FACT: store}, Path(tmp) / "tokens.yml"
|
||||
return cache_tokens.store_signature(
|
||||
{cache_tokens.STORE_FACT: store}, Path(tmp) / "tokens.yml"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@@ -764,9 +762,9 @@ class TestStoreSignature(unittest.TestCase):
|
||||
tokens_file = Path(tmp) / "tokens.yml"
|
||||
tokens_file.write_text(dump_yaml_str({"users": store}), encoding="utf-8")
|
||||
self.assertEqual(
|
||||
cache_users._store_signature({}, tokens_file),
|
||||
cache_users._store_signature(
|
||||
{cache_users_store.STORE_FACT: store}, tokens_file
|
||||
cache_tokens.store_signature({}, tokens_file),
|
||||
cache_tokens.store_signature(
|
||||
{cache_tokens.STORE_FACT: store}, tokens_file
|
||||
),
|
||||
)
|
||||
|
||||
@@ -774,10 +772,8 @@ class TestStoreSignature(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
absent = Path(tmp) / "tokens.yml"
|
||||
self.assertEqual(
|
||||
cache_users._store_signature({}, absent),
|
||||
cache_users._store_signature(
|
||||
{cache_users_store.STORE_FACT: {}}, absent
|
||||
),
|
||||
cache_tokens.store_signature({}, absent),
|
||||
cache_tokens.store_signature({cache_tokens.STORE_FACT: {}}, absent),
|
||||
)
|
||||
|
||||
def test_fields_and_padding_the_hydration_ignores_keep_the_signature(self):
|
||||
|
||||
Reference in new issue
Block a user