ksconnect 0.1.13 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ce87324cd6e4c21f489c752a6eb33944bd778e5
4
- data.tar.gz: b41e6e41d0d139d45d1cc7f2180fd171ba4358b1
3
+ metadata.gz: 06207d64b5e16054f9879283f9b3635d43486380
4
+ data.tar.gz: e716c4150e97ebb0f600a99dacbd27502b4d23b3
5
5
  SHA512:
6
- metadata.gz: 549d3295ac65cf384297a8077faf3610a2f9bbaeaa41d982945849d3f94a69a28f1ed7536696b0084f51dbf21d8b8f58dc33834f0b589ea1205cc5ed58fd3204
7
- data.tar.gz: 54bfee7aefbf5e248271a9c04b7d253d659e3126c20244835f5c457274e1c76e0632b1b2b2e0a9426fe0f4daae1eec037994126c982c2bde1c87030a4d3e3e56
6
+ metadata.gz: c1cd24055c673e2585f2dc362ffe2a456561f1934a72725ca342f26f5687c4423ae450cc50c3dbed8fde16ecd6e7ca08a8f916d5e774485c9afe7ca59720d2ac
7
+ data.tar.gz: 51c868da513607c93a184ae2e52ef8fe3da918a211400c3cefa9daffe3796dccb635dd1b0d5219df3348c54f3c9de6c449bd1cea16f65cc97856fa17ff9fac2a
@@ -19,46 +19,61 @@ class KSConnect
19
19
 
20
20
  @cache = ActiveSupport::HashWithIndifferentAccess.new if @use_cache
21
21
  @cache_uuid = nil
22
+
23
+ Redis.current ||= Redis.new(driver: :hiredis)
24
+ $redis ||= ConnectionPool.new(size: MAX_THREADS, timeout: 8) { Redis.current }
22
25
  end
23
26
 
24
27
  def []=(field, value)
25
28
  @cache[field] = value if @use_cache
26
- redis.hset(key, field, value)
27
- redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
29
+ $redis.with { |redis|
30
+ redis.hset(key, field, value)
31
+ redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
32
+ }
28
33
  end
29
34
 
30
35
  def setall(hash)
31
36
  @cache.merge!(hash) if @use_cache
32
- redis.mapped_hmset(key, hash)
33
- redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
37
+ $redis.with { |redis|
38
+ redis.mapped_hmset(key, hash)
39
+ redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
40
+ }
34
41
  end
35
42
 
36
43
  def [](field)
37
- if @use_cache
38
- @cache = redis.hgetall(key) if @cache.empty?
39
- @cache[field]
40
- else
41
- redis.hget(key, field)
42
- end
44
+ $redis.with { |redis|
45
+ if @use_cache
46
+ @cache = redis.hgetall(key) if @cache.empty?
47
+ @cache[field]
48
+ else
49
+ redis.hget(key, field)
50
+ end
51
+ }
43
52
  end
44
53
 
45
54
  def getall
46
- if @use_cache
47
- @cache = redis.hgetall(key) if @cache.empty?; @cache
48
- else
49
- redis.hgetall(key)
50
- end
55
+ $redis.with { |redis|
56
+ if @use_cache
57
+ @cache = redis.hgetall(key) if @cache.empty?; @cache
58
+ else
59
+ redis.hgetall(key)
60
+ end
61
+ }
51
62
  end
52
63
 
53
64
  def reload
54
- @cache = redis.hgetall(key) if @use_cache
55
- @cache
65
+ $redis.with { |redis|
66
+ @cache = redis.hgetall(key) if @use_cache
67
+ @cache
68
+ }
56
69
  end
57
70
 
58
71
  def delete(field)
59
- @cache.delete(field) if @use_cache
60
- redis.hdel(key, field)
61
- redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
72
+ $redis.with { |redis|
73
+ @cache.delete(field) if @use_cache
74
+ redis.hdel(key, field)
75
+ redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify
76
+ }
62
77
  end
63
78
 
64
79
  def key
@@ -69,23 +84,21 @@ class KSConnect
69
84
  private
70
85
 
71
86
  def set_data_uuid
