Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kiloG-0001FP-5m for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Nov 2020 22:01:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kiloD-0002Iv-0x for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Nov 2020 22:01:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kiloC-0002In-GZ for pgsql-hackers@lists.postgresql.org; Fri, 27 Nov 2020 22:01:40 +0000 Received: from tamriel.snowman.net ([70.109.60.50]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kilo8-0006g3-VN for pgsql-hackers@lists.postgresql.org; Fri, 27 Nov 2020 22:01:38 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 4673A5F799; Fri, 27 Nov 2020 17:01:36 -0500 (EST) Date: Fri, 27 Nov 2020 17:01:36 -0500 From: Stephen Frost To: vignesh C Cc: Bharath Rupireddy , Euler Taveira , PostgreSQL Hackers Subject: Re: Log message for GSS connection is missing once connection authorization is successful. Message-ID: <20201127220136.GQ16415@tamriel.snowman.net> References: <20201029135641.GH16415@tamriel.snowman.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="GbJjGCCbPc3OzCle" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --GbJjGCCbPc3OzCle Content-Type: multipart/mixed; boundary="N/9/fZ4M25wmf00Y" Content-Disposition: inline --N/9/fZ4M25wmf00Y Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Greetings, * vignesh C (vignesh21@gmail.com) wrote: > Thanks for testing this, I had missed testing this. The expression > matching was not correct. Attached v6 patch which includes the fix for > this. This generally looks pretty good to me. I did reword the commit message a bit, run pgindent, and added the appropriate log message for the last test (was there a reason you didn't include that..?). In general, this looks pretty good to commit to me. I'll look at it again over the weekend or early next week and unless there's objections, I'll push it. Thanks, Stephen --N/9/fZ4M25wmf00Y Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v7-0001-Improving-the-connection-authorization-message-fo.patch" Content-Transfer-Encoding: quoted-printable =46rom f78100d1c7401d4d47e6ee58f1baaac5b21b1216 Mon Sep 17 00:00:00 2001 =46rom: Stephen Frost Date: Fri, 27 Nov 2020 16:52:44 -0500 Subject: [PATCH] Add GSS information to connection authorized log message GSS information (if used) such as if the connection was authorized using GSS or if it was encrypted using GSS, and perhaps most importantly, what the GSS principal used for the authentication was, is extremely useful but wasn't being included in the connection authorized log message. Therefore, add to the connection authorized log message that information, in a similar manner to how we log SSL information when SSL is used for a connection. Author: Vignesh C Reviewed-by: Bharath Rupireddy Discussion: https://www.postgresql.org/message-id/CALDaNm2N1385_Ltoo%3DS7VG= T-ESu_bRQa-sC1wg6ikrM2L2Z49w%40mail.gmail.com --- src/backend/utils/init/postinit.c | 82 ++++++++------------- src/test/kerberos/t/001_auth.pl | 118 +++++++++++++++++++++--------- 2 files changed, 114 insertions(+), 86 deletions(-) diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/pos= tinit.c index f2dd8e4914..82d451569d 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -245,62 +245,40 @@ PerformAuthentication(Port *port) =20 if (Log_connections) { + StringInfoData logmsg; + + initStringInfo(&logmsg); if (am_walsender) - { -#ifdef USE_SSL - if (port->ssl_in_use) - ereport(LOG, - (port->application_name !=3D NULL - ? errmsg("replication connection authorized: user=3D%s application_= name=3D%s SSL enabled (protocol=3D%s, cipher=3D%s, bits=3D%d, compression= =3D%s)", - port->user_name, - port->application_name, - be_tls_get_version(port), - be_tls_get_cipher(port), - be_tls_get_cipher_bits(port), - be_tls_get_compression(port) ? _("on") : _("off")) - : errmsg("replication connection authorized: user=3D%s SSL enabled = (protocol=3D%s, cipher=3D%s, bits=3D%d, compression=3D%s)", - port->user_name, - be_tls_get_version(port), - be_tls_get_cipher(port), - be_tls_get_cipher_bits(port), - be_tls_get_compression(port) ? _("on") : _("off")))); - else -#endif - ereport(LOG, - (port->application_name !=3D NULL - ? errmsg("replication connection authorized: user=3D%s application_= name=3D%s", - port->user_name, - port->application_name) - : errmsg("replication connection authorized: user=3D%s", - port->user_name))); - } + appendStringInfo(&logmsg, _("replication connection authorized: user=3D= %s"), + port->user_name); else - { + appendStringInfo(&logmsg, _("connection authorized: user=3D%s"), + port->user_name); + if (!am_walsender) + appendStringInfo(&logmsg, _(" database=3D%s"), port->database_name); + + if (port->application_name !=3D NULL) + appendStringInfo(&logmsg, _(" application_name=3D%s"), + port->application_name); + #ifdef USE_SSL - if (port->ssl_in_use) - ereport(LOG, - (port->application_name !=3D NULL - ? errmsg("connection authorized: user=3D%s database=3D%s applicatio= n_name=3D%s SSL enabled (protocol=3D%s, cipher=3D%s, bits=3D%d, compression= =3D%s)", - port->user_name, port->database_name, port->application_name, - be_tls_get_version(port), - be_tls_get_cipher(port), - be_tls_get_cipher_bits(port), - be_tls_get_compression(port) ? _("on") : _("off")) - : errmsg("connection authorized: user=3D%s database=3D%s SSL enable= d (protocol=3D%s, cipher=3D%s, bits=3D%d, compression=3D%s)", - port->user_name, port->database_name, - be_tls_get_version(port), - be_tls_get_cipher(port), - be_tls_get_cipher_bits(port), - be_tls_get_compression(port) ? _("on") : _("off")))); - else + if (port->ssl_in_use) + appendStringInfo(&logmsg, _(" SSL enabled (protocol=3D%s, cipher=3D%s, = bits=3D%d, compression=3D%s)"), + be_tls_get_version(port), + be_tls_get_cipher(port), + be_tls_get_cipher_bits(port), + be_tls_get_compression(port) ? _("on") : _("off")); #endif - ereport(LOG, - (port->application_name !=3D NULL - ? errmsg("connection authorized: user=3D%s database=3D%s applicatio= n_name=3D%s", - port->user_name, port->database_name, port->application_name) - : errmsg("connection authorized: user=3D%s database=3D%s", - port->user_name, port->database_name))); - } +#ifdef ENABLE_GSS + if (be_gssapi_get_princ(port)) + appendStringInfo(&logmsg, _(" GSS (authenticated=3D%s, encrypted=3D%s, = principal=3D%s)"), + be_gssapi_get_auth(port) ? _("yes") : _("no"), + be_gssapi_get_enc(port) ? _("yes") : _("no"), + be_gssapi_get_princ(port)); +#endif + + ereport(LOG, errmsg_internal("%s", logmsg.data)); + pfree(logmsg.data); } =20 set_ps_display("startup"); diff --git a/src/test/kerberos/t/001_auth.pl b/src/test/kerberos/t/001_auth= =2Epl index b3aeea9574..e58e69b497 100644 --- a/src/test/kerberos/t/001_auth.pl +++ b/src/test/kerberos/t/001_auth.pl @@ -16,10 +16,11 @@ use warnings; use TestLib; use PostgresNode; use Test::More; +use Time::HiRes qw(usleep); =20 if ($ENV{with_gssapi} eq 'yes') { - plan tests =3D> 18; + plan tests =3D> 34; } else { @@ -74,6 +75,10 @@ my $kdc_datadir =3D "${TestLib::tmp_check}/krb5kdc"; my $kdc_pidfile =3D "${TestLib::tmp_check}/krb5kdc.pid"; my $keytab =3D "${TestLib::tmp_check}/krb5.keytab"; =20 +my $dbname =3D 'postgres'; +my $username =3D 'test1'; +my $application =3D '001_auth.pl'; + note "setting up Kerberos"; =20 my ($stdout, $krb5_version); @@ -160,6 +165,8 @@ my $node =3D get_new_node('node'); $node->init; $node->append_conf('postgresql.conf', "listen_addresses =3D '$hostaddr'"); $node->append_conf('postgresql.conf', "krb_server_keyfile =3D '$keytab'"); +$node->append_conf('postgresql.conf', "logging_collector =3D on"); +$node->append_conf('postgresql.conf', "log_connections =3D on"); $node->start; =20 $node->safe_psql('postgres', 'CREATE USER test1;'); @@ -169,7 +176,7 @@ note "running tests"; # Test connection success or failure, and if success, that query returns t= rue. sub test_access { - my ($node, $role, $query, $expected_res, $gssencmode, $test_name) =3D @_; + my ($node, $role, $query, $expected_res, $gssencmode, $test_name, $expect= _log_msg) =3D @_; =20 # need to connect over TCP/IP for Kerberos my ($res, $stdoutres, $stderrres) =3D $node->psql( @@ -192,6 +199,33 @@ sub test_access { is($res, $expected_res, $test_name); } + + # Verify specified log message is logged in the log file. + if ($expect_log_msg ne '') + { + my $current_logfiles =3D slurp_file($node->data_dir . '/current_logfiles= '); + note "current_logfiles =3D $current_logfiles"; + like($current_logfiles, qr|^stderr log/postgresql-.*log$|, + 'current_logfiles is sane'); + + my $lfname =3D $current_logfiles; + $lfname =3D~ s/^stderr //; + chomp $lfname; + + # might need to retry if logging collector process is slow... + my $max_attempts =3D 180 * 10; + my $first_logfile; + for (my $attempts =3D 0; $attempts < $max_attempts; $attempts++) + { + $first_logfile =3D slurp_file($node->data_dir . '/' . $lfname); + last if $first_logfile =3D~ m/\Q$expect_log_msg\E/; + usleep(100_000); + } + + like($first_logfile, qr/\Q$expect_log_msg\E/, + 'found expected log file content'); + } + return; } =20 @@ -223,11 +257,11 @@ $node->append_conf('pg_hba.conf', qq{host all all $hostaddr/32 gss map=3Dmymap}); $node->restart; =20 -test_access($node, 'test1', 'SELECT true', 2, '', 'fails without ticket'); +test_access($node, 'test1', 'SELECT true', 2, '', 'fails without ticket', = ''); =20 run_log [ $kinit, 'test1' ], \$test1_password or BAIL_OUT($?); =20 -test_access($node, 'test1', 'SELECT true', 2, '', 'fails without mapping'); +test_access($node, 'test1', 'SELECT true', 2, '', 'fails without mapping',= ''); =20 $node->append_conf('pg_ident.conf', qq{mymap /^(.*)\@$realm\$ \\1}); $node->restart; @@ -238,42 +272,49 @@ test_access( 'SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid =3D= pg_backend_pid();', 0, '', - 'succeeds with mapping with default gssencmode and host hba'); + 'succeeds with mapping with default gssencmode and host hba', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dyes, principal=3Dt= est1\@$realm)" +); + test_access( $node, - "test1", + 'test1', 'SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid =3D= pg_backend_pid();', 0, - "gssencmode=3Dprefer", - "succeeds with GSS-encrypted access preferred with host hba"); + 'gssencmode=3Dprefer', + 'succeeds with GSS-encrypted access preferred with host hba', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dyes, principal=3Dt= est1\@$realm)" +); test_access( $node, - "test1", + 'test1', 'SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid =3D= pg_backend_pid();', 0, - "gssencmode=3Drequire", - "succeeds with GSS-encrypted access required with host hba"); + 'gssencmode=3Drequire', + 'succeeds with GSS-encrypted access required with host hba', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dyes, principal=3Dt= est1\@$realm)" +); =20 # Test that we can transport a reasonable amount of data. test_query( $node, - "test1", + 'test1', 'SELECT * FROM generate_series(1, 100000);', qr/^1\n.*\n1024\n.*\n9999\n.*\n100000$/s, - "gssencmode=3Drequire", - "receiving 100K lines works"); + 'gssencmode=3Drequire', + 'receiving 100K lines works'); =20 test_query( $node, - "test1", + 'test1', "CREATE TABLE mytab (f1 int primary key);\n" . "COPY mytab FROM STDIN;\n" . join("\n", (1 .. 100000)) . "\n\\.\n" . "SELECT COUNT(*) FROM mytab;", qr/^100000$/s, - "gssencmode=3Drequire", - "sending 100K lines works"); + 'gssencmode=3Drequire', + 'sending 100K lines works'); =20 unlink($node->data_dir . '/pg_hba.conf'); $node->append_conf('pg_hba.conf', @@ -282,20 +323,24 @@ $node->restart; =20 test_access( $node, - "test1", + 'test1', 'SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid =3D= pg_backend_pid();', 0, - "gssencmode=3Dprefer", - "succeeds with GSS-encrypted access preferred and hostgssenc hba"); + 'gssencmode=3Dprefer', + 'succeeds with GSS-encrypted access preferred and hostgssenc hba', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dyes, principal=3Dt= est1\@$realm)" +); test_access( $node, - "test1", + 'test1', 'SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid =3D= pg_backend_pid();', 0, - "gssencmode=3Drequire", - "succeeds with GSS-encrypted access required and hostgssenc hba"); -test_access($node, "test1", 'SELECT true', 2, "gssencmode=3Ddisable", - "fails with GSS encryption disabled and hostgssenc hba"); + 'gssencmode=3Drequire', + 'succeeds with GSS-encrypted access required and hostgssenc hba', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dyes, principal=3Dt= est1\@$realm)" +); +test_access($node, 'test1', 'SELECT true', 2, 'gssencmode=3Ddisable', + 'fails with GSS encryption disabled and hostgssenc hba', ''); =20 unlink($node->data_dir . '/pg_hba.conf'); $node->append_conf('pg_hba.conf', @@ -304,21 +349,24 @@ $node->restart; =20 test_access( $node, - "test1", + 'test1', 'SELECT gss_authenticated and not encrypted from pg_stat_gssapi where pid= =3D pg_backend_pid();', 0, - "gssencmode=3Dprefer", - "succeeds with GSS-encrypted access preferred and hostnogssenc hba, but n= o encryption" + 'gssencmode=3Dprefer', + 'succeeds with GSS-encrypted access preferred and hostnogssenc hba, but n= o encryption', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dno, principal=3Dte= st1\@$realm)" ); -test_access($node, "test1", 'SELECT true', 2, "gssencmode=3Drequire", - "fails with GSS-encrypted access required and hostnogssenc hba"); +test_access($node, 'test1', 'SELECT true', 2, 'gssencmode=3Drequire', + 'fails with GSS-encrypted access required and hostnogssenc hba', ''); test_access( $node, - "test1", + 'test1', 'SELECT gss_authenticated and not encrypted from pg_stat_gssapi where pid= =3D pg_backend_pid();', 0, - "gssencmode=3Ddisable", - "succeeds with GSS encryption disabled and hostnogssenc hba"); + 'gssencmode=3Ddisable', + 'succeeds with GSS encryption disabled and hostnogssenc hba', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dno, principal=3Dte= st1\@$realm)" +); =20 truncate($node->data_dir . '/pg_ident.conf', 0); unlink($node->data_dir . '/pg_hba.conf'); @@ -332,4 +380,6 @@ test_access( 'SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid =3D= pg_backend_pid();', 0, '', - 'succeeds with include_realm=3D0 and defaults'); + 'succeeds with include_realm=3D0 and defaults', + "connection authorized: user=3D$username database=3D$dbname application_n= ame=3D$application GSS (authenticated=3Dyes, encrypted=3Dyes, principal=3Dt= est1\@$realm)" +); --=20 2.25.1 --N/9/fZ4M25wmf00Y-- --GbJjGCCbPc3OzCle Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJfwXc/AAoJEO1sijiDR2RVgQUP+QFFnHVvEys93gYHBwRnInPW 9BFUhsMGCUfXOuXi73n6taVy2mMRdx3Ff64PVdhV5sRvIIDo67xXhAsnZ6ZMtjks +FRhB3CctgjPOnZldkRtkFqKUM4TBWG7ovewWovLi9KOOBJKxiDndL4nFFR1eobU RZAXJgsYtRVZ6FymuqZn4zqQ2GulMtFZUhS3mGw2SQ5D3tras5wJFQ9AT9M2GBnl 0GoOd0LBLXHC195Qje9ado1dHKzsfik+CLW0b7hoXZ9wFW4PumaoRzDAA8BpYINQ cLjX5OH73c8zzoc7visMqQjBPc9tlQ59kSIS7RwxktuNfMcGBj6rgORvBO9Xy4Fp Emb+vdrLKLx3QRJd+z3NafYqlx+m6T9auJdWoBkwrt9CxfWEvyw3mEiPUuqy43kg YSaylg8OOOyBDF4TvDVKEmLpRw+5d0ByS0GQ0g+KUsHgDG4ClR2+UqHVKzjZ4Iub chvCeW8jrBWYOAU1T5ngKuN8llSHfxxpI7+AGw85yxPQf4dp1el/pDtqe17KKn3n TGa+t5LpXfMxhQjviKsQzgyjBkBBIbD+eWodwfg7gZQ1ruJCErLzAxgqmv65rhPM NsT+Lgz38Y0rttUqZ/wT6b1mcdLy0ZXTy/Ej8eThmdPIpfhR266XlzVabQ/ZynF7 PMUyGOgiBI5hRHeJ22w7 =gAsz -----END PGP SIGNATURE----- --GbJjGCCbPc3OzCle--