Mailing List Archive

rtir branch 5.0/parse-attachments-for-ip created. 5.0.1-35-gd3abb405
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 d3abb405c16c526015f523ed5da3de4d9505c7f9 (commit)

- Log -----------------------------------------------------------------
commit d3abb405c16c526015f523ed5da3de4d9505c7f9
Author: sunnavy <sunnavy@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 de74978775658a96558512ea32a4954d38180bce
Author: sunnavy <sunnavy@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.

diff --git a/lib/RT/Action/RTIR_FindIP.pm b/lib/RT/Action/RTIR_FindIP.pm
index 7ce4d8d7..29c11285 100644
--- a/lib/RT/Action/RTIR_FindIP.pm
+++ b/lib/RT/Action/RTIR_FindIP.pm
@@ -99,8 +99,9 @@ sub Commit {

my $how_many_can = $cf->MaxValues;

- my $attach = $self->TransactionObj->ContentObj;
- return 1 unless $attach && $attach->id;
+ my @attaches = grep defined, $self->TransactionObj->ContentObj,
+ @{ $self->TransactionObj->Attachments->ItemsArrayRef };
+ return 1 unless @attaches;

my %existing;
for( @{$cf->ValuesForObject( $ticket )->ItemsArrayRef} ) {
@@ -114,33 +115,40 @@ 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
- );
+ for my $attach ( @attaches ) {
+ my $content = $attach->Content or next;
+ 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;
}

return 1;

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


hooks/post-receive
--
rtir
_______________________________________________
rt-commit mailing list
rt-commit@lists.bestpractical.com
https://lists.bestpractical.com/mailman/listinfo/rt-commit