types.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package libcontainer
  2. import (
  3. "errors"
  4. "github.com/syndtr/gocapability/capability"
  5. )
  6. var (
  7. ErrUnkownNamespace = errors.New("Unknown namespace")
  8. ErrUnkownCapability = errors.New("Unknown capability")
  9. ErrUnsupported = errors.New("Unsupported method")
  10. )
  11. // namespaceList is used to convert the libcontainer types
  12. // into the names of the files located in /proc/<pid>/ns/* for
  13. // each namespace
  14. var (
  15. namespaceList = Namespaces{}
  16. capabilityList = Capabilities{
  17. {Key: "SETPCAP", Value: capability.CAP_SETPCAP, Enabled: false},
  18. {Key: "SYS_MODULE", Value: capability.CAP_SYS_MODULE, Enabled: false},
  19. {Key: "SYS_RAWIO", Value: capability.CAP_SYS_RAWIO, Enabled: false},
  20. {Key: "SYS_PACCT", Value: capability.CAP_SYS_PACCT, Enabled: false},
  21. {Key: "SYS_ADMIN", Value: capability.CAP_SYS_ADMIN, Enabled: false},
  22. {Key: "SYS_NICE", Value: capability.CAP_SYS_NICE, Enabled: false},
  23. {Key: "SYS_RESOURCE", Value: capability.CAP_SYS_RESOURCE, Enabled: false},
  24. {Key: "SYS_TIME", Value: capability.CAP_SYS_TIME, Enabled: false},
  25. {Key: "SYS_TTY_CONFIG", Value: capability.CAP_SYS_TTY_CONFIG, Enabled: false},
  26. {Key: "MKNOD", Value: capability.CAP_MKNOD, Enabled: false},
  27. {Key: "AUDIT_WRITE", Value: capability.CAP_AUDIT_WRITE, Enabled: false},
  28. {Key: "AUDIT_CONTROL", Value: capability.CAP_AUDIT_CONTROL, Enabled: false},
  29. {Key: "MAC_OVERRIDE", Value: capability.CAP_MAC_OVERRIDE, Enabled: false},
  30. {Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN, Enabled: false},
  31. {Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN, Enabled: false},
  32. }
  33. )
  34. type (
  35. Namespace struct {
  36. Key string `json:"key,omitempty"`
  37. Enabled bool `json:"enabled,omitempty"`
  38. Value int `json:"value,omitempty"`
  39. File string `json:"file,omitempty"`
  40. }
  41. Namespaces []*Namespace
  42. )
  43. func (ns *Namespace) String() string {
  44. return ns.Key
  45. }
  46. func GetNamespace(key string) *Namespace {
  47. for _, ns := range namespaceList {
  48. if ns.Key == key {
  49. cpy := *ns
  50. return &cpy
  51. }
  52. }
  53. return nil
  54. }
  55. // Contains returns true if the specified Namespace is
  56. // in the slice
  57. func (n Namespaces) Contains(ns string) bool {
  58. return n.Get(ns) != nil
  59. }
  60. func (n Namespaces) Get(ns string) *Namespace {
  61. for _, nsp := range n {
  62. if nsp != nil && nsp.Key == ns {
  63. return nsp
  64. }
  65. }
  66. return nil
  67. }
  68. type (
  69. Capability struct {
  70. Key string `json:"key,omitempty"`
  71. Enabled bool `json:"enabled"`
  72. Value capability.Cap `json:"value,omitempty"`
  73. }
  74. Capabilities []*Capability
  75. )
  76. func (c *Capability) String() string {
  77. return c.Key
  78. }
  79. func GetCapability(key string) *Capability {
  80. for _, capp := range capabilityList {
  81. if capp.Key == key {
  82. cpy := *capp
  83. return &cpy
  84. }
  85. }
  86. return nil
  87. }
  88. // Contains returns true if the specified Capability is
  89. // in the slice
  90. func (c Capabilities) Contains(capp string) bool {
  91. return c.Get(capp) != nil
  92. }
  93. func (c Capabilities) Get(capp string) *Capability {
  94. for _, cap := range c {
  95. if cap.Key == capp {
  96. return cap
  97. }
  98. }
  99. return nil
  100. }