regex - Logstash Ruby Filter to match email addresses -


this question has answer here:

i have ruby filter match email address in log message, remove it, , pass through anonymization filter, this...

  ruby {    code =>     "     begin       if !event['log_message'].nil?         if match = event['log_message'].match(/(\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b)/i)            event['user_email'] = match[1]         end       else         puts 'oddity parsing message: log_message nil'         puts event.to_yaml       end     rescue exception => e       puts 'exception parsing user email:'       puts e.message     end     " } if [user_email] {   anonymize {       algorithm => "sha1"     fields => ["user_email"]     key => "mysupersecretpassword"   }   ruby {     code =>       "       begin         event['message'].gsub!(/\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b/i, event['user_email'])         event['log_message'].gsub!(/\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b/i, event['user_email'])       rescue exception => e         puts 'exception replacing user-email in log:'         puts e.message       end       "       remove_field => ["user_email"]                 } } 

as of now, regex isn't catching of anything. tried replacing , got error (which "oddity parsing message" branch of code).

does know how this? don't need crazy over-the-top regex, 1 catch 99% of email addresses. regex tried use

if match = event['log_message'].match(/(\b[a-za-z0-9_.+=:-]+@[0-9a-za-z][0-9a-za-z-]{0,62}(?:\.(?:[0-9a-za-z][0-‌​9a-za-z-]{0,62}))*\b)/i) 

here's log line reference

76817815   11/jun/2016 00:04:28 +0000  info  [eventlistener-3] messagingsvc logdefault    > dosend - sending email... from: "test" <do-not-reply@test.com> 

note if can done easier / in more sane way using grok, i'm open removing ruby bit.

this html5 spec

 [a-za-z0-9.!#$%&'*+/=?^_\`{|}~-]+@[a-za-z0-9](?:[a-za-z0-9-]{0,61}[a-za-z0-9])?(?:\.[a-za-z0-9](?:[a-za-z0-9-]{0,61}[a-za-z0-9])?)* 

expanded

 [a-za-z0-9.!#$%&'*+/=?^_\`{|}~-]+   @  [a-za-z0-9]   (?: [a-za-z0-9-]{0,61} [a-za-z0-9] )?  (?:       \. [a-za-z0-9]        (?: [a-za-z0-9-]{0,61} [a-za-z0-9] )?  )* 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -