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

BPS Git Server git at git.bestpractical.com
Wed Dec 15 15:22:58 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  c5de54298077a50e86fdc6d7329fde025390afa2 (commit)

- Log -----------------------------------------------------------------
commit c5de54298077a50e86fdc6d7329fde025390afa2
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 aa97477cbfe5d02157fd61d80a70dc2541e02564
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..0faded05 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 $attachments = $self->TransactionObj->Attachments;
+    return 1 unless $attachments->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 $attachment = $self->TransactionObj->ContentObj || $attachments->Next;
+    while ( $attachment ) {
+        my $content = $attachment->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;
+        $attachment = $attachments->Next;
     }
 
     return 1;

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


hooks/post-receive
-- 
rtir


More information about the rt-commit mailing list