File: rt-munge-attachments

package info (click to toggle)
request-tracker4 4.4.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 62,888 kB
  • sloc: javascript: 130,444; perl: 65,442; sh: 1,350; makefile: 480; python: 37; php: 30
file content (170 lines) | stat: -rwxr-xr-x 5,476 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
#!/usr/bin/perl
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2023 Best Practical Solutions, LLC
#                                          <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
use strict;
use warnings;

# fix lib paths, some may be relative
BEGIN {    # BEGIN RT CMD BOILERPLATE
    require File::Spec;
    require Cwd;
    my @libs = ( "lib", "local/lib" );
    my $bin_path;

    for my $lib (@libs) {
        unless ( File::Spec->file_name_is_absolute($lib) ) {
            $bin_path
                ||= ( File::Spec->splitpath( Cwd::abs_path(__FILE__) ) )[1];
            $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
        }
        unshift @INC, $lib;
    }

}

# Read in the options
my %opts;
use Getopt::Long;
GetOptions( \%opts, "help|h", "search=s", "replacement=s", 'tickets=s' );

if ( $opts{'help'} || !$opts{'search'} ) {
    require Pod::Usage;
    print Pod::Usage::pod2usage( -verbose => 2 );
    exit;
}

use RT -init;

my $replacement = $opts{'replacement'} || '';

my $search = $opts{'search'};

my $attachments;
if ( $opts{tickets} ) {
    my @tickets = split /\s*,\s*/, $opts{tickets};

    $attachments = RT::Attachments->new( RT->SystemUser );
    my $txn_alias   = $attachments->TransactionAlias;
    $attachments->Limit(
        ALIAS => $txn_alias,
        FIELD => 'ObjectType',
        VALUE => 'RT::Ticket',
    );
    my $ticket_alias = $attachments->Join(
        ALIAS1 => $txn_alias,
        FIELD1 => 'ObjectId',
        TABLE2 => 'Tickets',
        FIELD2 => 'id',
    );
    $attachments->Limit(
        ALIAS    => $ticket_alias,
        FIELD    => 'EffectiveId',
        VALUE    => \@tickets,
        OPERATOR => 'IN',
    );
}
else {
    $attachments = RT::Attachments->new( RT->SystemUser );
}

my ( $ret, $msg ) = $attachments->ReplaceAttachments(
    Search      => Encode::decode( 'UTF-8', $search ),
    Replacement => Encode::decode( 'UTF-8', $replacement ),
    $opts{tickets} ? ( FilterBySearchString => 0 ) : (),
);
print STDERR $msg . "\n";


=head1 rt-munge-attachments

rt-munge-attachments - Remove or replace strings in attachment records

=head1 SYNOPSIS

    rt-munge-attachments --search="user1@example.com" --replace="removed-user@example.com"

=cut

=head1 DESCRIPTION

In RT, "attachments" contain all correspondence on tickets, not just file-based
attachments. Although attachments are normally not modified by RT once created,
this script provides a way to change or remove personalized data like email
addresses that might be contained in the headers or bodies of RT attachment
records. This allows admins to comply with GDPR regulations, but retain ticket
histories with remaining non-personal information.

A transaction is recorded when this script modifies content so there is a record that
something was changed on the ticket. Details of the change are not recorded since
the values would likely contain personal information which the script was run to
remove.

If modifying ticket history violates audit policies, admins should not use this
script.

=head1 OPTIONS

=over

=item --search=SEARCH

Provide a string to search the header and content columns in the attachments table.
If a match is found the content will be removed unless a replacement is provided.

=item --replace=REPLACEMENT

Provide a string to replace the value matched by the search.

=item --tickets=1,2,3

Limit attachments to the specified tickets. Note that if tickets are not
specified, RT will pre-filter attachments by the search string use SQL,
which might bypass attachments of which contents are encoded(like
base64). Use this option to prevent the pre-filter behavior.

=back