From 797b08c759dca281920fefb00a34be3f4b36932e Mon Sep 17 00:00:00 2001 From: Celso Fernandes Date: Sat, 2 Mar 2019 14:18:30 +0000 Subject: [PATCH 1/2] Support MySQL 8 --- spec/driver_spec.cr | 14 ++++++++++---- spec/spec_helper.cr | 10 +++++++++- src/mysql/packets.cr | 2 -- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/spec/driver_spec.cr b/spec/driver_spec.cr index 428d3e8..6ffc182 100644 --- a/spec/driver_spec.cr +++ b/spec/driver_spec.cr @@ -12,18 +12,24 @@ describe Driver do it "should connect with credentials" do with_db do |db| db.scalar("SELECT DATABASE()").should be_nil - db.scalar("SELECT CURRENT_USER()").should match(/^root@/) + db.scalar("SELECT CURRENT_USER()").should match(/^#{database_user}@/) # ensure user is deleted - db.exec "GRANT USAGE ON *.* TO crystal_test IDENTIFIED BY 'secret'" + db.exec "SET GLOBAL validate_password.length = 6" + db.exec "SET GLOBAL validate_password.number_count = 0" + db.exec "SET GLOBAL validate_password.policy=LOW" + db.exec "DROP USER IF EXISTS crystal_test" + db.exec "CREATE USER crystal_test IDENTIFIED WITH mysql_native_password BY 'secret'" + db.exec "GRANT USAGE ON *.* TO crystal_test" db.exec "DROP USER crystal_test" db.exec "DROP DATABASE IF EXISTS crystal_mysql_test" db.exec "FLUSH PRIVILEGES" # create test db with user db.exec "CREATE DATABASE crystal_mysql_test" - db.exec "CREATE USER crystal_test IDENTIFIED BY 'secret'" - db.exec "GRANT ALL PRIVILEGES ON crystal_mysql_test.* TO crystal_test" + db.exec "DROP USER IF EXISTS 'crystal_test'@'#{database_host}'" + db.exec "CREATE USER 'crystal_test'@'#{database_host}' IDENTIFIED WITH mysql_native_password BY 'secret'" + db.exec "GRANT ALL PRIVILEGES ON crystal_mysql_test.* TO 'crystal_test'@'#{database_host}'" db.exec "FLUSH PRIVILEGES" end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 9f22857..2c3597f 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -4,9 +4,17 @@ require "../src/mysql" include MySql def db_url(initial_db = nil) - "mysql://root@#{database_host}/#{initial_db}" + "mysql://#{database_user}:#{database_password}@#{database_host}/#{initial_db}" end def database_host ENV.fetch("DATABASE_HOST", "localhost") end + +def database_user + ENV.fetch("DATABASE_USER", "root") +end + +def database_password + ENV.fetch("DATABASE_PASSWORD", "crystal_mysql_spec") +end diff --git a/src/mysql/packets.cr b/src/mysql/packets.cr index c462920..1d07e5c 100644 --- a/src/mysql/packets.cr +++ b/src/mysql/packets.cr @@ -65,8 +65,6 @@ module MySql::Protocol def write(packet : MySql::WritePacket) caps = CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA - caps |= CLIENT_PLUGIN_AUTH if @password - caps |= CLIENT_CONNECT_WITH_DB if @initial_catalog packet.write_bytes caps, IO::ByteFormat::LittleEndian From 8aed0674ec2e00eb8bfaf2b66a955944b4a7a39f Mon Sep 17 00:00:00 2001 From: Celso Fernandes Date: Sat, 2 Mar 2019 19:34:12 +0000 Subject: [PATCH 2/2] Use a _stronger_ password for MySQL 8 so no need to alter `validate_password` rules --- spec/driver_spec.cr | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/spec/driver_spec.cr b/spec/driver_spec.cr index 6ffc182..2d24dca 100644 --- a/spec/driver_spec.cr +++ b/spec/driver_spec.cr @@ -15,11 +15,8 @@ describe Driver do db.scalar("SELECT CURRENT_USER()").should match(/^#{database_user}@/) # ensure user is deleted - db.exec "SET GLOBAL validate_password.length = 6" - db.exec "SET GLOBAL validate_password.number_count = 0" - db.exec "SET GLOBAL validate_password.policy=LOW" db.exec "DROP USER IF EXISTS crystal_test" - db.exec "CREATE USER crystal_test IDENTIFIED WITH mysql_native_password BY 'secret'" + db.exec "CREATE USER crystal_test IDENTIFIED WITH mysql_native_password BY 'Secret123!'" db.exec "GRANT USAGE ON *.* TO crystal_test" db.exec "DROP USER crystal_test" db.exec "DROP DATABASE IF EXISTS crystal_mysql_test" @@ -28,12 +25,12 @@ describe Driver do # create test db with user db.exec "CREATE DATABASE crystal_mysql_test" db.exec "DROP USER IF EXISTS 'crystal_test'@'#{database_host}'" - db.exec "CREATE USER 'crystal_test'@'#{database_host}' IDENTIFIED WITH mysql_native_password BY 'secret'" + db.exec "CREATE USER 'crystal_test'@'#{database_host}' IDENTIFIED WITH mysql_native_password BY 'Secret123!'" db.exec "GRANT ALL PRIVILEGES ON crystal_mysql_test.* TO 'crystal_test'@'#{database_host}'" db.exec "FLUSH PRIVILEGES" end - DB.open "mysql://crystal_test:secret@#{database_host}/crystal_mysql_test" do |db| + DB.open "mysql://crystal_test:Secret123!@#{database_host}/crystal_mysql_test" do |db| db.scalar("SELECT DATABASE()").should eq("crystal_mysql_test") db.scalar("SELECT CURRENT_USER()").should match(/^crystal_test@/) end