72
- begin
73
- tries ||= 3
74
- id = redis.hget("#{@plugin_name}:#{@type}", @domain_name)
75
- if id
76
- @cache_uuid = id
77
- else
78
- @cache_uuid = SecureRandom.uuid
79
- raise "Race on setting data key failed." unless redis.hsetnx("#{@plugin_name}:#{@type}", @domain_name, @cache_uuid)
87
+ $redis.with { |redis|
88
+ begin
89
+ tries ||= 3
90
+ id = redis.hget("#{@plugin_name}:#{@type}", @domain_name)
91
+ if id
92
+ @cache_uuid = id
93
+ else
94
+ @cache_uuid = SecureRandom.uuid
95
+ raise "Race on setting data key failed." unless redis.hsetnx("#{@plugin_name}:#{@type}", @domain_name, @cache_uuid)
96
+ end
97
+ rescue Exception => e
98
+ logger.error e.message
99
+ retry unless (tries -= 1).zero?
80
100
  end
81
- rescue Exception => e
82
- logger.error e.message
83
- retry unless (tries -= 1).zero?
84
- end
85
- end
86
-
87
- def redis
88
- Redis.current
101
+ }
89
102
  end
90
103
  end
91
104
  end
@@ -17,38 +17,47 @@ class KSConnect
17
17
  @plugin_name = plugin_name
18
18
  @data = Data.new(plugin_name, name, type: :data)
19
19
  @private_data = Data.new(plugin_name, name, type: :private_data, auto_notify: true)
20
+
21
+ Redis.current ||= Redis.new(driver: :hiredis)
22
+ $redis ||= ConnectionPool.new(size: MAX_THREADS, timeout: 8) { Redis.current }
20
23
  end
21
24
 
22
25
  def update_ip_address
23
- @ip_address = redis.hget(domains_key, name)
26
+ $redis.with { |redis|
27
+ @ip_address = redis.hget(domains_key, name)
28
+ }
24
29
  end
25
30
 
26
31
  def ip_address=(new_ip)
27
32
  @ip_address = new_ip
28
- redis.hset(domains_key, name, new_ip)
29
- redis.publish("core:push", { domain_name: name, plugin_name: "core", request_type: "update"}.to_json) if plugin_name == 'core'
33
+ $redis.with { |redis|
34
+ redis.hset(domains_key, name, new_ip)
35
+ redis.publish("core:push", { domain_name: name, plugin_name: "core", request_type: "update"}.to_json) if plugin_name == 'core'
36
+ }
30
37
  end
31
38
 
32
39
  def notify(data)
33
- redis.lpush("ks:notifications_push", data.merge({ domain_name: @name, plugin_name: @plugin_name }).to_json)
40
+ $redis.with { |redis|
41
+ redis.lpush("ks:notifications_push", data.merge({ domain_name: @name, plugin_name: @plugin_name }).to_json)
42
+ }
34
43
  end
35
44
 
36
45
  def add_issue(issue_type, data)
37
- redis.lpush("ks:issues_push", data.merge({ domain_name: @name, plugin_name: @plugin_name, issue_type: issue_type, request_type: 'add' }).to_json)
46
+ $redis.with { |redis|
47
+ redis.lpush("ks:issues_push", data.merge({ domain_name: @name, plugin_name: @plugin_name, issue_type: issue_type, request_type: 'add' }).to_json)
48
+ }
38
49
  end
39
50
 
40
51
  def clear_issue(issue_type)
41
- redis.lpush("ks:issues_push", { domain_name: @name, plugin_name: @plugin_name, issue_type: issue_type, request_type: 'remove' }.to_json)
52
+ $redis.with { |redis|
53
+ redis.lpush("ks:issues_push", { domain_name: @name, plugin_name: @plugin_name, issue_type: issue_type, request_type: 'remove' }.to_json)
54
+ }
42
55
  end
43
56
 
44
57
  private
45
58
 
46
- def redis
47
- Redis.current
48
- end
49
-
50
59
  def domains_key
51
- @domain_list_uuid ||= redis.hget("#{plugin_name}:data", "domain_names")
60
+ @domain_list_uuid ||= $redis.with { |redis| redis.hget("#{plugin_name}:data", "domain_names") }
52
61
  "kloudsec_data:#{@domain_list_uuid}"
53
62
  end
54
63
  end
