Module: XARF

Defined in:
lib/xarf.rb

Constant Summary

Version =
'0.6.1'
Spec =
'0.2'
Gem =
"http://rubygems.org/gems/xarf"
Web =
"http://www.x-arf.org"
License =
"http://www.gnu.org/licenses/lgpl-3.0.txt"
Cache =
LocalCache.new :size => 20, :ttl => 3600

Class Method Summary (collapse)

Class Method Details

+ (Object) load(opts_hsh)

How to use:

require 'xarf'

require 'pp'

Example from the x-arf.org website with online validation of provided schema (try this in irb!)

report_str = open("http://www.x-arf.org/examples/ssh-report.txt").read

report_mail_obj = Mail.new(report_str)

pp XARF.load :report => report_mail_obj, :validate => true

In general

report_hsh = XARF.load :report => "report_file.eml"

pp report_hsh

Other variants

mail_message_object = Mail.new(%{complete x-arf report email string})

XARF.load :report => mail_message_object, :schema => "some_schema_file.json"

XARF.load :report => "report_file.eml", :schema => "some_schema_file.json"

XARF.load :report => "report_file.eml", :validate => true

…return all a ruby hash

':validate => true' validates against the mentioned online scheme in the report and makes use of the cache.

'mail_message_object' is a mail object, see the mail gem.

The parser stores one single critical error or the last non-critical error message in an “Error”-key,

non-critical errors are collected in an “Errors”-key (listed as array).

Both keys are nil if no error occured. If there's only one “Error” key, it contains a critical error.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/xarf.rb', line 70

