Example Bash Script
Below is a sample Bash script named “add_data.sh” that creates directories under the /opt filesystem and writes a log file within the new directory:Generating an AppArmor Profile for the Script
Instead of creating a profile manually, you can use AppArmor’s built-in tools. First, install the AppArmor-utils package. On Ubuntu, run:Profiling the Script
-
Open a separate terminal window and run the Bash script to generate AppArmor events:
-
Return to the
aa-genprofprompt and presssto scan the system logs. The tool will then display multiple prompts for each event encountered, such as:To allow the execution of themkdircommand, choose the inherit option by enteringi. -
Further prompts might appear. For example:
Again, select the appropriate option—typically
ifor inherit if needed. -
Another prompt may request permission to access the tty interface. If a prompt with severity 9 appears when printing to the console, enter
a(allow). -
You might encounter a prompt asking for read access to a system file. For instance:
Since the script does not need access to this file, choose
dto deny access.
Ensure that you only allow permissions essential for your application to operate. Deny any unnecessary access to maintain a secure profile.
S to save and F to finish. You should see output similar to:
Verifying the Profile
To confirm that the profile is in enforce mode, use the following command:Testing the Enforced Profile
To verify that the enforced profile restricts unauthorized access, modify the script to change the log file path from/opt/app/data to /opt. Update the script as follows:
/opt/app directory, yielding a permission denied error when attempting to write directly to /opt.
Working with Existing AppArmor Profiles
To load an existing profile, use the AppArmor parser command. If no output is returned, the profile has been successfully loaded.To disable a profile, use the same command with the
-r flag and create a symlink to the profile in the /etc/apparmor.d/disable directory.
Now that you’ve learned how to create and enforce AppArmor profiles for a custom application, you can explore securing applications running within Kubernetes pods using AppArmor for enhanced security.