Skip to content

Commit 7ee610e

Browse files
committed
drop dependency on github.com/syndtr/gocapability
pkg/cap has the full list of the caps (for UT, originally), so we can drop dependency on github.com/syndtr/gocapability Signed-off-by: Akihiro Suda <[email protected]>
1 parent 9822173 commit 7ee610e

12 files changed

Lines changed: 56 additions & 1479 deletions

File tree

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ require (
4848
github.com/prometheus/client_golang v1.7.1
4949
github.com/sirupsen/logrus v1.7.0
5050
github.com/stretchr/testify v1.6.1
51-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
5251
github.com/tchap/go-patricia v2.2.6+incompatible
5352
github.com/urfave/cli v1.22.2
5453
go.etcd.io/bbolt v1.3.5

go.sum

Lines changed: 0 additions & 28 deletions
Large diffs are not rendered by default.

pkg/cap/cap_linux.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,19 @@ import (
2525
"strings"
2626

2727
"github.com/pkg/errors"
28-
"github.com/syndtr/gocapability/capability"
2928
)
3029

30+
// FromNumber returns a cap string like "CAP_SYS_ADMIN"
31+
// that corresponds to the given number like 21.
32+
//
33+
// FromNumber returns an empty string for unknown cap number.
34+
func FromNumber(num int) string {
35+
if num < 0 || num > len(capsLatest)-1 {
36+
return ""
37+
}
38+
return capsLatest[num]
39+
}
40+
3141
// FromBitmap parses an uint64 bitmap into string slice like
3242
// []{"CAP_SYS_ADMIN", ...}.
3343
//
@@ -37,17 +47,9 @@ func FromBitmap(v uint64) ([]string, []int) {
3747
res []string
3848
unknown []int
3949
)
40-
knownList := capability.List()
41-
known := make(map[string]struct{}, len(knownList))
42-
for _, f := range knownList {
43-
known[f.String()] = struct{}{}
44-
}
4550
for i := 0; i <= 63; i++ {
4651
if b := (v >> i) & 0x1; b == 0x1 {
47-
c := capability.Cap(i)
48-
sRaw := c.String()
49-
if _, ok := known[sRaw]; ok {
50-
s := "CAP_" + strings.ToUpper(sRaw)
52+
if s := FromNumber(i); s != "" {
5153
res = append(res, s)
5254
} else {
5355
unknown = append(unknown, i)
@@ -57,9 +59,25 @@ func FromBitmap(v uint64) ([]string, []int) {
5759
return res, unknown
5860
}
5961

62+
// Type is the type of capability
63+
type Type int
64+
65+
const (
66+
// Effective is CapEff
67+
Effective Type = 1 << iota
68+
// Effective is CapPrm
69+
Permitted
70+
// Inheritable is CapInh
71+
Inheritable
72+
// Bounding is CapBnd
73+
Bounding
74+
// Ambient is CapAmb
75+
Ambient
76+
)
77+
6078
// ParseProcPIDStatus returns uint64 bitmap value from /proc/<PID>/status file
61-
func ParseProcPIDStatus(r io.Reader) (map[capability.CapType]uint64, error) {
62-
res := make(map[capability.CapType]uint64)
79+
func ParseProcPIDStatus(r io.Reader) (map[Type]uint64, error) {
80+
res := make(map[Type]uint64)
6381
scanner := bufio.NewScanner(r)
6482
for scanner.Scan() {
6583
line := scanner.Text()
@@ -77,15 +95,15 @@ func ParseProcPIDStatus(r io.Reader) (map[capability.CapType]uint64, error) {
7795
}
7896
switch k {
7997
case "CapInh":
80-
res[capability.INHERITABLE] = ui64
98+
res[Inheritable] = ui64
8199
case "CapPrm":
82-
res[capability.PERMITTED] = ui64
100+
res[Permitted] = ui64
83101
case "CapEff":
84-
res[capability.EFFECTIVE] = ui64
102+
res[Effective] = ui64
85103
case "CapBnd":
86-
res[capability.BOUNDING] = ui64
104+
res[Bounding] = ui64
87105
case "CapAmb":
88-
res[capability.AMBIENT] = ui64
106+
res[Ambient] = ui64
89107
}
90108
}
91109
}
@@ -112,7 +130,7 @@ func Current() ([]string, error) {
112130
if err != nil {
113131
return nil, err
114132
}
115-
capEff := caps[capability.EFFECTIVE]
133+
capEff := caps[Effective]
116134
names, _ := FromBitmap(capEff)
117135
return names, nil
118136
}
@@ -163,10 +181,12 @@ var (
163181
// caps58 is the caps of kernel 5.8 (40 entries)
164182
caps58 = append(caps316, []string{"CAP_PERFMON", "CAP_BPF"}...)
165183
// caps59 is the caps of kernel 5.9 (41 entries)
166-
caps59 = append(caps58, "CAP_CHECKPOINT_RESTORE")
184+
caps59 = append(caps58, "CAP_CHECKPOINT_RESTORE")
185+
capsLatest = caps59
167186
)
168187

169-
// Known returns the known cap strings as of kernel 5.9
188+
// Known returns the known cap strings of the latest kernel.
189+
// The current latest kernel is 5.9.
170190
func Known() []string {
171-
return caps59
191+
return capsLatest
172192
}

pkg/cap/cap_linux_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
"github.com/stretchr/testify/assert"
24-
"github.com/syndtr/gocapability/capability"
2524
)
2625

2726
func TestCapsList(t *testing.T) {
@@ -30,6 +29,15 @@ func TestCapsList(t *testing.T) {
3029
assert.Len(t, caps59, 41)
3130
}
3231

32+
func TestFromNumber(t *testing.T) {
33+
assert.Equal(t, "CAP_CHOWN", FromNumber(0))
34+
assert.Equal(t, "CAP_SYS_ADMIN", FromNumber(21))
35+
assert.Equal(t, "CAP_CHECKPOINT_RESTORE", FromNumber(40))
36+
assert.Equal(t, "", FromNumber(-1))
37+
assert.Equal(t, "", FromNumber(63))
38+
assert.Equal(t, "", FromNumber(255))
39+
}
40+
3341
func TestFromBitmap(t *testing.T) {
3442
type testCase struct {
3543
comment string
@@ -139,12 +147,12 @@ nonvoluntary_ctxt_switches: 0
139147
`
140148
res, err := ParseProcPIDStatus(strings.NewReader(procPIDStatus))
141149
assert.NoError(t, err)
142-
expected := map[capability.CapType]uint64{
143-
capability.INHERITABLE: 0,
144-
capability.PERMITTED: 0xffffffffff,
145-
capability.EFFECTIVE: 0xffffffffff,
146-
capability.BOUNDING: 0xffffffffff,
147-
capability.AMBIENT: 0,
150+
expected := map[Type]uint64{
151+
Inheritable: 0,
152+
Permitted: 0xffffffffff,
153+
Effective: 0xffffffffff,
154+
Bounding: 0xffffffffff,
155+
Ambient: 0,
148156
}
149157
assert.EqualValues(t, expected, res)
150158
}

vendor/github.com/syndtr/gocapability/LICENSE

Lines changed: 0 additions & 24 deletions
This file was deleted.

vendor/github.com/syndtr/gocapability/capability/capability.go

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)