-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdevices.go
311 lines (263 loc) · 10.6 KB
/
devices.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) David Bond, Tailscale Inc, & Contributors
// SPDX-License-Identifier: MIT
package tailscale
import (
"context"
"encoding/json"
"net/http"
"time"
)
// DevicesResource provides access to https://tailscale.com/api#tag/devices.
type DevicesResource struct {
*Client
}
type DeviceRoutes struct {
Advertised []string `json:"advertisedRoutes"`
Enabled []string `json:"enabledRoutes"`
}
// Time wraps a time and allows for unmarshalling timestamps that represent an empty time as an empty string (e.g "")
// this is used by the tailscale API when it returns devices that have no created date, such as its hello service.
type Time struct {
time.Time
}
// MarshalJSON is an implementation of json.Marshal.
func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Time)
}
// UnmarshalJSON unmarshals the content of data as a time.Time, a blank string will keep the time at its zero value.
func (t *Time) UnmarshalJSON(data []byte) error {
if string(data) == `""` {
return nil
}
if err := json.Unmarshal(data, &t.Time); err != nil {
return err
}
return nil
}
type DERPRegion struct {
Preferred bool `json:"preferred,omitempty"`
LatencyMilliseconds float64 `json:"latencyMs"`
}
type ClientSupports struct {
HairPinning bool `json:"hairPinning"`
IPV6 bool `json:"ipv6"`
PCP bool `json:"pcp"`
PMP bool `json:"pmp"`
UDP bool `json:"udp"`
UPNP bool `json:"upnp"`
}
type ClientConnectivity struct {
Endpoints []string `json:"endpoints"`
DERP string `json:"derp"`
MappingVariesByDestIP bool `json:"mappingVariesByDestIP"`
// DERPLatency is mapped by region name (e.g. "New York City", "Seattle").
DERPLatency map[string]DERPRegion `json:"latency"`
ClientSupports ClientSupports `json:"clientSupports"`
}
type Device struct {
Addresses []string `json:"addresses"`
Name string `json:"name"`
ID string `json:"id"` // The legacy identifier for a device. Use NodeId instead.
NodeID string `json:"nodeId"` // The preferred identifier for a device.
Authorized bool `json:"authorized"`
User string `json:"user"`
Tags []string `json:"tags"`
KeyExpiryDisabled bool `json:"keyExpiryDisabled"`
BlocksIncomingConnections bool `json:"blocksIncomingConnections"`
ClientVersion string `json:"clientVersion"`
Created Time `json:"created"`
Expires Time `json:"expires"`
Hostname string `json:"hostname"`
IsExternal bool `json:"isExternal"`
LastSeen Time `json:"lastSeen"`
MachineKey string `json:"machineKey"`
NodeKey string `json:"nodeKey"`
OS string `json:"os"`
TailnetLockError string `json:"tailnetLockError"`
TailnetLockKey string `json:"tailnetLockKey"`
UpdateAvailable bool `json:"updateAvailable"`
// The below are only included in listings when querying `all` fields.
AdvertisedRoutes []string `json:"AdvertisedRoutes"`
EnabledRoutes []string `json:"enabledRoutes"`
ClientConnectivity *ClientConnectivity `json:"clientConnectivity"`
}
type DevicePostureAttributes struct {
Attributes map[string]any `json:"attributes"`
Expiries map[string]Time `json:"expiries"`
}
type DevicePostureAttributeRequest struct {
Value any `json:"value"`
Expiry Time `json:"expiry"`
Comment string `json:"comment"`
}
// GetWithAllFields gets the [Device] identified by `deviceID`.
// All fields will be populated.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) GetWithAllFields(ctx context.Context, deviceID string) (*Device, error) {
return dr.get(ctx, deviceID, true)
}
// Get gets the [Device] identified by `deviceID`.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) Get(ctx context.Context, deviceID string) (*Device, error) {
return dr.get(ctx, deviceID, false)
}
func (dr *DevicesResource) get(ctx context.Context, deviceID string, allFields bool) (*Device, error) {
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildURL("device", deviceID))
if err != nil {
return nil, err
}
if allFields {
q := req.URL.Query()
q.Set("fields", "all")
req.URL.RawQuery = q.Encode()
}
return body[Device](dr, req)
}
// GetPostureAttributes retrieves the posture attributes of the device identified by deviceID.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) GetPostureAttributes(ctx context.Context, deviceID string) (*DevicePostureAttributes, error) {
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildURL("device", deviceID, "attributes"))
if err != nil {
return nil, err
}
return body[DevicePostureAttributes](dr, req)
}
// SetPostureAttribute sets the posture attribute of the device identified by deviceID.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SetPostureAttribute(ctx context.Context, deviceID, attributeKey string, request DevicePostureAttributeRequest) error {
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "attributes", attributeKey), requestBody(request))
if err != nil {
return err
}
return dr.do(req, nil)
}
// ListWithAllFields lists every [Device] in the tailnet. Each [Device] in
// the response will have all fields populated.
func (dr *DevicesResource) ListWithAllFields(ctx context.Context) ([]Device, error) {
return dr.list(ctx, true)
}
// List lists every [Device] in the tailnet. The fields `EnabledRoutes`,
// `AdvertisedRoutes` and `ClientConnectivity` will be omitted from the resulting
// [Devices]. To get these fields, use `ListWithAllFields`.
func (dr *DevicesResource) List(ctx context.Context) ([]Device, error) {
return dr.list(ctx, false)
}
func (dr *DevicesResource) list(ctx context.Context, allFields bool) ([]Device, error) {
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildTailnetURL("devices"))
if err != nil {
return nil, err
}
if allFields {
q := req.URL.Query()
q.Set("fields", "all")
req.URL.RawQuery = q.Encode()
}
m := make(map[string][]Device)
err = dr.do(req, &m)
if err != nil {
return nil, err
}
return m["devices"], nil
}
// SetAuthorized marks the specified device as authorized or not.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SetAuthorized(ctx context.Context, deviceID string, authorized bool) error {
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "authorized"), requestBody(map[string]bool{
"authorized": authorized,
}))
if err != nil {
return err
}
return dr.do(req, nil)
}
// Delete deletes the device identified by deviceID.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) Delete(ctx context.Context, deviceID string) error {
req, err := dr.buildRequest(ctx, http.MethodDelete, dr.buildURL("device", deviceID))
if err != nil {
return err
}
return dr.do(req, nil)
}
// SetName updates the name of the device identified by deviceID.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SetName(ctx context.Context, deviceID, name string) error {
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "name"), requestBody(map[string]string{
"name": name,
}))
if err != nil {
return err
}
return dr.do(req, nil)
}
// SetTags updates the tags of the device identified by deviceID.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SetTags(ctx context.Context, deviceID string, tags []string) error {
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "tags"), requestBody(map[string][]string{
"tags": tags,
}))
if err != nil {
return err
}
return dr.do(req, nil)
}
// DeviceKey type represents the properties of the key of an individual device within
// the tailnet.
type DeviceKey struct {
KeyExpiryDisabled bool `json:"keyExpiryDisabled"` // Whether or not this device's key will ever expire.
}
// SetKey updates the properties of a device's key.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SetKey(ctx context.Context, deviceID string, key DeviceKey) error {
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "key"), requestBody(key))
if err != nil {
return err
}
return dr.do(req, nil)
}
// SetDeviceIPv4Address sets the Tailscale IPv4 address of the device.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SetIPv4Address(ctx context.Context, deviceID string, ipv4Address string) error {
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "ip"), requestBody(map[string]string{
"ipv4": ipv4Address,
}))
if err != nil {
return err
}
return dr.do(req, nil)
}
// SetSubnetRoutes sets which subnet routes are enabled to be routed by a device by replacing the existing list
// of subnet routes with the supplied routes. Routes can be enabled without a device advertising them (e.g. for preauth).
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SetSubnetRoutes(ctx context.Context, deviceID string, routes []string) error {
req, err := dr.buildRequest(ctx, http.MethodPost, dr.buildURL("device", deviceID, "routes"), requestBody(map[string][]string{
"routes": routes,
}))
if err != nil {
return err
}
return dr.do(req, nil)
}
// SubnetRoutes Retrieves the list of subnet routes that a device is advertising, as well as those that are
// enabled for it. Enabled routes are not necessarily advertised (e.g. for pre-enabling), and likewise, advertised
// routes are not necessarily enabled.
//
// Using the device `NodeID` is preferred, but its numeric `ID` value can also be used.
func (dr *DevicesResource) SubnetRoutes(ctx context.Context, deviceID string) (*DeviceRoutes, error) {
req, err := dr.buildRequest(ctx, http.MethodGet, dr.buildURL("device", deviceID, "routes"))
if err != nil {
return nil, err
}
return body[DeviceRoutes](dr, req)
}