Class: Dsc::Command Abstract

Inherits:
Object show all
Defined in:
lib/dsc/command.rb

Overview

This class is abstract.

This class defines an superclass for all dsc commands. It defines several helper methods which either define flags, options and commands or helpers to define them.

Direct Known Subclasses

AntiMalwareEventCommand, HostDetailCommand

Helper methods collapse

Misc global flags/options definitions collapse

Debug Level flag collapse

Fields flag collapse

Time filter flag collapse

Detail level flag collapse

Time format flag collapse

Command definitions collapse

Command Implementations collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_options) ⇒ Command

Returns a new instance of Command.

Parameters:

  • global_options (Hash)

    Global options passed to the dsc command_context.

Options Hash (global_options):

  • :manager (String)

    The hostname of the DeepSecurity Manager.

  • :port (String)

    The TCP port to use.

  • :tenant (String, nil)

    The tenant name or nil.

  • :username (String)

    The username.

  • :password (String)

    The password.

  • :P (Boolean)

    Show progessbar?

  • :debug (String, nil)

    The debug level.

  • :outfile (String)

    The outfile.



55
56
57
58
59
60
61
62
63
64
# File 'lib/dsc/command.rb', line 55

def initialize(global_options)
  @hostname = global_options[:manager]
  @port = global_options[:port].to_i
  @tenant = global_options[:tenant]
  @username = global_options[:username]
  @password = global_options[:password]
  @show_progress_bar = global_options[:P]
  @debug_level = parse_debug_level(global_options[:debug])
  @output = global_options[:outfile]
end

Class Method Details

.command_symbolSymbol

Class name without namespace as command_context symbol

Returns:

  • (Symbol)

    Class name without namespace as command_context symbol



34
35
36
# File 'lib/dsc/command.rb', line 34

def self.command_symbol
  transport_class_name.split(/(?=[A-Z])/).join("_").downcase.to_sym
end

.default_fieldsArray<String>

Note:

Needs to be overridden by subclass

Default fields if no argument is given

Returns:

  • (Array<String>)

    Default fields if no argument is given



245
246
247
# File 'lib/dsc/command.rb', line 245

def self.default_fields
  []
end

.default_fields_stringString

String of default fields for help string

Returns:

  • (String)

    String of default fields for help string



251
252
253
# File 'lib/dsc/command.rb', line 251

def self.default_fields_string
  default_fields.join(",")
end

.define_api_version_command(command_context) ⇒ void

This method returns an undefined value.

Define api_version command_context

Parameters:

  • command_context (CLI::App)

    The current context of the command.



400
401
402
403
404
405
406
407
# File 'lib/dsc/command.rb', line 400

def self.define_api_version_command(command_context)
  command_context.desc 'Display API Version'
  command_context.command :api_version do |api_version_command|
    api_version_command.action do |global_options, options, args|
      self.new(global_options).api_version_command(options, args)
    end
  end
end

.define_commands(command_context) ⇒ void

This method is abstract.

Define all commands for this available for this (sub) command_context

This method returns an undefined value.

Parameters:

  • command_context (CLI::App)

    The current context of the command.



386
387
# File 'lib/dsc/command.rb', line 386

def self.define_commands(command_context)
end

.define_debug_flag(command_context) ⇒ void

This method returns an undefined value.

Define debug level flag



232
233
234
235
236
# File 'lib/dsc/command.rb', line 232

def self.define_debug_flag(command_context)
  command_context.flag [:d, :debug],
                       :desc => "Enable client debug output. (One of #{Dsc::Command.valid_debug_levels_string})",
                       :arg_name => 'debug_level'
end

.define_detail_level_flag(command_context) ⇒ void

This method returns an undefined value.

Define detail_level flag



356
357
358
359
360
# File 'lib/dsc/command.rb', line 356

def self.define_detail_level_flag(command_context)
  command_context.flag [:detail_level],
                       :desc => "A detail level specifiying the extent of data returned. (Available values: #{self.valid_detail_levels_string})",
                       :default_value => "low"
end

