Class: GrimReaper::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/grim_reaper/core.rb

Overview

Main Grim Reaper class that orchestrates all modules

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Core

Returns a new instance of Core.



12
13
14
15
16
17
# File 'lib/grim_reaper/core.rb', line 12

def initialize(config = {})
  @config = config
  @grim_root = find_grim_root
  @modules = {}
  load_modules
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/grim_reaper/core.rb', line 10

def config
  @config
end

#grim_rootObject (readonly)

Returns the value of attribute grim_root.



10
11
12
# File 'lib/grim_reaper/core.rb', line 10

def grim_root
  @grim_root
end

#modulesObject (readonly)

Returns the value of attribute modules.



10
11
12
# File 'lib/grim_reaper/core.rb', line 10

def modules
  @modules
end

Instance Method Details

#backup_dirObject

Get backup directory



135
136
137
# File 'lib/grim_reaper/core.rb', line 135

def backup_dir
  ENV["HOME"] + "/.graveyard"
end

#execute(command, *args) ⇒ Object

Execute a command across all modules



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/grim_reaper/core.rb', line 84

def execute(command, *args)
  results = {}
  @modules.each do |name, module_instance|
    begin
      results[name] = module_instance.execute(command, *args)
    rescue => e
      results[name] = { error: e.message, module: name.to_s }
    end
  end
  results
end

#execute_ruby_command(command, *args) ⇒ Object

Execute a Ruby-specific command



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/grim_reaper/core.rb', line 110

def execute_ruby_command(command, *args)
  ruby_throne_path = File.join(@grim_root, "throne", "rb_grim_throne.sh")
  
  unless File.exist?(ruby_throne_path)
    raise Error, "Ruby throne script not found at #{ruby_throne_path}"
  end

  unless File.executable?(ruby_throne_path)
    File.chmod(0o755, ruby_throne_path)
  end

  cmd = [ruby_throne_path, command, *args].compact
  stdout, stderr, status = Open3.capture3(*cmd, chdir: @grim_root)

  {
    command: command,
    args: args,
    stdout: stdout,
    stderr: stderr,
    exit_code: status.exitstatus,
    success: status.success?
  }
end

#find_grim_rootObject

Find Grim Reaper root directory

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/grim_reaper/core.rb', line 20

def find_grim_root
  # Try to find from current directory
  current_dir = Dir.pwd
  10.times do
    if File.exist?(File.join(current_dir, "throne", "grim_throne.sh")) ||
       File.exist?(File.join(current_dir, "throne", "rb_grim_throne.sh")) ||
       File.exist?(File.join(current_dir, "tsk_flask", "grim_admin_server.py"))
      return current_dir
    end

    parent_dir = File.dirname(current_dir)
    break if parent_dir == current_dir
    current_dir = parent_dir
  end

  # Try common installation paths
  possible_paths = [
    ENV["HOME"] + "/reaper",
    ENV["HOME"] + "/.reaper",
    "/root/reaper",
    "/root/.reaper",
    "/usr/local/reaper",
    "/usr/share/reaper",
    Dir.pwd
  ]

  possible_paths.each do |path|
    if Dir.exist?(path) && (
      File.exist?(File.join(path, "throne", "grim_throne.sh")) ||
      File.exist?(File.join(path, "throne", "rb_grim_throne.sh")) ||
      File.exist?(File.join(path, "tsk_flask", "grim_admin_server.py"))
    )
      return path
    end
  end

  raise Error, "Could not find Grim Reaper root directory"
end

#get_otp_moduleObject

Get OTP module instance



71
72
73
# File 'lib/grim_reaper/core.rb', line 71

def get_otp_module
  @modules[:otp] || OtpModule.new(@config, @grim_root)
end

#install_dirObject

Get installation directory



140
141
142
# File 'lib/grim_reaper/core.rb', line 140

def install_dir
  ENV["HOME"] + "/reaper"
end

#load_modulesObject

Load all available Grim Reaper modules



60
61
62
63
64
65
66
67
68
# File 'lib/grim_reaper/core.rb', line 60

def load_modules
  @modules[:shell] = ShellModule.new(@config, @grim_root)
  @modules[:python] = PythonModule.new(@config, @grim_root)
  @modules[:go] = GoModule.new(@config, @grim_root)
  @modules[:security] = SecurityModule.new(@config, @grim_root)
  @modules[:otp] = OtpModule.new(@config, @grim_root)
rescue LoadError => e
  warn "Warning: Could not load module: #{e.message}"
end

#statusObject

Get status from all modules



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/grim_reaper/core.rb', line 97

def status
  status_data = {}
  @modules.each do |name, module_instance|
    begin
      status_data[name] = module_instance.status
    rescue => e
      status_data[name] = { status: "error", error: e.message, module: name.to_s }
    end
  end
  status_data
end

#validate_otp_access(otp_code) ⇒ Object

Validate OTP access if provided



76
77
78
79
80
81
# File 'lib/grim_reaper/core.rb', line 76

def validate_otp_access(otp_code)
  return true unless otp_code
  
  otp_module = get_otp_module
  return otp_module.verify_otp(otp_code)
end