Skip to content

Commit

Permalink
oci/caps: simplify, and remove types that were not needed
Browse files Browse the repository at this point in the history
The `CapabilityMapping` and `Capabilities` types appeared to be only
used locally, and added unneeded complexity.

This patch removes those types, and simplifies the logic to use a
map that maps names to `capability.Cap`s

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Aug 4, 2021
1 parent fc3f988 commit 58c4c12
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions oci/caps/utils.go
Expand Up @@ -9,26 +9,30 @@ import (
)

var (
allCaps []string
capabilityList Capabilities
allCaps []string

// capabilityList maps linux capability name to its value of capability.Cap
// type. This list contains nil entries for capabilities that are known, but
// not supported by the current kernel.
// Capabilities is one of the security systems in Linux Security Module (LSM)
// framework provided by the kernel.
// For more details on capabilities, see http://man7.org/linux/man-pages/man7/capabilities.7.html
capabilityList map[string]*capability.Cap
)

func init() {
last := capability.CAP_LAST_CAP
rawCaps := capability.List()
allCaps = make([]string, min(int(last+1), len(rawCaps)))
capabilityList = make(Capabilities, min(int(last+1), len(rawCaps)))
capabilityList = make(map[string]*capability.Cap, len(rawCaps))
for i, c := range rawCaps {
capName := "CAP_" + strings.ToUpper(c.String())
if c > last {
capabilityList[capName] = nil
continue
}
allCaps[i] = capName
capabilityList[capName] = &CapabilityMapping{
Key: capName,
Value: c,
}
capabilityList[capName] = &c
}
}

Expand All @@ -39,24 +43,6 @@ func min(a, b int) int {
return b
}

type (
// CapabilityMapping maps linux capability name to its value of capability.Cap type
// Capabilities is one of the security systems in Linux Security Module (LSM)
// framework provided by the kernel.
// For more details on capabilities, see http://man7.org/linux/man-pages/man7/capabilities.7.html
CapabilityMapping struct {
Key string `json:"key,omitempty"`
Value capability.Cap `json:"value,omitempty"`
}
// Capabilities contains all CapabilityMapping
Capabilities map[string]*CapabilityMapping
)

// String returns <key> of CapabilityMapping
func (c *CapabilityMapping) String() string {
return c.Key
}

// GetAllCapabilities returns all of the capabilities
func GetAllCapabilities() []string {
return allCaps
Expand Down

0 comments on commit 58c4c12

Please sign in to comment.