[Rt-commit] rt branch, 4.4/admin-gpg-keys, created. rt-4.4.3-69-gdacd7a6cb

? sunnavy sunnavy at bestpractical.com
Fri Nov 16 14:49:28 EST 2018


The branch, 4.4/admin-gpg-keys has been created
        at  dacd7a6cb5bd65b729a0937ddbdcc2fee69665fe (commit)

- Log -----------------------------------------------------------------
commit 70d9d4b1b840e5ab6239520043a6bbf7799e1c52
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Nov 17 03:31:15 2018 +0800

    Add admin page to manage GnuPG keys

diff --git a/lib/RT/Crypt/GnuPG.pm b/lib/RT/Crypt/GnuPG.pm
index 540a2482b..287592471 100644
--- a/lib/RT/Crypt/GnuPG.pm
+++ b/lib/RT/Crypt/GnuPG.pm
@@ -1810,6 +1810,16 @@ sub ImportKey {
     );
 }
 
+sub ReceiveKey {
+    my $self = shift;
+    my $key  = shift;
+
+    return $self->CallGnuPG(
+        Command     => "recv_keys",
+        CommandArgs => [ '--', $key ],
+    );
+}
+
 sub GnuPGPath {
     state $cache = RT->Config->Get('GnuPG')->{'GnuPG'};
     $cache = $_[1] if @_ > 1;
diff --git a/share/html/Admin/Tools/GnuPG.html b/share/html/Admin/Tools/GnuPG.html
new file mode 100644
index 000000000..1820939fe
--- /dev/null
+++ b/share/html/Admin/Tools/GnuPG.html
@@ -0,0 +1,175 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
+%#                                          <sales at 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 }}}
+<& /Admin/Elements/Header, Title => $title &>
+<& /Elements/Tabs &>
+
+<& /Elements/ListActions &>
+
+<&|/Widgets/TitleBox, title => loc('Import Keys') &>
+
+<form action="<% RT->Config->Get('WebPath')%>/Admin/Tools/GnuPG.html" name="ImportKeys" method="POST" enctype="multipart/form-data">
+    <input type="hidden" name="Action" value="Import" />
+    <table>
+% my %options = RT->Config->Get('GnuPGOptions');
+% if ( $options{'keyserver'} ) {
+        <tr>
+            <td align="right"><&|/l&>Key</&>:</td>
+            <td><input name="ImportKey" value="<% $ARGS{ImportKey} // '' %>" size="40" /></td>
+        </tr>
+% }
+        <tr>
+            <td align="right"><&|/l&>Content</&>:</td>
+            <td><textarea name="Content" rows="8" cols="72"><% $ARGS{Content} // '' %></textarea></td>
+        </tr>
+    </table>
+    <& /Elements/Submit, Label => loc('Import') &>
+</form>
+
+</&>
+
+<&|/Widgets/TitleBox, title => loc('GnuPG Public Keys') &>
+
+% if ( $public{info} && @{$public{info}} ) {
+<form action="<% RT->Config->Get('WebPath')%>/Admin/Tools/GnuPG.html" name="DeletePublicKeys" method="POST" enctype="multipart/form-data">
+    <input type="hidden" name="Action" value="DeletePublicKey" />
+    <table>
+        <tr>
+            <th><input type="checkbox" name="DeletePublicKeyAll" value="1" onclick="setCheckbox(this, 'DeletePublicKey')"></th>
+            <th><% loc('Summary') %></th>
+        </tr>
+% for my $item ( @{$public{info}} ) {
+        <tr>
+            <td><input type="checkbox" name="DeletePublicKey" value="<% $item->{Fingerprint} %>" <% $delete{$item->{Fingerprint}} ? 'checked="checked"' : '' |n %> /></td>
+            <td><% $item->{Formatted} %></td>
+        </tr>
+% }
+    </table>
+    <& /Elements/Submit, Label => loc('Delete'), CheckboxNameRegex => '/^DeletePublicKey(All)?$/', CheckAll => 1, ClearAll => 1 &>
+</form>
+% } else {
+<p><&|/l&>No public keys found.</&>
+% }
+
+</&>
+
+<&|/Widgets/TitleBox, title => loc('GnuPG Private Keys') &>
+
+% if ( $private{info} && @{$private{info}} ) {
+<form action="<% RT->Config->Get('WebPath')%>/Admin/Tools/GnuPG.html" name="DeletePrivateKeys" method="POST" enctype="multipart/form-data">
+    <input type="hidden" name="Action" value="DeletePrivateKey" />
+    <table>
+        <tr>
+            <th><input type="checkbox" name="DeletePrivateKeyAll" value="1" onclick="setCheckbox(this, 'DeletePrivateKey')"></th>
+            <th><% loc('Summary') %></th>
+        </tr>
+% for my $item ( @{$private{info}} ) {
+        <tr>
+            <td><input type="checkbox" name="DeletePrivateKey" value="<% $item->{Fingerprint} %>" <% $delete{$item->{Fingerprint}} ? 'checked="checked"' : '' |n %> /></td>
+            <td><% $item->{Formatted} %></td>
+        </tr>
+% }
+    </table>
+    <& /Elements/Submit, Label => loc('Delete'), CheckboxNameRegex => '/^DeletePrivateKey(All)?$/', CheckAll => 1, ClearAll => 1 &>
+</form>
+% } else {
+<p><&|/l&>No private keys found.</&>
+% }
+
+</&>
+<%INIT>
+
+my $title = loc('Manage GnuPG Keys');
+unless ( $session{'CurrentUser'}->HasRight( Object => $RT::System, Right => 'SuperUser' ) ) {
+    Abort( loc('This feature is only available to system administrators.') );
+}
+
+my @results;
+
+my %delete;
+
+require RT::Crypt::GnuPG;
+
+if ( $Action eq 'Import' ) {
+    if ( $ARGS{ImportKey} ) {
+        my %ret = RT::Crypt::GnuPG->ReceiveKey( $ARGS{ImportKey} );
+        push @results, split /\n+/, $ret{logger};
+    }
+
+    if ( $ARGS{Content} ) {
+        my %ret = RT::Crypt::GnuPG->ImportKey( $ARGS{Content} );
+        push @results, split /\n+/, $ret{logger};
+    }
+}
+elsif ( $Action =~ /^Delete(?:Public|Private)Key$/ ) {
+    if ( $ARGS{$Action} ) {
+        for my $key ( ref $ARGS{$Action} ? @{ $ARGS{$Action} } : $ARGS{$Action} ) {
+            $delete{$key} ||= 1;
+            my %ret = RT::Crypt::GnuPG->DeleteKey($key);
+            if ( $ret{logger} ) {
+                push @results, split /\n+/, $ret{logger};
+            }
+            else {
+                push @results, loc( "Key [_1] is deleted", substr( $key, -8 ) );
+            }
+        }
+    }
+}
+
+MaybeRedirectForResults(
+    Actions => \@results,
+    Path    => '/Admin/Tools/GnuPG.html',
+);
+
+my %public = RT::Crypt::GnuPG->GetKeysInfo( Force => 1 );
+my %private = RT::Crypt::GnuPG->GetKeysInfo( Force => 1, Type => 'private' );
+
+</%INIT>
+
+<%ARGS>
+$Action => 'List'
+</%ARGS>
diff --git a/share/html/Elements/Tabs b/share/html/Elements/Tabs
index 08b05705d..fdab6d4c1 100644
--- a/share/html/Elements/Tabs
+++ b/share/html/Elements/Tabs
@@ -296,6 +296,16 @@ my $build_admin_menu = sub {
         path        => '/Admin/Tools/Shredder',
     );
 
+    if ( RT->Config->Get('GnuPG')->{'Enable'}
+        && $session{'CurrentUser'}->HasRight( Right => 'SuperUser', Object => RT->System ) )
+    {
+        $admin_tools->child(
+            'gnupg'     => title => loc('Manage GnuPG Keys'),
+            description => loc('Manage GnuPG keys'),
+            path        => '/Admin/Tools/GnuPG.html',
+        );
+    }
+
     if ( $request_path =~ m{^/Admin/(Queues|Users|Groups|CustomFields|CustomRoles)} ) {
         my $type = $1;
         my $tabs = PageMenu();

commit dacd7a6cb5bd65b729a0937ddbdcc2fee69665fe
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Nov 17 03:31:40 2018 +0800

    Test admin page of "Manage GnuPG Keys"

diff --git a/t/web/admin_gnupg.t b/t/web/admin_gnupg.t
new file mode 100644
index 000000000..01ecc1576
--- /dev/null
+++ b/t/web/admin_gnupg.t
@@ -0,0 +1,57 @@
+use strict;
+use warnings;
+
+use RT::Test::GnuPG tests => undef;
+
+my ( $url, $m ) = RT::Test->started_ok;
+ok( $m->login, 'logged in' );
+
+$m->follow_link_ok( { text => 'Manage GnuPG Keys' } );
+$m->title_is('Manage GnuPG Keys');
+
+$m->text_contains('No public keys found');
+$m->text_contains('No private keys found');
+
+$m->submit_form_ok(
+    {   form_name => 'ImportKeys',
+        fields =>
+          { Content => RT::Test->file_content( [ 't', 'data', 'gnupg', 'keys', 'recipient-at-example.com.public.key' ] ), },
+    },
+    'Import keys for rt-test at example.com'
+);
+
+$m->text_contains('public key "Test User <recipient at example.com>" imported');
+$m->text_contains('Test User <recipient at example.com> (93EB9DE7)');
+$m->text_lacks('No public keys found');
+$m->text_contains('No private keys found');
+
+$m->form_name('DeletePublicKeys');
+$m->tick( 'DeletePublicKey', '7232A3C60F796865796370A54855ED8893EB9DE7' );
+$m->submit_form_ok( undef, 'Delete keys for recipient at example.com' );
+
+$m->text_contains('No public keys found');
+$m->text_contains('No private keys found');
+
+$m->submit_form_ok(
+    {   form_name => 'ImportKeys',
+        fields =>
+          { Content => RT::Test->file_content( [ 't', 'data', 'gnupg', 'keys', 'rt-test-at-example.com.2.secret.key' ] ), },
+    },
+    'Import keys for rt-test at example.com'
+);
+
+$m->text_contains('public key "RT Test the same <rt-test at example.com>" imported');
+$m->text_contains('secret key imported');
+$m->text_contains('RT Test the same <rt-test at example.com> (A831DBFB)');
+
+$m->text_lacks('No public keys found');
+$m->text_lacks('No private keys found');
+
+$m->form_name('DeletePrivateKeys');
+$m->tick( 'DeletePrivateKey', '4CFD3F7DCD464852DB980F26C798591AA831DBFB' );
+$m->submit_form_ok( undef, 'Delete keys for rt-test at example.com' );
+
+$m->text_contains('No public keys found');
+$m->text_contains('No private keys found');
+
+done_testing;

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


More information about the rt-commit mailing list