Repository navigation
Expand file tree
/
Copy pathgateways.go
More file actions
254 lines (236 loc) · 9.3 KB
/
Copy pathgateways.go
File metadata and controls
254 lines (236 loc) · 9.3 KB
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
package firezone
import (
"context"
"time"
)
// Gateway is a Firezone Gateway - a host that exposes a Site's
// Resources to Clients.
//
// IPv4 and IPv6 are the Gateway's tunnel addresses, allocated when the
// Gateway is created rather than on first connect, so both are always
// populated - including on a Gateway that has never connected. See
// LastSeenRemoteIP on the API's own responses for the public address.
type Gateway struct {
ID string `json:"id"`
Name string `json:"name"`
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
Online bool `json:"online"`
// GatewayTokenID is the token this Gateway last connected with.
// Empty until the Gateway connects for the first time.
GatewayTokenID string `json:"gateway_token_id,omitempty"`
// RotatedAt is when the token named by GatewayTokenID was rotated
// out, and is nil in the normal case. A non-nil value means a
// replacement token has been minted and this Gateway has not picked
// it up yet - see [Gateway.RotationPending].
RotatedAt *time.Time `json:"rotated_at,omitempty"`
}
// RotationPending reports whether a replacement token has been minted
// that this Gateway has not yet connected with.
//
// While it is true the current token stays valid only until the Gateway
// connects with the replacement or the API's rotation grace period
// elapses from [Gateway.RotatedAt], whichever comes first. A Gateway
// left in this state past the grace period is stranded: the API deletes
// the previous token once the replacement is confirmed, so rolling a
// host's configuration back to it will not work.
func (g *Gateway) RotationPending() bool {
return g.RotatedAt != nil
}
// ProvisionedGateway is a newly provisioned Gateway along with its
// one-time token secret, returned only from [GatewaysService.Provision].
// The API never re-exposes the token after creation - see
// [GatewaysService.Get], which returns a plain [Gateway] with no token
// field at all, so the type system itself prevents a caller from
// expecting a token after a refresh.
type ProvisionedGateway struct {
Gateway
// Token is the one-time Gateway token secret. Store it securely -
// it cannot be retrieved again.
Token string `json:"token"`
}
// ProvisionGatewayRequest is the request body for
// [GatewaysService.Provision]. Name is optional; the API generates a
// random name when omitted.
type ProvisionGatewayRequest struct {
Name string `json:"name,omitempty"`
}
// RotatedGatewayToken is the replacement token minted by
// [GatewaysService.RotateToken]. Like [ProvisionedGateway.Token], the
// secret is shown exactly once.
type RotatedGatewayToken struct {
// ID is the new token's ID, which becomes the Gateway's
// GatewayTokenID once it connects with this token.
ID string `json:"id"`
// Token is the replacement secret. Store it securely - it cannot be
// retrieved again.
Token string `json:"token"`
}
// UpdateGatewayRequest is the request body for [GatewaysService.Update].
// Name is the only mutable field - a Gateway's Site is permanent.
type UpdateGatewayRequest struct {
Name string `json:"name"`
}
// GatewaysService manages the Gateways belonging to a single Site.
// Obtain one via [SitesService.Gateways].
//
// # Token lifecycle
//
// A Gateway has at most one active token, and this service covers the
// whole of that token's life: [GatewaysService.Provision] creates the
// Gateway and mints its token together, [GatewaysService.RotateToken]
// replaces it, and [GatewaysService.Delete] destroys the Gateway and
// revokes it.
//
// Two API endpoints are deliberately left out of that set:
//
// - POST /sites/{site_id}/gateways/{gateway_id}/token creates a token
// for a Gateway that has none. Every Gateway this SDK creates goes
// through Provision, which already mints one, so calling it would
// always fail with 409 Conflict - the API allows only one active
// token per Gateway and directs callers to rotate instead. The
// endpoint is only useful for a Gateway created elsewhere, such as
// in the admin portal.
// - POST /sites/{site_id}/gateway_tokens creates a multi-owner token
// shared by all of a Site's Gateways. The API marks it deprecated in
// favour of the per-Gateway endpoint above.
//
// The DELETE counterparts under /sites/{site_id}/gateway_tokens are
// absent for the same reason: a token this SDK can create belongs to a
// Gateway, and deleting that Gateway revokes it. The one case this
// leaves uncovered is a Gateway stranded past a rotation grace period
// (see [Gateway.RotationPending]), which is recovered by deleting and
// re-provisioning the Gateway rather than by deleting its token.
//
// If you need to adopt Gateways created outside this SDK, the token
// endpoints above are the gap to fill - adding them is additive, and
// [IsConflict] is already here for the 409 the first one returns.
type GatewaysService struct {
client *Client
siteID string
}
func (s *GatewaysService) basePath(segments ...string) string {
return buildPath(append([]string{"sites", s.siteID, "gateways"}, segments...)...)
}
// Get fetches a single Gateway by ID.
func (s *GatewaysService) Get(ctx context.Context, id string) (*Gateway, error) {
if err := checkID("site ID", s.siteID); err != nil {
return nil, err
}
if err := checkID("Gateway ID", id); err != nil {
return nil, err
}
var gateway Gateway
if err := s.client.do(ctx, "GET", s.basePath(id), nil, nil, &gateway); err != nil {
return nil, err
}
return &gateway, nil
}
// GatewayListOptions extends ListOptions with Gateways-specific
// filters. There's no SiteID filter here - the Site is already fixed
// by which [GatewaysService] you called List on (see
// [SitesService.Gateways]).
type GatewayListOptions struct {
ListOptions
// Name filters to the Gateway with this exact name.
Name string
// IPv4 filters to the Gateway with this exact IPv4 address.
IPv4 string
// IPv6 filters to the Gateway with this exact IPv6 address.
IPv6 string
}
// List returns a page of the Site's Gateways. Pass nil for opts to use
// the API's default page size and no filters.
func (s *GatewaysService) List(ctx context.Context, opts *GatewayListOptions) (*Page[Gateway], error) {
if err := checkID("site ID", s.siteID); err != nil {
return nil, err
}
if opts == nil {
opts = &GatewayListOptions{}
}
q := filterQuery(opts.ListOptions,
[2]string{"name", opts.Name},
[2]string{"ipv4", opts.IPv4},
[2]string{"ipv6", opts.IPv6},
)
return doList[Gateway](ctx, s.client, "GET", s.basePath(), q)
}
// Provision creates a new Gateway and mints its single-owner token in
// one call. The returned Token is shown once - store it securely.
func (s *GatewaysService) Provision(ctx context.Context, req *ProvisionGatewayRequest) (*ProvisionedGateway, error) {
if err := checkID("site ID", s.siteID); err != nil {
return nil, err
}
body, err := wrapBody("gateway", req)
if err != nil {
return nil, err
}
var provisioned ProvisionedGateway
if err := s.client.do(ctx, "POST", s.basePath(), nil, body, &provisioned); err != nil {
return nil, err
}
return &provisioned, nil
}
// Update renames a Gateway.
func (s *GatewaysService) Update(ctx context.Context, id string, req *UpdateGatewayRequest) (*Gateway, error) {
if err := checkID("site ID", s.siteID); err != nil {
return nil, err
}
if err := checkID("Gateway ID", id); err != nil {
return nil, err
}
body, err := wrapBody("gateway", req)
if err != nil {
return nil, err
}
var gateway Gateway
if err := s.client.do(ctx, "PATCH", s.basePath(id), nil, body, &gateway); err != nil {
return nil, err
}
return &gateway, nil
}
// RotateToken mints a replacement single-owner token for the Gateway,
// returning the new secret once.
//
// The Gateway's current token is not invalidated immediately: it keeps
// working until the Gateway first connects with the replacement or the
// API's grace period elapses, whichever comes first. That window is the
// point - it exists so the replacement can be delivered to the Gateway
// host without downtime.
//
// Two consequences worth planning for:
//
// - Deliver the replacement and restart the Gateway before the grace
// period expires, or the Gateway is stranded. Poll
// [Gateway.RotationPending] to confirm pickup.
// - Once pickup is confirmed the previous token is deleted, so rolling
// a host's configuration back to it will not work.
//
// Rotating again before the Gateway picks up a pending replacement
// replaces only that pending token; the in-use one keeps its original
// deadline.
func (s *GatewaysService) RotateToken(ctx context.Context, id string) (*RotatedGatewayToken, error) {
if err := checkID("site ID", s.siteID); err != nil {
return nil, err
}
if err := checkID("Gateway ID", id); err != nil {
return nil, err
}
var rotated RotatedGatewayToken
if err := s.client.do(ctx, "POST", s.basePath(id, "token", "rotate"), nil, nil, &rotated); err != nil {
return nil, err
}
return &rotated, nil
}
// Delete deletes a Gateway, revoking its token. This is the only way
// this SDK revokes a token: see the [GatewaysService] doc comment for
// why the API's standalone token-deletion endpoints are not wrapped.
func (s *GatewaysService) Delete(ctx context.Context, id string) error {
if err := checkID("site ID", s.siteID); err != nil {
return err
}
if err := checkID("Gateway ID", id); err != nil {
return err
}
return s.client.do(ctx, "DELETE", s.basePath(id), nil, nil, nil)
}