@@ -15,6 +15,9 @@ class KSConnect
15
15
  @main_plugin = main
16
16
  @domains = {}
17
17
 
18
+ Redis.current ||= Redis.new(driver: :hiredis)
19
+ $redis ||= ConnectionPool.new(size: MAX_THREADS, timeout: 8) { Redis.current }
20
+
18
21
  load_domains
19
22
  subscribe_to_events
20
23
  end
@@ -31,7 +34,7 @@ class KSConnect
31
34
  # Note that it does not update the ip address of existing domains.
32
35
  def load_domains
33
36
  # load domain list
34
- domain_to_ip = redis.hgetall(domains_key)
37
+ domain_to_ip = $redis.with { |redis| redis.hgetall(domains_key) }
35
38
 
36
39
  # add new domains
37
40
  new_domains = domain_to_ip.keys - @domains.values.map(&:name)
@@ -67,14 +70,16 @@ class KSConnect
67
70
 
68
71
  def update_action(action_id, state, body="")
69
72
  raise "Updating action with empty action_id" if action_id.nil? or action_id.empty?
70
- redis.multi do |redis_transaction|
71
- redis_transaction.mapped_hmset "kloudsec_data:#{action_id}", {
72
- plugin_name: @name,
73
- uuid: action_id,
74
- state: state,
75
- response: body,
76
- }
77
- redis_transaction.expire "kloudsec_data:#{action_id}", 300
73
+ $redis.with do |redis|
74
+ redis.multi do |redis_transaction|
75
+ redis_transaction.mapped_hmset "kloudsec_data:#{action_id}", {
76
+ plugin_name: @name,
77
+ uuid: action_id,
78
+ state: state,
79
+ response: body,
80
+ }
81
+ redis_transaction.expire "kloudsec_data:#{action_id}", 300
82
+ end
78
83
  end
79
84
  end
80
85
 
@@ -103,11 +108,13 @@ class KSConnect
103
108
  raise "Invalid request type"
104
109
  end
105
110
 
106
- redis.publish("core:push", { domain_name: domain_name, plugin_name: @name, request_type: request_type }.to_json) if should_repush?
111
+ $redis.with { |redis|
112
+ redis.publish("core:push", { domain_name: domain_name, plugin_name: @name, request_type: request_type }.to_json) if should_repush?
113
+ }
107
114
  end
108
115
 
109
116
  def domains_key
110
- @domain_list_uuid ||= redis.hget("#{@name}:data", "domain_names")
117
+ @domain_list_uuid ||= $redis.with { |redis| redis.hget("#{@name}:data", "domain_names") }
111
118
  "kloudsec_data:#{@domain_list_uuid}"
112
119
  end
113
120
 
@@ -118,11 +125,7 @@ class KSConnect
118
125
  end
119
126
 
120
127
  def get_ip_for(domain_name)
121
- redis.hget(domains_key, domain_name)
122
- end
123
-
124
- def redis
125
- Redis.current
128
+ $redis.with { |redis| redis.hget(domains_key, domain_name) }
126
129
  end
127
130
  end
128
131
  end
@@ -1,8 +1,10 @@
1
+ require 'active_support/core_ext/object/try'
2
+
1
3
  class KSConnect
2
4
  module Helpers
3
5
  def ip_address_for(domain)
4
- if all_domains[domain].present?
5
- all_domains[domain].ip_address
6
+ if all_domains[domain.name]
7
+ all_domains[domain.name].ip_address
6
8
  else
7
9
  "kloudsec.com" # go to something safe if ip is invalid
8
10
  end
@@ -14,32 +16,40 @@ class KSConnect
14
16
  end
15
17
 
16
18
  def https_redirect_enabled?(domain)
17
- https_enabled?(domain) && plugins[:ssl].domains[domain].data['redirect'] == "true"
19
+ https_enabled?(domain) && plugins[:ssl].domains[domain.name].data['redirect'] == "true"
18
20
  end
19
21
 
20
22
  def https_rewriting_enabled?(domain)
21
- https_enabled?(domain) && plugins[:ssl].domains[domain].data['rewriteHTTPS'] == "true"
23
+ https_enabled?(domain) && plugins[:ssl].domains[domain.name].data['rewriteHTTPS'] == "true"
22
24
  end
23
25
 
