Class: Immunio::VMFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/immunio/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, dev_mode = false, debug_mode = false) ⇒ VMFactory

Returns a new instance of VMFactory.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/immunio/vm.rb', line 20

def initialize(key, secret, dev_mode = false, debug_mode = false)

  # Code is lua functions plus utils generated by the __init__ function
  @code_version = nil
  @functions = {}
  @utils = {}

  # Data is to hold serverdata
  @data_version = nil
  @data = nil

  @update_lock = Mutex.new

  # Cached VM - reusable by multiple clients
  @lua_vm = LuaVM.new(key, secret, dev_mode, debug_mode)
  @dev_mode = dev_mode
  @dev_checksum = ""
end

Instance Attribute Details

#code_versionObject (readonly)

Returns the value of attribute code_version.



18
19
20
# File 'lib/immunio/vm.rb', line 18

def code_version
  @code_version
end

#data_versionObject (readonly)

Returns the value of attribute data_version.



18
19
20
# File 'lib/immunio/vm.rb', line 18

def data_version
  @data_version
end

Instance Method Details

#current_stateObject



71
72
73
74
75
76
# File 'lib/immunio/vm.rb', line 71

def current_state
  {
      vmcode_version: @code_version,
      vmdata_version: @data_version
  }
end

#dev_mode_hook_updateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/immunio/vm.rb', line 87

def dev_mode_hook_update
  # Load default hook handlers if present (they do not ship with the gem)
  path = File.expand_path("#{Immunio::DIR}/../lua-hooks/hooks")
  Immunio.logger.info { "Checking hook handlers from #{path}" }

  unique_files = Dir[path + "/*.lua"].collect do |file|
    name = File.basename(file, ".*")

    fstat = File.stat(file)
    mtime = fstat.mtime
    size = fstat.size

    "-#{name}:#{mtime}:#{size}-"
  end
  checksum = unique_files.join
  if checksum == @dev_checksum
    Immunio.logger.info { "Reusing dev vm, files unchanged" }
    return
  end

  functions = Dir[path + "/*.lua"].inject({}) do |handlers, file|
    name = File.basename(file, ".*")

    # NB: because this code is part of the creation of a VM
    # calling any code that fires off a plugin could result
    # in an infinite loop.
    if File.respond_to? :read_without_immunio
      data = File.read_without_immunio file
    else
      data = File.read file
    end
    handlers[name] = data
    handlers
  end
  Immunio.logger.info { "Updating dev vm code, files changed" }
  update_code("DEV-#{Time.now.strftime("%H:%M:%S")}", functions)
  @dev_checksum = checksum
end

#new_vmObject



78
79
80
81
82
83
84
85
# File 'lib/immunio/vm.rb', line 78

def new_vm
  Immunio.logger.debug { "Creating new Lua VM" }
  dev_mode_hook_update if @dev_mode

  @update_lock.synchronize {
    return VM.new(@lua_vm, @functions, @utils, @data, @code_version, @data_version)
  }
end

#update_code(version, code) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/immunio/vm.rb', line 39

def update_code(version, code)
  functions = {}
  code.each { |fun_name, lua_code|
    functions[fun_name] = @lua_vm.create_function lua_code, fun_name
  }

  init_hook = functions.delete("__init__")
  if init_hook
    utils = @lua_vm.call(init_hook)
  else
    utils = {}
  end

  @update_lock.synchronize {
    @code_version = version
    @functions = functions
    @utils = utils
  }
  Immunio.logger.info { "Lua code updated to version #{version}." }
end

#update_data(version, data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/immunio/vm.rb', line 60

def update_data(version, data)
  # Allow server data to be overridden in the Agent config file
  data = data.deep_merge(Immunio.agent.config.vm_data)
  new_data = @lua_vm.create_object(data)
  @update_lock.synchronize {
    @data_version = version
    @data = new_data
  }
  Immunio.logger.debug { "Lua data updated to version #{version}." }
end