Skip to content

Commit a770bcc

Browse files
authored
tailscale: add recently introduced fields (#55)
* tailscale: add SetDeviceAuthorized * tailscale: update ACL fields Signed-off-by: Anton Tolchanov <[email protected]>
1 parent a1f4f5d commit a770bcc

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

tailscale/client.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ type (
286286
DisableIPv4 bool `json:"disableIPv4,omitempty" hujson:"DisableIPv4,omitempty"`
287287
OneCGNATRoute string `json:"oneCGNATRoute,omitempty" hujson:"OneCGNATRoute,omitempty"`
288288
RandomizeClientPort bool `json:"randomizeClientPort,omitempty" hujson:"RandomizeClientPort,omitempty"`
289+
290+
// As of Aug 2023 these fields are experimental and subject to change.
291+
Postures map[string][]string `json:"postures,omitempty" hujson:"Postures,omitempty"`
292+
DefaultSourcePosture []string `json:"defaultSrcPosture,omitempty" hujson:"DefaultSrcPosture,omitempty"`
289293
}
290294

291295
ACLAutoApprovers struct {
@@ -300,6 +304,9 @@ type (
300304
Source []string `json:"src,omitempty" hujson:"Src,omitempty"`
301305
Destination []string `json:"dst,omitempty" hujson:"Dst,omitempty"`
302306
Protocol string `json:"proto,omitempty" hujson:"Proto,omitempty"`
307+
308+
// Experimental.
309+
SourcePosture []string `json:"srcPosture,omitempty" hujson:"SrcPosture,omitempty"`
303310
}
304311

305312
ACLTest struct {
@@ -338,11 +345,13 @@ type (
338345
}
339346

340347
ACLSSH struct {
341-
Action string `json:"action,omitempty" hujson:"Action,omitempty"`
342-
Users []string `json:"users,omitempty" hujson:"Users,omitempty"`
343-
Source []string `json:"src,omitempty" hujson:"Src,omitempty"`
344-
Destination []string `json:"dst,omitempty" hujson:"Dst,omitempty"`
345-
CheckPeriod Duration `json:"checkPeriod,omitempty" hujson:"CheckPeriod,omitempty"`
348+
Action string `json:"action,omitempty" hujson:"Action,omitempty"`
349+
Users []string `json:"users,omitempty" hujson:"Users,omitempty"`
350+
Source []string `json:"src,omitempty" hujson:"Src,omitempty"`
351+
Destination []string `json:"dst,omitempty" hujson:"Dst,omitempty"`
352+
CheckPeriod Duration `json:"checkPeriod,omitempty" hujson:"CheckPeriod,omitempty"`
353+
Recorder []string `json:"recorder,omitempty" hujson:"Recorder,omitempty"`
354+
EnforceRecorder bool `json:"enforceRecorder,omitempty" hujson:"EnforceRecorder,omitempty"`
346355
}
347356

348357
NodeAttrGrant struct {
@@ -550,10 +559,15 @@ func (c *Client) Devices(ctx context.Context) ([]Device, error) {
550559

551560
// AuthorizeDevice marks the specified device identifier as authorized to join the tailnet.
552561
func (c *Client) AuthorizeDevice(ctx context.Context, deviceID string) error {
562+
return c.SetDeviceAuthorized(ctx, deviceID, true)
563+
}
564+
565+
// SetDeviceAuthorized marks the specified device as authorized or not.
566+
func (c *Client) SetDeviceAuthorized(ctx context.Context, deviceID string, authorized bool) error {
553567
const uriFmt = "/api/v2/device/%s/authorized"
554568

555569
req, err := c.buildRequest(ctx, http.MethodPost, fmt.Sprintf(uriFmt, deviceID), nil, map[string]bool{
556-
"authorized": true,
570+
"authorized": authorized,
557571
})
558572
if err != nil {
559573
return err

tailscale/client_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,25 @@ func TestClient_AuthorizeDevice(t *testing.T) {
638638
assert.EqualValues(t, true, body["authorized"])
639639
}
640640

641+
func TestClient_SetDeviceAuthorized(t *testing.T) {
642+
t.Parallel()
643+
644+
client, server := NewTestHarness(t)
645+
server.ResponseCode = http.StatusOK
646+
647+
const deviceID = "test"
648+
649+
for _, value := range []bool{true, false} {
650+
assert.NoError(t, client.SetDeviceAuthorized(context.Background(), deviceID, value))
651+
assert.Equal(t, http.MethodPost, server.Method)
652+
assert.Equal(t, "/api/v2/device/test/authorized", server.Path)
653+
654+
body := make(map[string]bool)
655+
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
656+
assert.EqualValues(t, value, body["authorized"])
657+
}
658+
}
659+
641660
func TestClient_CreateKey(t *testing.T) {
642661
t.Parallel()
643662

0 commit comments

Comments
 (0)