Skip to content

contrib/apparmor: add tests for generated template, add "header comment", and cleanup whitespace#13270

Draft
thaJeztah wants to merge 5 commits into
containerd:mainfrom
thaJeztah:apparmor_testable_templates
Draft

contrib/apparmor: add tests for generated template, add "header comment", and cleanup whitespace#13270
thaJeztah wants to merge 5 commits into
containerd:mainfrom
thaJeztah:apparmor_testable_templates

Conversation

@thaJeztah

@thaJeztah thaJeztah commented Apr 23, 2026

Copy link
Copy Markdown
Member

Copilot AI review requested due to automatic review settings April 23, 2026 12:49
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review Apr 23, 2026
@dosubot dosubot Bot added the kind/test label Apr 23, 2026
@thaJeztah
thaJeztah force-pushed the apparmor_testable_templates branch 3 times, most recently from 558494d to 4392a3e Compare April 23, 2026 12:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds golden-file based tests for the AppArmor profile generator in contrib/apparmor, updates the template to include a generated-by header comment, and normalizes template whitespace/output formatting.

Changes:

  • Introduce golden files covering multiple template configurations (ABI, tunables, abstractions, custom imports, daemon profile, rootlesskit).
  • Add a table-driven golden test with an -update flag to refresh expected outputs.
  • Update the template header/upstream reference and adjust whitespace trimming; allow tests to override macro-detection.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
contrib/apparmor/template.go Adds generated-by header, whitespace trimming tweaks, and loadData macroExists override hook for tests.
contrib/apparmor/template_test.go Adds table-driven golden tests and golden update helper.
contrib/apparmor/testdata/default.golden Golden output for default template.
contrib/apparmor/testdata/with-api3.golden Golden output when ABI 3.0 macro exists.
contrib/apparmor/testdata/with-tunables.golden Golden output when tunables/global is included.
contrib/apparmor/testdata/with-abstractions-base.golden Golden output when abstractions/base exists as an inner import.
contrib/apparmor/testdata/with-daemon-profile.golden Golden output with explicit daemon profile peer.
contrib/apparmor/testdata/with-custom-imports.golden Golden output with custom top-level imports.
contrib/apparmor/testdata/with-custom-inner-imports.golden Golden output with custom inner imports.
contrib/apparmor/testdata/with-rootlesskit.golden Golden output including RootlessKit peer signal rule.
contrib/apparmor/apparmor.go Updates loadData call sites for new signature; doc comment capitalization.
contrib/apparmor/apparmor_unsupported.go Doc comment capitalization consistency (AppArmor).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread contrib/apparmor/template_test.go Outdated
@thaJeztah
thaJeztah force-pushed the apparmor_testable_templates branch from 4392a3e to 0cfca3d Compare April 23, 2026 12:56
@thaJeztah

Copy link
Copy Markdown
Member Author

Ah, fun; looks like the profile we detect contains a newline;

        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -16,3 +16,4 @@
        	            	   # Manager may send signals to container processes.
        	            	-  signal (receive) peer=unconfined,
        	            	+  signal (receive) peer=unconfined
        	            	+,
        	            	   # Container processes may send signals amongst themselves.

Copilot AI review requested due to automatic review settings April 23, 2026 13:32

This comment was marked as duplicate.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread contrib/apparmor/template.go Outdated
@thaJeztah
thaJeztah force-pushed the apparmor_testable_templates branch from cbb52c4 to 18ccfd6 Compare April 23, 2026 18:46
const dir = "/etc/apparmor.d"

const defaultTemplate = `
const defaultTemplate = `# profile generated by github.com/containerd/containerd/v2/contrib/apparmor.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure if we wanted to add the DO NOT EDIT. here as well, so left that out for now.

Comment thread contrib/apparmor/template.go Outdated
// Normally profiles are suffixed by " (enforce)". AppArmor profiles cannot
// contain spaces so this doesn't restrict daemon profile names.
profile, _, _ = strings.Cut(profile, " ")
profile = strings.TrimSpace(profile)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this might actually be fixing a bug; a profile with a newline here looks to be invalid; I can open this commit as a separate PR first (in case we need / want to backport it)

Diff:
--- Expected
+++ Actual
@@ -16,3 +16,4 @@
   # Manager may send signals to container processes.
-  signal (receive) peer=unconfined,
+  signal (receive) peer=unconfined
+,
   # Container processes may send signals amongst themselves.

@thaJeztah
thaJeztah force-pushed the apparmor_testable_templates branch from 18ccfd6 to 24b1d09 Compare April 23, 2026 18:52
@thaJeztah
thaJeztah marked this pull request as draft April 23, 2026 18:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

With testify, expected values go to the left and use sub-tests.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
The profile we read from /proc/self/attr/current will contain a newline,
resulting in a stray newline to be added to the profile;

    Diff:
    --- Expected
    +++ Actual
    @@ -16,3 +16,4 @@
       # Manager may send signals to container processes.
    -  signal (receive) peer=unconfined,
    +  signal (receive) peer=unconfined
    +,
       # Container processes may send signals amongst themselves.

Trim whitespace to account for newlines and fix handling of whitespace as
AppArmor profile names are allowed to contain spaces when quoted; from the
[apparmor.d(5)] man-page:

    PROFILE NAME ( UNQUOTED PROFILE NAME | QUOTED PROFILE NAME )

    QUOTED PROFILE NAME = '"' UNQUOTED PROFILE NAME '"'

    UNQUOTED PROFILE NAME = (must start with alphanumeric character (after
    variable expansion), or '/' AARE have special meanings; see below. May
    include VARIABLE. Rules with embedded spaces or tabs must be quoted.)

While we don't use those names in our code, let's make the code correct.

Also update the template to use quotes.

[apparmor.d(5)]: https://manpages.ubuntu.com/manpages/xenial/man5/apparmor.d.5.html

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
- The generated profiles had a leading newline; replace it with
  something useful.
- Trim whitespace / stray newlines for conditional parts.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
thaJeztah force-pushed the apparmor_testable_templates branch from 0b211d7 to 26922ba Compare June 29, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

3 participants