@@ -291,13 +291,18 @@ func (d *Drawbridge) SetUpProtectedServiceTunnel() error {
291291 if emissaryRequestType != "PS_LIST" {
292292 emissaryRequestedServiceId = emissaryRequestPayload [8 :11 ]
293293 }
294+
295+ // Create and insert an Emissary event into the db.
294296 eventUUID , err := utils .NewUUID ()
295297 if err != nil {
296298 slog .Error ("Emissary Event" , slog .Any ("Error" , err ))
297299 }
300+ // Retrieve the client certificate from the connection by casting the connection as a tls.Conn.
301+ clientCert := clientConn .(* tls.Conn ).ConnectionState ().PeerCertificates [0 ]
302+ deviceUUID := clientCert .Subject .SerialNumber
298303 event := emissary.Event {
299304 ID : eventUUID ,
300- DeviceID : eventUUID ,
305+ DeviceID : deviceUUID ,
301306 ConnectionIP : conn .RemoteAddr ().String (),
302307 Type : emissaryRequestType ,
303308 TargetService : emissaryRequestedServiceId ,
@@ -312,7 +317,8 @@ func (d *Drawbridge) SetUpProtectedServiceTunnel() error {
312317 slog .Error ("Emissary Event" , slog .Any ("DB Error" , err ))
313318 }
314319
315- if emissaryRequestType == "PS_CONN" {
320+ switch emissaryRequestType {
321+ case "PS_CONN" :
316322 // May be used later after we standardize how and when to read the tcp connection into the buf above.
317323 // d.getRequestProtectedServiceName(clientConn)
318324 emissaryRequestedServiceIdNum , err := strconv .Atoi (emissaryRequestedServiceId )
@@ -357,18 +363,17 @@ func (d *Drawbridge) SetUpProtectedServiceTunnel() error {
357363 io .Copy (clientConn , resourceConn )
358364 // Shut down the connection.
359365 clientConn .Close ()
360-
361- } else {
366+ case "PS_LIST" :
362367 // On a new connection, write available services to TCP connection so Emissary can know which
363- // d.ProtectedServices
364-
368+ // Protected Services are available
365369 var serviceList string
366370 for _ , value := range d .ProtectedServices {
367371 // We pad the service id with zeros as we want a fixed-width id for easy parsing. This will allow support for up to 1000 Protected Services.
368372 serviceList += fmt .Sprintf ("%s%s," , utils .PadWithZeros (int (value .Service .ID )), value .Service .Name )
369373 }
370374 serviceConnectCommand := fmt .Sprintf ("PS_LIST: %s" , serviceList )
371375 clientConn .Write ([]byte (serviceConnectCommand ))
376+ default :
372377 }
373378 }(conn )
374379 }
0 commit comments