Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Flags:

-d enable debug logging (default: false)
-profile-dir directory for saving the profiles (default: /etc/apparmor.d/containers)
-u unload and remove profile (default: false)

Commands:

Expand Down
26 changes: 26 additions & 0 deletions apparmor/apparmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,29 @@ func (profile *ProfileConfig) Install(dir string) error {
}
return nil
}

// Uninstall unloads the profile with `apparmor_parser`
// then removes it from given directory
func (profile *ProfileConfig) Uninstall(dir string) error {
// Make sure the path exists
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}

cmd := exec.Command("/sbin/apparmor_parser", "-R", profile.Name)
// to use the parser directly we have to make sure we are in the correct
// dir with the profile
cmd.Dir = dir

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Unloading apparmor profile %s failed: %v (%s)", profile.Name, err, output)
}

// Last thing: remove profile file
if err := os.Remove(filepath.Join(dir, profile.Name)); err != nil {
return err
}

return nil
}
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var (
apparmorProfileDir string

debug bool
debug, uninstall bool
)

func main() {
Expand All @@ -35,6 +35,7 @@ func main() {
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.StringVar(&apparmorProfileDir, "profile-dir", "/etc/apparmor.d/containers", "directory for saving the profiles")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
p.FlagSet.BoolVar(&uninstall, "u", false, "unload and remove profile")

// Set the before function.
p.Before = func(ctx context.Context) error {
Expand Down Expand Up @@ -75,12 +76,21 @@ func main() {
profile.Name = fmt.Sprintf("docker-%s", profile.Name)
}

// install the profile
if err := profile.Install(apparmorProfileDir); err != nil {
logrus.Fatalf("Installing profile %s failed: %v", profile.Name, err)
}
if !uninstall {
// install the profile
if err := profile.Install(apparmorProfileDir); err != nil {
logrus.Fatalf("Installing profile %s failed: %v", profile.Name, err)
}

fmt.Printf("Profile installed successfully you can now run the profile with\n`docker run --security-opt=\"apparmor:%s\"`\n", profile.Name)
} else {
// uninstall the profile (ie unload and remove)
if err := profile.Uninstall(apparmorProfileDir); err != nil {
logrus.Fatalf("Uninstalling profile %s failed: %v", profile.Name, err)
}

fmt.Printf("Profile installed successfully you can now run the profile with\n`docker run --security-opt=\"apparmor:%s\"`\n", profile.Name)
fmt.Printf("Profile %s uninstalled successfully\n", profile.Name)
}

return nil
}
Expand Down