Class: StreamAuditor

Inherits:
SoarAuditorApi::AuditorAPI
  • Object
show all
Defined in:
lib/stream_auditor.rb,
lib/stream_auditor/version.rb

Overview

An IO stream (or file) implementation of SoarAuditorApi::AuditorAPI

This implementation supports auditing to:

  • an already open IO object (or anything that implements IO#<< and IO#flush),

  • the standard error stream ($stderr),

  • the standard output stream ($stdout), or

  • a file.

Developers should not need to work directly with this class. Instead, they should configure it through the SOAR auditing provider.

Examples:

Log to file


require "soar_auditing_provider"
require "stream_auditor"

config = {
  "auditing" => {
    "provider" => "SoarAuditingProvider::AuditingProvider",
    "level" => "debug",
    "direct_auditor_call" => "true",
    "auditors" => {
      "local" => {
        "adaptor" => "StreamAuditor",
        "stream" => "/var/log/application.log"
      }
    }
  }
}

auditor = SoarAuditingProvider::AuditingProvider.new(config["auditing"])
auditor.info("Auditor initialized")

Constant Summary collapse

VERSION =
"1.2.1"

Instance Method Summary collapse

Instance Method Details

#audit(data) ⇒ Object

Write data to the configured stream

The stream is immediately flushed after the data is written.

Parameters:

  • data (Object)

    the String (or Object with to_s method) to write. If the string is not newline-terminated, a newline is added.



51
52
53
54
# File 'lib/stream_auditor.rb', line 51

def audit(data)
  stream << data.to_s.chomp + "\n"
  stream.flush
end

#configuration_is_valid?(configuration) ⇒ true, false

Validates the configuration

Parameters:

  • configuration (Hash)

    the configuration to validate

Returns:

  • (true)

    if the configuration is valid

  • (false)

    if the configuration is invalid

See Also:



104
105
106
107
108
109
# File 'lib/stream_auditor.rb', line 104

def configuration_is_valid?(configuration)
  return false unless (configuration.keys - ["adaptor", "nfrs", "stream"]).empty?

  s = configuration["stream"]
  want_default_stream?(s) or want_stderr_stream?(s) or want_stdout_stream?(s) or want_io_stream?(s) or want_path_stream?(s)
end

#configure(configuration = nil) ⇒ Object

Apply the configuration supplied to initialize

Parameters:

  • configuration (Hash) (defaults to: nil)

    This method accepts nil or a Hash, but the auditor API only calls this method when the configuration is not nil.

    The configuration may contain the following String keys:

    • adaptor - ignored (for compatibility with the SOAR auditing provider

    • stream - the stream to audit to, one of:

      • an IO object (or anything that implements IO#<< and IO#flush)

      • the string $stderr for the standard error stream

      • the string $stdout for the standard output stream

      • the string path to a file

    If the stream key is ommitted, the default is $stderr.

    When the stream is the path to a file:

    • any missing intermediate directories will be created (mode 0700),

    • the file will be created if missing (mode 0600),

    • the file is opened in append mode.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/stream_auditor.rb', line 81

def configure(configuration = nil)
  super
  if configuration
    s = configuration["stream"]
    @stream = if want_stdout_stream?(s) then $stdout
              elsif want_stderr_stream?(s) then $stderr
              elsif want_io_stream?(s) then s
              elsif want_path_stream?(s) then creative_open_file(s)
              end
  end
end

#prefer_direct_call?true

Hint direct auditor call preference to SOAR Auditor API

Returns:

  • (true)

    always



116
117
118
# File 'lib/stream_auditor.rb', line 116

def prefer_direct_call?
  true
end