Skip to content

Commit 9e595a3

Browse files
authored
Fix long role names for MySQL auto users (#60377)
Previously, role names were effectively limited to 30 characters even though MySQL supports up to 32. The activation procedure now: - Unquotes the role name from JSON - Stores it in a TEXT variable (not a narrow VARCHAR) - Re-quotes with proper escaping before GRANT This allows full-length (32-char) role names to work.
1 parent 691ce81 commit 9e595a3

9 files changed

+20
-22
lines changed

lib/srv/db/mysql/autousers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ func getCreateProcedureCommand(conn *clientConn, procedureName string) (string,
544544
const (
545545
// procedureVersion is a hard-coded string that is set as procedure
546546
// comments to indicate the procedure version.
547-
procedureVersion = "teleport-auto-user-v4"
547+
procedureVersion = "teleport-auto-user-v5"
548548

549549
// mysqlMaxUsernameLength is the maximum username/role length for MySQL.
550550
//

lib/srv/db/mysql/sql/mariadb_activate_user.sql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
CREATE PROCEDURE teleport_activate_user(IN username VARCHAR(80), IN details JSON)
1+
CREATE PROCEDURE teleport_activate_user(IN username TEXT, IN details JSON)
22
proc_label:BEGIN
33
DECLARE is_auto_user INT DEFAULT 0;
44
DECLARE is_active INT DEFAULT 0;
55
DECLARE is_same_user INT DEFAULT 0;
66
DECLARE role_index INT DEFAULT 0;
7-
DECLARE cur_role VARCHAR(128) DEFAULT '';
7+
DECLARE cur_role TEXT DEFAULT '';
88
DECLARE cur_roles TEXT DEFAULT '';
99
SET @roles = JSON_EXTRACT(details, "$.roles");
1010
SET @teleport_user = JSON_VALUE(details, "$.attributes.user");
@@ -72,11 +72,10 @@ proc_label:BEGIN
7272
CALL teleport_revoke_roles(username);
7373
SET role_index = 0;
7474
WHILE role_index < JSON_LENGTH(@roles) DO
75-
SELECT JSON_EXTRACT(@roles, CONCAT('$[',role_index,']')) INTO cur_role;
75+
SELECT JSON_UNQUOTE(JSON_EXTRACT(@roles, CONCAT('$[',role_index,']'))) INTO cur_role;
7676
SELECT role_index + 1 INTO role_index;
7777

78-
-- role extracted from JSON already has double quotes.
79-
SET @sql := CONCAT_WS(' ', 'GRANT', cur_role, 'TO', QUOTE(@all_in_one_role));
78+
SET @sql := CONCAT_WS(' ', 'GRANT', QUOTE(cur_role), 'TO', QUOTE(@all_in_one_role));
8079
PREPARE stmt FROM @sql;
8180
EXECUTE stmt;
8281
DEALLOCATE PREPARE stmt;

lib/srv/db/mysql/sql/mariadb_deactivate_user.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE PROCEDURE teleport_deactivate_user(IN username VARCHAR(80))
1+
CREATE PROCEDURE teleport_deactivate_user(IN username TEXT)
22
BEGIN
33
DECLARE is_active INT DEFAULT 0;
44
SELECT COUNT(USER) INTO is_active FROM information_schema.processlist WHERE USER = username;

lib/srv/db/mysql/sql/mariadb_delete_user.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
CREATE PROCEDURE teleport_delete_user(IN username VARCHAR(80))
1+
CREATE PROCEDURE teleport_delete_user(IN username TEXT)
22
BEGIN
33
-- Defaults to dropping user.
4-
DECLARE state VARCHAR(5);
4+
DECLARE state TEXT;
55
DECLARE is_active INT DEFAULT 0;
66
DECLARE view_count INT DEFAULT 0;
77
DECLARE procedure_count INT DEFAULT 0;

lib/srv/db/mysql/sql/mariadb_revoke_roles.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
CREATE PROCEDURE teleport_revoke_roles(IN username VARCHAR(80))
1+
CREATE PROCEDURE teleport_revoke_roles(IN username TEXT)
22
BEGIN
3-
DECLARE cur_user CHAR(128);
4-
DECLARE cur_role CHAR(128);
3+
DECLARE cur_user TEXT;
4+
DECLARE cur_role TEXT;
55
DECLARE done INT DEFAULT FALSE;
66
-- Revoke all roles assigned to the all-in-one role, and all roles assigned
77
-- to the username (expect 'teleport-auto-user')

lib/srv/db/mysql/sql/mysql_activate_user.sql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
CREATE PROCEDURE teleport_activate_user(IN username VARCHAR(32), IN details JSON)
1+
CREATE PROCEDURE teleport_activate_user(IN username TEXT, IN details JSON)
22
proc_label:BEGIN
33
DECLARE is_auto_user INT DEFAULT 0;
44
DECLARE is_active INT DEFAULT 0;
55
DECLARE is_same_user INT DEFAULT 0;
66
DECLARE are_roles_same INT DEFAULT 0;
77
DECLARE role_index INT DEFAULT 0;
8-
DECLARE role VARCHAR(32) DEFAULT '';
8+
DECLARE role TEXT DEFAULT '';
99
DECLARE cur_roles TEXT DEFAULT '';
1010
SET @roles = details->"$.roles";
1111
SET @teleport_user = details->>"$.attributes.user";
@@ -57,11 +57,10 @@ proc_label:BEGIN
5757

5858
-- Assign roles.
5959
WHILE role_index < JSON_LENGTH(@roles) DO
60-
SELECT JSON_EXTRACT(@roles, CONCAT('$[',role_index,']')) INTO role;
60+
SELECT JSON_UNQUOTE(JSON_EXTRACT(@roles, CONCAT('$[',role_index,']'))) INTO role;
6161
SELECT role_index + 1 INTO role_index;
6262

63-
-- role extracted from JSON already has double quotes.
64-
SET @sql := CONCAT_WS(' ', 'GRANT', role, 'TO', QUOTE(username));
63+
SET @sql := CONCAT_WS(' ', 'GRANT', QUOTE(role), 'TO', QUOTE(username));
6564
PREPARE stmt FROM @sql;
6665
EXECUTE stmt;
6766
DEALLOCATE PREPARE stmt;

lib/srv/db/mysql/sql/mysql_deactivate_user.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CREATE PROCEDURE teleport_deactivate_user(IN username VARCHAR(32))
1+
CREATE PROCEDURE teleport_deactivate_user(IN username TEXT)
22
BEGIN
33
DECLARE is_active INT DEFAULT 0;
44
SELECT COUNT(USER) INTO is_active FROM information_schema.processlist WHERE USER = username;

lib/srv/db/mysql/sql/mysql_delete_user.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
CREATE PROCEDURE teleport_delete_user(IN username VARCHAR(32))
1+
CREATE PROCEDURE teleport_delete_user(IN username TEXT)
22
BEGIN
33
-- Defaults to dropping user.
4-
DECLARE state VARCHAR(5) DEFAULT 'TP003';
4+
DECLARE state TEXT DEFAULT 'TP003';
55
DECLARE is_active INT DEFAULT 0;
66

77
-- Views and procedures rely on the definer to work correctly. Dropping the

lib/srv/db/mysql/sql/mysql_revoke_roles.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CREATE PROCEDURE teleport_revoke_roles(IN username VARCHAR(32))
1+
CREATE PROCEDURE teleport_revoke_roles(IN username TEXT)
22
BEGIN
3-
DECLARE role VARCHAR(32) DEFAULT '';
3+
DECLARE role TEXT DEFAULT '';
44
DECLARE done INT DEFAULT 0;
55
DECLARE role_cursor CURSOR FOR SELECT FROM_USER FROM mysql.role_edges WHERE FROM_USER != 'teleport-auto-user' AND TO_USER = username;
66
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

0 commit comments

Comments
 (0)