gratan 0.2.9 → 0.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98951da30ecfba75652d74de749bef4b91adcd7a
4
- data.tar.gz: f3e9f9df2a22400a79f7106dc2700154cfbf46e0
3
+ metadata.gz: 9b5841f8e6566f6d2f1f0902a187e585d74b61f3
4
+ data.tar.gz: 6f1d5b4e0b61102836904955de4328fd46537cf8
5
5
  SHA512:
6
- metadata.gz: cb22d874f085d3d640d765c0eefdb89ad3a2197de69877ff441e56b49825f5c6c37cadcd38bfed24246388b2b77e654c99d16693259f5dca36ec18c9d5460529
7
- data.tar.gz: aa16326bdca0ae05556469fc81149798b13dadfd1989d5154620faf0ff6d89d56da239f65d906edef9f17e76dbe557025fa955f1f015b3c128ab11504cc5e192
6
+ metadata.gz: 1ce0aa5bf26ccfbe52b8ab489c40ee5aee0c8cec8d29e9ec9c7dd369bd79a164b42017c4421c008fb31d1c916b75e4123ee834206536c1734a0129b552923754
7
+ data.tar.gz: 0a5de053a1d33126a5c8c24c2e573a7d446be8904fe9bc619e798eb3f2585a16b62de5e47105a1987b34322142dffd8eb0120406b1df5f2b1dd66b2de763521a
@@ -1,7 +1,24 @@
1
- sudo: false
1
+ sudo: true
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.0.0
5
+ - 2.1
6
+ - 2.2
5
7
  script:
6
8
  - bundle install
7
9
  - bundle exec rake
