validate.go 652 B

12345678910111213141516171819202122232425262728293031
  1. package capabilities
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/syndtr/gocapability/capability"
  6. )
  7. // CapValid checks whether a capability is valid
  8. func CapValid(c string, hostSpecific bool) error {
  9. isValid := false
  10. if !strings.HasPrefix(c, "CAP_") {
  11. return fmt.Errorf("capability %s must start with CAP_", c)
  12. }
  13. for _, cap := range capability.List() {
  14. if c == fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())) {
  15. if hostSpecific && cap > LastCap() {
  16. return fmt.Errorf("%s is not supported on the current host", c)
  17. }
  18. isValid = true
  19. break
  20. }
  21. }
  22. if !isValid {
  23. return fmt.Errorf("invalid capability: %s", c)
  24. }
  25. return nil
  26. }