-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdevicemanager.go
283 lines (261 loc) · 8.08 KB
/
devicemanager.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"sync"
"time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"gopkg.in/square/go-jose.v2/jwt"
)
func init() {
rand.Seed(time.Now().Unix())
}
// DeviceManager embeds an AgentDevice and implements functionality related to
// configuring the device and system based on information retrieved from
// wiresteward servers.
type DeviceManager struct {
agentDevice
cachedToken string // cache the token on every renew lease request in case we need to use it on a renewal triggered by healthchecks
configMutex sync.Mutex
config *WirestewardPeerConfig // To keep the current config
serverURLs []string
backoff *backoff // backoff timer for retries to get a new lease
healthCheck *healthCheck // Pointer to the device manager running healthchek
healthCheckConf agentHealthCheckConfig
renewLeaseChan chan struct{}
stopLeaseBackoff chan struct{}
inBackoffLoop bool // bool to signal if there is a backoff loop in progress
httpClientTimeout Duration
}
func newDeviceManager(deviceName string, mtu int, wirestewardURLs []string, httpClientTimeout Duration, hcc agentHealthCheckConfig) *DeviceManager {
var device agentDevice
if *flagDeviceType == "wireguard" {
device = newWireguardDevice(deviceName, mtu)
} else {
device = newTunDevice(deviceName, mtu)
}
return &DeviceManager{
agentDevice: device,
serverURLs: wirestewardURLs,
backoff: newBackoff(1*time.Second, 64*time.Second, 2),
healthCheck: &healthCheck{running: false},
healthCheckConf: hcc,
renewLeaseChan: make(chan struct{}),
stopLeaseBackoff: make(chan struct{}),
inBackoffLoop: false,
httpClientTimeout: httpClientTimeout,
}
}
func (dm *DeviceManager) isHealthChecked() bool {
return len(dm.serverURLs) > 1
}
// Run starts the AgentDevice by calling its Run() method and proceeds to
// initialise it.
func (dm *DeviceManager) Run() error {
if err := dm.agentDevice.Run(); err != nil {
return fmt.Errorf("Error starting device `%s`: %w", dm.Name(), err)
}
if err := dm.ensureLinkUp(); err != nil {
return err
}
// Check if there is a private key or generate one
_, privKey, err := getKeys(dm.Name())
if err != nil {
return fmt.Errorf("Cannot get keys for device `%s`: %w", dm.Name(), err)
}
// the base64 value of an empty key will come as
// AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
if privKey == "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" {
logger.Verbosef(
"No keys found for device `%s`, generating a new pair",
dm.Name(),
)
newKey, err := wgtypes.GeneratePrivateKey()
if err != nil {
return err
}
if err := setPrivateKey(dm.Name(), newKey.String()); err != nil {
return err
}
}
if len(dm.serverURLs) > 0 {
go dm.renewLoop()
}
return nil
}
func (dm *DeviceManager) renewLoop() {
for {
select {
case <-dm.renewLeaseChan:
logger.Verbosef("Renewing lease for device:%s\n", dm.Name())
if err := dm.renewLease(); err != nil {
if err == jwt.ErrExpired {
// stop retrying - token is expired, it will need manual refresh
logger.Errorf("%v", err)
break
}
go func() {
dm.inBackoffLoop = true
duration := dm.backoff.Duration()
logger.Errorf("Cannot update lease for %s, will retry in %s: %s", dm.Name(), duration, err)
select {
case <-time.After(duration):
dm.renewLeaseChan <- struct{}{}
case <-dm.stopLeaseBackoff:
break
}
dm.inBackoffLoop = false
}()
} else {
dm.backoff.Reset()
}
}
}
}
func (dm *DeviceManager) nextServer() string {
return dm.serverURLs[rand.Intn(len(dm.serverURLs))]
}
// RenewTokenAndLease is called via the agent to renew the cached token data and
// trigger a lease renewal
func (dm *DeviceManager) RenewTokenAndLease(token string) {
dm.cachedToken = token
dm.healthCheck.Stop() // stop a running healthcheck that could also trigger renewals
if dm.inBackoffLoop {
dm.stopLeaseBackoff <- struct{}{} // Stop existing backoff loops
}
dm.backoff.Reset() // Reset backoff timer
dm.renewLeaseChan <- struct{}{}
}
// renewLease uses the provided oauth2 token to retrieve a new leases from one
// of the healthy wiresteward servers associated with the underlying device. If
// healthchecks are disabled then all serveres would be considered healthy. The
// received configuration is then applied to the device.
func (dm *DeviceManager) renewLease() error {
if dm.cachedToken == "" {
return fmt.Errorf("Empty cached token")
}
if err := validateJWTToken(dm.cachedToken); err != nil {
return err
}
publicKey, _, err := getKeys(dm.Name())
if err != nil {
return fmt.Errorf("Could not get keys from device %s: %w", dm.Name(), err)
}
serverURL := dm.nextServer()
if serverURL == "" {
return fmt.Errorf("No healthy servers found for device: %s", dm.Name())
}
oldConfig := dm.config
peers := []wgtypes.PeerConfig{}
config, wgServerAddr, err := requestWirestewardPeerConfig(serverURL, dm.cachedToken, publicKey, dm.httpClientTimeout)
if err != nil {
logger.Errorf(
"Could not get wiresteward peer config from `%s`: %v",
serverURL,
err,
)
return err
}
peers = append(peers, *config.PeerConfig)
dm.configMutex.Lock()
logger.Verbosef(
"Configuring offered ip address %s on device %s",
config.LocalAddress,
dm.Name(),
)
// TODO: Depending on the implementation of updateDeviceConfig, if the
// update fails partially, we might end up with the wrong "old" config
// and fail to cleanup properly when we update the next time.
if err := dm.updateDeviceConfig(oldConfig, config); err != nil {
logger.Errorf(
"Could not update peer configuration for `%s`: %v",
serverURL,
err,
)
} else {
dm.config = config
}
dm.configMutex.Unlock()
if err := setPeers(dm.Name(), peers); err != nil {
return fmt.Errorf("Error setting new peers for device %s: %w", dm.Name(), err)
}
// Start health checking if we have an address for the server wg client
// and more servers to potentially fell over.
if wgServerAddr != "" && len(dm.serverURLs) > 1 {
dm.healthCheck.Stop()
hc, err := newHealthCheck(
dm.Name(),
wgServerAddr,
dm.healthCheckConf.Interval,
dm.healthCheckConf.IntervalAfterFailure,
dm.healthCheckConf.Timeout,
dm.healthCheckConf.Threshold,
dm.renewLeaseChan,
)
if err != nil {
return fmt.Errorf("Cannot create healthchek: %v", err)
}
dm.healthCheck = hc
go dm.healthCheck.Run()
}
return nil
}
// WirestewardPeerConfig embeds wgtypes.PeerConfig and additional configuration
// received from a wiresteward server.
type WirestewardPeerConfig struct {
*wgtypes.PeerConfig
LocalAddress *net.IPNet
}
func newWirestewardPeerConfigFromLeaseResponse(lr *leaseResponse) (*WirestewardPeerConfig, string, error) {
ip, mask, err := net.ParseCIDR(lr.IP)
if err != nil {
return nil, "", err
}
address := &net.IPNet{IP: ip, Mask: mask.Mask}
pc, err := newPeerConfig(lr.PubKey, "", lr.Endpoint, lr.AllowedIPs)
if err != nil {
return nil, "", err
}
return &WirestewardPeerConfig{
PeerConfig: pc,
LocalAddress: address,
}, lr.ServerWireguardIP, nil
}
func requestWirestewardPeerConfig(serverURL, token, publicKey string, timeout Duration) (*WirestewardPeerConfig, string, error) {
// Marshal key into json
r, err := json.Marshal(&leaseRequest{PubKey: publicKey})
if err != nil {
return nil, "", err
}
// Prepare the request
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/newPeerLease", serverURL),
bytes.NewBuffer(r),
)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{Timeout: timeout.Duration}
resp, err := client.Do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, "", fmt.Errorf("Response status: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, "", fmt.Errorf("error reading response body: %w,", err)
}
response := &leaseResponse{}
if err := json.Unmarshal(body, response); err != nil {
return nil, "", err
}
return newWirestewardPeerConfigFromLeaseResponse(response)
}