def XARF.load(opts_hsh)
  
  # 1. usage check, errors are not caught here!
  if opts_hsh[:report].nil? # what about zero arguments? what about undefined variable?
     raise "Fatal error: Please provide report with :report => report_mail_object  or :report => filename"
  end
  
  # 2. load the report email
  begin
   if opts_hsh[:report].class == Mail::Message
     mail_object = opts_hsh[:report]          
   elsif opts_hsh[:report].class == String
     mail_object = Mail.read(opts_hsh[:report]) # create Mail object from file
   else
     raise "Critical Error: Provided mail object is not a File object nor a Mail Message object. " 
   end
   rescue Exception => error
    return {"Error" => "Critical Error: File or Report cannot be loaded, because: #{error}. "} # all errors while loading are caught, it makes no sense to proceed any further
  end
  
  # 3. Some basic spec tests, stop parsing here if errors occur
  begin
    return {"Error" => "Critical Error: The report is not a multipart email. "} if not mail_object.multipart?
    return {"Error" => "Critical Error: this report is a multipart/report message, thus not a xarf report. "} if mail_object.multipart_report?
    if mail_object.parts.length < 2
       return {"Error" => "Critical Error: The report does not meet the basic xarf specs, mime parts are missing. "}
    end
    if mail_object.parts.length > 3
       return {"Error" => "Critical Error: The report does not meet the basic xarf specs, too many mime parts. "}
    end
    if not mail_object.header["X-XARF"].nil?
       if not mail_object.header["X-XARF"].to_s =~ /plain/i
          return {"Error" => "Critical Error: the X-XARF header is not set to PLAIN. Please provide plain xarf reports only."}
       end
    end
    # catch anything else and stop
    rescue Exception => error
     return {"Error" => "Critical Error: The report does not meet the basic xarf specs, because: #{error}. "}
      return hash
  end
  
  # 4. Load yaml part, stop proceeding here if errors occur
  begin
     hash = YAML.load(mail_object.parts[1].body.decoded)
     rescue Exception => error
      return {"Error" => "Critical Error: The data representation in the body of the 2nd MIME part cannot be loaded by the YAML loader, because: #{error}. "}
  end
  
  # 5. check if loaded document is a ruby hash (so it actually represents a yaml/json object), if not, do not proceed any further
  if not hash.class == Hash
     classtype = hash.class.to_s
     hash = Hash.new
     hash["Error"] = "Critical Error: The loaded data of the 2nd MIME body does not represent a json object, its Ruby class-type is " + classtype + ", thus it is not a key/value listing. "
     return hash
  end
  
  # non-critical errors will be collected in Errors-key 
  hash["Errors"] = Array.new

  # 6a.Check if referenced Schema-URL is ok
  if hash["Schema-URL"].nil?
  #if not hash["Schema-URL"].nil?
  #   if not hash["Schema-URL"].include? "x-arf.org" and hash["Category"] != "private"
  #      hash["Error"] = "Schema-URL does not reference to x-arf.org. "
  #      hash["Errors"].push(hash["Error"])
  #   end
  #else
     hash["Error"] = "No Schema-URL provided. "
     hash["Errors"].push(hash["Error"])
  end

  # 6b. Check if Version is set well
  if not hash["Version"].nil?
     if not hash["Version"].to_f == XARF::Spec.to_f
        hash["Error"] = "Version must be set to " + XARF::Spec + ". "
        hash["Errors"].push(hash["Error"])
     end
  #else
  #   hash["Error"] = "No Version provided. "
  #   hash["Errors"].push(hash["Error"])
  end

  # 6c. Some non-critical checks # TODO checken ob die Werte überhaupt exisitieren...
  begin
    if not mail_object.parts[0].content_type.include? 'text/plain'
       hash["Error"] = "The report does not meet the basic xarf specs, the first mime part is not text/plain. "
       hash["Errors"].push(hash["Error"])
    end
    if not mail_object.parts[0].content_type_parameters["charset"].to_s =~ /utf\-8/i 
       hash["Error"] = "The report does not meet the basic xarf specs, the first mime part is not utf-8 defined. "
       hash["Errors"].push(hash["Error"])
    end
    if not mail_object.parts[1].content_type.include? 'text/plain'
       hash["Error"] = "The report does not meet the basic xarf specs, the second mime part is not text/plain. "
       hash["Errors"].push(hash["Error"])
    end
    if not mail_object.parts[1].content_type_parameters["charset"].to_s =~ /utf\-8/i 
       hash["Error"] = "The report does not meet the basic xarf specs, the second mime part is not utf-8 defined. "
       hash["Errors"].push(hash["Error"])
    end
    if not mail_object.parts[1].content_type_parameters["name"] == "report.txt"
       hash["Error"] = "The report does not meet the basic xarf specs, the content type parameter name of the second mime part is not report.txt. "
       hash["Errors"].push(hash["Error"])
    end
    rescue Exception => error
        hash["Error"] = "The report does not meet the basic xarf specs, because: #{error}. "
         hash["Errors"].push(hash["Error"])
  end

  # 6d. X-ARF Header used?
  if not mail_object.header["X-ARF"].nil?
     hash["Error"] = "The report contains a X-ARF header: The X-ARF header is depreciated in v0.2. Please use the X-XARF header only. "
     hash["Errors"].push(hash["Error"])
  end
  # 6e. X-XARF Header used?
  if mail_object.header["X-XARF"].nil?
       hash["Error"] = "Error: The report does not meet the basic xarf spec v0.2, the X-XARF header is missing. "
       hash["Errors"].push(hash["Error"])
  end
  
  # Which json-schema version will be used
  #if not opts_hsh[:schema_version].nil?
  #     if opts_hsh[:schema_version] == :draft1 or opts_hsh[:schema_version] == :draft2 or opts_hsh[:schema_version] == :draft3
  #        j_schema_version = opts_hsh[:schema_version]
  #     else
  #        j_schema_version = :draft2
  #     end
  #end
  
  # 7a. if set do validation with provided :schema (only) 
  if not opts_hsh[:schema].nil?
     begin
       schema_hash = File.open(opts_hsh[:schema], "rb"){|f| JSON.parse(f.read)}
          errors = JSON::Validator.fully_validate(schema_hash, hash, :version => :draft2)
          errors.each do |schema_error| 
               if not schema_error.include? "ISO-8601"
                  hash["Error"] = "#{schema_error.to_s}. "
                  hash["Errors"].push(hash["Error"])
               end
          end
       rescue Exception => error
        hash["Error"] = "Schema validation with a local file failed, because: #{error}. "
         hash["Errors"].push(hash["Error"])
     end
  end
  
  # 7b. if set do schema validation against online schema
  if not opts_hsh[:validate].nil? and opts_hsh[:validate] == true   
     if hash["Schema-URL"].nil?
        hash["Error"] = "Schema validation online failed, because: No Schema-URL "
        hash["Errors"].push(hash["Error"])
     end
     begin
       if not Cache.read(hash["Schema-URL"]).nil? and Cache.exist?(hash["Schema-URL"])
           #puts "*** DEBUG: Accessing CACHE "
          schema_hash = Cache.read(hash["Schema-URL"]) 
       else
           #puts "*** DEBUG: Access ONLINE "
          schema_hash = JSON.parse(open(hash["Schema-URL"]).read)
          Cache.write(hash["Schema-URL"],schema_hash) 
       end
          #  Online Schema Validation is done here
          errors = JSON::Validator.fully_validate(schema_hash, hash, :version => :draft2)
          errors.each do |schema_error| 
               if not schema_error.include? "ISO-8601"
                  hash["Error"] = "#{schema_error.to_s}. "
                  hash["Errors"].push(hash["Error"])
               end
          end
       # Rescue if anything in this begin/end block goes wrong
       rescue Exception => error
        hash["Error"] = "Schema validation online failed, because: #{error}. "
         hash["Errors"].push(hash["Error"])
     end     
  end

  # 8. return resulted hash if not already done - you never know...
  hash["Error"] = nil if hash["Error"].nil?
  hash["Errors"] = nil if hash["Errors"].empty?
  return hash
  
end

+ (Object) rekey_str_to_sym(hash)



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

def XARF.rekey_str_to_sym(hash)                                                          
    hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end

+ (Object) rekey_sym_to_str(hash)



25
26
27
# File 'lib/xarf.rb', line 25

def XARF.rekey_sym_to_str(hash)
    hash.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
end