2015-10-13 20:37:54 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"text/template"
|
2015-11-23 18:41:18 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/aaparser"
|
2015-10-13 20:37:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type profileData struct {
|
2016-02-14 02:04:16 -05:00
|
|
|
Version int
|
2015-10-13 20:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
log.Fatal("pass a filename to save the profile in.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the arg
|
|
|
|
apparmorProfilePath := os.Args[1]
|
|
|
|
|
2016-02-14 02:04:16 -05:00
|
|
|
version, err := aaparser.GetVersion()
|
2015-10-13 20:37:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
data := profileData{
|
2016-02-14 02:04:16 -05:00
|
|
|
Version: version,
|
2015-10-13 20:37:54 -04:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|