2014-07-10 18:11:35 -04:00
|
|
|
package execdriver
|
|
|
|
|
2014-07-10 18:31:01 -04:00
|
|
|
import (
|
2014-07-10 19:38:11 -04:00
|
|
|
"fmt"
|
2014-07-10 18:31:01 -04:00
|
|
|
"strings"
|
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2014-07-24 18:25:29 -04:00
|
|
|
"github.com/docker/libcontainer/security/capabilities"
|
2014-07-10 18:31:01 -04:00
|
|
|
)
|
2014-07-10 18:11:35 -04:00
|
|
|
|
2014-07-10 19:38:11 -04:00
|
|
|
func TweakCapabilities(basics, adds, drops []string) ([]string, error) {
|
|
|
|
var (
|
|
|
|
newCaps []string
|
|
|
|
allCaps = capabilities.GetAllCapabilities()
|
|
|
|
)
|
2014-07-10 19:02:39 -04:00
|
|
|
|
2014-07-10 19:38:11 -04:00
|
|
|
// look for invalid cap in the drop list
|
|
|
|
for _, cap := range drops {
|
|
|
|
if strings.ToLower(cap) == "all" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !utils.StringsContainsNoCase(allCaps, cap) {
|
2014-07-16 15:14:26 -04:00
|
|
|
return nil, fmt.Errorf("Unknown capability drop: %q", cap)
|
2014-07-10 19:38:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle --cap-add=all
|
2014-07-10 19:02:39 -04:00
|
|
|
if utils.StringsContainsNoCase(adds, "all") {
|
|
|
|
basics = capabilities.GetAllCapabilities()
|
|
|
|
}
|
|
|
|
|
2014-07-10 18:31:01 -04:00
|
|
|
if !utils.StringsContainsNoCase(drops, "all") {
|
|
|
|
for _, cap := range basics {
|
2014-07-10 19:38:11 -04:00
|
|
|
// skip `all` aready handled above
|
|
|
|
if strings.ToLower(cap) == "all" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't drop `all`, add back all the non-dropped caps
|
2014-07-10 18:31:01 -04:00
|
|
|
if !utils.StringsContainsNoCase(drops, cap) {
|
2014-07-16 14:47:55 -04:00
|
|
|
newCaps = append(newCaps, strings.ToUpper(cap))
|
2014-07-10 18:31:01 -04:00
|
|
|
}
|
2014-07-10 18:11:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cap := range adds {
|
2014-07-10 19:38:11 -04:00
|
|
|
// skip `all` aready handled above
|
2014-07-10 18:31:01 -04:00
|
|
|
if strings.ToLower(cap) == "all" {
|
2014-07-10 19:02:39 -04:00
|
|
|
continue
|
2014-07-10 18:31:01 -04:00
|
|
|
}
|
2014-07-10 19:38:11 -04:00
|
|
|
|
|
|
|
if !utils.StringsContainsNoCase(allCaps, cap) {
|
2014-07-16 15:14:26 -04:00
|
|
|
return nil, fmt.Errorf("Unknown capability to add: %q", cap)
|
2014-07-10 19:38:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add cap if not already in the list
|
|
|
|
if !utils.StringsContainsNoCase(newCaps, cap) {
|
2014-07-16 14:47:55 -04:00
|
|
|
newCaps = append(newCaps, strings.ToUpper(cap))
|
2014-07-10 18:11:35 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-16 15:14:26 -04:00
|
|
|
|
2014-07-10 19:38:11 -04:00
|
|
|
return newCaps, nil
|
2014-07-10 18:11:35 -04:00
|
|
|
}
|