Skip to content

Commit 6a29a11

Browse files
author
Ma Shimiao
committed
validate: optimize capabilites check
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
1 parent 7ff34fc commit 6a29a11

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

cmd/oci-runtime-tool/validate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/urfave/cli"
8-
97
"github.com/opencontainers/runtime-tools/validate"
8+
"github.com/urfave/cli"
109
)
1110

1211
var bundleValidateFlags = []cli.Flag{

generate/generate.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,12 @@ func lastCap() capability.Cap {
679679
return last
680680
}
681681

682-
func checkCap(c string, hostSpecific bool) error {
682+
func ValidCap(c string, hostSpecific bool) error {
683683
isValid := false
684684
cp := strings.ToUpper(c)
685685

686686
for _, cap := range capability.List() {
687-
if cp == strings.ToUpper(cap.String()) {
687+
if cp == fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())) {
688688
if hostSpecific && cap > lastCap() {
689689
return fmt.Errorf("CAP_%s is not supported on the current host", cp)
690690
}
@@ -694,7 +694,7 @@ func checkCap(c string, hostSpecific bool) error {
694694
}
695695

696696
if !isValid {
697-
return fmt.Errorf("Invalid value passed for adding capability")
697+
return fmt.Errorf("Invalid capability value: %s", cp)
698698
}
699699
return nil
700700
}
@@ -709,12 +709,11 @@ func (g *Generator) ClearProcessCapabilities() {
709709

710710
// AddProcessCapability adds a process capability into g.spec.Process.Capabilities.
711711
func (g *Generator) AddProcessCapability(c string) error {
712-
if err := checkCap(c, g.HostSpecific); err != nil {
712+
cp := fmt.Sprintf("CAP_%s", strings.ToUpper(c))
713+
if err := ValidCap(cp, g.HostSpecific); err != nil {
713714
return err
714715
}
715716

716-
cp := fmt.Sprintf("CAP_%s", strings.ToUpper(c))
717-
718717
g.initSpec()
719718
for _, cap := range g.spec.Process.Capabilities {
720719
if strings.ToUpper(cap) == cp {
@@ -728,12 +727,11 @@ func (g *Generator) AddProcessCapability(c string) error {
728727

729728
// DropProcessCapability drops a process capability from g.spec.Process.Capabilities.
730729
func (g *Generator) DropProcessCapability(c string) error {
731-
if err := checkCap(c, g.HostSpecific); err != nil {
730+
cp := fmt.Sprintf("CAP_%s", strings.ToUpper(c))
731+
if err := ValidCap(cp, g.HostSpecific); err != nil {
732732
return err
733733
}
734734

735-
cp := fmt.Sprintf("CAP_%s", strings.ToUpper(c))
736-
737735
g.initSpec()
738736
for i, cap := range g.spec.Process.Capabilities {
739737
if strings.ToUpper(cap) == cp {

validate/validate.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,30 @@ import (
1414

1515
"github.com/Sirupsen/logrus"
1616
"github.com/blang/semver"
17+
"github.com/opencontainers/runtime-tools/generate"
1718
rspec "github.com/opencontainers/runtime-spec/specs-go"
1819
)
1920

2021
const specConfig = "config.json"
2122

2223
var (
2324
defaultRlimits = []string{
25+
"RLIMIT_AS",
26+
"RLIMIT_CORE",
2427
"RLIMIT_CPU",
25-
"RLIMIT_FSIZE",
2628
"RLIMIT_DATA",
27-
"RLIMIT_STACK",
28-
"RLIMIT_CORE",
29-
"RLIMIT_RSS",
30-
"RLIMIT_NPROC",
31-
"RLIMIT_NOFILE",
32-
"RLIMIT_MEMLOCK",
33-
"RLIMIT_AS",
29+
"RLIMIT_FSIZE",
3430
"RLIMIT_LOCKS",
35-
"RLIMIT_SIGPENDING",
31+
"RLIMIT_MEMLOCK",
3632
"RLIMIT_MSGQUEUE",
3733
"RLIMIT_NICE",
34+
"RLIMIT_NOFILE",
35+
"RLIMIT_NPROC",
36+
"RLIMIT_RSS",
3837
"RLIMIT_RTPRIO",
3938
"RLIMIT_RTTIME",
39+
"RLIMIT_SIGPENDING",
40+
"RLIMIT_STACK",
4041
}
4142
defaultCaps = []string{
4243
"CAP_CHOWN",
@@ -217,19 +218,18 @@ func (v *Validator) CheckProcess() (msgs []string) {
217218
}
218219
}
219220

220-
for index := 0; index < len(process.Capabilities); index++ {
221-
capability := process.Capabilities[index]
222-
if !capValid(capability) {
223-
msgs = append(msgs, fmt.Sprintf("capability %q is not valid, man capabilities(7)", process.Capabilities[index]))
221+
for _, capability := range process.Capabilities {
222+
if err := generate.ValidCap(capability, v.HostSpecific); err != nil {
223+
msgs = append(msgs, fmt.Sprintf("capability %q is not valid, man capabilities(7)", capability))
224224
}
225225
}
226226

227-
for index := 0; index < len(process.Rlimits); index++ {
228-
if !rlimitValid(process.Rlimits[index].Type) {
229-
msgs = append(msgs, fmt.Sprintf("rlimit type %q is invalid.", process.Rlimits[index].Type))
227+
for _, rlimit := range process.Rlimits {
228+
if !rlimitValid(rlimit.Type) {
229+
msgs = append(msgs, fmt.Sprintf("rlimit type %q is invalid.", rlimit.Type))
230230
}
231-
if process.Rlimits[index].Hard < process.Rlimits[index].Soft {
232-
msgs = append(msgs, fmt.Sprintf("hard limit of rlimit %s should not be less than soft limit.", process.Rlimits[index].Type))
231+
if rlimit.Hard < rlimit.Soft {
232+
msgs = append(msgs, fmt.Sprintf("hard limit of rlimit %s should not be less than soft limit.", rlimit.Type))
233233
}
234234
}
235235

@@ -457,15 +457,6 @@ func envValid(env string) bool {
457457
return true
458458
}
459459

460-
func capValid(capability string) bool {
461-
for _, val := range defaultCaps {
462-
if val == capability {
463-
return true
464-
}
465-
}
466-
return false
467-
}
468-
469460
func rlimitValid(rlimit string) bool {
470461
for _, val := range defaultRlimits {
471462
if val == rlimit {

0 commit comments

Comments
 (0)