Skip to content

Instantly share code, notes, and snippets.

@0x49D1
Last active April 23, 2021 07:53
Show Gist options
  • Save 0x49D1/3053cb9e8fbb2345ee7469bca94a9dd3 to your computer and use it in GitHub Desktop.
Save 0x49D1/3053cb9e8fbb2345ee7469bca94a9dd3 to your computer and use it in GitHub Desktop.
Grant all procedures that were changed TODAY (or other date as parameter) in any database (as parameter) for any user (as parameter). Works for MariaDB too.
---------------------------------------------
-- Creates procedure that takes schema/user/modificationFromDate as parameters and calls it for some schema/user/today in the end. This works for MariaDB too.
-- Example of grant as concat select
-- SELECT CONCAT('GRANT EXECUTE ON PROCEDURE YOUR_SCHEMA.', routine_name, ' TO user@`10.1.%`;') FROM information_schema.routines where routine_schema = 'YOUR_SCHEMA' AND ROUTINE_TYPE = 'PROCEDURE';
-- SELECT CONCAT('GRANT EXECUTE ON FUNCTION YOUR_SCHEMA.', routine_name, ' TO user@`10.1.%`;') FROM information_schema.routines where routine_schema = 'YOUR_SCHEMA' AND ROUTINE_TYPE = 'FUNCTION';
---- Grant all procedures that were changed TODAY (or other date as parameter) in any database (as parameter) for any user (as parameter)
DROP PROCEDURE IF EXISTS SysExecuteGrantsForModifiedProcedures;
CREATE PROCEDURE SysExecuteGrantsForModifiedProcedures(IN p_schema VARCHAR(120), IN p_mysqlUser VARCHAR(120),
IN p_fromModificationDate DATETIME)
BEGIN
DECLARE v_done INT DEFAULT FALSE;
DECLARE v_statement TEXT;
# Get all the modified procedures/functions
DECLARE cur1 CURSOR FOR SELECT CONCAT('GRANT EXECUTE ON PROCEDURE `', ROUTINE_SCHEMA, '`.`', routine_name, '` TO ',
p_mysqlUser, ';')
FROM information_schema.routines
where routine_schema = p_schema
AND ROUTINE_TYPE = 'PROCEDURE'
AND date(LAST_ALTERED) >= date(p_fromModificationDate)
UNION
SELECT CONCAT('GRANT EXECUTE ON FUNCTION `', ROUTINE_SCHEMA, '`.`', routine_name, '` TO ',
p_mysqlUser, ';')
FROM information_schema.routines
where routine_schema = p_schema
AND ROUTINE_TYPE = 'FUNCTION'
AND date(LAST_ALTERED) >= date(p_fromModificationDate);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = TRUE;
SET max_sp_recursion_depth = 1024;
OPEN cur1;
read_loop:
LOOP
FETCH cur1 INTO v_statement;
IF v_done THEN
LEAVE read_loop;
END IF;
PREPARE stmt FROM v_statement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE cur1;
end;
-- And execute the procedure
CALL SysExecuteGrantsForModifiedProcedures('YOUR_SCHEMA', 'user@`10.1.%`', NOW());
---------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment