fix(schema): reject extensions of kinds in restricted namespaces
This pull request was written by nisse, an AI agent, and opened with the GitHub account of the person who runs it. That person reviews it before marking it ready for review.
Why
A schema extension (extensions.nodes) could target a kind in a restricted namespace such as Core, Builtin, Profile or Template, and the schema load accepted it. As a result, a user schema could change built-in kinds:
- Adding attributes and relationships to
CoreNumberPool,CoreAccountorBuiltinTagwas accepted. - Setting
state: absentonCoreAccount.passwordremoved the attribute, and login then failed.
Defining the same kind directly under nodes: was already rejected with Restricted namespace 'Core' used on 'Account', so the two paths behaved differently.
Goal: a user schema cannot change a kind in a restricted namespace in any way, additions included. This fix targets stable even though it changes behavior, as decided for this bug.
Non-goals:
- A way to attach user-defined relationships to resource pools. The issue asks for this as a separate feature request.
- An automatic migration for deployments that already extended built-in kinds.
Closes #10418
What changed
Behavior:
- Loading a schema fails when an extension's
kindis inRESTRICTED_NAMESPACES, with the errorRestricted namespace 'Core' used on extension of 'CoreAccount'.
Implementation:
SchemaRoot._validate_namespacesinbackend/infrahub/core/schema/__init__.pynow also checksextensions.nodes.- The namespace is read from the kind with the leading
[A-Z][a-z0-9]+match, the shape thatNAMESPACE_REGEXallows. A user namespace that only starts with a restricted name, such asCoreinfra, is therefore not rejected. - The unit tests are in
backend/tests/unit/core/schema/test_schema_root.py.
Docs:
- New deprecation guide,
docs/docs/release-notes/deprecation-guides/restricted-namespace-extensions.mdx, linked from the changelog entry and fromdocs/docs/schema/extensions.mdx. It covers:- how to find and remove the rejected extensions;
- that fields an earlier extension added stay in the instance's schema, because a schema load removes a field only when the file sets
state: absenton it, and that those fields can no longer be removed; - how to model new links as relationships on your own nodes, with the built-in kind as the peer, and that existing relationship data is not moved;
- that adding fields shown on a built-in kind is not supported until the separate feature request.
docs/docs/git-integration/develop-changes.mdx: the worked example extendedBuiltinTag. It now adds anOrganizationLabelnode with acolorattribute.docs/_templates/schema/attribute.j2, regenerated intodocs/docs/reference/schema/attribute.mdx: the example extendedCoreProposedChange. It now extendsInfraDevice, and the invalidattribute:key and its indentation are fixed in the same lines.docs/docs/schema/extensions.mdx: new note that extensions cannot target restricted namespaces.
Changelog: changelog/10418.fixed.md.
What stayed the same: no GraphQL or database schema changes, and no change to how valid extensions are merged.
How to review
- The validation change is about five lines in
backend/infrahub/core/schema/__init__.py. - Check the namespace extraction from the kind for edge cases. For example, a namespace with digits gives
Builtin2TagāBuiltin2, which is allowed. - Check the deprecation guide's statement that fields added by an earlier extension stay on the instance. It is based on the documented incremental schema load and was not tested against a running instance.
- The Python SDK integration tests (
python_sdk/tests/integration/conftest.py, fixturesschema_extension_01andschema_extension_02) andpython_sdk/tests/fixtures/models/valid_schemas/rack.ymlextendBuiltinTagorBuiltinLocation. The integration tests will fail against a server that includes this fix. They live in the SDK repository and need a separate PR there.
How to test
uv run pytest backend/tests/unit/core/schema/test_schema_root.py
uv run pytest backend/tests/component/core/test_schema.py -k validate_reserved_names
Impact & rollout
- Backward compatibility: this is a breaking change. A schema file or Git repository that extends a built-in kind, such as
BuiltinTag,CoreNumberPoolorCorePrefixPool, fails to load after the upgrade until that extension is removed. The deprecation guide describes the steps. - Config/env changes: none.
Checklist
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...) - External docs updated (if user-facing or ops-facing change)
- Internal .md docs updated (internal knowledge and AI code tools knowledge)
- I have reviewed AI generated content
š¤ Generated with Claude Code
Jira: IFC-3063