2014-03-05 22:57:20 +00:00
|
|
|
package apparmor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-04-08 17:10:51 +00:00
|
|
|
"io"
|
2014-03-05 22:57:20 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-03-12 18:13:24 +00:00
|
|
|
"path"
|
2014-03-05 22:57:20 +00:00
|
|
|
)
|
|
|
|
|
2014-04-08 17:10:51 +00:00
|
|
|
const (
|
|
|
|
DefaultProfilePath = "/etc/apparmor.d/docker"
|
|
|
|
)
|
|
|
|
|
|
|
|
func InstallDefaultProfile(backupPath string) error {
|
2014-03-05 22:57:20 +00:00
|
|
|
if !IsEnabled() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-08 17:10:51 +00:00
|
|
|
// If the profile already exists, check if we already have a backup
|
|
|
|
// if not, do the backup and override it. (docker 0.10 upgrade changed the apparmor profile)
|
|
|
|
// see gh#5049, apparmor blocks signals in ubuntu 14.04
|
2014-03-05 22:57:20 +00:00
|
|
|
if _, err := os.Stat(DefaultProfilePath); err == nil {
|
2014-04-08 17:10:51 +00:00
|
|
|
if _, err := os.Stat(backupPath); err == nil {
|
|
|
|
// If both the profile and the backup are present, do nothing
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Make sure the directory exists
|
|
|
|
if err := os.MkdirAll(path.Dir(backupPath), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the backup file
|
|
|
|
f, err := os.Create(backupPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2014-04-13 14:20:04 +00:00
|
|
|
|
2014-04-08 17:10:51 +00:00
|
|
|
src, err := os.Open(DefaultProfilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
2014-04-13 14:20:04 +00:00
|
|
|
|
2014-04-08 17:10:51 +00:00
|
|
|
if _, err := io.Copy(f, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-05 22:57:20 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 18:13:24 +00:00
|
|
|
// Make sure /etc/apparmor.d exists
|
|
|
|
if err := os.MkdirAll(path.Dir(DefaultProfilePath), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-14 05:22:45 +00:00
|
|
|
f, err := os.OpenFile(DefaultProfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := generateProfile(f); err != nil {
|
|
|
|
f.Close()
|
2014-03-05 22:57:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-04-14 05:22:45 +00:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
cmd := exec.Command("/sbin/apparmor_parser", "-r", "-W", "docker")
|
|
|
|
// to use the parser directly we have to make sure we are in the correct
|
|
|
|
// dir with the profile
|
|
|
|
cmd.Dir = "/etc/apparmor.d"
|
2014-03-05 22:57:20 +00:00
|
|
|
|
2014-04-14 05:22:45 +00:00
|
|
|
output, err := cmd.CombinedOutput()
|
2014-04-13 23:37:12 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
if e, ok := err.(*exec.Error); ok {
|
2014-04-14 05:22:45 +00:00
|
|
|
// keeping with the current profile load code, if the parser does not
|
|
|
|
// exist then just return
|
2014-04-13 23:37:12 +00:00
|
|
|
if e.Err == exec.ErrNotFound || os.IsNotExist(e.Err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 22:57:20 +00:00
|
|
|
return fmt.Errorf("Error loading docker profile: %s (%s)", err, output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|