fix: send ssh username to guacd (default root) for console SSH

This commit is contained in:
2026-07-17 11:54:33 +01:00
parent 50907448d2
commit 1ad5b5d6db
6 changed files with 43 additions and 5 deletions
+15 -1
View File
@@ -89,7 +89,7 @@ func portOr(v, def int) string {
// BuildGuacParams assembles the guacd connection parameter map for a protocol.
// privateKey is the decrypted SSH private key (ssh only); rdpUser/rdpPass are
// used for rdp. None of these values are persisted or logged by the caller.
func BuildGuacParams(srv *models.Server, protocol, privateKey, rdpUser, rdpPass string) (*GuacParams, error) {
func BuildGuacParams(srv *models.Server, protocol, sshUser, privateKey, rdpUser, rdpPass string) (*GuacParams, error) {
host := srv.IPAddress
switch protocol {
case "ssh":
@@ -97,6 +97,10 @@ func BuildGuacParams(srv *models.Server, protocol, privateKey, rdpUser, rdpPass
"hostname": host,
"port": portOr(srv.SSHPort, 22),
}
if sshUser == "" {
sshUser = "root"
}
p["username"] = sshUser
if privateKey != "" {
p["private-key"] = privateKey
}
@@ -200,6 +204,16 @@ func ConsumeConsoleRDPCreds(sessionID string) (username, password string, err er
return username, password, nil
}
// SetConsoleSSHUser persists the SSH username to use on the session doc.
func SetConsoleSSHUser(sessionID, username string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := db.Col("console_sessions").UpdateOne(ctx,
bson.M{"session_id": sessionID},
bson.M{"$set": bson.M{"ssh_username": username}})
return err
}
func EndConsoleSession(sessionID string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
+6 -3
View File
@@ -46,7 +46,7 @@ func TestSessionTokenTampered(t *testing.T) {
func TestBuildGuacParamsSSH(t *testing.T) {
srv := &models.Server{IPAddress: "10.0.0.5", SSHPort: 22}
p, err := BuildGuacParams(srv, "ssh", "PRIVATE-KEY-DATA", "", "")
p, err := BuildGuacParams(srv, "ssh", "", "PRIVATE-KEY-DATA", "", "")
if err != nil {
t.Fatalf("err: %v", err)
}
@@ -59,11 +59,14 @@ func TestBuildGuacParamsSSH(t *testing.T) {
if p.Params["private-key"] != "PRIVATE-KEY-DATA" {
t.Fatalf("missing private-key")
}
if p.Params["username"] != "root" {
t.Fatalf("expected default username root, got %q", p.Params["username"])
}
}
func TestBuildGuacParamsRDP(t *testing.T) {
srv := &models.Server{IPAddress: "10.0.0.9", RDPPort: 3389}
p, err := BuildGuacParams(srv, "rdp", "", "administrator", "s3cret")
p, err := BuildGuacParams(srv, "rdp", "", "", "administrator", "s3cret")
if err != nil {
t.Fatalf("err: %v", err)
}
@@ -77,7 +80,7 @@ func TestBuildGuacParamsRDP(t *testing.T) {
func TestBuildGuacParamsUnknownProtocol(t *testing.T) {
srv := &models.Server{IPAddress: "10.0.0.9"}
if _, err := BuildGuacParams(srv, "telnet", "", "", ""); err == nil {
if _, err := BuildGuacParams(srv, "telnet", "", "", "", ""); err == nil {
t.Fatalf("expected error for unknown protocol")
}
}