Class: Dsc::Command Abstract
Overview
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
Helper methods collapse
-
.command_symbol ⇒ Symbol
Class name without namespace as command_context symbol.
-
.schema ⇒ Hash<Symbol => SavonHelper::TypeMapping]
The schema of the transport class.
-
.transport_class_name ⇒ String
Transport class name without namespace.
-
.transport_class_string ⇒ String
Human readable transport class name without namespace.
-
#authenticate {|manager| ... } ⇒ void
Provides an authenticated connection to the DeepSecurity Manager while executing the block.
-
#connect {|manager| ... } ⇒ void
Provides a connection to the DeepSecurity Manager while executing the block.
-
#output {|output| ... } ⇒ void
Provide an open output while executing the block.
- #to_display_string(value) ⇒ Object
Misc global flags/options definitions collapse
-
.define_global_flags(command_context) ⇒ void
Define flags.
-
.define_manager_flag(command_context) ⇒ void
Define manager hostname flag.
-
.define_outfile_flag(command_context) ⇒ void
Define outfile flag.
-
.define_password_flag(command_context) ⇒ void
Define password flag.
-
.define_port_flag(command_context) ⇒ void
Define manager TCP Port flag.
-
.define_progress_bar_option(command_context) ⇒ void
Define progress_bar option.
-
.define_tenant_flag(command_context) ⇒ void
Define tenant flag.
-
.define_username_flag(command_context) ⇒ void
Define username flag.
Debug Level flag collapse
-
.define_debug_flag(command_context) ⇒ void
Define debug level flag.
-
.valid_debug_levels ⇒ Array<String>
Valid debug levels.
-
.valid_debug_levels_string ⇒ String
String of debug levels for help string.
-
#parse_debug_level(argument) ⇒ nil, DeepSecurity::LOG_MAPPING
Parse debug level argument.
Fields flag collapse
-
.default_fields ⇒ Array<String>
Default fields if no argument is given.
-
.default_fields_string ⇒ String
String of default fields for help string.
-
.define_fields_flag(command_context) ⇒ void
Define fields flag.
-
.valid_fields ⇒ Array<String>
Sorted list of available fields.
-
.valid_fields_string ⇒ String
String of available fields for help string.
-
#parse_fields(fields_string_or_filename_argument) ⇒ Array<String>
Parse fields argument.
Time filter flag collapse
-
.define_time_filter_flag(command_context) ⇒ void
Define time_filter flag.
-
.valid_time_filters ⇒ Hash<Symbol => DeepSecurity::TimeFilter>
Valid timefilter mapping (symbol to instance).
-
.valid_time_filters_string ⇒ Object
Valid time filter string for help string @return[String] Valid time filters.
-
#parse_time_filter(argument) ⇒ DeepSecurity::TimeFilter
Parse time_filter argument.
Detail level flag collapse
-
.define_detail_level_flag(command_context) ⇒ void
Define detail_level flag.
-
.valid_detail_levels ⇒ Array<String>
Valid detail levels.
-
.valid_detail_levels_string ⇒ String
Valid detail levels for help string.
-
#parse_detail_level(argument) ⇒ EnumHostDetailLevel
Parse detail_level argument.
Time format flag collapse
-
.define_time_format_flag(command_context) ⇒ void
Define detail_level flag.
-
#parse_time_format(argument) ⇒ EnumHostDetailLevel
Parse detail_level argument.
Command definitions collapse
-
.define_api_version_command(command_context) ⇒ void
Define
api_versioncommand_context. - .define_commands(command_context) ⇒ void abstract
-
.define_list_command(command_context) {|list_command| ... } ⇒ void
Define
listcommand_context. -
.define_manager_time_command(command_context) ⇒ void
Define
manager_timecommand_context. -
.define_misc_commands(command_context) ⇒ void
Define some simple commands.
-
.define_schema_command(command_context) {|schema_command| ... } ⇒ void
Define
schemacommand_context.
Command Implementations collapse
-
#api_version_command(options, args) ⇒ void
api_versionImplementation. -
#manager_time_command(options, args) ⇒ void
manager_timeImplementation. -
#schema_command(options, args) ⇒ void
schemaImplementation.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(global_options) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(global_options) ⇒ Command
Returns a new instance of Command.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dsc/command.rb', line 55 def initialize() @hostname = [:manager] @port = [:port].to_i @tenant = [:tenant] @username = [:username] @password = [:password] @show_progress_bar = [:P] @debug_level = parse_debug_level([:debug]) @output = [:outfile] end |
Class Method Details
.command_symbol ⇒ 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_fields ⇒ Array<String>
Needs to be overridden by subclass
Default fields if no argument is given
245 246 247 |
# File 'lib/dsc/command.rb', line 245 def self.default_fields [] end |
.default_fields_string ⇒ 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
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 |, , args| self.new().api_version_command(, args) end end end |
.define_commands(command_context) ⇒ void
Define all commands for this available for this (sub) command_context
This method returns an undefined value.
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) (command_context) end |
.define_list_command(command_context) {|list_command| ... } ⇒ void
This method returns an undefined value.
Define 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 |, , args| self.new().list_command(, args) end end end |
.define_manager_flag(command_context) ⇒ void
This method returns an undefined value.
Define manager hostname flag
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
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 |, , args| self.new().manager_time_command(, args) end end end |
.define_misc_commands(command_context) ⇒ void
This method returns an undefined value.
Define some simple commands.
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
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
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
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
200 201 202 203 204 |
# File 'lib/dsc/command.rb', line 200 def self.(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
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 |, , args| self.new().schema_command(, args) end end end |
.define_tenant_flag(command_context) ⇒ void
This method returns an undefined value.
Define tenant flag
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
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 |
.schema ⇒ Hash<Symbol => SavonHelper::TypeMapping]
The schema of the transport class
40 41 42 |
# File 'lib/dsc/command.rb', line 40 def self.schema transport_class.all_type_mappings end |
.transport_class ⇒ DeepSecurity::TransportObject
DeepSecurity object covered by this class.
14 15 16 |
# File 'lib/dsc/command.rb', line 14 def self.transport_class nil end |
.transport_class_name ⇒ 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_string ⇒ 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_levels ⇒ 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_string ⇒ 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_levels ⇒ 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_string ⇒ 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_fields ⇒ 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_string ⇒ 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_filters ⇒ Hash<Symbol => DeepSecurity::TimeFilter>
Valid timefilter mapping (symbol to instance)
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_string ⇒ Object
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
Does not require authentication
This method returns an undefined value.
api_version Implementation.
Display the API version in use by the DeepSecurity Manager.
462 463 464 465 466 467 468 |
# File 'lib/dsc/command.rb', line 462 def api_version_command(, 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.
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.}" 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.
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
Does not require authentication
This method returns an undefined value.
manager_time Implementation.
Display the local time of the DeepSecurity Manager.
476 477 478 479 480 481 482 |
# File 'lib/dsc/command.rb', line 476 def manager_time_command(, 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.
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
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
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
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
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
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
Does not require authentication
This method returns an undefined value.
schema Implementation.
Display schema of the current datatype (defined by transport_class).
490 491 492 493 494 495 496 497 |
# File 'lib/dsc/command.rb', line 490 def schema_command(, 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 |