10
+ env:
11
+ - MYSQL_VERSION=5.6.27
12
+ install:
13
+ - "sudo apt-get -y remove mysql-common mysql-server-5.5 mysql-server-core-5.5 mysql-client-5.5 mysql-client-core-5.5"
14
+ - "sudo apt-get -y autoremove"
15
+ - "sudo apt-get -y install libaio1"
16
+ - "wget -O mysql-$MYSQL_VERSION.deb http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-$MYSQL_VERSION-debian6.0-x86_64.deb/from/http://cdn.mysql.com/"
17
+ - "sudo dpkg -i mysql-$MYSQL_VERSION.deb"
18
+ - "sudo cp -f /opt/mysql/server-5.6/support-files/mysql.server /etc/init.d/mysql.server"
19
+ - "sudo ln -sf /opt/mysql/server-5.6/bin/* /usr/bin/"
20
+ - "sudo sed -i'' 's/table_cache/table_open_cache/' /etc/mysql/my.cnf"
21
+ - "sudo sed -i'' 's/log_slow_queries/slow_query_log/' /etc/mysql/my.cnf"
22
+ - "sudo sed -i'' 's/basedir[^=]\\+=.*$/basedir = \\/opt\\/mysql\\/server-5.6/' /etc/mysql/my.cnf"
23
+ - "sudo sed -i'' 's/^socket *=.*$/socket = \\/tmp\\/mysql.sock/' /etc/mysql/my.cnf"
24
+ - "sudo /etc/init.d/mysql.server start"
data/README.md CHANGED
@@ -8,6 +8,11 @@ It defines the state of MySQL permissions using Ruby DSL, and updates permission
8
8
  [![Build Status](https://travis-ci.org/winebarrel/gratan.svg?branch=master)](https://travis-ci.org/winebarrel/gratan)
9
9
  [![Coverage Status](https://coveralls.io/repos/winebarrel/gratan/badge.svg?branch=master)](https://coveralls.io/r/winebarrel/gratan?branch=master)
10
10
 
11
+ ## Notice
12
+
13
+ * `>= 0.3.0`
14
+ * Support template
15
+
11
16
  ## Installation
12
17
 
13
18
  Add this line to your application's Gemfile:
@@ -93,6 +98,33 @@ user "scott", ["localhost", "192.168.%"], expired: '2014/10/10' do
93
98
  end
94
99
  ```
95
100
 
101
+ ### Use template
102
+
103
+ ```ruby
104
+ template 'all db template' do
105
+ on '*.*' do
106
+ grant 'SELECT'
107
+ end
108
+ end
109
+
110
+ template 'test db template' do
111
+ grant context.default
112
+
113
+ context.extra.each do |priv|
114
+ grant priv
115
+ end
116
+ end
117
+
118
+ user 'scott', 'localhost', identified: 'tiger' do
119
+ include_template 'all db template'
120
+
121
+ on 'test.*' do
122
+ context.default = 'SELECT'
123
+ include_template 'test db template', extra: ['INSERT', 'UPDATE']
124
+ end
125
+ end
126
+ ```
127
+
96
128
  ## Similar tools
97
129
  * [Codenize.tools](http://codenize.tools/)
98
130
 
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency 'mysql2'
22
22
  spec.add_dependency 'term-ansicolor'
23
23
  spec.add_dependency 'deep_merge'
24
+ spec.add_dependency 'hashie'
24
25
  spec.add_development_dependency 'bundler'
25
26
  spec.add_development_dependency 'rake', '~> 10.0'
26
27
  spec.add_development_dependency 'rspec', '>= 3.0.0'
@@ -5,9 +5,11 @@ require 'time'
5
5
  require 'deep_merge'
6
6
  require 'mysql2'
7
7
  require 'term/ansicolor'
8
+ require 'hashie'
8
9
 
9
10
  module Gratan; end
10
11
  require 'gratan/logger'
12
+ require 'gratan/template_helper'
11
13
  require 'gratan/client'
12
14
  require 'gratan/driver'
13
15
  require 'gratan/dsl'
@@ -113,6 +113,25 @@ class Gratan::Driver
113
113
  ]
114
114
 
115
115
  update(sql)
116
+
117
+ if (identifier || '').empty?
118
+ set_password(user, host, identifier)
119
+ end
120
+ end
121
+
122
+ def set_password(user, host, password, options = {})
123
+ password ||= ''
124
+
125
+ unless options[:hash]
126
+ password = "PASSWORD('#{escape(password)}')"
127
+ end
128
+
129
+ sql = 'SET PASSWORD FOR %s = %s' % [
130
+ quote_user(user, host),
131
+ password,
132
+ ]
133
+
134
+ update(sql)
116
135
  end
117
136
 
118
137
  def set_require(user, host, required)
@@ -1,6 +1,7 @@
1
1
  class Gratan::DSL::Context
2
2
  include Gratan::DSL::Validator
3
3
  include Gratan::Logger::Helper
4
+ include Gratan::TemplateHelper
4
5
 
5
6
  def self.eval(dsl, path, options = {})
6
7
  self.new(path, options) do
@@ -14,11 +15,22 @@ class Gratan::DSL::Context
14
15
  @path = path
15
16
  @options = options
16
17
  @result = {}
18
+
19
+ @context = Hashie::Mash.new(
20
+ :path => path,
21
+ :options => options,
22
+ :templates => {}
23
+ )
24
+
17
25
  instance_eval(&block)
18
26
  end
19
27
 
20
28
  private
21
29
 
30
+ def template(name, &block)
31
+ @context.templates[name.to_s] = block
32
+ end
33
+
22
34
  def require(file)
23
35
  grantfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))
24
36
 
@@ -52,7 +64,7 @@ class Gratan::DSL::Context
52
64
  end
53
65
 
54
66
  @result[[name, host]] = {
55
- :objects => Gratan::DSL::Context::User.new(name, host, @options, &block).result,
67
+ :objects => Gratan::DSL::Context::User.new(@context, name, host, @options, &block).result,
56
68
  :options => options,
57
69
  }
58
70
  end
@@ -1,14 +1,16 @@
1
1
  class Gratan::DSL::Context::On
2
2
  include Gratan::DSL::Validator
3
+ include Gratan::TemplateHelper
3
4
 
4
5
  attr_reader :result
5
6
 
6
- def initialize(user, host, object, options, &block)
7
+ def initialize(context, user, host, object, options, &block)
7
8
  @object_identifier = "User `#{user}@#{host}` on `#{object}`"
8
9
  @user = user
9
10
  @host = host
10
11
  @object = object
11
12
  @options = options
13
+ @context = context.merge(:object => object, :grant_options => options)
12
14
  @result = []
13
15
  instance_eval(&block)
14
16
  end
@@ -1,14 +1,16 @@
1
1
  class Gratan::DSL::Context::User
2
2
  include Gratan::DSL::Validator
3
3
  include Gratan::Logger::Helper
4
+ include Gratan::TemplateHelper
4
5
 
5
6
  attr_reader :result
6
7
 
7
- def initialize(user, host, options, &block)
8
+ def initialize(context, user, host, options, &block)
8
9
  @object_identifier = "User `#{user}@#{host}`"
9
10
  @user = user
10
11
  @host = host
11
12
  @options = options
13
+ @context = context.merge(:user => user, :host => host, :user_options => options)
12
14
  @result = {}
13
15
  instance_eval(&block)
14
16
  end
@@ -29,7 +31,7 @@ class Gratan::DSL::Context::User
29
31
  end
30
32
  end
31
33
 
32
- grant = {:privs => Gratan::DSL::Context::On.new(@user, @host, name, @options, &block).result}
34
+ grant = {:privs => Gratan::DSL::Context::On.new(@context, @user, @host, name, @options, &block).result}
33
35
  grant[:with] = options[:with] if options[:with]
34
36
  @result[name] = grant
35
37
  end
@@ -0,0 +1,20 @@
1
+ module Gratan
2
+ module TemplateHelper
3
+ def include_template(template_name, context = {})
4
+ tmplt = @context.templates[template_name.to_s]
5
+
6
+ unless tmplt
7
+ raise "Template `#{template_name}` is not defined"
8
+ end
9
+
10
+ context_orig = @context
11
+ @context = @context.merge(context)
12
+ instance_eval(&tmplt)
13
+ @context = context_orig
14
+ end
15
+
16
+ def context
17
+ @context
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Gratan
2
- VERSION = '0.2.9'
2
+ VERSION = '0.3.0.beta'
3
3
  end
@@ -0,0 +1,41 @@
1
+ describe 'Gratan::Client#apply' do
2
+ context 'when create user with template' do
3
+ subject { client }
4
+
5
+ it do
6
+ result = apply(subject) {
7
+ <<-RUBY
8
+ template 'all db template' do
9
+ on '*.*' do
10
+ grant 'SELECT'
11
+ end
12
+ end
13
+
14
+ template 'test db template' do
15
+ grant context.default
16
+
17
+ context.extra.each do |priv|
18
+ grant priv
19
+ end
20
+ end
21
+
22
+ user 'scott', 'localhost', identified: 'tiger' do
23
+ include_template 'all db template'
24
+
25
+ on 'test.*' do
26
+ context.default = 'SELECT'
27
+ include_template 'test db template', extra: ['INSERT', 'UPDATE']
28
+ end
29
+ end
30
+ RUBY
31
+ }
32
+
33
+ expect(result).to be_truthy
34
+
35
+ expect(show_grants).to match_array [
36
+ "GRANT SELECT ON *.* TO 'scott'@'localhost' IDENTIFIED BY PASSWORD '*F2F68D0BB27A773C1D944270E5FAFED515A3FA40'",
37
+ "GRANT SELECT, INSERT, UPDATE ON `test`.* TO 'scott'@'localhost'",
38
+ ]
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -1,125 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gratan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.3.0.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-29 00:00:00.000000000 Z
11
+ date: 2015-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: term-ansicolor
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: deep_merge
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: hashie
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ">="
73
+ - - '>='
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ">="
80
+ - - '>='
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - "~>"
87
+ - - ~>
74
88
  - !ruby/object:Gem::Version
75
89
  version: '10.0'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - "~>"
94
+ - - ~>
81
95
  - !ruby/object:Gem::Version
82
96
  version: '10.0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - ">="
101
+ - - '>='
88
102
  - !ruby/object:Gem::Version
89
103
  version: 3.0.0
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - ">="
108
+ - - '>='
95
109
  - !ruby/object:Gem::Version
96
110
  version: 3.0.0
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: timecop
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
- - - ">="
115
+ - - '>='
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
- - - ">="
122
+ - - '>='
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: coveralls
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - ">="
129
+ - - '>='
116
130
  - !ruby/object:Gem::Version
117
131
  version: '0'
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - ">="
136
+ - - '>='
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0'
125
139
  description: Gratan is a tool to manage MySQL permissions using Ruby DSL.
@@ -130,9 +144,9 @@ executables:
130
144
  extensions: []
131
145
  extra_rdoc_files: []
132
146
  files:
133
- - ".gitignore"
134
- - ".rspec"
135
- - ".travis.yml"
147
+ - .gitignore
148
+ - .rspec
149
+ - .travis.yml
136
150
  - Gemfile
137
151
  - LICENSE.txt
138
152
  - README.md
@@ -156,6 +170,7 @@ files:
156
170
  - lib/gratan/identifier/csv.rb
157
171
  - lib/gratan/identifier/null.rb
158
172
  - lib/gratan/logger.rb
173
+ - lib/gratan/template_helper.rb
159
174
  - lib/gratan/version.rb
160
175
  - spec/change/change_grants_2_spec.rb
161
176
  - spec/change/change_grants_3_spec.rb
@@ -176,6 +191,7 @@ files:
176
191
  - spec/create/create_user_target_spec.rb
177
192
  - spec/create/create_user_with_func_prcd_spec.rb
178
193
  - spec/create/create_user_with_ignore_object_spec.rb
194
+ - spec/create/create_user_with_template_spec.rb
179
195
  - spec/drop/drop_user_2_spec.rb
180
196
  - spec/drop/drop_user_spec.rb
181
197
  - spec/drop/expire_user_spec.rb
@@ -196,17 +212,17 @@ require_paths:
196
212
  - lib
197
213
  required_ruby_version: !ruby/object:Gem::Requirement
198
214
  requirements:
199
- - - ">="
215
+ - - '>='
200
216
  - !ruby/object:Gem::Version
201
217
  version: '0'
202
218
  required_rubygems_version: !ruby/object:Gem::Requirement
203
219
  requirements:
204
- - - ">="
220
+ - - '>'
205
221
  - !ruby/object:Gem::Version
206
- version: '0'
222
+ version: 1.3.1
207
223
  requirements: []
208
224
  rubyforge_project:
209
- rubygems_version: 2.4.5
225
+ rubygems_version: 2.0.14.1
210
226
  signing_key:
211
227
  specification_version: 4
212
228
  summary: Gratan is a tool to manage MySQL permissions using Ruby DSL.
@@ -230,6 +246,7 @@ test_files:
230
246
  - spec/create/create_user_target_spec.rb
231
247
  - spec/create/create_user_with_func_prcd_spec.rb
232
248
  - spec/create/create_user_with_ignore_object_spec.rb
249
+ - spec/create/create_user_with_template_spec.rb
233
250
  - spec/drop/drop_user_2_spec.rb
234
251
  - spec/drop/drop_user_spec.rb
235
252
  - spec/drop/expire_user_spec.rb