Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit e86e5df

Browse files
committed
Fixed to support MySQL 8 Series.
1 parent 8c64932 commit e86e5df

File tree

7 files changed

+53
-31
lines changed

7 files changed

+53
-31
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ user "scott", "%" do
9292
grant "USAGE"
9393
end
9494

95-
on "test.*", expired: '2014/10/08', identified: "PASSWORD '*ABCDEF'" do
95+
on "test.*", expired: '2014/10/08', identified: "*ABCDEF" do
9696
grant "SELECT"
9797
grant "INSERT"
9898
end
@@ -141,9 +141,9 @@ end
141141

142142
```sh
143143
bundle install
144-
docker-compose up -d
144+
docker compose up -d
145145
bundle exec rake
146-
# MYSQL57=1 bundle exec rake
146+
# MYSQL80=1 bundle exec rake
147147
```
148148

149149
## Similar tools

compose.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
services:
2+
mysql57:
3+
image: "mysql:5.7.42-debian"
4+
platform: linux/amd64
5+
ports:
6+
- "3307:3306"
7+
environment:
8+
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
9+
TZ: 'Asia/Tokyo'
10+
restart: always
11+
networks:
12+
- mysql-network
13+
mysql80:
14+
image: "mysql:8.0.28-debian"
15+
platform: linux/amd64
16+
ports:
17+
- "3308:3306"
18+
environment:
19+
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
20+
TZ: 'Asia/Tokyo'
21+
command: --default-authentication-plugin=mysql_native_password
22+
restart: always
23+
networks:
24+
- mysql-network
25+
networks:
26+
mysql-network:
27+
driver: bridge

docker-compose.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/gratan/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ def walk_options(user, host, expected_options, actual_options)
132132
end
133133

134134
def walk_identified(user, host, expected_identified, actual_identified)
135-
if actual_identified == 'PASSWORD <secret>'
135+
if actual_identified == '<secret>'
136136
unless @options[:ignore_password_secret]
137-
log(:warn, "cannot change the password (`PASSWORD <secret>`)", :color => :yellow)
137+
log(:warn, "cannot change the password (`<secret>`)", :color => :yellow)
138138
end
139139
elsif expected_identified != actual_identified
140140
@driver.identify(user, host, expected_identified)

lib/gratan/driver.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,19 @@ def flush_privileges
6363

6464
def create_user(user, host, options = {})
6565
objects = options[:objects]
66+
identified = options[:options][:identified]
67+
required = options[:required]
68+
with_option = options[:with]
69+
auth_plugin = options[:auth_plugin] || "mysql_native_password"
6670
grant_options = options[:options]
6771
granted = false
6872

73+
sql = "CREATE USER #{quote_user(user, host)}"
74+
sql << " IDENTIFIED WITH #{auth_plugin} AS #{quote_identifier(identified)}" if identified
75+
sql << " REQUIRE #{required}" if required
76+
sql << " WITH #{with_option}" if with_option
77+
update(sql)
78+
6979
objects.each do |object_or_regexp, object_options|
7080
expand_object(object_or_regexp).each do |object|
7181
grant(user, host, object, grant_options.merge(object_options))
@@ -85,7 +95,6 @@ def drop_user(user, host)
8595

8696
def grant(user, host, object, options)
8797
privs = options.fetch(:privs)
88-
identified = options[:identified]
8998
required = options[:required]
9099
with_option = options[:with]
91100

@@ -95,10 +104,8 @@ def grant(user, host, object, options)
95104
quote_user(user, host),
96105
]
97106

98-
sql << " IDENTIFIED BY #{quote_identifier(identified)}" if identified
99107
sql << " REQUIRE #{required}" if required
100108
sql << " WITH #{with_option}" if with_option
101-
102109
begin
103110
update(sql)
104111
rescue Mysql2::Error => e
@@ -110,8 +117,8 @@ def grant(user, host, object, options)
110117
end
111118
end
112119

113-
def identify(user, host, identifier)
114-
sql = 'GRANT USAGE ON *.* TO %s IDENTIFIED BY %s' % [
120+
def identify(user, host, identifier, auth_plugin = "mysql_native_password")
121+
sql = "ALTER USER %s IDENTIFIED WITH #{auth_plugin} AS %s" % [
115122
quote_user(user, host),
116123
quote_identifier(identifier),
117124
]
@@ -127,7 +134,7 @@ def set_password(user, host, password, options = {})
127134
password ||= ''
128135

129136
unless options[:hash]
130-
password = "PASSWORD('#{escape(password)}')"
137+
password = "SELECT CONCAT('*', UPPER(SHA1(UNHEX(SHA1('#{escape(password)}'))))) AS PASSWORD"
131138
end
132139

133140
sql = 'SET PASSWORD FOR %s = %s' % [

lib/gratan/grant_parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def parse_identified
6666
end
6767

6868
def parse_main
69-
md = /\AGRANT\s+(.+?)\s+ON\s+(.+?)\s+TO\s+'(.*)'@'(.+)'\z/.match(@stmt)
69+
md = /\AGRANT\s+(.+?)\s+ON\s+(.+?)\s+TO\s+'(.*)'@'(.+)'\z/.match(@stmt.gsub('`', '\''))
7070
privs, object, user, host = md.captures
7171
@parsed[:privs] = parse_privs(privs.strip)
72-
@parsed[:object] = object.gsub('`', '').strip
72+
@parsed[:object] = object.gsub('\'', '').strip
7373
@parsed[:user] = user
7474
@parsed[:host] = host
7575
end

spec/spec_helper.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
end
1414
end
1515

16-
def mysql57?
17-
ENV['MYSQL57'] == '1'
16+
def mysql80?
17+
ENV['MYSQL80'] == '1'
1818
end
1919

20-
MYSQL_PORT = mysql57? ? 14407 : 14406
20+
MYSQL_PORT = mysql80? ? 3308 : 3307
2121

2222
def mysql
2323
client = nil
@@ -127,7 +127,7 @@ def show_grants
127127
end
128128
end
129129

130-
if mysql57?
130+
if mysql80?
131131
grants.each do |grant|
132132
end
133133
end
@@ -148,7 +148,7 @@ def client(user_options = {})
148148
logger: Logger.new('/dev/null'),
149149
}
150150

151-
if mysql57?
151+
if mysql80?
152152
options.update(
153153
override_sql_mode: true,
154154
use_show_create_user: true,
@@ -189,7 +189,7 @@ def apply(cli = client)
189189

190190
class Array
191191
def normalize
192-
if mysql57?
192+
if mysql80?
193193
self.map do |i|
194194
i.sub(/ IDENTIFIED BY PASSWORD '[^']+'/, '')
195195
.sub(/ REQUIRE \w+\b/, '')

0 commit comments

Comments
 (0)