.define_fields_flag(command_context) ⇒ void

This method returns an undefined value.

Define fields flag



287
288
289
290
291
# File 'lib/dsc/command.rb', line 287

def self.define_fields_flag(command_context)
  command_context.flag [:fields],
                       :desc => "A comma separated list of fields to display or a file containing those fields. (Available fields: #{self.valid_fields_string})",
                       :default_value => self.default_fields_string
end

.define_global_flags(command_context) ⇒ void

This method returns an undefined value.

Define flags



120
121
122
123
124
125
126
127
128
129
# File 'lib/dsc/command.rb', line 120

def self.define_global_flags(command_context)
  define_debug_flag(command_context)
  define_manager_flag(command_context)
  define_port_flag(command_context)
  define_tenant_flag(command_context)
  define_username_flag(command_context)
  define_password_flag(command_context)
  define_outfile_flag(command_context)
  define_progress_bar_option(command_context)
end

.define_list_command(command_context) {|list_command| ... } ⇒ void

This method returns an undefined value.

Define list command_context

Parameters:

  • command_context (CLI::App)

    The current context of the command.

Yields:

  • (list_command)

    Gives the list command_context to the block

Yield Parameters:

  • list_command (GLI::Command)

    The just defined list command_context



426
427
428
429
430
431
432
433
434
435
# File 'lib/dsc/command.rb', line 426

def self.define_list_command(command_context)
  command_context.desc "List #{self.transport_class_string}s"
  command_context.command :list do |list_command|
    define_fields_flag(list_command)
    yield list_command if block_given?
    list_command.action do |global_options, options, args|
      self.new(global_options).list_command(options, args)
    end
  end
end

.define_manager_flag(command_context) ⇒ void

This method returns an undefined value.

Define manager hostname flag

Parameters:

  • command_context (CLI::App)

    The current context of the command.



134
135
136
137
138
# File 'lib/dsc/command.rb', line 134

def self.define_manager_flag(command_context)
  command_context.flag [:m, :manager],
                       :desc => 'Deep Security Manager Host',
                       :arg_name => 'hostname'
end

.define_manager_time_command(command_context) ⇒ void

This method returns an undefined value.

Define manager_time command_context

Parameters:

  • command_context (CLI::App)

    The current context of the command.



412
413
414
415
416
417
418
419
# File 'lib/dsc/command.rb', line 412

def self.define_manager_time_command(command_context)
  command_context.desc 'Display Manager time'
  command_context.command :manager_time do |manager_time_command|
    manager_time_command.action do |global_options, options, args|
      self.new(global_options).manager_time_command(options, args)
    end
  end
end

.define_misc_commands(command_context) ⇒ void

This method returns an undefined value.

Define some simple commands.

Parameters:

  • command_context (CLI::App)

    The current context of the command.



392
393
394
395
# File 'lib/dsc/command.rb', line 392

def self.define_misc_commands(command_context)
  self.define_api_version_command(command_context)
  self.define_manager_time_command(command_context)
end

.define_outfile_flag(command_context) ⇒ void

This method returns an undefined value.

Define outfile flag

Parameters:

  • command_context (CLI::App)

    The current context of the command.



182
183
184
185
186
# File 'lib/dsc/command.rb', line 182

def self.define_outfile_flag(command_context)
  command_context.flag [:o, :outfile],
                       :desc => 'Output filename',
                       :default_value => '--'
end

.define_password_flag(command_context) ⇒ void

This method returns an undefined value.

Define password flag

Parameters:

  • command_context (CLI::App)

    The current context of the command.



173
174
175
176
177
# File 'lib/dsc/command.rb', line 173

def self.define_password_flag(command_context)
  command_context.flag [:p, :password],
                       :desc => 'Password',
                       :arg_name => 'password'
end

.define_port_flag(command_context) ⇒ void

This method returns an undefined value.

Define manager TCP Port flag

Parameters:

  • command_context (CLI::App)

    The current context of the command.



143
144
145
146
147
148
# File 'lib/dsc/command.rb', line 143

