gratan 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +86 -0
  8. data/Rakefile +5 -0
  9. data/bin/gratan +132 -0
  10. data/gratan.gemspec +28 -0
  11. data/lib/gratan/client.rb +211 -0
  12. data/lib/gratan/driver.rb +166 -0
  13. data/lib/gratan/dsl/context/on.rb +19 -0
  14. data/lib/gratan/dsl/context/user.rb +25 -0
  15. data/lib/gratan/dsl/context.rb +57 -0
  16. data/lib/gratan/dsl/converter.rb +74 -0
  17. data/lib/gratan/dsl/validator.rb +13 -0
  18. data/lib/gratan/dsl.rb +9 -0
  19. data/lib/gratan/exporter.rb +49 -0
  20. data/lib/gratan/ext/string_ext.rb +25 -0
  21. data/lib/gratan/grant_parser.rb +68 -0
  22. data/lib/gratan/identifier/auto.rb +28 -0
  23. data/lib/gratan/identifier/csv.rb +25 -0
  24. data/lib/gratan/identifier/null.rb +5 -0
  25. data/lib/gratan/identifier.rb +2 -0
  26. data/lib/gratan/logger.rb +28 -0
  27. data/lib/gratan/version.rb +3 -0
  28. data/lib/gratan.rb +24 -0
  29. data/spec/change/change_grants_2_spec.rb +154 -0
  30. data/spec/change/change_grants_3_spec.rb +164 -0
  31. data/spec/change/change_grants_4_spec.rb +37 -0
  32. data/spec/change/change_grants_spec.rb +209 -0
  33. data/spec/create/create_user_2_spec.rb +139 -0
  34. data/spec/create/create_user_3_spec.rb +115 -0
  35. data/spec/create/create_user_spec.rb +194 -0
  36. data/spec/drop/drop_user_2_spec.rb +77 -0
  37. data/spec/drop/drop_user_spec.rb +67 -0
  38. data/spec/drop/expire_user_spec.rb +179 -0
  39. data/spec/export/export_spec.rb +119 -0
  40. data/spec/misc/misc_spec.rb +74 -0
  41. data/spec/misc/require_spec.rb +77 -0
  42. data/spec/spec_helper.rb +118 -0
  43. metadata +198 -0
