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