func generateProfile(out io.Writer) error { compiled, err := template.New("apparmor_profile").Parse(baseTemplate) if err != nil { return err } data := &data{ Name: "docker-default", } if tunablesExists() { data.Imports = append(data.Imports, "#include <tunables/global>") } else { data.Imports = append(data.Imports, "@{PROC}=/proc/") } if abstractionsExists() { data.InnerImports = append(data.InnerImports, "#include <abstractions/base>") } data.MajorVersion, data.MinorVersion, err = aaparser.GetVersion() if err != nil { return err } data.ExecPath, err = exec.LookPath("docker") if err != nil { return err } if err := compiled.Execute(out, data); err != nil { return err } return nil }
// generateDefault creates an apparmor profile from ProfileData. func (p *profileData) generateDefault(out io.Writer) error { compiled, err := templates.NewParse("apparmor_profile", baseTemplate) if err != nil { return err } if macroExists("tunables/global") { p.Imports = append(p.Imports, "#include <tunables/global>") } else { p.Imports = append(p.Imports, "@{PROC}=/proc/") } if macroExists("abstractions/base") { p.InnerImports = append(p.InnerImports, "#include <abstractions/base>") } ver, err := aaparser.GetVersion() if err != nil { return err } p.Version = ver if err := compiled.Execute(out, p); err != nil { return err } return nil }
func main() { if len(os.Args) < 2 { log.Fatal("pass a filename to save the profile in.") } // parse the arg apparmorProfilePath := os.Args[1] majorVersion, minorVersion, err := aaparser.GetVersion() if err != nil { log.Fatal(err) } data := profileData{ MajorVersion: majorVersion, MinorVersion: minorVersion, } fmt.Printf("apparmor_parser is of version %+v\n", data) // parse the template compiled, err := template.New("apparmor_profile").Parse(dockerProfileTemplate) if err != nil { log.Fatalf("parsing template failed: %v", err) } // make sure /etc/apparmor.d exists if err := os.MkdirAll(path.Dir(apparmorProfilePath), 0755); err != nil { log.Fatal(err) } f, err := os.OpenFile(apparmorProfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { log.Fatal(err) } defer f.Close() if err := compiled.Execute(f, data); err != nil { log.Fatalf("executing template failed: %v", err) } fmt.Printf("created apparmor profile for version %+v at %q\n", data, apparmorProfilePath) }