types.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. type Mounts []Mount
  12. func (s Mounts) OfType(t string) Mounts {
  13. out := Mounts{}
  14. for _, m := range s {
  15. if m.Type == t {
  16. out = append(out, m)
  17. }
  18. }
  19. return out
  20. }
  21. type Mount struct {
  22. Type string `json:"type,omitempty"`
  23. Source string `json:"source,omitempty"` // Source path, in the host namespace
  24. Destination string `json:"destination,omitempty"` // Destination path, in the container
  25. Writable bool `json:"writable,omitempty"`
  26. Private bool `json:"private,omitempty"`
  27. }
  28. // namespaceList is used to convert the libcontainer types
  29. // into the names of the files located in /proc/<pid>/ns/* for
  30. // each namespace
  31. var (
  32. namespaceList = Namespaces{}
  33. capabilityList = Capabilities{
  34. {Key: "SETPCAP", Value: capability.CAP_SETPCAP},
  35. {Key: "SYS_MODULE", Value: capability.CAP_SYS_MODULE},
  36. {Key: "SYS_RAWIO", Value: capability.CAP_SYS_RAWIO},
  37. {Key: "SYS_PACCT", Value: capability.CAP_SYS_PACCT},
  38. {Key: "SYS_ADMIN", Value: capability.CAP_SYS_ADMIN},
  39. {Key: "SYS_NICE", Value: capability.CAP_SYS_NICE},
  40. {Key: "SYS_RESOURCE", Value: capability.CAP_SYS_RESOURCE},
  41. {Key: "SYS_TIME", Value: capability.CAP_SYS_TIME},
  42. {Key: "SYS_TTY_CONFIG", Value: capability.CAP_SYS_TTY_CONFIG},
  43. {Key: "MKNOD", Value: capability.CAP_MKNOD},
  44. {Key: "AUDIT_WRITE", Value: capability.CAP_AUDIT_WRITE},
  45. {Key: "AUDIT_CONTROL", Value: capability.CAP_AUDIT_CONTROL},
  46. {Key: "MAC_OVERRIDE", Value: capability.CAP_MAC_OVERRIDE},
  47. {Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN},
  48. {Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN},
  49. {Key: "SYSLOG", Value: capability.CAP_SYSLOG},
  50. }
  51. )
  52. type (
  53. Namespace struct {
  54. Key string `json:"key,omitempty"`
  55. Value int `json:"value,omitempty"`
  56. File string `json:"file,omitempty"`
  57. }
  58. Namespaces []*Namespace
  59. )
  60. func (ns *Namespace) String() string {
  61. return ns.Key
  62. }
  63. func GetNamespace(key string) *Namespace {
  64. for _, ns := range namespaceList {
  65. if ns.Key == key {
  66. cpy := *ns
  67. return &cpy
  68. }
  69. }
  70. return nil
  71. }
  72. // Contains returns true if the specified Namespace is
  73. // in the slice
  74. func (n Namespaces) Contains(ns string) bool {
  75. return n.Get(ns) != nil
  76. }
  77. func (n Namespaces) Get(ns string) *Namespace {
  78. for _, nsp := range n {
  79. if nsp != nil && nsp.Key == ns {
  80. return nsp
  81. }
  82. }
  83. return nil
  84. }
  85. type (
  86. Capability struct {
  87. Key string `json:"key,omitempty"`
  88. Value capability.Cap `json:"value,omitempty"`
  89. }
  90. Capabilities []*Capability
  91. )
  92. func (c *Capability) String() string {
  93. return c.Key
  94. }
  95. func GetCapability(key string) *Capability {
  96. for _, capp := range capabilityList {
  97. if capp.Key == key {
  98. cpy := *capp
  99. return &cpy
  100. }
  101. }
  102. return nil
  103. }
  104. // Contains returns true if the specified Capability is
  105. // in the slice
  106. func (c Capabilities) Contains(capp string) bool {
  107. return c.Get(capp) != nil
  108. }
  109. func (c Capabilities) Get(capp string) *Capability {
  110. for _, cap := range c {
  111. if cap.Key == capp {
  112. return cap
  113. }
  114. }
  115. return nil
  116. }