capabilities.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package capabilities
  14. import (
  15. "sync"
  16. )
  17. // Capabilities defines the set of capabilities available within the system.
  18. // For now these are global. Eventually they may be per-user
  19. type Capabilities struct {
  20. AllowPrivileged bool
  21. // Pod sources from which to allow privileged capabilities like host networking, sharing the host
  22. // IPC namespace, and sharing the host PID namespace.
  23. PrivilegedSources PrivilegedSources
  24. // PerConnectionBandwidthLimitBytesPerSec limits the throughput of each connection (currently only used for proxy, exec, attach)
  25. PerConnectionBandwidthLimitBytesPerSec int64
  26. }
  27. // PrivilegedSources defines the pod sources allowed to make privileged requests for certain types
  28. // of capabilities like host networking, sharing the host IPC namespace, and sharing the host PID namespace.
  29. type PrivilegedSources struct {
  30. // List of pod sources for which using host network is allowed.
  31. HostNetworkSources []string
  32. // List of pod sources for which using host pid namespace is allowed.
  33. HostPIDSources []string
  34. // List of pod sources for which using host ipc is allowed.
  35. HostIPCSources []string
  36. }
  37. // TODO: Clean these up into a singleton
  38. var once sync.Once
  39. var lock sync.Mutex
  40. var capabilities *Capabilities
  41. // Initialize the capability set. This can only be done once per binary, subsequent calls are ignored.
  42. func Initialize(c Capabilities) {
  43. // Only do this once
  44. once.Do(func() {
  45. capabilities = &c
  46. })
  47. }
  48. // Setup the capability set. It wraps Initialize for improving usability.
  49. func Setup(allowPrivileged bool, privilegedSources PrivilegedSources, perConnectionBytesPerSec int64) {
  50. Initialize(Capabilities{
  51. AllowPrivileged: allowPrivileged,
  52. PrivilegedSources: privilegedSources,
  53. PerConnectionBandwidthLimitBytesPerSec: perConnectionBytesPerSec,
  54. })
  55. }
  56. // SetCapabilitiesForTests. Convenience method for testing. This should only be called from tests.
  57. func SetForTests(c Capabilities) {
  58. lock.Lock()
  59. defer lock.Unlock()
  60. capabilities = &c
  61. }
  62. // Returns a read-only copy of the system capabilities.
  63. func Get() Capabilities {
  64. lock.Lock()
  65. defer lock.Unlock()
  66. // This check prevents clobbering of capabilities that might've been set via SetForTests
  67. if capabilities == nil {
  68. Initialize(Capabilities{
  69. AllowPrivileged: false,
  70. PrivilegedSources: PrivilegedSources{
  71. HostNetworkSources: []string{},
  72. HostPIDSources: []string{},
  73. HostIPCSources: []string{},
  74. },
  75. })
  76. }
  77. return *capabilities
  78. }