gratan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,164 @@
1
+ describe 'Gratan::Client#apply' do
2
+ before(:each) do
3
+ apply {
4
+ <<-RUBY
5
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
6
+ on '*.*' do
7
+ grant 'SELECT'
8
+ grant 'INSERT'
9
+ end
10
+
11
+ on 'test.*' do
12
+ grant 'UPDATE'
13
+ grant 'DELETE'
14
+ end
15
+
16
+ on 'mysql.user' do
17
+ grant 'SELECT (user)'
18
+ end
19
+ end
20
+
21
+ user 'bob', 'localhost' do
22
+ on '*.*' do
23
+ grant 'USAGE'
24
+ end
25
+
26
+ on 'test.*' do
27
+ grant 'ALL PRIVILEGES'
28
+ end
29
+ end
30
+ RUBY
31
+ }
32
+ end
33
+
34
+ context 'when grant privs' do
35
+ subject { client(dry_run: true) }
36
+
37
+ it do
38
+ apply(subject) {
39
+ <<-RUBY
40
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
41
+ on '*.*' do
42
+ grant 'SELECT'
43
+ grant 'INSERT'
44
+ grant 'UPDATE'
45
+ grant 'DELETE'
46
+ end
47
+
48
+ on 'test.*' do
49
+ grant 'SELECT'
50
+ grant 'INSERT'
51
+ grant 'UPDATE'
52
+ grant 'DELETE'
53
+ end
54
+
55
+ on 'mysql.user' do
56
+ grant 'SELECT (user)'
57
+ grant 'UPDATE (host)'
58
+ end
59
+ end
60
+
61
+ user 'bob', 'localhost' do
62
+ on '*.*' do
63
+ grant 'USAGE'
64
+ end
65
+
66
+ on 'test.*' do
67
+ grant 'ALL PRIVILEGES'
68
+ end
69
+ end
70
+ RUBY
71
+ }
72
+
73
+ expect(show_grants).to match_array [
74
+ "GRANT ALL PRIVILEGES ON `test`.* TO 'bob'@'localhost'",
75
+ "GRANT SELECT (user) ON `mysql`.`user` TO 'scott'@'localhost'",
76
+ "GRANT SELECT, INSERT ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40' REQUIRE SSL",
77
+ "GRANT UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
78
+ "GRANT USAGE ON *.* TO 'bob'@'localhost'",
79
+ ]
80
+ end
81
+ end
82
+
83
+ context 'when revoke privs' do
84
+ subject { client(dry_run: true) }
85
+
86
+ it do
87
+ apply(subject) {
88
+ <<-RUBY
89
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
90
+ on '*.*' do
91
+ grant 'SELECT'
92
+ end
93
+
94
+ on 'mysql.user' do
95
+ grant 'UPDATE (host)'
96
+ end
97
+ end
98
+
99
+ user 'bob', 'localhost' do
100
+ on '*.*' do
101
+ grant 'USAGE'
102
+ end
103
+
104
+ on 'test.*' do
105
+ grant 'ALL PRIVILEGES'
106
+ end
107
+ end
108
+ RUBY
109
+ }
110
+
111
+ expect(show_grants).to match_array [
112
+ "GRANT ALL PRIVILEGES ON `test`.* TO 'bob'@'localhost'",
113
+ "GRANT SELECT (user) ON `mysql`.`user` TO 'scott'@'localhost'",
114
+ "GRANT SELECT, INSERT ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40' REQUIRE SSL",
115
+ "GRANT UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
116
+ "GRANT USAGE ON *.* TO 'bob'@'localhost'",
117
+ ]
118
+ end
119
+ end
120
+
121
+ context 'when grant/revoke privs' do
122
+ subject { client(dry_run: true) }
123
+
124
+ it do
125
+ apply(subject) {
126
+ <<-RUBY
127
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
128
+ on '*.*' do
129
+ grant 'UPDATE'
130
+ grant 'DELETE'
131
+ end
132
+
133
+ on 'test.*' do
134
+ grant 'SELECT'
135
+ grant 'INSERT'
136
+ end
137
+
138
+ on 'mysql.user' do
139
+ grant 'UPDATE (host)'
140
+ end
141
+ end
142
+
143
+ user 'mary', 'localhost' do
144
+ on '*.*' do
145
+ grant 'USAGE'
146
+ end
147
+
148
+ on 'test.*' do
149
+ grant 'ALL PRIVILEGES'
150
+ end
151
+ end
152
+ RUBY
153
+ }
154
+
155
+ expect(show_grants).to match_array [
156
+ "GRANT ALL PRIVILEGES ON `test`.* TO 'bob'@'localhost'",
157
+ "GRANT SELECT (user) ON `mysql`.`user` TO 'scott'@'localhost'",
158
+ "GRANT SELECT, INSERT ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40' REQUIRE SSL",
159
+ "GRANT UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
160
+ "GRANT USAGE ON *.* TO 'bob'@'localhost'",
161
+ ]
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,37 @@
1
+ describe 'Gratan::Client#apply' do
2
+ context 'when revoke privs with grant option' do
3
+ before do
4
+ apply {
5
+ <<-RUBY
6
+ user 'scott', 'localhost' do
7
+ on '*.*' do
8
+ grant 'USAGE'
9
+ end
10
+
11
+ on 'test.*', with: 'GRANT OPTION' do
12
+ grant 'ALL PRIVILEGES'
13
+ end
14
+ end
15
+ RUBY
16
+ }
17
+ end
18
+
19
+ subject { client }
20
+
21
+ it do
22
+ apply(subject) {
23
+ <<-RUBY
24
+ user 'scott', 'localhost' do
25
+ on '*.*' do
26
+ grant 'USAGE'
27
+ end
28
+ end
29
+ RUBY
30
+ }
31
+
32
+ expect(show_grants).to match_array [
33
+ "GRANT USAGE ON *.* TO 'scott'@'localhost'",
34
+ ]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,209 @@
1
+ describe 'Gratan::Client#apply' do
2
+ before(:each) do
3
+ apply {
4
+ <<-RUBY
5
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
6
+ on '*.*' do
7
+ grant 'SELECT'
8
+ grant 'INSERT'
9
+ end
10
+
11
+ on 'test.*' do
12
+ grant 'UPDATE'
13
+ grant 'DELETE'
14
+ end
15
+
16
+ on 'mysql.user' do
17
+ grant 'SELECT (user)'
18
+ end
19
+ end
20
+
21
+ user 'bob', 'localhost' do
22
+ on '*.*' do
23
+ grant 'USAGE'
24
+ end
25
+
26
+ on 'test.*' do
27
+ grant 'ALL PRIVILEGES'
28
+ end
29
+ end
30
+ RUBY
31
+ }
32
+ end
33
+
34
+ context 'when grant privs' do
35
+ subject { client }
36
+
37
+ it do
38
+ apply(subject) {
39
+ <<-RUBY
40
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
41
+ on '*.*' do
42
+ grant 'SELECT'
43
+ grant 'INSERT'
44
+ grant 'UPDATE'
45
+ grant 'DELETE'
46
+ end
47
+
48
+ on 'test.*' do
49
+ grant 'SELECT'
50
+ grant 'INSERT'
51
+ grant 'UPDATE'
52
+ grant 'DELETE'
53
+ end
54
+
55
+ on 'mysql.user' do
56
+ grant 'SELECT (user)'
57
+ grant 'UPDATE (host)'
58
+ end
59
+ end
60
+
61
+ user 'bob', 'localhost' do
62
+ on '*.*' do
63
+ grant 'USAGE'
64
+ end
65
+
66
+ on 'test.*' do
67
+ grant 'ALL PRIVILEGES'
68
+ end
69
+ end
70
+ RUBY
71
+ }
72
+
73
+ expect(show_grants).to match_array [
74
+ "GRANT ALL PRIVILEGES ON `test`.* TO 'bob'@'localhost'",
75
+ "GRANT SELECT (user), UPDATE (host) ON `mysql`.`user` TO 'scott'@'localhost'",
76
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40' REQUIRE SSL",
77
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
78
+ "GRANT USAGE ON *.* TO 'bob'@'localhost'",
79
+ ]
80
+ end
81
+ end
82
+
83
+ context 'when revoke privs' do
84
+ subject { client }
85
+
86
+ it do
87
+ apply(subject) {
88
+ <<-RUBY
89
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
90
+ on '*.*' do
91
+ grant 'SELECT'
92
+ end
93
+
94
+ on 'mysql.user' do
95
+ grant 'UPDATE (host)'
96
+ end
97
+ end
98
+
99
+ user 'bob', 'localhost' do
100
+ on '*.*' do
101
+ grant 'USAGE'
102
+ end
103
+
104
+ on 'test.*' do
105
+ grant 'ALL PRIVILEGES'
106
+ end
107
+ end
108
+ RUBY
109
+ }
110
+
111
+ expect(show_grants).to match_array [
112
+ "GRANT ALL PRIVILEGES ON `test`.* TO 'bob'@'localhost'",
113
+ "GRANT SELECT ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40' REQUIRE SSL",
114
+ "GRANT UPDATE (host) ON `mysql`.`user` TO 'scott'@'localhost'",
115
+ "GRANT USAGE ON *.* TO 'bob'@'localhost'",
116
+ ]
117
+ end
118
+ end
119
+
120
+ context 'when grant/revoke privs' do
121
+ subject { client }
122
+
123
+ it do
124
+ apply(subject) {
125
+ <<-RUBY
126
+ user 'scott', 'localhost', identified: 'tiger', required: 'SSL' do
127
+ on '*.*' do
128
+ grant 'UPDATE'
129
+ grant 'DELETE'
130
+ end
131
+
132
+ on 'test.*' do
133
+ grant 'SELECT'
134
+ grant 'INSERT'
135
+ end
136
+
137
+ on 'mysql.user' do
138
+ grant 'UPDATE (host)'
139
+ end
140
+ end
141
+
142
+ user 'mary', 'localhost' do
143
+ on '*.*' do
144
+ grant 'USAGE'
145
+ end
146
+
147
+ on 'test.*' do
148
+ grant 'ALL PRIVILEGES'
149
+ end
150
+ end
151
+ RUBY
152
+ }
153
+
154
+ expect(show_grants).to match_array [
155
+ "GRANT ALL PRIVILEGES ON `test`.* TO 'mary'@'localhost'",
156
+ "GRANT SELECT, INSERT ON `test`.* TO 'scott'@'localhost'",
157
+ "GRANT UPDATE (host) ON `mysql`.`user` TO 'scott'@'localhost'",
158
+ "GRANT UPDATE, DELETE ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40' REQUIRE SSL",
159
+ "GRANT USAGE ON *.* TO 'mary'@'localhost'",
160
+ ]
161
+ end
162
+ end
163
+
164
+ context 'when all privileges is normalized' do
165
+ subject { client }
166
+
167
+ it do
168
+ result = apply(subject) {
169
+ <<-RUBY
170
+ user 'scott', 'localhost', required: 'SSL' do
171
+ on '*.*' do
172
+ grant 'SELECT'
173
+ grant 'INSERT'
174
+ end
175
+
176
+ on 'test.*' do
177
+ grant 'UPDATE'
178
+ grant 'DELETE'
179
+ end
180
+
181
+ on 'mysql.user' do
182
+ grant 'SELECT (user)'
183
+ end
184
+ end
185
+
186
+ user 'bob', 'localhost' do
187
+ on '*.*' do
188
+ grant 'USAGE'
189
+ end
190
+
191
+ on 'test.*' do
192
+ grant 'ALL'
193
+ end
194
+ end
195
+ RUBY
196
+ }
197
+
198
+ expect(result).to be_falsey
199
+
200
+ expect(show_grants).to match_array [
201
+ "GRANT ALL PRIVILEGES ON `test`.* TO 'bob'@'localhost'",
202
+ "GRANT SELECT (user) ON `mysql`.`user` TO 'scott'@'localhost'",
203
+ "GRANT SELECT, INSERT ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40' REQUIRE SSL",
204
+ "GRANT UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
205
+ "GRANT USAGE ON *.* TO 'bob'@'localhost'",
206
+ ]
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,139 @@
1
+ describe 'Gratan::Client#apply' do
2
+ context 'when create user with auto identify' do
3
+ let(:auto_identifier) do
4
+ identifier = Gratan::Identifier::Auto.new('/dev/null')
5
+ allow(identifier).to receive(:mkpasswd) { 'foobarzoo' }
6
+ identifier
7
+ end
8
+
9
+ subject { client(identifier: auto_identifier) }
10
+
11
+ it do
12
+ apply(subject) {
13
+ <<-RUBY
14
+ user 'scott', 'localhost' do
15
+ on '*.*' do
16
+ grant 'SELECT'
17
+ grant 'INSERT'
18
+ grant 'UPDATE'
19
+ grant 'DELETE'
20
+ end
21
+
22
+ on 'test.*' do
23
+ grant 'SELECT'
24
+ grant 'INSERT'
25
+ grant 'UPDATE'
26
+ grant 'DELETE'
27
+ end
28
+ end
29
+ RUBY
30
+ }
31
+
32
+ expect(show_grants).to match_array [
33
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*6F498C84277BCC2089932690304BD4EDABC74547'",
34
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
35
+ ]
36
+ end
37
+ end
38
+
39
+ context 'when create user with csv identify' do
40
+ let(:csv_identifier) do
41
+ identifier = nil
42
+
43
+ csv = <<-CSV
44
+ scott@localhost,foobarzoo
45
+ CSV
46
+
47
+ tempfile(csv) do |f|
48
+ identifier = Gratan::Identifier::CSV.new(f.path)
49
+ end
50
+
51
+ identifier
52
+ end
53
+
54
+ subject { client(identifier: csv_identifier) }
55
+
56
+ it do
57
+ apply(subject) {
58
+ <<-RUBY
59
+ user 'scott', 'localhost' do
60
+ on '*.*' do
61
+ grant 'SELECT'
62
+ grant 'INSERT'
63
+ grant 'UPDATE'
64
+ grant 'DELETE'
65
+ end
66
+
67
+ on 'test.*' do
68
+ grant 'SELECT'
69
+ grant 'INSERT'
70
+ grant 'UPDATE'
71
+ grant 'DELETE'
72
+ end
73
+ end
74
+ RUBY
75
+ }
76
+
77
+ expect(show_grants).to match_array [
78
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*6F498C84277BCC2089932690304BD4EDABC74547'",
79
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
80
+ ]
81
+ end
82
+ end
83
+
84
+ context 'when create user with csv identify (there is no password)' do
85
+ let(:logger) do
86
+ logger = Logger.new('/dev/null')
87
+ expect(logger).to receive(:warn).with('[WARN] password for `scott@localhost` can not be found')
88
+ logger
89
+ end
90
+
91
+ let(:csv_identifier) do
92
+ identifier = nil
93
+
94
+ csv = <<-CSV
95
+ scott2@localhost,foobarzoo
96
+ CSV
97
+
98
+ tempfile(csv) do |f|
99
+ identifier = Gratan::Identifier::CSV.new(f.path, logger: logger)
100
+ end
101
+
102
+ identifier
103
+ end
104
+
105
+ subject do
106
+ client(
107
+ identifier: csv_identifier,
108
+ logger: logger
109
+ )
110
+ end
111
+
112
+ it do
113
+ apply(subject) {
114
+ <<-RUBY
115
+ user 'scott', 'localhost' do
116
+ on '*.*' do
117
+ grant 'SELECT'
118
+ grant 'INSERT'
119
+ grant 'UPDATE'
120
+ grant 'DELETE'
121
+ end
122
+
123
+ on 'test.*' do
124
+ grant 'SELECT'
125
+ grant 'INSERT'
126
+ grant 'UPDATE'
127
+ grant 'DELETE'
128
+ end
129
+ end
130
+ RUBY
131
+ }
132
+
133
+ expect(show_grants).to match_array [
134
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'scott'@'localhost'",
135
+ "GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'scott'@'localhost'",
136
+ ]
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,115 @@
1
+ describe 'Gratan::Client#apply' do
2
+ context 'when create user' do
3
+ subject { client(dry_run: true) }
4
+
5
+ it do
6
+ result = apply(subject) {
7
+ <<-RUBY
8
+ user 'scott', 'localhost', identified: 'tiger' do
9
+ on '*.*' do
10
+ grant 'SELECT'
11
+ grant 'INSERT'
12
+ grant 'UPDATE'
13
+ grant 'DELETE'
14
+ end
15
+
16
+ on 'test.*' do
17
+ grant 'SELECT'
18
+ grant 'INSERT'
19
+ grant 'UPDATE'
20
+ grant 'DELETE'
21
+ end
22
+ end
23
+ RUBY
24
+ }
25
+
26
+ expect(result).to be_falsey
27
+ expect(show_grants).to match_array []
28
+ end
29
+ end
30
+
31
+ context 'when add user' do
32
+ before do
33
+ apply {
34
+ <<-RUBY
35
+ user 'bob', '%', required: 'SSL' do
36
+ on '*.*' do
37
+ grant 'ALL PRIVILEGES'
38
+ end
39
+
40
+ on 'test.*' do
41
+ grant 'SELECT'
42
+ end
43
+ end
44
+ RUBY
45
+ }
46
+ end
47
+
48
+ subject { client(dry_run: true) }
49
+
50
+ it do
51
+ apply(subject) {
52
+ <<-RUBY
53
+ user 'bob', '%', required: 'SSL' do
54
+ on '*.*' do
55
+ grant 'ALL PRIVILEGES'
56
+ end
57
+
58
+ on 'test.*' do
59
+ grant 'SELECT'
60
+ end
61
+ end
62
+
63
+ user 'scott', 'localhost', identified: 'tiger' do
64
+ on '*.*' do
65
+ grant 'SELECT'
66
+ grant 'INSERT'
67
+ grant 'UPDATE'
68
+ grant 'DELETE'
69
+ end
70
+
71
+ on 'test.*' do
72
+ grant 'SELECT'
73
+ grant 'INSERT'
74
+ grant 'UPDATE'
75
+ grant 'DELETE'
76
+ end
77
+ end
78
+ RUBY
79
+ }
80
+
81
+ expect(show_grants).to match_array [
82
+ "GRANT ALL PRIVILEGES ON *.* TO 'bob'@'%' REQUIRE SSL",
83
+ "GRANT SELECT ON `test`.* TO 'bob'@'%'",
84
+ ]
85
+ end
86
+ end
87
+
88
+ context 'when create user with grant option' do
89
+ subject { client(dry_run: true) }
90
+
91
+ it do
92
+ apply(subject) {
93
+ <<-RUBY
94
+ user 'scott', 'localhost', identified: 'tiger' do
95
+ on '*.*', with: 'grant option' do
96
+ grant 'SELECT'
97
+ grant 'INSERT'
98
+ grant 'UPDATE'
99
+ grant 'DELETE'
100
+ end
101
+
102
+ on 'test.*' do
103
+ grant 'SELECT'
104
+ grant 'INSERT'
105
+ grant 'UPDATE'
106
+ grant 'DELETE'
107
+ end
108
+ end
109
+ RUBY
110
+ }
111
+
112
+ expect(show_grants).to match_array []
113
+ end
114
+ end
115
+ end