1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

oci/caps: generate list of all capabilities on "init"

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-09-16 14:07:50 +02:00
parent 0ec6f7ea23
commit d786a52364
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -8,18 +8,24 @@ import (
"github.com/syndtr/gocapability/capability" "github.com/syndtr/gocapability/capability"
) )
var capabilityList Capabilities var (
allCaps []string
capabilityList Capabilities
)
func init() { func init() {
last := capability.CAP_LAST_CAP last := capability.CAP_LAST_CAP
rawCaps := capability.List() rawCaps := capability.List()
allCaps = make([]string, min(int(last+1), len(rawCaps)))
capabilityList = make(Capabilities, min(int(last+1), len(rawCaps))) capabilityList = make(Capabilities, min(int(last+1), len(rawCaps)))
for i, c := range rawCaps { for i, c := range rawCaps {
if c > last { if c > last {
continue continue
} }
capName := "CAP_" + strings.ToUpper(c.String())
allCaps[i] = capName
capabilityList[i] = &CapabilityMapping{ capabilityList[i] = &CapabilityMapping{
Key: "CAP_" + strings.ToUpper(c.String()), Key: capName,
Value: c, Value: c,
} }
} }
@ -52,11 +58,7 @@ func (c *CapabilityMapping) String() string {
// GetAllCapabilities returns all of the capabilities // GetAllCapabilities returns all of the capabilities
func GetAllCapabilities() []string { func GetAllCapabilities() []string {
output := make([]string, len(capabilityList)) return allCaps
for i, c := range capabilityList {
output[i] = c.String()
}
return output
} }
// inSlice tests whether a string is contained in a slice of strings or not. // inSlice tests whether a string is contained in a slice of strings or not.
@ -78,7 +80,6 @@ const allCapabilities = "ALL"
func NormalizeLegacyCapabilities(caps []string) ([]string, error) { func NormalizeLegacyCapabilities(caps []string) ([]string, error) {
var normalized []string var normalized []string
valids := GetAllCapabilities()
for _, c := range caps { for _, c := range caps {
c = strings.ToUpper(c) c = strings.ToUpper(c)
if c == allCapabilities { if c == allCapabilities {
@ -88,7 +89,7 @@ func NormalizeLegacyCapabilities(caps []string) ([]string, error) {
if !strings.HasPrefix(c, "CAP_") { if !strings.HasPrefix(c, "CAP_") {
c = "CAP_" + c c = "CAP_" + c
} }
if !inSlice(valids, c) { if !inSlice(allCaps, c) {
return nil, errdefs.InvalidParameter(fmt.Errorf("unknown capability: %q", c)) return nil, errdefs.InvalidParameter(fmt.Errorf("unknown capability: %q", c))
} }
normalized = append(normalized, c) normalized = append(normalized, c)