fix(schema): reject extensions of kinds in restricted namespaces

Merged
OG
6d ago
+158-42

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, CoreAccount or BuiltinTag was accepted.
  • Setting state: absent on CoreAccount.password removed 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 kind is in RESTRICTED_NAMESPACES, with the error Restricted namespace 'Core' used on extension of 'CoreAccount'.

Implementation:

  • SchemaRoot._validate_namespaces in backend/infrahub/core/schema/__init__.py now also checks extensions.nodes.
  • The namespace is read from the kind with the leading [A-Z][a-z0-9]+ match, the shape that NAMESPACE_REGEX allows. A user namespace that only starts with a restricted name, such as Coreinfra, 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 from docs/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: absent on 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 extended BuiltinTag. It now adds an OrganizationLabel node with a color attribute.
  • docs/_templates/schema/attribute.j2, regenerated into docs/docs/reference/schema/attribute.mdx: the example extended CoreProposedChange. It now extends InfraDevice, and the invalid attribute: 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, fixtures schema_extension_01 and schema_extension_02) and python_sdk/tests/fixtures/models/valid_schemas/rack.yml extend BuiltinTag or BuiltinLocation. 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, CoreNumberPool or CorePrefixPool, 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