def self.define_port_flag(command_context)
  command_context.flag [:port],
                       :desc => 'Webservice Port',
                       :arg_name => 'port',
                       :default_value => '4119'
end

.define_progress_bar_option(command_context) ⇒ void

This method returns an undefined value.

Define progress_bar option

Parameters:

  • command_context (CLI::App)

    The current context of the command.



200
201
202
203
204
# File 'lib/dsc/command.rb', line 200

def self.define_progress_bar_option(command_context)
  command_context.switch [:P, :'progress_bar'],
                         :desc => 'Show progressbar',
                         :default_value => false
end

.define_schema_command(command_context) {|schema_command| ... } ⇒ void

This method returns an undefined value.

Define schema command_context

Parameters:

  • command_context (CLI::App)

    The current context of the command.

Yields:

Yield Parameters:

  • schema_command (GLI::Command)

    The just defined schema command_context



442
443
444
445
446
447
448
449
450
# File 'lib/dsc/command.rb', line 442

def self.define_schema_command(command_context)
  command_context.desc "Show #{self.transport_class_string} schema"
  command_context.command :schema do |schema_command|
    yield schema_command if block_given?
    schema_command.action do |global_options, options, args|
      self.new(global_options).schema_command(options, args)
    end
  end
end

.define_tenant_flag(command_context) ⇒ void

This method returns an undefined value.

Define tenant flag

Parameters:

  • command_context (CLI::App)

    The current context of the command.



153
154
155
156
157
158
# File 'lib/dsc/command.rb', line 153

def self.define_tenant_flag(command_context)
  command_context.flag [:t, :tenant],
                       :desc => 'Tenat Name',
                       :arg_name => 'tenat',
                       :default_value => ''
end

.define_time_filter_flag(command_context) ⇒ void

This method returns an undefined value.

Define time_filter flag



324
325
326
327
328
# File 'lib/dsc/command.rb', line 324

def self.define_time_filter_flag(command_context)
  command_context.flag [:time_filter],
                       :desc => "A filter specifying the time interval to query (One of #{self.valid_time_filters_string})",
                       :default_value => "last_day"
end

.define_time_format_flag(command_context) ⇒ void

This method returns an undefined value.

Define detail_level flag



374
375
376
377
# File 'lib/dsc/command.rb', line 374

def self.define_time_format_flag(command_context)
  command_context.flag [:time_format],
                       :desc => "An strftime() compatible string to use for outputting date/time."
end

.define_username_flag(command_context) ⇒ void

This method returns an undefined value.

Define username flag

Parameters:

  • command_context (CLI::App)

    The current context of the command.



163
164
165
166
167
168
# File 'lib/dsc/command.rb', line 163

def self.define_username_flag(command_context)
  command_context.flag [:u, :username],
                       :desc => 'Username',
                       :arg_name => 'username',
                       :default_value => 'MasterAdmin'
end

.schemaHash<Symbol => SavonHelper::TypeMapping]

The schema of the transport class

Returns:



40
41
42
# File 'lib/dsc/command.rb', line 40

def self.schema
  transport_class.all_type_mappings
end

.transport_classDeepSecurity::TransportObject

This method is abstract.

DeepSecurity object covered by this class.



14
15
16
# File 'lib/dsc/command.rb', line 14

def self.transport_class
  nil
end

.transport_class_nameString

Transport class name without namespace

Returns:

  • (String)

    Transport class name without namespace



22
23
24
# File 'lib/dsc/command.rb', line 22

def self.transport_class_name
  transport_class.name_without_namespace
end

.transport_class_stringString

Human readable transport class name without namespace

Returns:

  • (String)

    Human readable transport class name without namespace



28
29
30
# File 'lib/dsc/command.rb', line 28

def self.transport_class_string
  transport_class_name.split(/(?=[A-Z])/).join(" ")
end

.valid_debug_levelsArray<String>

Valid debug levels

Returns:

  • (Array<String>)

    Valid debug levels



212
213
214
# File 'lib/dsc/command.rb', line 212