24
26
  def waf_enabled?(domain)
25
- plugins[:web_shield].domains.include?(domain)
27
+ plugins[:web_shield].domains.has_key?(domain.name)
26
28
  end
27
29
 
28
30
  def waf_learning?(domain)
29
- waf_enabled?(domain) && plugins[:web_shield].domains[domain].data['learning']
31
+ waf_enabled?(domain) && plugins[:web_shield].domains[domain.name].data['learning']
32
+ end
33
+
34
+ def waf_location_config(domain)
35
+ waf_enabled?(domain) ? plugins[:web_shield].domains[domain.name].private_data['location.conf'] : ""
36
+ end
37
+
38
+ def waf_server_config(domain)
39
+ waf_enabled?(domain) ? plugins[:web_shield].domains[domain.name].private_data['server.conf'] : ""
30
40
  end
31
41
 
32
42
  def pagespeed_enabled?(domain)
33
- plugins[:mod_cache].domains.include?(domain)
43
+ plugins[:mod_cache].domains.has_key?(domain.name)
34
44
  end
35
45
 
36
46
  def pending_autossl?(domain)
37
47
  p, k = autossl_verification_path_and_key_for(domain)
38
- plugins[:autossl].domains.include?(domain) && p && k
48
+ plugins[:autossl].domains.has_key?(domain.name) && p && k
39
49
  end
40
50
 
41
51
  def autossl_verification_path_and_key_for(domain)
42
- d = plugins[:autossl].domains[domain].private_data
52
+ d = plugins[:autossl].domains[domain.name].try(:private_data)
43
53
  if d
44
54
  return d['verify_endpoint'], d['verify_content']
45
55
  else
@@ -48,8 +58,8 @@ class KSConnect
48
58
  end
49
59
 
50
60
  def ssl_key_and_cert_for(domain)
51
- ssl = plugins[:ssl].domains[domain].private_data
52
- autossl = plugins[:autossl].domains[domain].private_data
61
+ ssl = plugins[:ssl].domains[domain.name].try(:private_data)
62
+ autossl = plugins[:autossl].domains[domain.name].try(:private_data)
53
63
 
54
64
  if ssl && ssl['key'] && ssl['cert']
55
65
  return ssl['key'], ssl['cert']
data/lib/ksconnect.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'redis'
2
+ require 'thread/pool'
3
+ require 'connection_pool'
2
4
  require 'ksconnect/api'
3
5
  require 'ksconnect/api/plugin'
4
6
  require 'ksconnect/api/plugin/config'
@@ -6,15 +8,19 @@ require 'ksconnect/api/plugin/data'
6
8
  require 'ksconnect/api/plugin/domain'
7
9
  require 'ksconnect/helpers'
8
10
  require 'ksconnect/logs'
9
- require 'thread/pool'
10
11
 
11
12
  class KSConnect
12
13
  include Logs
13
14
  attr_reader :api
14
15
  attr_reader :plugin
15
16
 
17
+ MIN_THREADS = ENV['MIN_THREADS'] || 5
18
+ MAX_THREADS = ENV['MAX_THREADS'] || 25
19
+
16
20
  def initialize(*args)
17
21
  Redis.current ||= Redis.new(driver: :hiredis)
22
+ $redis ||= ConnectionPool.new(size: MAX_THREADS, timeout: 8) { Redis.current }
23
+
18
24
  plugins = args
19
25
 
20
26
  additional_options = args.last.is_a?(Hash) ? args.last : nil
@@ -28,7 +34,7 @@ class KSConnect
28
34
  end
29
35
 
30
36
  def self.thread_pool
31
- @@thread_pool ||= Thread.pool(3, 50)
37
+ @@thread_pool ||= Thread.pool(MIN_THREADS, MAX_THREADS)
32
38
  end
33
39
 
34
40
  def self.channel(name)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ksconnect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Poon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-01 00:00:00.000000000 Z
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -66,6 +66,90 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: connection_pool
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis-namespace
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
+ - !ruby/object:Gem::Dependency
112
+ name: concurrent-ruby
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: mocha
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
69
153
  description: |-
70
154
  KSConnect provides a connection interface for Kloudsec plugins by exposing a simple to use
71
155
  API for managing and synchronizing plugin data.