Class: KSConnect::API::Plugin

Inherits:
Object
  • Object
show all
Includes:
Logs
Defined in:
lib/ksconnect/api/plugin.rb,
lib/ksconnect/api/plugin/data.rb,
lib/ksconnect/api/plugin/config.rb,
lib/ksconnect/api/plugin/domain.rb

Defined Under Namespace

Classes: Config, Data, Domain

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logs

included

Constructor Details

#initialize(name, main = false) ⇒ Plugin

Returns a new instance of Plugin.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ksconnect/api/plugin.rb', line 13

def initialize(name, main = false)
  @name = name
  @main_plugin = main
  @domains = {}

  Redis.current ||= Redis.new(driver: :hiredis)
  $redis ||= ConnectionPool.new(size: MAX_THREADS, timeout: 8) { Redis.current }

  load_domains
  subscribe_to_events
end

Instance Attribute Details

#configObject



25
26
27
# File 'lib/ksconnect/api/plugin.rb', line 25

def config
  @config ||= Config.new
end

#domainsObject

Returns the value of attribute domains.



9
10
11
# File 'lib/ksconnect/api/plugin.rb', line 9

def domains
  @domains
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/ksconnect/api/plugin.rb', line 10

def name
  @name
end

Instance Method Details

#configure {|config| ... } ⇒ Object

Yields:



29
30
31
# File 'lib/ksconnect/api/plugin.rb', line 29

def configure
  yield(config)
end

#domains_keyObject



101
102
103
104
# File 'lib/ksconnect/api/plugin.rb', line 101

def domains_key
  @domain_list_uuid ||= $redis.with { |redis| redis.hget("#{@name}:data", "domain_names") }
  "kloudsec_data:#{@domain_list_uuid}"
end

#load_domainsObject

This method loads the domain list from Redis, adding or removing domains as appropriate. Note that it does not update the ip address of existing domains.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ksconnect/api/plugin.rb', line 35

def load_domains
  # load domain list
  domain_to_ip = $redis.with { |redis| redis.hgetall(domains_key) }

  # add new domains
  new_domains = domain_to_ip.keys - @domains.values.map(&:name)
  new_domains.each { |domain_name| @domains[domain_name] = Domain.new(domain_name, domain_to_ip[domain_name], @name) }

  # remove old domains
  @domains.select! { |k, _| domain_to_ip.keys.include?(k) }
end

#perform_request(request) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ksconnect/api/plugin.rb', line 60

def perform_request(request)
  request.stringify_keys!

  domain_name = request['domain_name']
  logger.warn "Invalid push request with no domain: #{request}" and return unless domain_name

  request_type = request['request_type']
  case request_type
    when 'initialize'
      @domains[domain_name] = Domain.new(domain_name, get_ip_for(domain_name), @name)
      config.on_initialize.call(request)
    when 'update'
      if @domains[domain_name]
        @domains[domain_name].update_ip_address
      else
        @domains[domain_name] = Domain.new(domain_name, get_ip_for(domain_name), @name)
      end
      config.on_update.call(request)
    when 'teardown'
      config.on_teardown.call(request)
      @domains.delete(domain_name)
    else # default to generic `on_push` type
      begin
        keys_to_echo = { domain_name: request['domain_name'], website_plugin_id: request['website_plugin_id'], request_id: request['request_id'] }
        result = config.on_push.call(request)
        update_action(keys_to_echo.merge!({ state: :done, data: result }))
      rescue => e
        update_action(keys_to_echo.merge({ state: :failed }))
        raise e
      end
  end
end

#subscribe_to_eventsObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ksconnect/api/plugin.rb', line 47

def subscribe_to_events
  KSConnect.channel("#{@name}:push") do |message|
    begin
      json = JSON.parse(message)
      perform_request(json)
    rescue Exception => e
      logger.error e
      logger.error "Error parsing message as JSON: #{message}"
      next
    end
  end
end

#update_action(result) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/ksconnect/api/plugin.rb', line 93

def update_action(result)
  if ENV['KSCONNECT_READ_ONLY'].nil?
    $redis.with do |redis|
      redis.lpush("ks:actions_push", result.to_json)
    end
  end
end