@@ -2,15 +2,15 @@ import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
2
2
3
3
export async function lambdaHandler ( ev : APIGatewayProxyEvent ) : Promise < APIGatewayProxyResult > {
4
4
// TODO: https://tailscale.com/kb/1213/webhooks#verifying-an-event-signature
5
- // console.log(`Received event: ${JSON.stringify(ev)}`); // TODO: add verbose logging flag?
5
+ // console.log(`Received event: ${JSON.stringify(ev)}`);
6
6
7
7
let processedCount = 0 ;
8
8
let ignoredCount = 0 ;
9
9
let erroredCount = 0 ;
10
10
try {
11
11
let decodedBody = ev . body ;
12
12
if ( ev . isBase64Encoded ) {
13
- decodedBody = Buffer . from ( ev . body ! , ' base64' ) . toString ( ' utf8' ) ;
13
+ decodedBody = Buffer . from ( ev . body ! , " base64" ) . toString ( " utf8" ) ;
14
14
}
15
15
const tailnetEvents : TailnetEvent [ ] = JSON . parse ( decodedBody ! ) ;
16
16
const results : ProcessingResult [ ] = [ ] ;
@@ -56,7 +56,6 @@ function generateResponseBody(statusCode: number, ev: APIGatewayProxyEvent, proc
56
56
statusCode : statusCode ,
57
57
body : JSON . stringify ( {
58
58
message : ( statusCode == 200 ? "ok" : "An error occurred." ) ,
59
- // requestId: ev.requestContext.requestId, // TODO: This requestId doesn't match what's in the lambda logs.
60
59
eventResults : {
61
60
processed : processedCount ,
62
61
errored : erroredCount ,
@@ -103,11 +102,11 @@ async function nodeNeedsApprovalHandler(event: TailnetEvent): Promise<Processing
103
102
[ "windows" , "macos" , "linux" ] . includes ( attributesResponseJson [ "attributes" ] [ "node:os" ] )
104
103
&& attributesResponseJson [ "attributes" ] [ "node:tsReleaseTrack" ] == "stable"
105
104
) {
106
- // approve device
107
- await approveDevice ( eventData ) ;
105
+ // authorize device
106
+ await authorizeDevice ( eventData ) ;
108
107
}
109
108
else {
110
- console . log ( `NOT approving device [${ eventData . nodeID } :${ eventData . deviceName } ] with attributes [${ JSON . stringify ( attributesResponseJson ) } ]` ) ;
109
+ console . log ( `NOT authorizing device [${ eventData . nodeID } :${ eventData . deviceName } ] with attributes [${ JSON . stringify ( attributesResponseJson ) } ]` ) ;
111
110
}
112
111
113
112
return { event : event , result : "SUCCESS" , } as ProcessingResult ;
@@ -120,7 +119,7 @@ export const ENV_TAILSCALE_OAUTH_CLIENT_ID = "OAUTH_CLIENT_ID";
120
119
export const ENV_TAILSCALE_OAUTH_CLIENT_SECRET = "OAUTH_CLIENT_SECRET" ;
121
120
const TAILSCALE_CONTROL_URL = "https://login.tailscale.com" ;
122
121
123
- // https://github .com/tailscale/tailscale/blob/main/publicapi/device.md#get-device-posture- attributes
122
+ // https://tailscale .com/api#tag/devices/GET/device/{deviceId}/ attributes
124
123
async function getDeviceAttributes ( event : TailnetEventDeviceData ) : Promise < Response > {
125
124
console . log ( `Getting device attributes [${ event . nodeID } ]` ) ;
126
125
const data = await makeAuthenticatedRequest ( "GET" , `${ TAILSCALE_CONTROL_URL } /api/v2/device/${ event . nodeID } /attributes` ) ;
@@ -130,7 +129,7 @@ async function getDeviceAttributes(event: TailnetEventDeviceData): Promise<Respo
130
129
return data ;
131
130
}
132
131
133
- // https://github .com/tailscale/tailscale/blob/main/publicapi/ device.md#get-device
132
+ // https://tailscale .com/api#tag/devices/GET/ device/{deviceId}
134
133
async function getDevice ( event : TailnetEventDeviceData ) : Promise < Response > {
135
134
console . log ( `Getting device [${ event . nodeID } ]` ) ;
136
135
const data = await makeAuthenticatedRequest ( "GET" , `${ TAILSCALE_CONTROL_URL } /api/v2/device/${ event . nodeID } ` ) ;
@@ -140,12 +139,12 @@ async function getDevice(event: TailnetEventDeviceData): Promise<Response> {
140
139
return data ;
141
140
}
142
141
143
- // https://github .com/tailscale/tailscale/blob/main/publicapi/device.md#authorize-device
144
- async function approveDevice ( device : TailnetEventDeviceData ) {
145
- console . log ( `Approving device [${ device . nodeID } :${ device . deviceName } ]` ) ;
142
+ // https://tailscale .com/api#tag/devices/POST/device/{deviceId}/authorized
143
+ async function authorizeDevice ( device : TailnetEventDeviceData ) {
144
+ console . log ( `Authorizing device [${ device . nodeID } :${ device . deviceName } ]` ) ;
146
145
const data = await makeAuthenticatedRequest ( "POST" , `${ TAILSCALE_CONTROL_URL } /api/v2/device/${ device . nodeID } /authorized` , JSON . stringify ( { "authorized" : true } ) ) ;
147
146
if ( ! data . ok ) {
148
- throw new Error ( `Failed to approve device [${ device . nodeID } :${ device . deviceName } ]` ) ;
147
+ throw new Error ( `Failed to authorize device [${ device . nodeID } :${ device . deviceName } ]` ) ;
149
148
}
150
149
}
151
150
@@ -185,7 +184,7 @@ const makeAuthenticatedRequest = async function (method: "GET" | "POST", url: st
185
184
}
186
185
187
186
async function httpsRequest ( url : string , options : any ) : Promise < Response > {
188
- // console.log(`Making HTTP request to [${url}] with options [${JSON.stringify(options)}]`); // TODO: add verbose logging flag?
187
+ // console.log(`Making HTTP request to [${url}] with options [${JSON.stringify(options)}]`);
189
188
return await fetch ( url , options ) ;
190
189
}
191
190
0 commit comments