fix: populate console defaults on register when unset (setOnInsert never fired)

This commit is contained in:
2026-07-17 11:12:57 +01:00
parent aeee7aeccf
commit f06009b152
+18 -16
View File
@@ -122,25 +122,27 @@ func RegisterServer(serverID, preRegToken, hostname, ipAddress, osInfo string) (
osType := OSTypeFromInfo(osInfo)
protocols, sshPort, rdpPort := defaultConsoleFields(osType)
setFields := bson.M{
"hostname": hostname,
"ip_address": ipAddress,
"os_info": osInfo,
"os_type": osType,
"agent_token_hash": tokenHash,
"status": "active",
"last_seen": now,
"pre_reg_token": "",
"pre_reg_expires": nil,
}
if len(s.ConsoleProtocols) == 0 {
setFields["console_protocols"] = protocols
setFields["ssh_port"] = sshPort
setFields["rdp_port"] = rdpPort
}
_, err = db.Col("servers").UpdateOne(ctx,
bson.M{"server_id": serverID},
bson.M{
"$set": bson.M{
"hostname": hostname,
"ip_address": ipAddress,
"os_info": osInfo,
"os_type": osType,
"agent_token_hash": tokenHash,
"status": "active",
"last_seen": now,
"pre_reg_token": "",
"pre_reg_expires": nil,
},
"$setOnInsert": bson.M{
"console_protocols": protocols,
"ssh_port": sshPort,
"rdp_port": rdpPort,
},
"$set": setFields,
},
)
if err != nil {