types.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, Enabled: false},
  35. {Key: "SYS_MODULE", Value: capability.CAP_SYS_MODULE, Enabled: false},
  36. {Key: "SYS_RAWIO", Value: capability.CAP_SYS_RAWIO, Enabled: false},
  37. {Key: "SYS_PACCT", Value: capability.CAP_SYS_PACCT, Enabled: false},
  38. {Key: "SYS_ADMIN", Value: capability.CAP_SYS_ADMIN, Enabled: false},
  39. {Key: "SYS_NICE", Value: capability.CAP_SYS_NICE, Enabled: false},
  40. {Key: "SYS_RESOURCE", Value: capability.CAP_SYS_RESOURCE, Enabled: false},
  41. {Key: "SYS_TIME", Value: capability.CAP_SYS_TIME, Enabled: false},
  42. {Key: "SYS_TTY_CONFIG", Value: capability.CAP_SYS_TTY_CONFIG, Enabled: false},
  43. {Key: "MKNOD", Value: capability.CAP_MKNOD, Enabled: false},
  44. {Key: "AUDIT_WRITE", Value: capability.CAP_AUDIT_WRITE, Enabled: false},
  45. {Key: "AUDIT_CONTROL", Value: capability.CAP_AUDIT_CONTROL, Enabled: false},
  46. {Key: "MAC_OVERRIDE", Value: capability.CAP_MAC_OVERRIDE, Enabled: false},
  47. {Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN, Enabled: false},
  48. {Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN, Enabled: false},
  49. {Key: "SYSLOG", Value: capability.CAP_SYSLOG, Enabled: false},
  50. }
  51. )
  52. type (
  53. Namespace struct {
  54. Key string `json:"key,omitempty"`
  55. Enabled bool `json:"enabled,omitempty"`
  56. Value int `json:"value,omitempty"`
  57. File string `json:"file,omitempty"`
  58. }
  59. Namespaces []*Namespace
  60. )
  61. func (ns *Namespace) String() string {
  62. return ns.Key
  63. }
  64. func GetNamespace(key string) *Namespace {
  65. for _, ns := range namespaceList {
  66. if ns.Key == key {
  67. cpy := *ns
  68. return &cpy
  69. }
  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 n.Get(ns) != nil
  77. }
  78. func (n Namespaces) Get(ns string) *Namespace {
  79. for _, nsp := range n {
  80. if nsp != nil && nsp.Key == ns {
  81. return nsp
  82. }
  83. }
  84. return nil
  85. }
  86. type (
  87. Capability struct {
  88. Key string `json:"key,omitempty"`
  89. Enabled bool `json:"enabled"`
  90. Value capability.Cap `json:"value,omitempty"`
  91. }
  92. Capabilities []*Capability
  93. )
  94. func (c *Capability) String() string {
  95. return c.Key
  96. }
  97. func GetCapability(key string) *Capability {
  98. for _, capp := range capabilityList {
  99. if capp.Key == key {
  100. cpy := *capp
  101. return &cpy
  102. }
  103. }
  104. return nil
  105. }
  106. // Contains returns true if the specified Capability is
  107. // in the slice
  108. func (c Capabilities) Contains(capp string) bool {
  109. return c.Get(capp) != nil
  110. }
  111. func (c Capabilities) Get(capp string) *Capability {
  112. for _, cap := range c {
  113. if cap.Key == capp {
  114. return cap
  115. }
  116. }
  117. return nil
  118. }