Skip to content

Commit

Permalink
Merge pull request #481 from giuseppe/get-bounding-caps
Browse files Browse the repository at this point in the history
capabilities: add new method BoundingSet()
  • Loading branch information
openshift-merge-robot authored Mar 19, 2021
2 parents c744128 + d6662e8 commit c1d6a76
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
30 changes: 29 additions & 1 deletion pkg/capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var (
// Used internally and populated during init().
capabilityList []string

// Used internally and populated during init().
capsList []capability.Cap

// ErrUnknownCapability is thrown when an unknown capability is processed.
ErrUnknownCapability = errors.New("unknown capability")

Expand All @@ -28,6 +31,10 @@ var (
// Useful on the CLI for `--cap-add=all` etc.
const All = "ALL"

func getCapName(c capability.Cap) string {
return "CAP_" + strings.ToUpper(c.String())
}

func init() {
last := capability.CAP_LAST_CAP
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
Expand All @@ -38,7 +45,8 @@ func init() {
if cap > last {
continue
}
capabilityList = append(capabilityList, "CAP_"+strings.ToUpper(cap.String()))
capsList = append(capsList, cap)
capabilityList = append(capabilityList, getCapName(cap))
}
}

Expand All @@ -52,6 +60,26 @@ func stringInSlice(s string, sl []string) bool {
return false
}

// BoundingSet returns the capabilities in the current bounding set
func BoundingSet() ([]string, error) {
currentCaps, err := capability.NewPid2(0)
if err != nil {
return nil, err
}
err = currentCaps.Load()
if err != nil {
return nil, err
}
var r []string
for _, c := range capsList {
if !currentCaps.Get(capability.BOUNDING, c) {
continue
}
r = append(r, getCapName(c))
}
return r, nil
}

// AllCapabilities returns all known capabilities.
func AllCapabilities() []string {
return capabilityList
Expand Down
6 changes: 6 additions & 0 deletions pkg/capabilities/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func TestAllCapabilities(t *testing.T) {
require.Nil(t, err)
}

func TestBoundingCapabilities(t *testing.T) {
caps, err := BoundingSet()
require.Nil(t, err)
assert.True(t, len(caps) > 0)
}

func TestMergeCapabilitiesDropVerify(t *testing.T) {
adds := []string{"CAP_SYS_ADMIN", "CAP_SETUID"}
drops := []string{"CAP_NET_ADMIN", "cap_chown"}
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package version

// Version is the version of the build.
const Version = "0.35.3-dev"
const Version = "0.35.4-dev"

0 comments on commit c1d6a76

Please sign in to comment.