github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/contrib/apparmor/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"path"
     8  	"text/template"
     9  
    10  	"github.com/demonoid81/moby/pkg/aaparser"
    11  )
    12  
    13  type profileData struct {
    14  	Version int
    15  }
    16  
    17  func main() {
    18  	if len(os.Args) < 2 {
    19  		log.Fatal("pass a filename to save the profile in.")
    20  	}
    21  
    22  	// parse the arg
    23  	apparmorProfilePath := os.Args[1]
    24  
    25  	version, err := aaparser.GetVersion()
    26  	if err != nil {
    27  		log.Fatal(err)
    28  	}
    29  	data := profileData{
    30  		Version: version,
    31  	}
    32  	fmt.Printf("apparmor_parser is of version %+v\n", data)
    33  
    34  	// parse the template
    35  	compiled, err := template.New("apparmor_profile").Parse(dockerProfileTemplate)
    36  	if err != nil {
    37  		log.Fatalf("parsing template failed: %v", err)
    38  	}
    39  
    40  	// make sure /etc/apparmor.d exists
    41  	if err := os.MkdirAll(path.Dir(apparmorProfilePath), 0755); err != nil {
    42  		log.Fatal(err)
    43  	}
    44  
    45  	f, err := os.OpenFile(apparmorProfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  	defer f.Close()
    50  
    51  	if err := compiled.Execute(f, data); err != nil {
    52  		log.Fatalf("executing template failed: %v", err)
    53  	}
    54  
    55  	fmt.Printf("created apparmor profile for version %+v at %q\n", data, apparmorProfilePath)
    56  }