tenant_check 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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +48 -0
- data/.travis.yml +5 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/rake +29 -0
- data/bin/rspec +29 -0
- data/bin/setup +8 -0
- data/lib/tenant_check/active_record/extensions.rb +166 -0
- data/lib/tenant_check/notification.rb +39 -0
- data/lib/tenant_check/rack.rb +18 -0
- data/lib/tenant_check/stack_trace_fliter.rb +15 -0
- data/lib/tenant_check/version.rb +5 -0
- data/lib/tenant_check.rb +114 -0
- data/tenant_check.gemspec +26 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2c3352a7575b13cc9278df4276eb0fe4d0f12e33c458d957ce1e01408c2fd99f
|
4
|
+
data.tar.gz: e86621dc02c63d64d7a4ddf71fe6fe621623d6936c0d210956a51690ab64085b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eebdbf0719d645fdb208a00d0e46be2a56ba9bfc72f3175431df68bc765865a628d241f81c48b7b1bb973901af3ad32673a54bed6a73f2124ee77ab9e6dd829b
|
7
|
+
data.tar.gz: 168e790052c2d4e0479b94c51c8ab870ee577aa46c7b0dc0c246bf8a827baea0881fc9b52e9a27016bb84da8d19dbacb775a101494b5846252db3fccd25eaf69
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.4
|
5
|
+
Exclude:
|
6
|
+
- 'bin/*'
|
7
|
+
- '*.gemspec'
|
8
|
+
|
9
|
+
Lint/AmbiguousBlockAssociation:
|
10
|
+
Exclude:
|
11
|
+
- spec/**/*.rb
|
12
|
+
|
13
|
+
Style/BlockDelimiters:
|
14
|
+
EnforcedStyle: braces_for_chaining
|
15
|
+
|
16
|
+
Style/TrailingCommaInArrayLiteral:
|
17
|
+
EnforcedStyleForMultiline: comma
|
18
|
+
|
19
|
+
Style/NonNilCheck:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Naming/VariableNumber:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Naming/HeredocDelimiterNaming:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Style/Documentation:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Metrics/LineLength:
|
32
|
+
Max: 120
|
33
|
+
|
34
|
+
Metrics/MethodLength:
|
35
|
+
Max: 25
|
36
|
+
|
37
|
+
Metrics/BlockLength:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Metrics/AbcSize:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Metrics/CyclomaticComplexity:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Metrics/PerceivedComplexity:
|
47
|
+
Enabled: false
|
48
|
+
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Shunichi Ikegami
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# TenantCheck
|
2
|
+
|
3
|
+
Detect tenant unsafe queries in Rails app.
|
4
|
+
|
5
|
+
## CAVEAT
|
6
|
+
|
7
|
+
This gem is in an early stage of development.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'tenant_check', group: :development
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install tenant_check
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# in config/initializers/tenant_check.rb
|
29
|
+
if Rails.env.development?
|
30
|
+
TenantCheck.tenant_class = Tenant # your tenant class
|
31
|
+
TenantCheck.enable = true
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class Tenant < ApplicationRecord
|
37
|
+
has_many :users
|
38
|
+
has_many :tasks
|
39
|
+
end
|
40
|
+
|
41
|
+
class Task < ApplicationRecord
|
42
|
+
belongs_to :tenant
|
43
|
+
belongs_to :user, optional: true
|
44
|
+
end
|
45
|
+
|
46
|
+
class User < ApplicationRecord
|
47
|
+
belongs_to :tenant
|
48
|
+
has_many :tasks
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# unsafe queries. (output warnings to log)
|
54
|
+
user = User.first # the query without tenant is unsafe.
|
55
|
+
user.tasks.to_a # the query based on unsafe record is unsafe.
|
56
|
+
|
57
|
+
# safe queries. (no warnings)
|
58
|
+
tenant = Tenant.first # tenant query is safe.
|
59
|
+
tenant_user = tenant.users.first # the query based on tenant is safe.
|
60
|
+
tenant_user.tasks.to_a # the query based on safe record is safe.
|
61
|
+
current_user.tasks.to_a # devise current_user is safe and the query based on it is safe.
|
62
|
+
```
|
63
|
+
|
64
|
+
### temporarlly disable tenant check
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
users = TenantCheck.ignored { User.all }
|
68
|
+
```
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
73
|
+
|
74
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
75
|
+
|
76
|
+
## TODO
|
77
|
+
- test for various rails versions
|
78
|
+
- support calculation methods
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/shunichi/tenant_check.
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tenant_check"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rake' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rake", "rake")
|
data/bin/rspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rspec' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rspec-core", "rspec")
|
data/bin/setup
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module TenantCheck
|
6
|
+
module ActiveRecord
|
7
|
+
module TenantMethodExtension
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
attr_accessor :_tenant_check_safe
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module TenantSafetyCheck
|
16
|
+
class << self
|
17
|
+
def safe_preloading
|
18
|
+
Thread.current[:tenant_check_safe_preloading]
|
19
|
+
end
|
20
|
+
|
21
|
+
def safe_preloading=(value)
|
22
|
+
Thread.current[:tenant_check_safe_preloading] = value
|
23
|
+
end
|
24
|
+
|
25
|
+
def safe_preload(safe)
|
26
|
+
prev, self.safe_preloading = safe_preloading, true if safe # rubocop:disable Style/ParallelAssignment
|
27
|
+
yield
|
28
|
+
ensure
|
29
|
+
self.safe_preloading = prev if safe
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def check_tenant_safety(sql_descpription = nil)
|
36
|
+
return true if TenantSafetyCheck.safe_preloading || klass.name == ::TenantCheck.tenant_class_name
|
37
|
+
return true if respond_to?(:proxy_association) && proxy_association.owner._tenant_check_safe
|
38
|
+
unless tenant_safe_where_clause?(where_clause)
|
39
|
+
c = caller
|
40
|
+
lines = c.join("\n")
|
41
|
+
unless ::TenantCheck.safe_caller_patterns.any? { |reg| reg.match?(lines) }
|
42
|
+
::TenantCheck.add_notification ::TenantCheck::Notification.new(c, klass, sql_descpription || to_sql)
|
43
|
+
return false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
true
|
47
|
+
end
|
48
|
+
|
49
|
+
def tenant_safe_where_clause?(clause)
|
50
|
+
predicates = clause.send(:predicates)
|
51
|
+
equalities = predicates.grep(Arel::Nodes::Equality)
|
52
|
+
equalities.any? do |node|
|
53
|
+
attribute = node.left
|
54
|
+
return true if ::TenantCheck.tenant_associations[attribute.relation.name]&.member?(attribute.name)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
module CollectionProxyExtension
|
60
|
+
include TenantSafetyCheck
|
61
|
+
|
62
|
+
def load_target
|
63
|
+
return super unless ::TenantCheck.enable_and_started?
|
64
|
+
return super if loaded?
|
65
|
+
|
66
|
+
safe = check_tenant_safety
|
67
|
+
result = TenantSafetyCheck.safe_preload(safe) do
|
68
|
+
super
|
69
|
+
end
|
70
|
+
if safe
|
71
|
+
Array(target).each do |record|
|
72
|
+
record._tenant_check_safe = true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
result
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
module RelationExtension
|
80
|
+
include TenantSafetyCheck
|
81
|
+
|
82
|
+
def pluck(*column_names)
|
83
|
+
return super unless ::TenantCheck.enable_and_started?
|
84
|
+
return super if has_include?(column_names.first)
|
85
|
+
check_tenant_safety('pluck')
|
86
|
+
super
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def exec_queries(&block)
|
92
|
+
return super unless ::TenantCheck.enable_and_started?
|
93
|
+
|
94
|
+
safe = check_tenant_safety
|
95
|
+
|
96
|
+
result = TenantSafetyCheck.safe_preload(safe) do
|
97
|
+
super
|
98
|
+
end
|
99
|
+
|
100
|
+
if safe
|
101
|
+
@records.each do |record|
|
102
|
+
record._tenant_check_safe = true
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
result
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
module ActiveRecordExtension
|
111
|
+
def find_by_sql(sql, binds = [], preparable: nil, &block)
|
112
|
+
puts '************************* find_by_sql'
|
113
|
+
puts caller
|
114
|
+
puts '************************* sql'
|
115
|
+
pp sql
|
116
|
+
puts '************************* binds'
|
117
|
+
pp binds
|
118
|
+
puts '************************* to_sql'
|
119
|
+
puts connection.to_sql(sql, binds)
|
120
|
+
puts '************************* dump_arel'
|
121
|
+
dump_arel(sql)
|
122
|
+
super
|
123
|
+
end
|
124
|
+
|
125
|
+
def dump_arel(arel, depth = 0)
|
126
|
+
case arel
|
127
|
+
when Array
|
128
|
+
arel.each do |node|
|
129
|
+
dump_arel(node, depth + 1)
|
130
|
+
end
|
131
|
+
when ::Arel::SelectManager
|
132
|
+
indent_puts(depth, '[SelectManager]')
|
133
|
+
dump_arel(arel.ast, depth + 1)
|
134
|
+
pp arel
|
135
|
+
when ::Arel::Nodes::SelectStatement
|
136
|
+
indent_puts(depth, '[SelectStatement]')
|
137
|
+
dump_arel(arel.cores, depth + 1)
|
138
|
+
when ::Arel::Nodes::SelectCore
|
139
|
+
indent_puts(depth, '[SelectCore]')
|
140
|
+
indent_puts(depth + 1, 'projections=')
|
141
|
+
dump_arel(arel.projections, depth + 1)
|
142
|
+
indent_puts(depth + 1, 'join_source=')
|
143
|
+
dump_arel(arel.source, depth + 2)
|
144
|
+
indent_puts(depth + 1, 'wheres=')
|
145
|
+
dump_arel(arel.wheres, depth + 1)
|
146
|
+
when ::Arel::Nodes::Node
|
147
|
+
indent_puts(depth, "[#{arel.class}]")
|
148
|
+
when ::Arel::Attributes::Attribute
|
149
|
+
indent_puts(depth, "[#{arel.class}]")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def indent_puts(depth, str)
|
154
|
+
puts ' ' * depth + str
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
class << self
|
159
|
+
def apply_patch
|
160
|
+
::ActiveRecord::Base.include TenantMethodExtension
|
161
|
+
::ActiveRecord::Relation.prepend RelationExtension
|
162
|
+
::ActiveRecord::Associations::CollectionProxy.prepend CollectionProxyExtension
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tenant_check/stack_trace_fliter'
|
4
|
+
|
5
|
+
module TenantCheck
|
6
|
+
class Notification
|
7
|
+
attr_reader :callers, :filtered_callers, :base_class, :sql
|
8
|
+
|
9
|
+
def initialize(callers, base_class, sql)
|
10
|
+
@callers = callers
|
11
|
+
@filtered_callers = StackTraceFilter.filter_in_project(callers)
|
12
|
+
@base_class = base_class
|
13
|
+
@sql = sql
|
14
|
+
end
|
15
|
+
|
16
|
+
def filtered_callers_or_callers
|
17
|
+
filtered_callers.empty? ? callers : filtered_callers
|
18
|
+
end
|
19
|
+
|
20
|
+
def eql?(other)
|
21
|
+
base_class == other.base_class && callers == other.callers
|
22
|
+
end
|
23
|
+
|
24
|
+
def hash
|
25
|
+
[base_class, callers].hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def message
|
29
|
+
<<~EOS
|
30
|
+
>>> Query without tenant condition detected!
|
31
|
+
class: #{base_class}
|
32
|
+
sql: #{sql}
|
33
|
+
stacktrace:
|
34
|
+
#{filtered_callers_or_callers.join("\n")}
|
35
|
+
|
36
|
+
EOS
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TenantCheck
|
4
|
+
class Rack
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
TenantCheck.start
|
11
|
+
result = @app.call(env)
|
12
|
+
TenantCheck.output_notifications
|
13
|
+
result
|
14
|
+
ensure
|
15
|
+
TenantCheck.end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module TenantCheck
|
4
|
+
module StackTraceFilter
|
5
|
+
class << self
|
6
|
+
def filter_in_project(paths)
|
7
|
+
app_root = defined?(Rails) ? Rails.root.to_s : Dir.pwd
|
8
|
+
bundler_path = Bundler.bundle_path.to_s
|
9
|
+
paths.select do |path|
|
10
|
+
path.include?(app_root) && !path.include?(bundler_path)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/tenant_check.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'tenant_check/version'
|
4
|
+
require 'active_support/lazy_load_hooks'
|
5
|
+
require 'set'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
module TenantCheck
|
9
|
+
autoload :Notification, 'tenant_check/notification'
|
10
|
+
autoload :Rack, 'tenant_check/rack'
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_reader :enable
|
14
|
+
|
15
|
+
def enable=(value)
|
16
|
+
@enable = value
|
17
|
+
|
18
|
+
return if @patched || !value
|
19
|
+
@patched = true
|
20
|
+
ActiveSupport.on_load(:active_record) do
|
21
|
+
require 'tenant_check/active_record/extensions'
|
22
|
+
::TenantCheck::ActiveRecord.apply_patch
|
23
|
+
::Rails.configuration.middleware.use TenantCheck::Rack if defined? Rails
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def tenant_class=(klass)
|
28
|
+
if ::TenantCheck.tenant_class_name != nil && ::TenantCheck.tenant_class_name != klass.name
|
29
|
+
raise 'TenantCheck.tenant_class= must be called only once'
|
30
|
+
end
|
31
|
+
::TenantCheck.tenant_class_name = klass.name
|
32
|
+
|
33
|
+
klass.reflections.each do |_, reflection|
|
34
|
+
case reflection
|
35
|
+
when ::ActiveRecord::Reflection::HasManyReflection, ::ActiveRecord::Reflection::HasOneReflection
|
36
|
+
::TenantCheck.add_association(reflection.klass.arel_table.name, reflection.foreign_key)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_accessor :tenant_class_name
|
42
|
+
attr_writer :tenant_associations, :safe_caller_patterns
|
43
|
+
|
44
|
+
def tenant_associations
|
45
|
+
@tenant_associations ||= {}
|
46
|
+
end
|
47
|
+
|
48
|
+
def add_association(table, foreign_key)
|
49
|
+
tenant_associations[table] ||= Set.new
|
50
|
+
tenant_associations[table].add(foreign_key)
|
51
|
+
end
|
52
|
+
|
53
|
+
def default_safe_caller_patterns
|
54
|
+
[
|
55
|
+
/^.*devise.*`serialize_from_session'.*$/,
|
56
|
+
]
|
57
|
+
end
|
58
|
+
|
59
|
+
def safe_caller_patterns
|
60
|
+
@safe_caller_patterns ||= default_safe_caller_patterns
|
61
|
+
end
|
62
|
+
|
63
|
+
def start
|
64
|
+
Thread.current[:tenant_check_start] = true
|
65
|
+
Thread.current[:tenant_check_notifications] = Set.new
|
66
|
+
end
|
67
|
+
|
68
|
+
def end
|
69
|
+
Thread.current[:tenant_check_start] = nil
|
70
|
+
Thread.current[:tenant_check_notifications] = nil
|
71
|
+
end
|
72
|
+
|
73
|
+
def started?
|
74
|
+
Thread.current[:tenant_check_start]
|
75
|
+
end
|
76
|
+
|
77
|
+
def enable_and_started?
|
78
|
+
enable && started? && !temporally_disabled?
|
79
|
+
end
|
80
|
+
|
81
|
+
def temporally_disabled?
|
82
|
+
Thread.current[:tenant_check_temporally_disabled]
|
83
|
+
end
|
84
|
+
|
85
|
+
def temporally_disabled=(value)
|
86
|
+
Thread.current[:tenant_check_temporally_disabled] = value
|
87
|
+
end
|
88
|
+
|
89
|
+
def ignored
|
90
|
+
prev, self.temporally_disabled = temporally_disabled?, true # rubocop:disable Style/ParallelAssignment
|
91
|
+
yield
|
92
|
+
ensure
|
93
|
+
self.temporally_disabled = prev
|
94
|
+
end
|
95
|
+
|
96
|
+
def notifications
|
97
|
+
Thread.current[:tenant_check_notifications]
|
98
|
+
end
|
99
|
+
|
100
|
+
def add_notification(notification)
|
101
|
+
notifications.add(notification) if started?
|
102
|
+
end
|
103
|
+
|
104
|
+
def output_notifications
|
105
|
+
notifications.each do |notification|
|
106
|
+
logger.warn(notification.message)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def logger
|
111
|
+
@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "tenant_check/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tenant_check"
|
8
|
+
spec.version = TenantCheck::VERSION
|
9
|
+
spec.authors = ["Shunichi Ikegami"]
|
10
|
+
spec.email = ["sike.tm@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{detect queries without tenant.}
|
13
|
+
spec.description = %q{detect queries without tenant.}
|
14
|
+
spec.homepage = "https://github.com/shunichi/tenant_check"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tenant_check
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shunichi Ikegami
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: detect queries without tenant.
|
56
|
+
email:
|
57
|
+
- sike.tm@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/rake
|
72
|
+
- bin/rspec
|
73
|
+
- bin/setup
|
74
|
+
- lib/tenant_check.rb
|
75
|
+
- lib/tenant_check/active_record/extensions.rb
|
76
|
+
- lib/tenant_check/notification.rb
|
77
|
+
- lib/tenant_check/rack.rb
|
78
|
+
- lib/tenant_check/stack_trace_fliter.rb
|
79
|
+
- lib/tenant_check/version.rb
|
80
|
+
- tenant_check.gemspec
|
81
|
+
homepage: https://github.com/shunichi/tenant_check
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.7.6
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: detect queries without tenant.
|
105
|
+
test_files: []
|