keymanager.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package keymanager
  2. // keymanager does the allocation, rotation and distribution of symmetric
  3. // keys to the agents. This is to securely bootstrap network communication
  4. // between agents. It can be used for encrypting gossip between the agents
  5. // which is used to exchange service discovery and overlay network control
  6. // plane information. It can also be used to encrypt overlay data traffic.
  7. import (
  8. cryptorand "crypto/rand"
  9. "encoding/binary"
  10. "sync"
  11. "time"
  12. "github.com/docker/swarmkit/api"
  13. "github.com/docker/swarmkit/log"
  14. "github.com/docker/swarmkit/manager/state/store"
  15. "github.com/pkg/errors"
  16. "golang.org/x/net/context"
  17. )
  18. const (
  19. // DefaultKeyLen is the default length (in bytes) of the key allocated
  20. DefaultKeyLen = 16
  21. // DefaultKeyRotationInterval used by key manager
  22. DefaultKeyRotationInterval = 12 * time.Hour
  23. // SubsystemGossip handles gossip protocol between the agents
  24. SubsystemGossip = "networking:gossip"
  25. // SubsystemIPSec is overlay network data encryption subsystem
  26. SubsystemIPSec = "networking:ipsec"
  27. // DefaultSubsystem is gossip
  28. DefaultSubsystem = SubsystemGossip
  29. // number of keys to mainrain in the key ring.
  30. keyringSize = 3
  31. )
  32. // map of subsystems and corresponding encryption algorithm. Initially only
  33. // AES_128 in GCM mode is supported.
  34. var subsysToAlgo = map[string]api.EncryptionKey_Algorithm{
  35. SubsystemGossip: api.AES_128_GCM,
  36. SubsystemIPSec: api.AES_128_GCM,
  37. }
  38. type keyRing struct {
  39. lClock uint64
  40. keys []*api.EncryptionKey
  41. }
  42. // Config for the keymanager that can be modified
  43. type Config struct {
  44. ClusterName string
  45. Keylen int
  46. RotationInterval time.Duration
  47. Subsystems []string
  48. }
  49. // KeyManager handles key allocation, rotation & distribution
  50. type KeyManager struct {
  51. config *Config
  52. store *store.MemoryStore
  53. keyRing *keyRing
  54. ctx context.Context
  55. cancel context.CancelFunc
  56. mu sync.Mutex
  57. }
  58. // DefaultConfig provides the default config for keymanager
  59. func DefaultConfig() *Config {
  60. return &Config{
  61. ClusterName: store.DefaultClusterName,
  62. Keylen: DefaultKeyLen,
  63. RotationInterval: DefaultKeyRotationInterval,
  64. Subsystems: []string{SubsystemGossip, SubsystemIPSec},
  65. }
  66. }
  67. // New creates an instance of keymanager with the given config
  68. func New(store *store.MemoryStore, config *Config) *KeyManager {
  69. for _, subsys := range config.Subsystems {
  70. if subsys != SubsystemGossip && subsys != SubsystemIPSec {
  71. return nil
  72. }
  73. }
  74. return &KeyManager{
  75. config: config,
  76. store: store,
  77. keyRing: &keyRing{lClock: genSkew()},
  78. }
  79. }
  80. func (k *KeyManager) allocateKey(ctx context.Context, subsys string) *api.EncryptionKey {
  81. key := make([]byte, k.config.Keylen)
  82. _, err := cryptorand.Read(key)
  83. if err != nil {
  84. panic(errors.Wrap(err, "key generated failed"))
  85. }
  86. k.keyRing.lClock++
  87. return &api.EncryptionKey{
  88. Subsystem: subsys,
  89. Algorithm: subsysToAlgo[subsys],
  90. Key: key,
  91. LamportTime: k.keyRing.lClock,
  92. }
  93. }
  94. func (k *KeyManager) updateKey(cluster *api.Cluster) error {
  95. return k.store.Update(func(tx store.Tx) error {
  96. cluster = store.GetCluster(tx, cluster.ID)
  97. if cluster == nil {
  98. return nil
  99. }
  100. cluster.EncryptionKeyLamportClock = k.keyRing.lClock
  101. cluster.NetworkBootstrapKeys = k.keyRing.keys
  102. return store.UpdateCluster(tx, cluster)
  103. })
  104. }
  105. func (k *KeyManager) rotateKey(ctx context.Context) error {
  106. var (
  107. clusters []*api.Cluster
  108. err error
  109. )
  110. k.store.View(func(readTx store.ReadTx) {
  111. clusters, err = store.FindClusters(readTx, store.ByName(k.config.ClusterName))
  112. })
  113. if err != nil {
  114. log.G(ctx).Errorf("reading cluster config failed, %v", err)
  115. return err
  116. }
  117. cluster := clusters[0]
  118. if len(cluster.NetworkBootstrapKeys) == 0 {
  119. panic(errors.New("no key in the cluster config"))
  120. }
  121. subsysKeys := map[string][]*api.EncryptionKey{}
  122. for _, key := range k.keyRing.keys {
  123. subsysKeys[key.Subsystem] = append(subsysKeys[key.Subsystem], key)
  124. }
  125. k.keyRing.keys = []*api.EncryptionKey{}
  126. // We maintain the latest key and the one before in the key ring to allow
  127. // agents to communicate without disruption on key change.
  128. for subsys, keys := range subsysKeys {
  129. if len(keys) == keyringSize {
  130. min := 0
  131. for i, key := range keys[1:] {
  132. if key.LamportTime < keys[min].LamportTime {
  133. min = i
  134. }
  135. }
  136. keys = append(keys[0:min], keys[min+1:]...)
  137. }
  138. keys = append(keys, k.allocateKey(ctx, subsys))
  139. subsysKeys[subsys] = keys
  140. }
  141. for _, keys := range subsysKeys {
  142. k.keyRing.keys = append(k.keyRing.keys, keys...)
  143. }
  144. return k.updateKey(cluster)
  145. }
  146. // Run starts the keymanager, it doesn't return
  147. func (k *KeyManager) Run(ctx context.Context) error {
  148. k.mu.Lock()
  149. ctx = log.WithModule(ctx, "keymanager")
  150. var (
  151. clusters []*api.Cluster
  152. err error
  153. )
  154. k.store.View(func(readTx store.ReadTx) {
  155. clusters, err = store.FindClusters(readTx, store.ByName(k.config.ClusterName))
  156. })
  157. if err != nil {
  158. log.G(ctx).Errorf("reading cluster config failed, %v", err)
  159. k.mu.Unlock()
  160. return err
  161. }
  162. cluster := clusters[0]
  163. if len(cluster.NetworkBootstrapKeys) == 0 {
  164. for _, subsys := range k.config.Subsystems {
  165. for i := 0; i < keyringSize; i++ {
  166. k.keyRing.keys = append(k.keyRing.keys, k.allocateKey(ctx, subsys))
  167. }
  168. }
  169. if err := k.updateKey(cluster); err != nil {
  170. log.G(ctx).Errorf("store update failed %v", err)
  171. }
  172. } else {
  173. k.keyRing.lClock = cluster.EncryptionKeyLamportClock
  174. k.keyRing.keys = cluster.NetworkBootstrapKeys
  175. }
  176. ticker := time.NewTicker(k.config.RotationInterval)
  177. defer ticker.Stop()
  178. k.ctx, k.cancel = context.WithCancel(ctx)
  179. k.mu.Unlock()
  180. for {
  181. select {
  182. case <-ticker.C:
  183. k.rotateKey(ctx)
  184. case <-k.ctx.Done():
  185. return nil
  186. }
  187. }
  188. }
  189. // Stop stops the running instance of key manager
  190. func (k *KeyManager) Stop() error {
  191. k.mu.Lock()
  192. defer k.mu.Unlock()
  193. if k.cancel == nil {
  194. return errors.New("keymanager is not started")
  195. }
  196. k.cancel()
  197. return nil
  198. }
  199. // genSkew generates a random uint64 number between 0 and 65535
  200. func genSkew() uint64 {
  201. b := make([]byte, 2)
  202. if _, err := cryptorand.Read(b); err != nil {
  203. panic(err)
  204. }
  205. return uint64(binary.BigEndian.Uint16(b))
  206. }