def self.valid_debug_levels
  DeepSecurity::LOG_MAPPING.keys
end

.valid_debug_levels_stringString

String of debug levels for help string

Returns:

  • (String)

    String of debug levels for help string



218
219
220
# File 'lib/dsc/command.rb', line 218

def self.valid_debug_levels_string
  valid_debug_levels.join(", ")
end

.valid_detail_levelsArray<String>

Valid detail levels

Returns:

  • (Array<String>)

    Valid detail levels



336
337
338
# File 'lib/dsc/command.rb', line 336

def self.valid_detail_levels
  DeepSecurity::EnumHostDetailLevel.keys()
end

.valid_detail_levels_stringString

Valid detail levels for help string

Returns:

  • (String)

    Valid detail levels for help string



342
343
344
# File 'lib/dsc/command.rb', line 342

def self.valid_detail_levels_string
  valid_detail_levels.map(&:downcase).join(", ")
end

.valid_fieldsArray<String>

Sorted list of available fields

Returns:

  • (Array<String>)

    Sorted list of available fields



257
258
259
# File 'lib/dsc/command.rb', line 257

def self.valid_fields
  transport_class.defined_attributes.sort
end

.valid_fields_stringString

String of available fields for help string

Returns:

  • (String)

    String of available fields for help string



263
264
265
# File 'lib/dsc/command.rb', line 263

def self.valid_fields_string
  valid_fields.join(", ")
end

.valid_time_filtersHash<Symbol => DeepSecurity::TimeFilter>

Valid timefilter mapping (symbol to instance)

Returns:



299
300
301
302
303
304
305
306
# File 'lib/dsc/command.rb', line 299

def self.valid_time_filters
  {
      :last_hour => DeepSecurity::TimeFilter.last_hour,
      :last_24_hours => DeepSecurity::TimeFilter.last_24_hours,
      :last_7_days => DeepSecurity::TimeFilter.last_7_days,
      :last_day => DeepSecurity::TimeFilter.last_day
  }
end

.valid_time_filters_stringObject

Valid time filter string for help string @return[String] Valid time filters



310
311
312
# File 'lib/dsc/command.rb', line 310

def self.valid_time_filters_string
  valid_time_filters.keys.join(', ')
end

Instance Method Details

#api_version_command(options, args) ⇒ void

Note:

Does not require authentication

This method returns an undefined value.

api_version Implementation. Display the API version in use by the DeepSecurity Manager.

Parameters:

  • options (Hash<Symbol => Object>)

    Merged global/local options from GLI

  • args (Array<String>)

    Arguments from GLI



462
463
464
465
466
467
468
# File 'lib/dsc/command.rb', line 462

def api_version_command(options, args)
  output do |output|
    connect do |manager|
      output.puts manager.api_version()
    end
  end
end

#authenticate {|manager| ... } ⇒ void

This method returns an undefined value.

Provides an authenticated connection to the DeepSecurity Manager while executing the block.

Yields:

  • (manager)

    Gives the manager to the block

Yield Parameters:



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dsc/command.rb', line 95

def authenticate
  connect do |manager|
    begin
      manager.connect(@tenant, @username, @password)
      yield manager
    rescue DeepSecurity::AuthenticationFailedException => e
      puts "Authentication failed! #{e.message}"
    ensure
      manager.disconnect()
    end
  end
end

#connect {|manager| ... } ⇒ void

This method returns an undefined value.

Provides a connection to the DeepSecurity Manager while executing the block.

Yields:

  • (manager)

    Gives the manager to the block

Yield Parameters:



86
87
88
89
# File 'lib/dsc/command.rb', line 86

def connect
  manager = DeepSecurity::Manager.server(@hostname, @port, @debug_level)
  yield manager
end

#manager_time_command(options, args) ⇒ void

Note:

Does not require authentication

This method returns an undefined value.

manager_time Implementation. Display the local time of the DeepSecurity Manager.

Parameters:

  • options (Hash<Symbol => Object>)

    Merged global/local options from GLI

  • args (Array<String>)

    Arguments from GLI



