[Rt-commit] rtir branch 5.0/parse-attachments-for-ip created. 5.0.1-35-g9b376900

BPS Git Server git at git.bestpractical.com
Tue Nov 23 21:03:38 UTC 2021


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rtir".

The branch, 5.0/parse-attachments-for-ip has been created
        at  9b376900493b46b55596c6584b66d5198f6603f2 (commit)

- Log -----------------------------------------------------------------
commit 9b376900493b46b55596c6584b66d5198f6603f2
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Nov 23 23:36:14 2021 +0800

    Test IP extraction from more attachments

diff --git a/t/custom-fields/ip.t b/t/custom-fields/ip.t
index feea2125..0a8c3efd 100644
--- a/t/custom-fields/ip.t
+++ b/t/custom-fields/ip.t
@@ -340,6 +340,93 @@ diag "check IPs separated by commas and semicolons" if $ENV{'TEST_VERBOSE'};
     ok($has{'16.16.16.16'}, "IP is there");
 }
 
+diag "check IPs in main content and attachment" if $ENV{'TEST_VERBOSE'};
+{
+    $agent->goto_create_ticket('Incident Reports');
+    my $form  = $agent->form_name('TicketCreate');
+    my $input = $form->find_input('Attach');
+    $input->filename('attach1');
+    $input->content('1.2.3.4, 4.4.4.4');
+
+    $agent->submit_form_ok(
+        {   fields => {
+                Subject => "test ip",
+                Content => '2.2.2.2',
+            },
+            button => 'Create',
+        }
+    );
+
+    my $ticket = RT::Test->last_ticket;
+    my $values = $ticket->CustomFieldValues('IP');
+    my %has;
+    $has{ $_->Content }++ foreach @{ $values->ItemsArrayRef };
+    is( scalar values %has, 1, "one IP was added" );
+    ok( !grep( $_ != 1, values %has ), "no duplicated values" );
+    ok( $has{'2.2.2.2'},               "IP is there" );
+}
+
+diag "check IPs not in main content but in attachment" if $ENV{'TEST_VERBOSE'};
+{
+
+    $agent->goto_create_ticket('Incident Reports');
+    my $form  = $agent->form_name('TicketCreate');
+    my $input = $form->find_input('Attach');
+    $input->filename('attach1');
+    $input->content('1.2.3.4, 4.4.4.4');
+
+    $agent->submit_form_ok(
+        {   fields => {
+                Subject => "test ip",
+                Content => 'test attachment',
+            },
+            button => 'Create',
+        }
+    );
+
+    my $ticket = RT::Test->last_ticket;
+    my $values = $ticket->CustomFieldValues('IP');
+    my %has;
+    $has{ $_->Content }++ foreach @{ $values->ItemsArrayRef };
+    is( scalar values %has, 2, "two IPs were added" );
+    ok( !grep( $_ != 1, values %has ), "no duplicated values" );
+    ok( $has{'1.2.3.4'},               "IP is there" );
+    ok( $has{'4.4.4.4'},               "IP is there" );
+}
+
+diag "check IPs not in main content but in multiple attachments" if $ENV{'TEST_VERBOSE'};
+{
+    $agent->goto_create_ticket('Incident Reports');
+    my $form  = $agent->form_name('TicketCreate');
+    my $input = $form->find_input('Attach');
+    $input->filename('attach1');
+    $input->content('1.2.3.4, 4.4.4.4');
+    $agent->click('AddMoreAttach');
+
+    $form  = $agent->form_name('TicketCreate');
+    $input = $form->find_input('Attach');
+    $input->filename('attach2');
+    $input->content('8.8.8.8');
+
+    $agent->submit_form_ok(
+        {   fields => {
+                Subject => "test ip",
+                Content => 'test attachment',
+            },
+            button => 'Create',
+        }
+    );
+
+    my $ticket = RT::Test->last_ticket;
+    my $values = $ticket->CustomFieldValues('IP');
+    my %has;
+    $has{ $_->Content }++ foreach @{ $values->ItemsArrayRef };
+    is( scalar values %has, 2, "two IPs were added" );
+    ok( !grep( $_ != 1, values %has ), "no duplicated values" );
+    ok( $has{'1.2.3.4'},               "IP is there" );
+    ok( $has{'4.4.4.4'},               "IP is there" );
+}
+
 diag "search tickets by IP" if $ENV{'TEST_VERBOSE'};
 {
     my $id = $agent->create_ir( {

commit 11359c406a2dbaad3e3de03b1d6000e2b9bab31a
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Nov 23 22:22:00 2021 +0800

    Extract IP from more attachments if main content doesn't have any.
    
    Once there are some IP extracted from an attachment, then the rest of
    attachments won't be checked. This is initially to handle the case where
    the real incident report is forwarded as an attachment.

diff --git a/lib/RT/Action/RTIR_FindIP.pm b/lib/RT/Action/RTIR_FindIP.pm
index 7ce4d8d7..7d15d44b 100644
--- a/lib/RT/Action/RTIR_FindIP.pm
+++ b/lib/RT/Action/RTIR_FindIP.pm
@@ -99,8 +99,8 @@ sub Commit {
 
     my $how_many_can = $cf->MaxValues;
 
-    my $attach = $self->TransactionObj->ContentObj;
-    return 1 unless $attach && $attach->id;
+    my $attaches = $self->TransactionObj->Attachments;
+    return 1 unless $attaches->Count;
 
     my %existing;
     for( @{$cf->ValuesForObject( $ticket )->ItemsArrayRef} ) {
@@ -114,33 +114,42 @@ sub Commit {
 
     my $spots_left = $how_many_can - keys %existing;
 
-    my $content = $attach->Content || '';
-    while ( $content =~ m/$IP_re/go ) {
-        if ( $1 && defined $2 ) { # IPv6/mask
-            my $range = $2 == 128 ? $1 : (Net::CIDR::cidr2range( "$1/$2" ))[0]
-                or next;
-            $spots_left -= $self->AddIP(
-                IP => $range, CustomField => $cf, Skip => \%existing
-            );
+    my $attach = $self->TransactionObj->ContentObj || $attaches->Next;
+    while ( $attach ) {
+        my $content = $attach->Content || '';
+        my $found;
+        while ( $content =~ m/$IP_re/go ) {
+            $found ||= 1;
+            if ( $1 && defined $2 ) { # IPv6/mask
+                my $range = $2 == 128 ? $1 : (Net::CIDR::cidr2range( "$1/$2" ))[0]
+                    or next;
+                $spots_left -= $self->AddIP(
+                    IP => $range, CustomField => $cf, Skip => \%existing
+                );
+            }
+            elsif ( $1 ) { # IPv6
+                $spots_left -= $self->AddIP(
+                    IP => $1, CustomField => $cf, Skip => \%existing
+                );
+            }
+            elsif ( $3 ) { # IPv4
+                $spots_left -= $self->AddIP(
+                    IP => $3, CustomField => $cf, Skip => \%existing
+                );
+            }
+            elsif ( $4 && defined $5 ) { # IPv4/mask
+                my $cidr = join( '.', map { $_||0 } (split /\./, $4)[0..3] ) ."/$5";
+                my $range = (Net::CIDR::cidr2range( $cidr ))[0] or next;
+                $spots_left -= $self->AddIP(
+                    IP => $range, CustomField => $cf, Skip => \%existing
+                );
+            }
+            return 1 unless $spots_left;
         }
-        elsif ( $1 ) { # IPv6
-            $spots_left -= $self->AddIP(
-                IP => $1, CustomField => $cf, Skip => \%existing
-            );
-        }
-        elsif ( $3 ) { # IPv4
-            $spots_left -= $self->AddIP(
-                IP => $3, CustomField => $cf, Skip => \%existing
-            );
-        }
-        elsif ( $4 && defined $5 ) { # IPv4/mask
-            my $cidr = join( '.', map { $_||0 } (split /\./, $4)[0..3] ) ."/$5";
-            my $range = (Net::CIDR::cidr2range( $cidr ))[0] or next;
-            $spots_left -= $self->AddIP(
-                IP => $range, CustomField => $cf, Skip => \%existing
-            );
-        }
-        return 1 unless $spots_left;
+
+        # Skip the rest attachments if found any
+        last if $found;
+        $attach = $attaches->Next;
     }
 
     return 1;

-----------------------------------------------------------------------


hooks/post-receive
-- 
rtir


More information about the rt-commit mailing list