types.go 3.0 KB

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