476
477
478
479
480
481
482
# File 'lib/dsc/command.rb', line 476

def manager_time_command(options, args)
  output do |output|
    connect do |manager|
      output.puts manager.manager_time()
    end
  end
end

#output {|output| ... } ⇒ void

This method returns an undefined value.

Provide an open output while executing the block.

Yields:

  • (output)

    Gives the output to the block

Yield Parameters:

  • output (IO)

    Opened IO



72
73
74
75
76
77
78
79
80
# File 'lib/dsc/command.rb', line 72

def output
  unless @output == '--'
    output = File.open(option, 'w')
  else
    output = STDOUT
  end
  yield output
  output.close() unless @output == '--'
end

#parse_debug_level(argument) ⇒ nil, DeepSecurity::LOG_MAPPING

Parse debug level argument

Returns:



224
225
226
227
228
# File 'lib/dsc/command.rb', line 224

def parse_debug_level(argument)
  return nil if argument.blank?
  return argument.to_sym if (DeepSecurity::LOG_MAPPING.keys.include?(argument.to_sym))
  :debug
end

#parse_detail_level(argument) ⇒ EnumHostDetailLevel

Parse detail_level argument

Returns:

  • (EnumHostDetailLevel)

    Detail level



348
349
350
351
352
# File 'lib/dsc/command.rb', line 348

def parse_detail_level(argument)
  detail_level = DeepSecurity::EnumHostDetailLevel[argument.upcase.strip]
  raise "Unknown detail level filter" if detail_level.nil?
  detail_level
end

#parse_fields(fields_string_or_filename_argument) ⇒ Array<String>

Parse fields argument. Either split the string or read from file

Returns:

  • (Array<String>)

    parse fields



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/dsc/command.rb', line 269

def parse_fields(fields_string_or_filename_argument)
  filename = File.absolute_path(fields_string_or_filename_argument)
  if File.exists?(filename)
    fields_string = ""
    File.readlines(filename).each do |line|
      fields_string = fields_string + " " + line.strip_comments
    end
  else
    fields_string = fields_string_or_filename_argument
  end
  fields = fields_string.split(/[\s,]/).map(&:strip).reject(&:blank?)
  unknown_fields = fields.reject { |each| self.class.transport_class.has_attribute_chain(each) }
  raise "Unknown filename or field found (#{unknown_fields.join(', ')}) - known fields are: #{self.class.valid_fields.join(', ')}" unless unknown_fields.empty?
  fields
end

#parse_time_filter(argument) ⇒ DeepSecurity::TimeFilter

Parse time_filter argument

Returns:



316
317
318
319
320
# File 'lib/dsc/command.rb', line 316

def parse_time_filter(argument)
  filter = self.class.valid_time_filters[argument.to_sym]
  raise "Unknown time filter" if filter.nil?
  filter
end

#parse_time_format(argument) ⇒ EnumHostDetailLevel

Parse detail_level argument

Returns:

  • (EnumHostDetailLevel)

    Detail level



368
369
370
# File 'lib/dsc/command.rb', line 368

def parse_time_format(argument)
  $time_format = argument.nil? ? "" : argument
end

#schema_command(options, args) ⇒ void

Note:

Does not require authentication

This method returns an undefined value.

schema Implementation. Display schema of the current datatype (defined by transport_class).

Parameters:

  • options (Hash<Symbol => Object>)

    Merged global/local options from GLI

  • args (Array<String>)

    Arguments from GLI



490
491
492
493
494
495
496
497
# File 'lib/dsc/command.rb', line 490

def schema_command(options, args)
  output do |output|
    schema = self.class.schema()
    schema.keys.sort.each do |key|
      output.puts "#{key} (#{schema[key].type_string}): #{schema[key].description}"
    end
  end
end

#to_display_string(value) ⇒ Object



108
109
110
111
112
# File 'lib/dsc/command.rb', line 108

def to_display_string(value)
  return "" if value.blank?
  return value.strftime($time_format) if (value.is_a?(DateTime) && !$time_format.nil?)
  value.to_s
end