types.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. {Key: "SETUID", Value: capability.CAP_SETUID},
  51. {Key: "SETGID", Value: capability.CAP_SETGID},
  52. {Key: "CHOWN", Value: capability.CAP_CHOWN},
  53. {Key: "NET_RAW", Value: capability.CAP_NET_RAW},
  54. {Key: "DAC_OVERRIDE", Value: capability.CAP_DAC_OVERRIDE},
  55. {Key: "FOWNER", Value: capability.CAP_FOWNER},
  56. }
  57. )
  58. type (
  59. Namespace struct {
  60. Key string `json:"key,omitempty"`
  61. Value int `json:"value,omitempty"`
  62. File string `json:"file,omitempty"`
  63. }
  64. Namespaces []*Namespace
  65. )
  66. func (ns *Namespace) String() string {
  67. return ns.Key
  68. }
  69. func GetNamespace(key string) *Namespace {
  70. for _, ns := range namespaceList {
  71. if ns.Key == key {
  72. cpy := *ns
  73. return &cpy
  74. }
  75. }
  76. return nil
  77. }
  78. // Contains returns true if the specified Namespace is
  79. // in the slice
  80. func (n Namespaces) Contains(ns string) bool {
  81. return n.Get(ns) != nil
  82. }
  83. func (n Namespaces) Get(ns string) *Namespace {
  84. for _, nsp := range n {
  85. if nsp != nil && nsp.Key == ns {
  86. return nsp
  87. }
  88. }
  89. return nil
  90. }
  91. type (
  92. Capability struct {
  93. Key string `json:"key,omitempty"`
  94. Value capability.Cap `json:"value,omitempty"`
  95. }
  96. Capabilities []*Capability
  97. )
  98. func (c *Capability) String() string {
  99. return c.Key
  100. }
  101. func GetCapability(key string) *Capability {
  102. for _, capp := range capabilityList {
  103. if capp.Key == key {
  104. cpy := *capp
  105. return &cpy
  106. }
  107. }
  108. return nil
  109. }
  110. func GetAllCapabilities() []string {
  111. output := make([]string, len(capabilityList))
  112. for i, capability := range capabilityList {
  113. output[i] = capability.String()
  114. }
  115. return output
  116. }
  117. // Contains returns true if the specified Capability is
  118. // in the slice
  119. func (c Capabilities) Contains(capp string) bool {
  120. return c.Get(capp) != nil
  121. }
  122. func (c Capabilities) Get(capp string) *Capability {
  123. for _, cap := range c {
  124. if cap.Key == capp {
  125. return cap
  126. }
  127. }
  128. return nil
  129. }