@@ -0,0 +1,77 @@
1
+ describe 'Gratan::Client#apply' do
2
+ context 'when load group file' do
3
+ subject { client }
4
+
5
+ it do
6
+ dsl = <<-RUBY
7
+ user 'scott', 'localhost', identified: 'tiger' do
8
+ on '*.*' do
9
+ grant 'SELECT'
10
+ grant 'INSERT'
11
+ grant 'UPDATE'
12
+ grant 'DELETE'
13
+ end
14
+
15
+ on 'test.*' do
16
+ grant 'SELECT'
17
+ grant 'INSERT'
18
+ grant 'UPDATE'
19
+ grant 'DELETE'
20
+ end
21
+ end
22
+ RUBY
23
+
24
+ tempfile(dsl) do |f|
25
+ apply(subject) {
26
+ <<-RUBY
27
+ require 'time'
28
+ require '#{f.path}'
29
+ RUBY
30
+ }
31
+ end
32
+
33
+ expect(show_grants).to match_array [
34
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40'",
35
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
36
+ ]
37
+ end
38
+ end
39
+
40
+ context 'when load group file (.rb)' do
41
+ subject { client }
42
+
43
+ it do
44
+ dsl = <<-RUBY
45
+ user 'scott', 'localhost', identified: 'tiger' do
46
+ on '*.*' do
47
+ grant 'SELECT'
48
+ grant 'INSERT'
49
+ grant 'UPDATE'
50
+ grant 'DELETE'
51
+ end
52
+
53
+ on 'test.*' do
54
+ grant 'SELECT'
55
+ grant 'INSERT'
56
+ grant 'UPDATE'
57
+ grant 'DELETE'
58
+ end
59
+ end
60
+ RUBY
61
+
62
+ tempfile(dsl, ext: '.rb') do |f|
63
+ apply(subject) {
64
+ <<-RUBY
65
+ require 'time'
66
+ require '#{f.path.sub(/\.rb\z/, '')}'
67
+ RUBY
68
+ }
69
+ end
70
+
71
+ expect(show_grants).to match_array [
72
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40'",
73
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
74
+ ]
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,118 @@
1
+ $: << File.expand_path('..', __FILE__)
2
+
3
+ if ENV['TRAVIS']
4
+ require 'simplecov'
5
+ require 'coveralls'
6
+
7
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
8
+ SimpleCov.start do
9
+ add_filter "spec/"
10
+ end
11
+ end
12
+
13
+ require 'gratan'
14
+ require 'tempfile'
15
+ require 'timecop'
16
+
17
+ IGNORE_USER = /\A(|root)\z/
18
+
19
+ RSpec.configure do |config|
20
+ config.before(:each) do
21
+ clean_grants
22
+ end
23
+ end
24
+
25
+ def mysql
26
+ client = nil
27
+ retval = nil
28
+
29
+ begin
30
+ client = Mysql2::Client.new(host: 'localhost', username: 'root')
31
+ retval = yield(client)
32
+ ensure
33
+ client.close if client
34
+ end
35
+
36
+ retval
37
+ end
38
+
39
+ def select_users(client)
40
+ users = []
41
+
42
+ client.query('SELECT user, host FROM mysql.user').each do |row|
43
+ users << [row['user'], row['host']]
44
+ end
45
+
46
+ users
47
+ end
48
+
49
+ def clean_grants
50
+ mysql do |client|
51
+ select_users(client).each do |user, host|
52
+ next if IGNORE_USER =~ user
53
+ user_host = "'%s'@'%s'" % [client.escape(user), client.escape(host)]
54
+ client.query("DROP USER #{user_host}")
55
+ end
56
+ end
57
+ end
58
+
59
+ def show_grants
60
+ grants = []
61
+
62
+ mysql do |client|
63
+ select_users(client).each do |user, host|
64
+ next if IGNORE_USER =~ user
65
+ user_host = "'%s'@'%s'" % [client.escape(user), client.escape(host)]
66
+
67
+ client.query("SHOW GRANTS FOR #{user_host}").each do |row|
68
+ grants << row.values.first
69
+ end
70
+ end
71
+ end
72
+
73
+ grants.sort
74
+ end
75
+
76
+ def client(user_options = {})
77
+ if user_options[:ignore_user]
78
+ user_options[:ignore_user] = Regexp.union(IGNORE_USER, user_options[:ignore_user])
79
+ end
80
+
81
+ options = {
82
+ host: 'localhost',
83
+ username: 'root',
84
+ ignore_user: IGNORE_USER,
85
+ logger: Logger.new('/dev/null'),
86
+ }
87
+
88
+ if ENV['DEBUG']
89
+ logger = Gratan::Logger.instance
90
+ logger.set_debug(true)
91
+
92
+ options.update(
93
+ debug: true,
94
+ logger: logger
95
+ )
96
+ end
97
+
98
+ options = options.merge(user_options)
99
+ Gratan::Client.new(options)
100
+ end
101
+
102
+ def tempfile(content, options = {})
103
+ basename = "#{File.basename __FILE__}.#{$$}"
104
+ basename = [basename, options[:ext]] if options[:ext]
105
+
106
+ Tempfile.open(basename) do |f|
107
+ f.puts(content)
108
+ f.flush
109
+ f.rewind
110
+ yield(f)
111
+ end
112
+ end
113
+
114
+ def apply(cli = client)
115
+ tempfile(yield) do |f|
116
+ cli.apply(f.path)
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gratan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: term-ansicolor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Gratan is a tool to manage MySQL permissions using Ruby DSL.
112
+ email:
113
+ - sgwr_dts@yahoo.co.jp
114
+ executables:
115
+ - gratan
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - .travis.yml
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/gratan
127
+ - gratan.gemspec
128
+ - lib/gratan.rb
129
+ - lib/gratan/client.rb
130
+ - lib/gratan/driver.rb
131
+ - lib/gratan/dsl.rb
132
+ - lib/gratan/dsl/context.rb
133
+ - lib/gratan/dsl/context/on.rb
134
+ - lib/gratan/dsl/context/user.rb
135
+ - lib/gratan/dsl/converter.rb
136
+ - lib/gratan/dsl/validator.rb
137
+ - lib/gratan/exporter.rb
138
+ - lib/gratan/ext/string_ext.rb
139
+ - lib/gratan/grant_parser.rb
140
+ - lib/gratan/identifier.rb
141
+ - lib/gratan/identifier/auto.rb
142
+ - lib/gratan/identifier/csv.rb
143
+ - lib/gratan/identifier/null.rb
144
+ - lib/gratan/logger.rb
145
+ - lib/gratan/version.rb
146
+ - spec/change/change_grants_2_spec.rb
147
+ - spec/change/change_grants_3_spec.rb
148
+ - spec/change/change_grants_4_spec.rb
149
+ - spec/change/change_grants_spec.rb
150
+ - spec/create/create_user_2_spec.rb
151
+ - spec/create/create_user_3_spec.rb
152
+ - spec/create/create_user_spec.rb
153
+ - spec/drop/drop_user_2_spec.rb
154
+ - spec/drop/drop_user_spec.rb
155
+ - spec/drop/expire_user_spec.rb
156
+ - spec/export/export_spec.rb
157
+ - spec/misc/misc_spec.rb
158
+ - spec/misc/require_spec.rb
159
+ - spec/spec_helper.rb
160
+ homepage: https://github.com/winebarrel/gratan
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - '>='
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.1
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Gratan is a tool to manage MySQL permissions using Ruby DSL.
184
+ test_files:
185
+ - spec/change/change_grants_2_spec.rb
186
+ - spec/change/change_grants_3_spec.rb
187
+ - spec/change/change_grants_4_spec.rb
188
+ - spec/change/change_grants_spec.rb
189
+ - spec/create/create_user_2_spec.rb
190
+ - spec/create/create_user_3_spec.rb
191
+ - spec/create/create_user_spec.rb
192
+ - spec/drop/drop_user_2_spec.rb
193
+ - spec/drop/drop_user_spec.rb
194
+ - spec/drop/expire_user_spec.rb
195
+ - spec/export/export_spec.rb
196
+ - spec/misc/misc_spec.rb
197
+ - spec/misc/require_spec.rb
198
+ - spec/spec_helper.rb